View Javadoc
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  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertTrue;
14  
15  import java.time.Instant;
16  
17  import org.junit.Test;
18  
19  public class InstantComparatorTest {
20  
21  	private final InstantComparator cmp = new InstantComparator();
22  
23  	@Test
24  	public void compareNow() {
25  		Instant now = Instant.now();
26  		assertEquals(0, cmp.compare(now, now));
27  		assertEquals(0, cmp.compare(now, now, true));
28  	}
29  
30  	@Test
31  	public void compareSeconds() {
32  		Instant now = Instant.now();
33  		Instant t = Instant.ofEpochSecond(now.getEpochSecond());
34  		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
35  		assertEquals(0, cmp.compare(t, s));
36  		assertEquals(0, cmp.compare(t, t));
37  		assertEquals(0, cmp.compare(s, t));
38  	}
39  
40  	@Test
41  	public void compareSecondsOnly() {
42  		Instant now = Instant.now();
43  		Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 987654321);
44  		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
45  		assertEquals(0, cmp.compare(t, s, true));
46  		assertEquals(0, cmp.compare(t, t, true));
47  		assertEquals(0, cmp.compare(s, t, true));
48  	}
49  
50  	@Test
51  	public void compareSecondsUnequal() {
52  		Instant now = Instant.now();
53  		Instant t = Instant.ofEpochSecond(now.getEpochSecond());
54  		Instant s = Instant.ofEpochSecond(now.getEpochSecond() - 1L);
55  		assertTrue(cmp.compare(s, t) < 0);
56  		assertTrue(cmp.compare(t, s) > 0);
57  	}
58  
59  	@Test
60  	public void compareMillisEqual() {
61  		Instant now = Instant.now();
62  		Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
63  		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
64  		assertEquals(0, cmp.compare(s, t));
65  		assertEquals(0, cmp.compare(t, t));
66  		assertEquals(0, cmp.compare(t, s));
67  		s = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
68  		assertEquals(0, cmp.compare(s, t));
69  		assertEquals(0, cmp.compare(t, s));
70  		s = Instant.ofEpochSecond(now.getEpochSecond(), 123400000);
71  		assertEquals(0, cmp.compare(s, t));
72  		assertEquals(0, cmp.compare(t, s));
73  	}
74  
75  	@Test
76  	public void compareMillisUnequal() {
77  		Instant now = Instant.now();
78  		Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123000000);
79  		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 122000000);
80  		assertTrue(cmp.compare(s, t) < 0);
81  		assertTrue(cmp.compare(t, s) > 0);
82  		t = Instant.ofEpochSecond(now.getEpochSecond(), 130000000);
83  		assertTrue(cmp.compare(s, t) < 0);
84  		assertTrue(cmp.compare(t, s) > 0);
85  		t = Instant.ofEpochSecond(now.getEpochSecond(), 200000000);
86  		assertTrue(cmp.compare(s, t) < 0);
87  		assertTrue(cmp.compare(t, s) > 0);
88  		s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123000000);
89  		assertTrue(cmp.compare(s, t) < 0);
90  		assertTrue(cmp.compare(t, s) > 0);
91  	}
92  
93  	@Test
94  	public void compareMicrosEqual() {
95  		Instant now = Instant.now();
96  		Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
97  		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
98  		assertEquals(0, cmp.compare(s, t));
99  		assertEquals(0, cmp.compare(t, s));
100 		s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
101 		assertEquals(0, cmp.compare(s, t));
102 		assertEquals(0, cmp.compare(t, s));
103 	}
104 
105 	@Test
106 	public void compareMicrosUnequal() {
107 		Instant now = Instant.now();
108 		Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456000);
109 		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123455000);
110 		assertTrue(cmp.compare(s, t) < 0);
111 		assertTrue(cmp.compare(t, s) > 0);
112 		t = Instant.ofEpochSecond(now.getEpochSecond(), 123460000);
113 		assertTrue(cmp.compare(s, t) < 0);
114 		assertTrue(cmp.compare(t, s) > 0);
115 		t = Instant.ofEpochSecond(now.getEpochSecond(), 123500000);
116 		assertTrue(cmp.compare(s, t) < 0);
117 		assertTrue(cmp.compare(t, s) > 0);
118 		s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456000);
119 		assertTrue(cmp.compare(s, t) < 0);
120 		assertTrue(cmp.compare(t, s) > 0);
121 	}
122 
123 	@Test
124 	public void compareNanosEqual() {
125 		Instant now = Instant.now();
126 		Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
127 		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
128 		assertEquals(0, cmp.compare(s, t));
129 		assertEquals(0, cmp.compare(t, s));
130 	}
131 
132 	@Test
133 	public void compareNanosUnequal() {
134 		Instant now = Instant.now();
135 		Instant t = Instant.ofEpochSecond(now.getEpochSecond(), 123456789);
136 		Instant s = Instant.ofEpochSecond(now.getEpochSecond(), 123456700);
137 		assertTrue(cmp.compare(s, t) < 0);
138 		assertTrue(cmp.compare(t, s) > 0);
139 		t = Instant.ofEpochSecond(now.getEpochSecond(), 123456800);
140 		assertTrue(cmp.compare(s, t) < 0);
141 		assertTrue(cmp.compare(t, s) > 0);
142 		s = Instant.ofEpochSecond(now.getEpochSecond() - 1L, 123456789);
143 		assertTrue(cmp.compare(s, t) < 0);
144 		assertTrue(cmp.compare(t, s) > 0);
145 		s = Instant.ofEpochSecond(now.getEpochSecond(), 123456788);
146 		assertTrue(cmp.compare(s, t) < 0);
147 		assertTrue(cmp.compare(t, s) > 0);
148 	}
149 }