14 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
|
17 |
– |
import java.time.temporal.ChronoUnit; |
17 |
|
import java.util.concurrent.CountDownLatch; |
18 |
|
import java.util.concurrent.TimeUnit; |
19 |
|
|
29 |
|
return new TestSuite(TimeUnitTest.class); |
30 |
|
} |
31 |
|
|
32 |
< |
// (loops to 88888 check increments at all time divisions.) |
33 |
< |
|
34 |
< |
/** |
35 |
< |
* convert correctly converts sample values across the units |
36 |
< |
*/ |
37 |
< |
public void testConvert() { |
38 |
< |
for (long t = 0; t < 88888; ++t) { |
39 |
< |
assertEquals(t*60*60*24, |
40 |
< |
SECONDS.convert(t, DAYS)); |
41 |
< |
assertEquals(t*60*60, |
42 |
< |
SECONDS.convert(t, HOURS)); |
44 |
< |
assertEquals(t*60, |
45 |
< |
SECONDS.convert(t, MINUTES)); |
46 |
< |
assertEquals(t, |
47 |
< |
SECONDS.convert(t, SECONDS)); |
48 |
< |
assertEquals(t, |
49 |
< |
SECONDS.convert(1000L*t, MILLISECONDS)); |
50 |
< |
assertEquals(t, |
51 |
< |
SECONDS.convert(1000000L*t, MICROSECONDS)); |
52 |
< |
assertEquals(t, |
53 |
< |
SECONDS.convert(1000000000L*t, NANOSECONDS)); |
54 |
< |
|
55 |
< |
assertEquals(1000L*t*60*60*24, |
56 |
< |
MILLISECONDS.convert(t, DAYS)); |
57 |
< |
assertEquals(1000L*t*60*60, |
58 |
< |
MILLISECONDS.convert(t, HOURS)); |
59 |
< |
assertEquals(1000L*t*60, |
60 |
< |
MILLISECONDS.convert(t, MINUTES)); |
61 |
< |
assertEquals(1000L*t, |
62 |
< |
MILLISECONDS.convert(t, SECONDS)); |
63 |
< |
assertEquals(t, |
64 |
< |
MILLISECONDS.convert(t, MILLISECONDS)); |
65 |
< |
assertEquals(t, |
66 |
< |
MILLISECONDS.convert(1000L*t, MICROSECONDS)); |
67 |
< |
assertEquals(t, |
68 |
< |
MILLISECONDS.convert(1000000L*t, NANOSECONDS)); |
69 |
< |
|
70 |
< |
assertEquals(1000000L*t*60*60*24, |
71 |
< |
MICROSECONDS.convert(t, DAYS)); |
72 |
< |
assertEquals(1000000L*t*60*60, |
73 |
< |
MICROSECONDS.convert(t, HOURS)); |
74 |
< |
assertEquals(1000000L*t*60, |
75 |
< |
MICROSECONDS.convert(t, MINUTES)); |
76 |
< |
assertEquals(1000000L*t, |
77 |
< |
MICROSECONDS.convert(t, SECONDS)); |
78 |
< |
assertEquals(1000L*t, |
79 |
< |
MICROSECONDS.convert(t, MILLISECONDS)); |
80 |
< |
assertEquals(t, |
81 |
< |
MICROSECONDS.convert(t, MICROSECONDS)); |
82 |
< |
assertEquals(t, |
83 |
< |
MICROSECONDS.convert(1000L*t, NANOSECONDS)); |
84 |
< |
|
85 |
< |
assertEquals(1000000000L*t*60*60*24, |
86 |
< |
NANOSECONDS.convert(t, DAYS)); |
87 |
< |
assertEquals(1000000000L*t*60*60, |
88 |
< |
NANOSECONDS.convert(t, HOURS)); |
89 |
< |
assertEquals(1000000000L*t*60, |
90 |
< |
NANOSECONDS.convert(t, MINUTES)); |
91 |
< |
assertEquals(1000000000L*t, |
92 |
< |
NANOSECONDS.convert(t, SECONDS)); |
93 |
< |
assertEquals(1000000L*t, |
94 |
< |
NANOSECONDS.convert(t, MILLISECONDS)); |
95 |
< |
assertEquals(1000L*t, |
96 |
< |
NANOSECONDS.convert(t, MICROSECONDS)); |
97 |
< |
assertEquals(t, |
98 |
< |
NANOSECONDS.convert(t, NANOSECONDS)); |
32 |
> |
void testConversion(TimeUnit x, TimeUnit y, long n, long expected) { |
33 |
> |
assertEquals(expected, x.convert(n, y)); |
34 |
> |
switch (x) { |
35 |
> |
case NANOSECONDS: assertEquals(expected, y.toNanos(n)); break; |
36 |
> |
case MICROSECONDS: assertEquals(expected, y.toMicros(n)); break; |
37 |
> |
case MILLISECONDS: assertEquals(expected, y.toMillis(n)); break; |
38 |
> |
case SECONDS: assertEquals(expected, y.toSeconds(n)); break; |
39 |
> |
case MINUTES: assertEquals(expected, y.toMinutes(n)); break; |
40 |
> |
case HOURS: assertEquals(expected, y.toHours(n)); break; |
41 |
> |
case DAYS: assertEquals(expected, y.toDays(n)); break; |
42 |
> |
default: throw new AssertionError(); |
43 |
|
} |
100 |
– |
} |
44 |
|
|
45 |
< |
/** |
103 |
< |
* toNanos correctly converts sample values in different units to |
104 |
< |
* nanoseconds |
105 |
< |
*/ |
106 |
< |
public void testToNanos() { |
107 |
< |
for (long t = 0; t < 88888; ++t) { |
108 |
< |
assertEquals(t*1000000000L*60*60*24, |
109 |
< |
DAYS.toNanos(t)); |
110 |
< |
assertEquals(t*1000000000L*60*60, |
111 |
< |
HOURS.toNanos(t)); |
112 |
< |
assertEquals(t*1000000000L*60, |
113 |
< |
MINUTES.toNanos(t)); |
114 |
< |
assertEquals(1000000000L*t, |
115 |
< |
SECONDS.toNanos(t)); |
116 |
< |
assertEquals(1000000L*t, |
117 |
< |
MILLISECONDS.toNanos(t)); |
118 |
< |
assertEquals(1000L*t, |
119 |
< |
MICROSECONDS.toNanos(t)); |
120 |
< |
assertEquals(t, |
121 |
< |
NANOSECONDS.toNanos(t)); |
122 |
< |
} |
45 |
> |
if (n > 0) testConversion(x, y, -n, -expected); |
46 |
|
} |
47 |
|
|
48 |
< |
/** |
49 |
< |
* toMicros correctly converts sample values in different units to |
50 |
< |
* microseconds |
51 |
< |
*/ |
52 |
< |
public void testToMicros() { |
53 |
< |
for (long t = 0; t < 88888; ++t) { |
54 |
< |
assertEquals(t*1000000L*60*60*24, |
55 |
< |
DAYS.toMicros(t)); |
56 |
< |
assertEquals(t*1000000L*60*60, |
57 |
< |
HOURS.toMicros(t)); |
135 |
< |
assertEquals(t*1000000L*60, |
136 |
< |
MINUTES.toMicros(t)); |
137 |
< |
assertEquals(1000000L*t, |
138 |
< |
SECONDS.toMicros(t)); |
139 |
< |
assertEquals(1000L*t, |
140 |
< |
MILLISECONDS.toMicros(t)); |
141 |
< |
assertEquals(t, |
142 |
< |
MICROSECONDS.toMicros(t)); |
143 |
< |
assertEquals(t, |
144 |
< |
NANOSECONDS.toMicros(t*1000L)); |
145 |
< |
} |
146 |
< |
} |
147 |
< |
|
148 |
< |
/** |
149 |
< |
* toMillis correctly converts sample values in different units to |
150 |
< |
* milliseconds |
151 |
< |
*/ |
152 |
< |
public void testToMillis() { |
153 |
< |
for (long t = 0; t < 88888; ++t) { |
154 |
< |
assertEquals(t*1000L*60*60*24, |
155 |
< |
DAYS.toMillis(t)); |
156 |
< |
assertEquals(t*1000L*60*60, |
157 |
< |
HOURS.toMillis(t)); |
158 |
< |
assertEquals(t*1000L*60, |
159 |
< |
MINUTES.toMillis(t)); |
160 |
< |
assertEquals(1000L*t, |
161 |
< |
SECONDS.toMillis(t)); |
162 |
< |
assertEquals(t, |
163 |
< |
MILLISECONDS.toMillis(t)); |
164 |
< |
assertEquals(t, |
165 |
< |
MICROSECONDS.toMillis(t*1000L)); |
166 |
< |
assertEquals(t, |
167 |
< |
NANOSECONDS.toMillis(t*1000000L)); |
48 |
> |
void testConversion(TimeUnit x, TimeUnit y) { |
49 |
> |
long ratio = x.toNanos(1)/y.toNanos(1); |
50 |
> |
assertTrue(ratio > 0); |
51 |
> |
long[] ns = { 0, 1, 2, Long.MAX_VALUE/ratio, Long.MIN_VALUE/ratio }; |
52 |
> |
for (long n : ns) { |
53 |
> |
testConversion(y, x, n, n * ratio); |
54 |
> |
long[] ks = { n * ratio, n * ratio + 1, n * ratio - 1 }; |
55 |
> |
for (long k : ks) { |
56 |
> |
testConversion(x, y, k, k / ratio); |
57 |
> |
} |
58 |
|
} |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
< |
* toSeconds correctly converts sample values in different units to |
173 |
< |
* seconds |
62 |
> |
* Conversion methods correctly convert sample values |
63 |
|
*/ |
64 |
< |
public void testToSeconds() { |
65 |
< |
for (long t = 0; t < 88888; ++t) { |
66 |
< |
assertEquals(t*60*60*24, |
67 |
< |
DAYS.toSeconds(t)); |
68 |
< |
assertEquals(t*60*60, |
69 |
< |
HOURS.toSeconds(t)); |
70 |
< |
assertEquals(t*60, |
71 |
< |
MINUTES.toSeconds(t)); |
72 |
< |
assertEquals(t, |
184 |
< |
SECONDS.toSeconds(t)); |
185 |
< |
assertEquals(t, |
186 |
< |
MILLISECONDS.toSeconds(t*1000L)); |
187 |
< |
assertEquals(t, |
188 |
< |
MICROSECONDS.toSeconds(t*1000000L)); |
189 |
< |
assertEquals(t, |
190 |
< |
NANOSECONDS.toSeconds(t*1000000000L)); |
191 |
< |
} |
192 |
< |
} |
64 |
> |
public void testConversions() { |
65 |
> |
// Sanity check |
66 |
> |
assertEquals(1, NANOSECONDS.toNanos(1)); |
67 |
> |
assertEquals(1000L * NANOSECONDS.toNanos(1), MICROSECONDS.toNanos(1)); |
68 |
> |
assertEquals(1000L * MICROSECONDS.toNanos(1), MILLISECONDS.toNanos(1)); |
69 |
> |
assertEquals(1000L * MILLISECONDS.toNanos(1), SECONDS.toNanos(1)); |
70 |
> |
assertEquals(60L * SECONDS.toNanos(1), MINUTES.toNanos(1)); |
71 |
> |
assertEquals(60L * MINUTES.toNanos(1), HOURS.toNanos(1)); |
72 |
> |
assertEquals(24L * HOURS.toNanos(1), DAYS.toNanos(1)); |
73 |
|
|
74 |
< |
/** |
75 |
< |
* toMinutes correctly converts sample values in different units to |
196 |
< |
* minutes |
197 |
< |
*/ |
198 |
< |
public void testToMinutes() { |
199 |
< |
for (long t = 0; t < 88888; ++t) { |
200 |
< |
assertEquals(t*60*24, |
201 |
< |
DAYS.toMinutes(t)); |
202 |
< |
assertEquals(t*60, |
203 |
< |
HOURS.toMinutes(t)); |
204 |
< |
assertEquals(t, |
205 |
< |
MINUTES.toMinutes(t)); |
206 |
< |
assertEquals(t, |
207 |
< |
SECONDS.toMinutes(t*60)); |
208 |
< |
assertEquals(t, |
209 |
< |
MILLISECONDS.toMinutes(t*1000L*60)); |
210 |
< |
assertEquals(t, |
211 |
< |
MICROSECONDS.toMinutes(t*1000000L*60)); |
212 |
< |
assertEquals(t, |
213 |
< |
NANOSECONDS.toMinutes(t*1000000000L*60)); |
74 |
> |
for (TimeUnit x : TimeUnit.values()) { |
75 |
> |
assertEquals(x.toNanos(1), NANOSECONDS.convert(1, x)); |
76 |
|
} |
215 |
– |
} |
77 |
|
|
78 |
< |
/** |
79 |
< |
* toHours correctly converts sample values in different units to |
80 |
< |
* hours |
81 |
< |
*/ |
221 |
< |
public void testToHours() { |
222 |
< |
for (long t = 0; t < 88888; ++t) { |
223 |
< |
assertEquals(t*24, |
224 |
< |
DAYS.toHours(t)); |
225 |
< |
assertEquals(t, |
226 |
< |
HOURS.toHours(t)); |
227 |
< |
assertEquals(t, |
228 |
< |
MINUTES.toHours(t*60)); |
229 |
< |
assertEquals(t, |
230 |
< |
SECONDS.toHours(t*60*60)); |
231 |
< |
assertEquals(t, |
232 |
< |
MILLISECONDS.toHours(t*1000L*60*60)); |
233 |
< |
assertEquals(t, |
234 |
< |
MICROSECONDS.toHours(t*1000000L*60*60)); |
235 |
< |
assertEquals(t, |
236 |
< |
NANOSECONDS.toHours(t*1000000000L*60*60)); |
237 |
< |
} |
238 |
< |
} |
239 |
< |
|
240 |
< |
/** |
241 |
< |
* toDays correctly converts sample values in different units to |
242 |
< |
* days |
243 |
< |
*/ |
244 |
< |
public void testToDays() { |
245 |
< |
for (long t = 0; t < 88888; ++t) { |
246 |
< |
assertEquals(t, |
247 |
< |
DAYS.toDays(t)); |
248 |
< |
assertEquals(t, |
249 |
< |
HOURS.toDays(t*24)); |
250 |
< |
assertEquals(t, |
251 |
< |
MINUTES.toDays(t*60*24)); |
252 |
< |
assertEquals(t, |
253 |
< |
SECONDS.toDays(t*60*60*24)); |
254 |
< |
assertEquals(t, |
255 |
< |
MILLISECONDS.toDays(t*1000L*60*60*24)); |
256 |
< |
assertEquals(t, |
257 |
< |
MICROSECONDS.toDays(t*1000000L*60*60*24)); |
258 |
< |
assertEquals(t, |
259 |
< |
NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
260 |
< |
} |
78 |
> |
for (TimeUnit x : TimeUnit.values()) |
79 |
> |
for (TimeUnit y : TimeUnit.values()) |
80 |
> |
if (x.toNanos(1) >= y.toNanos(1)) |
81 |
> |
testConversion(x, y); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
102 |
|
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); |
103 |
|
assertEquals(Long.MIN_VALUE, |
104 |
|
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); |
105 |
+ |
|
106 |
+ |
for (TimeUnit x : TimeUnit.values()) |
107 |
+ |
for (TimeUnit y : TimeUnit.values()) { |
108 |
+ |
long ratio = x.toNanos(1) / y.toNanos(1); |
109 |
+ |
if (ratio >= 1) { |
110 |
+ |
assertEquals(ratio, y.convert(1, x)); |
111 |
+ |
assertEquals(1, x.convert(ratio, y)); |
112 |
+ |
long max = Long.MAX_VALUE/ratio; |
113 |
+ |
assertEquals(max * ratio, y.convert(max, x)); |
114 |
+ |
assertEquals(-max * ratio, y.convert(-max, x)); |
115 |
+ |
assertEquals(max, x.convert(max * ratio, y)); |
116 |
+ |
assertEquals(-max, x.convert(-max * ratio, y)); |
117 |
+ |
if (max < Long.MAX_VALUE) { |
118 |
+ |
assertEquals(Long.MAX_VALUE, y.convert(max + 1, x)); |
119 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x)); |
120 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x)); |
121 |
+ |
} |
122 |
+ |
assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x)); |
123 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x)); |
124 |
+ |
} |
125 |
+ |
} |
126 |
|
} |
127 |
|
|
128 |
|
/** |
134 |
|
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
135 |
|
assertEquals(Long.MIN_VALUE, |
136 |
|
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
137 |
+ |
|
138 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
139 |
+ |
long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1); |
140 |
+ |
if (ratio >= 1) { |
141 |
+ |
long max = Long.MAX_VALUE/ratio; |
142 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
143 |
+ |
assertEquals(z * ratio, x.toNanos(z)); |
144 |
+ |
if (max < Long.MAX_VALUE) { |
145 |
+ |
assertEquals(Long.MAX_VALUE, x.toNanos(max + 1)); |
146 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1)); |
147 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1)); |
148 |
+ |
} |
149 |
+ |
assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE)); |
150 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE)); |
151 |
+ |
if (max < Integer.MAX_VALUE) { |
152 |
+ |
assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE)); |
153 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE)); |
154 |
+ |
} |
155 |
+ |
} |
156 |
+ |
} |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
/** |
160 |
+ |
* toMicros saturates positive too-large values to Long.MAX_VALUE |
161 |
+ |
* and negative to LONG.MIN_VALUE |
162 |
+ |
*/ |
163 |
+ |
public void testToMicrosSaturate() { |
164 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
165 |
+ |
long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1); |
166 |
+ |
if (ratio >= 1) { |
167 |
+ |
long max = Long.MAX_VALUE/ratio; |
168 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
169 |
+ |
assertEquals(z * ratio, x.toMicros(z)); |
170 |
+ |
if (max < Long.MAX_VALUE) { |
171 |
+ |
assertEquals(Long.MAX_VALUE, x.toMicros(max + 1)); |
172 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1)); |
173 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1)); |
174 |
+ |
} |
175 |
+ |
assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE)); |
176 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE)); |
177 |
+ |
if (max < Integer.MAX_VALUE) { |
178 |
+ |
assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE)); |
179 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE)); |
180 |
+ |
} |
181 |
+ |
} |
182 |
+ |
} |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
/** |
186 |
+ |
* toMillis saturates positive too-large values to Long.MAX_VALUE |
187 |
+ |
* and negative to LONG.MIN_VALUE |
188 |
+ |
*/ |
189 |
+ |
public void testToMillisSaturate() { |
190 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
191 |
+ |
long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1); |
192 |
+ |
if (ratio >= 1) { |
193 |
+ |
long max = Long.MAX_VALUE/ratio; |
194 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
195 |
+ |
assertEquals(z * ratio, x.toMillis(z)); |
196 |
+ |
if (max < Long.MAX_VALUE) { |
197 |
+ |
assertEquals(Long.MAX_VALUE, x.toMillis(max + 1)); |
198 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1)); |
199 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1)); |
200 |
+ |
} |
201 |
+ |
assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE)); |
202 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE)); |
203 |
+ |
if (max < Integer.MAX_VALUE) { |
204 |
+ |
assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE)); |
205 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE)); |
206 |
+ |
} |
207 |
+ |
} |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
/** |
212 |
+ |
* toSeconds saturates positive too-large values to Long.MAX_VALUE |
213 |
+ |
* and negative to LONG.MIN_VALUE |
214 |
+ |
*/ |
215 |
+ |
public void testToSecondsSaturate() { |
216 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
217 |
+ |
long ratio = x.toNanos(1) / SECONDS.toNanos(1); |
218 |
+ |
if (ratio >= 1) { |
219 |
+ |
long max = Long.MAX_VALUE/ratio; |
220 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
221 |
+ |
assertEquals(z * ratio, x.toSeconds(z)); |
222 |
+ |
if (max < Long.MAX_VALUE) { |
223 |
+ |
assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1)); |
224 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1)); |
225 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1)); |
226 |
+ |
} |
227 |
+ |
assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE)); |
228 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE)); |
229 |
+ |
if (max < Integer.MAX_VALUE) { |
230 |
+ |
assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE)); |
231 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE)); |
232 |
+ |
} |
233 |
+ |
} |
234 |
+ |
} |
235 |
+ |
} |
236 |
+ |
|
237 |
+ |
/** |
238 |
+ |
* toMinutes saturates positive too-large values to Long.MAX_VALUE |
239 |
+ |
* and negative to LONG.MIN_VALUE |
240 |
+ |
*/ |
241 |
+ |
public void testToMinutesSaturate() { |
242 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
243 |
+ |
long ratio = x.toNanos(1) / MINUTES.toNanos(1); |
244 |
+ |
if (ratio > 1) { |
245 |
+ |
long max = Long.MAX_VALUE/ratio; |
246 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
247 |
+ |
assertEquals(z * ratio, x.toMinutes(z)); |
248 |
+ |
assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1)); |
249 |
+ |
assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1)); |
250 |
+ |
assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE)); |
251 |
+ |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE)); |
252 |
+ |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1)); |
253 |
+ |
} |
254 |
+ |
} |
255 |
+ |
} |
256 |
+ |
|
257 |
+ |
/** |
258 |
+ |
* toHours saturates positive too-large values to Long.MAX_VALUE |
259 |
+ |
* and negative to LONG.MIN_VALUE |
260 |
+ |
*/ |
261 |
+ |
public void testToHoursSaturate() { |
262 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
263 |
+ |
long ratio = x.toNanos(1) / HOURS.toNanos(1); |
264 |
+ |
if (ratio >= 1) { |
265 |
+ |
long max = Long.MAX_VALUE/ratio; |
266 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
267 |
+ |
assertEquals(z * ratio, x.toHours(z)); |
268 |
+ |
if (max < Long.MAX_VALUE) { |
269 |
+ |
assertEquals(Long.MAX_VALUE, x.toHours(max + 1)); |
270 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(-max - 1)); |
271 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1)); |
272 |
+ |
} |
273 |
+ |
assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE)); |
274 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE)); |
275 |
+ |
} |
276 |
+ |
} |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
|
* toString returns name of unit |
281 |
|
*/ |
282 |
|
public void testToString() { |
283 |
+ |
assertEquals("NANOSECONDS", NANOSECONDS.toString()); |
284 |
+ |
assertEquals("MICROSECONDS", MICROSECONDS.toString()); |
285 |
+ |
assertEquals("MILLISECONDS", MILLISECONDS.toString()); |
286 |
|
assertEquals("SECONDS", SECONDS.toString()); |
287 |
+ |
assertEquals("MINUTES", MINUTES.toString()); |
288 |
+ |
assertEquals("HOURS", HOURS.toString()); |
289 |
+ |
assertEquals("DAYS", DAYS.toString()); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
|
* name returns name of unit |
294 |
|
*/ |
295 |
|
public void testName() { |
296 |
< |
assertEquals("SECONDS", SECONDS.name()); |
296 |
> |
for (TimeUnit x : TimeUnit.values()) |
297 |
> |
assertEquals(x.toString(), x.name()); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
305 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
306 |
|
public void realRun() throws InterruptedException { |
307 |
|
Object o = new Object(); |
319 |
– |
TimeUnit tu = MILLISECONDS; |
320 |
– |
|
308 |
|
try { |
309 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
309 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
310 |
|
threadShouldThrow(); |
311 |
|
} catch (IllegalMonitorStateException success) {} |
312 |
|
}}); |
322 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
323 |
|
public void realRun() throws InterruptedException { |
324 |
|
Object o = new Object(); |
338 |
– |
TimeUnit tu = MILLISECONDS; |
325 |
|
|
326 |
|
Thread.currentThread().interrupt(); |
327 |
|
try { |
328 |
|
synchronized (o) { |
329 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
329 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
330 |
|
} |
331 |
|
shouldThrow(); |
332 |
|
} catch (InterruptedException success) {} |
335 |
|
pleaseInterrupt.countDown(); |
336 |
|
try { |
337 |
|
synchronized (o) { |
338 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
338 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
339 |
|
} |
340 |
|
shouldThrow(); |
341 |
|
} catch (InterruptedException success) {} |
343 |
|
}}); |
344 |
|
|
345 |
|
await(pleaseInterrupt); |
346 |
< |
assertThreadStaysAlive(t); |
346 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
347 |
|
t.interrupt(); |
348 |
|
awaitTermination(t); |
349 |
|
} |
355 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
356 |
|
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
357 |
|
public void realRun() throws InterruptedException { |
358 |
< |
Thread.sleep(LONG_DELAY_MS); |
358 |
> |
Thread.sleep(LONGER_DELAY_MS); |
359 |
|
}}); |
360 |
|
final Thread t = newStartedThread(new CheckedRunnable() { |
361 |
|
public void realRun() throws InterruptedException { |
376 |
– |
TimeUnit tu = MILLISECONDS; |
362 |
|
Thread.currentThread().interrupt(); |
363 |
|
try { |
364 |
< |
tu.timedJoin(s, LONG_DELAY_MS); |
364 |
> |
MILLISECONDS.timedJoin(s, LONGER_DELAY_MS); |
365 |
|
shouldThrow(); |
366 |
|
} catch (InterruptedException success) {} |
367 |
|
assertFalse(Thread.interrupted()); |
368 |
|
|
369 |
|
pleaseInterrupt.countDown(); |
370 |
|
try { |
371 |
< |
tu.timedJoin(s, LONG_DELAY_MS); |
371 |
> |
MILLISECONDS.timedJoin(s, LONGER_DELAY_MS); |
372 |
|
shouldThrow(); |
373 |
|
} catch (InterruptedException success) {} |
374 |
|
assertFalse(Thread.interrupted()); |
375 |
|
}}); |
376 |
|
|
377 |
|
await(pleaseInterrupt); |
378 |
< |
assertThreadStaysAlive(t); |
378 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
379 |
|
t.interrupt(); |
380 |
|
awaitTermination(t); |
381 |
|
s.interrupt(); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
< |
* timedSleep throws InterruptedException when interrupted |
386 |
> |
* timeUnit.sleep throws InterruptedException when interrupted |
387 |
|
*/ |
388 |
|
public void testTimedSleep_Interruptible() { |
389 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
390 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
391 |
|
public void realRun() throws InterruptedException { |
407 |
– |
TimeUnit tu = MILLISECONDS; |
392 |
|
Thread.currentThread().interrupt(); |
393 |
|
try { |
394 |
< |
tu.sleep(LONG_DELAY_MS); |
394 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
395 |
|
shouldThrow(); |
396 |
|
} catch (InterruptedException success) {} |
397 |
|
assertFalse(Thread.interrupted()); |
398 |
|
|
399 |
|
pleaseInterrupt.countDown(); |
400 |
|
try { |
401 |
< |
tu.sleep(LONG_DELAY_MS); |
401 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
402 |
|
shouldThrow(); |
403 |
|
} catch (InterruptedException success) {} |
404 |
|
assertFalse(Thread.interrupted()); |
405 |
|
}}); |
406 |
|
|
407 |
|
await(pleaseInterrupt); |
408 |
< |
assertThreadStaysAlive(t); |
408 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
409 |
|
t.interrupt(); |
410 |
|
awaitTermination(t); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
< |
* a deserialized serialized unit is the same instance |
414 |
> |
* timeUnit.sleep(x) for x <= 0 does not sleep at all. |
415 |
|
*/ |
416 |
< |
public void testSerialization() throws Exception { |
417 |
< |
TimeUnit x = MILLISECONDS; |
418 |
< |
assertSame(x, serialClone(x)); |
416 |
> |
public void testTimedSleep_nonPositive() throws InterruptedException { |
417 |
> |
boolean interrupt = randomBoolean(); |
418 |
> |
if (interrupt) Thread.currentThread().interrupt(); |
419 |
> |
randomTimeUnit().sleep(0L); |
420 |
> |
randomTimeUnit().sleep(-1L); |
421 |
> |
randomTimeUnit().sleep(Long.MIN_VALUE); |
422 |
> |
if (interrupt) assertTrue(Thread.interrupted()); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
< |
* tests for toChronoUnit. |
426 |
> |
* a deserialized/reserialized unit is the same instance |
427 |
|
*/ |
428 |
< |
public void testToChronoUnit() throws Exception { |
441 |
< |
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); |
442 |
< |
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); |
443 |
< |
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); |
444 |
< |
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); |
445 |
< |
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); |
446 |
< |
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); |
447 |
< |
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); |
448 |
< |
|
449 |
< |
// Every TimeUnit has a defined ChronoUnit equivalent |
428 |
> |
public void testSerialization() throws Exception { |
429 |
|
for (TimeUnit x : TimeUnit.values()) |
430 |
< |
assertSame(x, TimeUnit.of(x.toChronoUnit())); |
452 |
< |
} |
453 |
< |
|
454 |
< |
/** |
455 |
< |
* tests for TimeUnit.of(ChronoUnit). |
456 |
< |
*/ |
457 |
< |
public void testTimeUnitOf() throws Exception { |
458 |
< |
assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); |
459 |
< |
assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); |
460 |
< |
assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); |
461 |
< |
assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); |
462 |
< |
assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); |
463 |
< |
assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); |
464 |
< |
assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); |
465 |
< |
|
466 |
< |
assertThrows(NullPointerException.class, |
467 |
< |
() -> TimeUnit.of((ChronoUnit)null)); |
468 |
< |
|
469 |
< |
// ChronoUnits either round trip to their TimeUnit |
470 |
< |
// equivalents, or throw IllegalArgumentException. |
471 |
< |
for (ChronoUnit cu : ChronoUnit.values()) { |
472 |
< |
final TimeUnit tu; |
473 |
< |
try { |
474 |
< |
tu = TimeUnit.of(cu); |
475 |
< |
} catch (IllegalArgumentException acceptable) { |
476 |
< |
continue; |
477 |
< |
} |
478 |
< |
assertSame(cu, tu.toChronoUnit()); |
479 |
< |
} |
430 |
> |
assertSame(x, serialClone(x)); |
431 |
|
} |
432 |
|
|
433 |
|
} |