You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InstantComparatorTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2019, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.treewalk;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertTrue;
  13. import java.time.Instant;
  14. import org.junit.Test;
  15. public class InstantComparatorTest {
  16. private final InstantComparator cmp = new InstantComparator();
  17. @Test
  18. public void compareNow() {
  19. Instant now = Instant.now();
  20. assertEquals(0, cmp.compare(now, now));
  21. assertEquals(0, cmp.compare(now, now, true));
  22. }
  23. @Test
  24. public void compareSeconds() {
  25. Instant now = Instant.now();
  26. Instant t = Instant.ofEpochSecond(now.getEpochSecond());
  27. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  28. assertEquals(0, cmp.compare(t, s));
  29. assertEquals(0, cmp.compare(t, t));
  30. assertEquals(0, cmp.compare(s, t));
  31. }
  32. @Test
  33. public void compareSecondsOnly() {
  34. Instant now = Instant.now();
  35. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 987654321);
  36. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  37. assertEquals(0, cmp.compare(t, s, true));
  38. assertEquals(0, cmp.compare(t, t, true));
  39. assertEquals(0, cmp.compare(s, t, true));
  40. }
  41. @Test
  42. public void compareSecondsUnequal() {
  43. Instant now = Instant.now();
  44. Instant t = Instant.ofEpochSecond(now.getEpochSecond());
  45. Instant s = Instant.ofEpochSecond(now.getEpochSecond() - 1L);
  46. assertTrue(cmp.compare(s, t) < 0);
  47. assertTrue(cmp.compare(t, s) > 0);
  48. }
  49. @Test
  50. public void compareMillisEqual() {
  51. Instant now = Instant.now();
  52. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
  53. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  54. assertEquals(0, cmp.compare(s, t));
  55. assertEquals(0, cmp.compare(t, t));
  56. assertEquals(0, cmp.compare(t, s));
  57. s = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
  58. assertEquals(0, cmp.compare(s, t));
  59. assertEquals(0, cmp.compare(t, s));
  60. s = Instant.ofEpochSecond(now.getEpochSecond(), 123400000);
  61. assertEquals(0, cmp.compare(s, t));
  62. assertEquals(0, cmp.compare(t, s));
  63. }
  64. @Test
  65. public void compareMillisUnequal() {
  66. Instant now = Instant.now();
  67. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
  68. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 122000000);
  69. assertTrue(cmp.compare(s, t) < 0);
  70. assertTrue(cmp.compare(t, s) > 0);
  71. t = Instant.ofEpochSecond(now.getEpochSecond(), 130000000);
  72. assertTrue(cmp.compare(s, t) < 0);
  73. assertTrue(cmp.compare(t, s) > 0);
  74. t = Instant.ofEpochSecond(now.getEpochSecond(), 200000000);
  75. assertTrue(cmp.compare(s, t) < 0);
  76. assertTrue(cmp.compare(t, s) > 0);
  77. s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123000000);
  78. assertTrue(cmp.compare(s, t) < 0);
  79. assertTrue(cmp.compare(t, s) > 0);
  80. }
  81. @Test
  82. public void compareMicrosEqual() {
  83. Instant now = Instant.now();
  84. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
  85. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  86. assertEquals(0, cmp.compare(s, t));
  87. assertEquals(0, cmp.compare(t, s));
  88. s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
  89. assertEquals(0, cmp.compare(s, t));
  90. assertEquals(0, cmp.compare(t, s));
  91. }
  92. @Test
  93. public void compareMicrosUnequal() {
  94. Instant now = Instant.now();
  95. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
  96. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123455000);
  97. assertTrue(cmp.compare(s, t) < 0);
  98. assertTrue(cmp.compare(t, s) > 0);
  99. t = Instant.ofEpochSecond(now.getEpochSecond(), 123460000);
  100. assertTrue(cmp.compare(s, t) < 0);
  101. assertTrue(cmp.compare(t, s) > 0);
  102. t = Instant.ofEpochSecond(now.getEpochSecond(), 123500000);
  103. assertTrue(cmp.compare(s, t) < 0);
  104. assertTrue(cmp.compare(t, s) > 0);
  105. s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456000);
  106. assertTrue(cmp.compare(s, t) < 0);
  107. assertTrue(cmp.compare(t, s) > 0);
  108. }
  109. @Test
  110. public void compareNanosEqual() {
  111. Instant now = Instant.now();
  112. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  113. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  114. assertEquals(0, cmp.compare(s, t));
  115. assertEquals(0, cmp.compare(t, s));
  116. }
  117. @Test
  118. public void compareNanosUnequal() {
  119. Instant now = Instant.now();
  120. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  121. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
  122. assertTrue(cmp.compare(s, t) < 0);
  123. assertTrue(cmp.compare(t, s) > 0);
  124. t = Instant.ofEpochSecond(now.getEpochSecond(), 123456800);
  125. assertTrue(cmp.compare(s, t) < 0);
  126. assertTrue(cmp.compare(t, s) > 0);
  127. s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456789);
  128. assertTrue(cmp.compare(s, t) < 0);
  129. assertTrue(cmp.compare(t, s) > 0);
  130. s = Instant.ofEpochSecond(now.getEpochSecond(), 123456788);
  131. assertTrue(cmp.compare(s, t) < 0);
  132. assertTrue(cmp.compare(t, s) > 0);
  133. }
  134. }