1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.treewalk;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertTrue;
47
48 import java.time.Instant;
49
50 import org.junit.Test;
51
52 public class InstantComparatorTest {
53
54 private final InstantComparator cmp = new InstantComparator();
55
56 @Test
57 public void compareNow() {
58 Instant now = Instant.now();
59 assertEquals(0, cmp.compare(now, now));
60 assertEquals(0, cmp.compare(now, now, true));
61 }
62
63 @Test
64 public void compareSeconds() {
65 Instant now = Instant.now();
66 Instant t = Instant.ofEpochSecond(now.getEpochSecond());
67 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
68 assertEquals(0, cmp.compare(t, s));
69 assertEquals(0, cmp.compare(t, t));
70 assertEquals(0, cmp.compare(s, t));
71 }
72
73 @Test
74 public void compareSecondsOnly() {
75 Instant now = Instant.now();
76 Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 987654321);
77 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
78 assertEquals(0, cmp.compare(t, s, true));
79 assertEquals(0, cmp.compare(t, t, true));
80 assertEquals(0, cmp.compare(s, t, true));
81 }
82
83 @Test
84 public void compareSecondsUnequal() {
85 Instant now = Instant.now();
86 Instant t = Instant.ofEpochSecond(now.getEpochSecond());
87 Instant s = Instant.ofEpochSecond(now.getEpochSecond() - 1L);
88 assertTrue(cmp.compare(s, t) < 0);
89 assertTrue(cmp.compare(t, s) > 0);
90 }
91
92 @Test
93 public void compareMillisEqual() {
94 Instant now = Instant.now();
95 Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
96 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
97 assertEquals(0, cmp.compare(s, t));
98 assertEquals(0, cmp.compare(t, t));
99 assertEquals(0, cmp.compare(t, s));
100 s = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
101 assertEquals(0, cmp.compare(s, t));
102 assertEquals(0, cmp.compare(t, s));
103 s = Instant.ofEpochSecond(now.getEpochSecond(), 123400000);
104 assertEquals(0, cmp.compare(s, t));
105 assertEquals(0, cmp.compare(t, s));
106 }
107
108 @Test
109 public void compareMillisUnequal() {
110 Instant now = Instant.now();
111 Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
112 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 122000000);
113 assertTrue(cmp.compare(s, t) < 0);
114 assertTrue(cmp.compare(t, s) > 0);
115 t = Instant.ofEpochSecond(now.getEpochSecond(), 130000000);
116 assertTrue(cmp.compare(s, t) < 0);
117 assertTrue(cmp.compare(t, s) > 0);
118 t = Instant.ofEpochSecond(now.getEpochSecond(), 200000000);
119 assertTrue(cmp.compare(s, t) < 0);
120 assertTrue(cmp.compare(t, s) > 0);
121 s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123000000);
122 assertTrue(cmp.compare(s, t) < 0);
123 assertTrue(cmp.compare(t, s) > 0);
124 }
125
126 @Test
127 public void compareMicrosEqual() {
128 Instant now = Instant.now();
129 Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
130 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
131 assertEquals(0, cmp.compare(s, t));
132 assertEquals(0, cmp.compare(t, s));
133 s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
134 assertEquals(0, cmp.compare(s, t));
135 assertEquals(0, cmp.compare(t, s));
136 }
137
138 @Test
139 public void compareMicrosUnequal() {
140 Instant now = Instant.now();
141 Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
142 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123455000);
143 assertTrue(cmp.compare(s, t) < 0);
144 assertTrue(cmp.compare(t, s) > 0);
145 t = Instant.ofEpochSecond(now.getEpochSecond(), 123460000);
146 assertTrue(cmp.compare(s, t) < 0);
147 assertTrue(cmp.compare(t, s) > 0);
148 t = Instant.ofEpochSecond(now.getEpochSecond(), 123500000);
149 assertTrue(cmp.compare(s, t) < 0);
150 assertTrue(cmp.compare(t, s) > 0);
151 s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456000);
152 assertTrue(cmp.compare(s, t) < 0);
153 assertTrue(cmp.compare(t, s) > 0);
154 }
155
156 @Test
157 public void compareNanosEqual() {
158 Instant now = Instant.now();
159 Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
160 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
161 assertEquals(0, cmp.compare(s, t));
162 assertEquals(0, cmp.compare(t, s));
163 }
164
165 @Test
166 public void compareNanosUnequal() {
167 Instant now = Instant.now();
168 Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
169 Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
170 assertTrue(cmp.compare(s, t) < 0);
171 assertTrue(cmp.compare(t, s) > 0);
172 t = Instant.ofEpochSecond(now.getEpochSecond(), 123456800);
173 assertTrue(cmp.compare(s, t) < 0);
174 assertTrue(cmp.compare(t, s) > 0);
175 s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456789);
176 assertTrue(cmp.compare(s, t) < 0);
177 assertTrue(cmp.compare(t, s) > 0);
178 s = Instant.ofEpochSecond(now.getEpochSecond(), 123456788);
179 assertTrue(cmp.compare(s, t) < 0);
180 assertTrue(cmp.compare(t, s) > 0);
181 }
182 }