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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2019, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.treewalk;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.time.Instant;
  47. import org.junit.Test;
  48. public class InstantComparatorTest {
  49. private final InstantComparator cmp = new InstantComparator();
  50. @Test
  51. public void compareNow() {
  52. Instant now = Instant.now();
  53. assertEquals(0, cmp.compare(now, now));
  54. assertEquals(0, cmp.compare(now, now, true));
  55. }
  56. @Test
  57. public void compareSeconds() {
  58. Instant now = Instant.now();
  59. Instant t = Instant.ofEpochSecond(now.getEpochSecond());
  60. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  61. assertEquals(0, cmp.compare(t, s));
  62. assertEquals(0, cmp.compare(t, t));
  63. assertEquals(0, cmp.compare(s, t));
  64. }
  65. @Test
  66. public void compareSecondsOnly() {
  67. Instant now = Instant.now();
  68. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 987654321);
  69. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  70. assertEquals(0, cmp.compare(t, s, true));
  71. assertEquals(0, cmp.compare(t, t, true));
  72. assertEquals(0, cmp.compare(s, t, true));
  73. }
  74. @Test
  75. public void compareSecondsUnequal() {
  76. Instant now = Instant.now();
  77. Instant t = Instant.ofEpochSecond(now.getEpochSecond());
  78. Instant s = Instant.ofEpochSecond(now.getEpochSecond() - 1L);
  79. assertTrue(cmp.compare(s, t) < 0);
  80. assertTrue(cmp.compare(t, s) > 0);
  81. }
  82. @Test
  83. public void compareMillisEqual() {
  84. Instant now = Instant.now();
  85. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
  86. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  87. assertEquals(0, cmp.compare(s, t));
  88. assertEquals(0, cmp.compare(t, t));
  89. assertEquals(0, cmp.compare(t, s));
  90. s = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
  91. assertEquals(0, cmp.compare(s, t));
  92. assertEquals(0, cmp.compare(t, s));
  93. s = Instant.ofEpochSecond(now.getEpochSecond(), 123400000);
  94. assertEquals(0, cmp.compare(s, t));
  95. assertEquals(0, cmp.compare(t, s));
  96. }
  97. @Test
  98. public void compareMillisUnequal() {
  99. Instant now = Instant.now();
  100. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
  101. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 122000000);
  102. assertTrue(cmp.compare(s, t) < 0);
  103. assertTrue(cmp.compare(t, s) > 0);
  104. t = Instant.ofEpochSecond(now.getEpochSecond(), 130000000);
  105. assertTrue(cmp.compare(s, t) < 0);
  106. assertTrue(cmp.compare(t, s) > 0);
  107. t = Instant.ofEpochSecond(now.getEpochSecond(), 200000000);
  108. assertTrue(cmp.compare(s, t) < 0);
  109. assertTrue(cmp.compare(t, s) > 0);
  110. s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123000000);
  111. assertTrue(cmp.compare(s, t) < 0);
  112. assertTrue(cmp.compare(t, s) > 0);
  113. }
  114. @Test
  115. public void compareMicrosEqual() {
  116. Instant now = Instant.now();
  117. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
  118. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  119. assertEquals(0, cmp.compare(s, t));
  120. assertEquals(0, cmp.compare(t, s));
  121. s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
  122. assertEquals(0, cmp.compare(s, t));
  123. assertEquals(0, cmp.compare(t, s));
  124. }
  125. @Test
  126. public void compareMicrosUnequal() {
  127. Instant now = Instant.now();
  128. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
  129. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123455000);
  130. assertTrue(cmp.compare(s, t) < 0);
  131. assertTrue(cmp.compare(t, s) > 0);
  132. t = Instant.ofEpochSecond(now.getEpochSecond(), 123460000);
  133. assertTrue(cmp.compare(s, t) < 0);
  134. assertTrue(cmp.compare(t, s) > 0);
  135. t = Instant.ofEpochSecond(now.getEpochSecond(), 123500000);
  136. assertTrue(cmp.compare(s, t) < 0);
  137. assertTrue(cmp.compare(t, s) > 0);
  138. s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456000);
  139. assertTrue(cmp.compare(s, t) < 0);
  140. assertTrue(cmp.compare(t, s) > 0);
  141. }
  142. @Test
  143. public void compareNanosEqual() {
  144. Instant now = Instant.now();
  145. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  146. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  147. assertEquals(0, cmp.compare(s, t));
  148. assertEquals(0, cmp.compare(t, s));
  149. }
  150. @Test
  151. public void compareNanosUnequal() {
  152. Instant now = Instant.now();
  153. Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
  154. Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
  155. assertTrue(cmp.compare(s, t) < 0);
  156. assertTrue(cmp.compare(t, s) > 0);
  157. t = Instant.ofEpochSecond(now.getEpochSecond(), 123456800);
  158. assertTrue(cmp.compare(s, t) < 0);
  159. assertTrue(cmp.compare(t, s) > 0);
  160. s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456789);
  161. assertTrue(cmp.compare(s, t) < 0);
  162. assertTrue(cmp.compare(t, s) > 0);
  163. s = Instant.ofEpochSecond(now.getEpochSecond(), 123456788);
  164. assertTrue(cmp.compare(s, t) < 0);
  165. assertTrue(cmp.compare(t, s) > 0);
  166. }
  167. }