[cvs] / jsr166 / src / test / tck / TimeUnitTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/TimeUnitTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (view) (download)

1 : dl 1.1 /*
2 : dl 1.6 * Written by Doug Lea with assistance from members of JCP JSR-166
3 :     * Expert Group and released to the public domain, as explained at
4 : jsr166 1.19 * http://creativecommons.org/publicdomain/zero/1.0/
5 : jsr166 1.10 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 :     * Pat Fisher, Mike Judd.
7 : dl 1.1 */
8 :    
9 : jsr166 1.24 import static java.util.concurrent.TimeUnit.DAYS;
10 :     import static java.util.concurrent.TimeUnit.HOURS;
11 :     import static java.util.concurrent.TimeUnit.MICROSECONDS;
12 :     import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 :     import static java.util.concurrent.TimeUnit.MINUTES;
14 :     import static java.util.concurrent.TimeUnit.NANOSECONDS;
15 :     import static java.util.concurrent.TimeUnit.SECONDS;
16 :    
17 : jsr166 1.26 import java.time.temporal.ChronoUnit;
18 : jsr166 1.22 import java.util.concurrent.CountDownLatch;
19 :     import java.util.concurrent.TimeUnit;
20 : jsr166 1.24
21 :     import junit.framework.Test;
22 :     import junit.framework.TestSuite;
23 : dl 1.1
24 : dl 1.3 public class TimeUnitTest extends JSR166TestCase {
25 : dl 1.1 public static void main(String[] args) {
26 : jsr166 1.25 main(suite(), args);
27 : dl 1.1 }
28 : jsr166 1.10
29 : dl 1.1 public static Test suite() {
30 : jsr166 1.14 return new TestSuite(TimeUnitTest.class);
31 : dl 1.1 }
32 :    
33 : dl 1.9 // (loops to 88888 check increments at all time divisions.)
34 :    
35 : dl 1.4 /**
36 : dl 1.9 * convert correctly converts sample values across the units
37 : dl 1.4 */
38 : dl 1.1 public void testConvert() {
39 : dl 1.9 for (long t = 0; t < 88888; ++t) {
40 : jsr166 1.10 assertEquals(t*60*60*24,
41 : jsr166 1.23 SECONDS.convert(t, DAYS));
42 : jsr166 1.10 assertEquals(t*60*60,
43 : jsr166 1.23 SECONDS.convert(t, HOURS));
44 : jsr166 1.10 assertEquals(t*60,
45 : jsr166 1.23 SECONDS.convert(t, MINUTES));
46 : jsr166 1.10 assertEquals(t,
47 : jsr166 1.23 SECONDS.convert(t, SECONDS));
48 : jsr166 1.10 assertEquals(t,
49 : jsr166 1.23 SECONDS.convert(1000L*t, MILLISECONDS));
50 : jsr166 1.10 assertEquals(t,
51 : jsr166 1.23 SECONDS.convert(1000000L*t, MICROSECONDS));
52 : jsr166 1.10 assertEquals(t,
53 : jsr166 1.23 SECONDS.convert(1000000000L*t, NANOSECONDS));
54 : dl 1.9
55 : jsr166 1.10 assertEquals(1000L*t*60*60*24,
56 : jsr166 1.23 MILLISECONDS.convert(t, DAYS));
57 : jsr166 1.10 assertEquals(1000L*t*60*60,
58 : jsr166 1.23 MILLISECONDS.convert(t, HOURS));
59 : jsr166 1.10 assertEquals(1000L*t*60,
60 : jsr166 1.23 MILLISECONDS.convert(t, MINUTES));
61 : jsr166 1.10 assertEquals(1000L*t,
62 : jsr166 1.23 MILLISECONDS.convert(t, SECONDS));
63 : jsr166 1.10 assertEquals(t,
64 : jsr166 1.23 MILLISECONDS.convert(t, MILLISECONDS));
65 : jsr166 1.10 assertEquals(t,
66 : jsr166 1.23 MILLISECONDS.convert(1000L*t, MICROSECONDS));
67 : jsr166 1.10 assertEquals(t,
68 : jsr166 1.23 MILLISECONDS.convert(1000000L*t, NANOSECONDS));
69 : dl 1.9
70 : jsr166 1.10 assertEquals(1000000L*t*60*60*24,
71 : jsr166 1.23 MICROSECONDS.convert(t, DAYS));
72 : jsr166 1.10 assertEquals(1000000L*t*60*60,
73 : jsr166 1.23 MICROSECONDS.convert(t, HOURS));
74 : jsr166 1.10 assertEquals(1000000L*t*60,
75 : jsr166 1.23 MICROSECONDS.convert(t, MINUTES));
76 : jsr166 1.10 assertEquals(1000000L*t,
77 : jsr166 1.23 MICROSECONDS.convert(t, SECONDS));
78 : jsr166 1.10 assertEquals(1000L*t,
79 : jsr166 1.23 MICROSECONDS.convert(t, MILLISECONDS));
80 : jsr166 1.10 assertEquals(t,
81 : jsr166 1.23 MICROSECONDS.convert(t, MICROSECONDS));
82 : jsr166 1.10 assertEquals(t,
83 : jsr166 1.23 MICROSECONDS.convert(1000L*t, NANOSECONDS));
84 : dl 1.9
85 : jsr166 1.10 assertEquals(1000000000L*t*60*60*24,
86 : jsr166 1.23 NANOSECONDS.convert(t, DAYS));
87 : jsr166 1.10 assertEquals(1000000000L*t*60*60,
88 : jsr166 1.23 NANOSECONDS.convert(t, HOURS));
89 : jsr166 1.10 assertEquals(1000000000L*t*60,
90 : jsr166 1.23 NANOSECONDS.convert(t, MINUTES));
91 : jsr166 1.10 assertEquals(1000000000L*t,
92 : jsr166 1.23 NANOSECONDS.convert(t, SECONDS));
93 : jsr166 1.10 assertEquals(1000000L*t,
94 : jsr166 1.23 NANOSECONDS.convert(t, MILLISECONDS));
95 : jsr166 1.10 assertEquals(1000L*t,
96 : jsr166 1.23 NANOSECONDS.convert(t, MICROSECONDS));
97 : jsr166 1.10 assertEquals(t,
98 : jsr166 1.23 NANOSECONDS.convert(t, NANOSECONDS));
99 : dl 1.1 }
100 : jsr166 1.28
101 :     for (TimeUnit x : TimeUnit.values()) {
102 :     long[] zs = {
103 :     0, 1, -1,
104 :     Integer.MAX_VALUE, Integer.MIN_VALUE,
105 :     Long.MAX_VALUE, Long.MIN_VALUE,
106 :     };
107 :     for (long z : zs) assertEquals(z, x.convert(z, x));
108 :     }
109 : dl 1.1 }
110 :    
111 : dl 1.4 /**
112 : dl 1.5 * toNanos correctly converts sample values in different units to
113 :     * nanoseconds
114 : dl 1.4 */
115 : dl 1.1 public void testToNanos() {
116 : dl 1.9 for (long t = 0; t < 88888; ++t) {
117 : jsr166 1.10 assertEquals(t*1000000000L*60*60*24,
118 : jsr166 1.23 DAYS.toNanos(t));
119 : jsr166 1.10 assertEquals(t*1000000000L*60*60,
120 : jsr166 1.23 HOURS.toNanos(t));
121 : jsr166 1.10 assertEquals(t*1000000000L*60,
122 : jsr166 1.23 MINUTES.toNanos(t));
123 : jsr166 1.10 assertEquals(1000000000L*t,
124 : jsr166 1.23 SECONDS.toNanos(t));
125 : jsr166 1.10 assertEquals(1000000L*t,
126 : jsr166 1.23 MILLISECONDS.toNanos(t));
127 : jsr166 1.10 assertEquals(1000L*t,
128 : jsr166 1.23 MICROSECONDS.toNanos(t));
129 : jsr166 1.10 assertEquals(t,
130 : jsr166 1.23 NANOSECONDS.toNanos(t));
131 : dl 1.3 }
132 :     }
133 :    
134 : dl 1.4 /**
135 : dl 1.5 * toMicros correctly converts sample values in different units to
136 :     * microseconds
137 : dl 1.4 */
138 : dl 1.3 public void testToMicros() {
139 : dl 1.9 for (long t = 0; t < 88888; ++t) {
140 : jsr166 1.10 assertEquals(t*1000000L*60*60*24,
141 : jsr166 1.23 DAYS.toMicros(t));
142 : jsr166 1.10 assertEquals(t*1000000L*60*60,
143 : jsr166 1.23 HOURS.toMicros(t));
144 : jsr166 1.10 assertEquals(t*1000000L*60,
145 : jsr166 1.23 MINUTES.toMicros(t));
146 : jsr166 1.10 assertEquals(1000000L*t,
147 : jsr166 1.23 SECONDS.toMicros(t));
148 : jsr166 1.10 assertEquals(1000L*t,
149 : jsr166 1.23 MILLISECONDS.toMicros(t));
150 : jsr166 1.10 assertEquals(t,
151 : jsr166 1.23 MICROSECONDS.toMicros(t));
152 : jsr166 1.10 assertEquals(t,
153 : jsr166 1.23 NANOSECONDS.toMicros(t*1000L));
154 : dl 1.3 }
155 :     }
156 :    
157 : dl 1.4 /**
158 : dl 1.5 * toMillis correctly converts sample values in different units to
159 :     * milliseconds
160 : dl 1.4 */
161 : dl 1.3 public void testToMillis() {
162 : dl 1.9 for (long t = 0; t < 88888; ++t) {
163 : jsr166 1.10 assertEquals(t*1000L*60*60*24,
164 : jsr166 1.23 DAYS.toMillis(t));
165 : jsr166 1.10 assertEquals(t*1000L*60*60,
166 : jsr166 1.23 HOURS.toMillis(t));
167 : jsr166 1.10 assertEquals(t*1000L*60,
168 : jsr166 1.23 MINUTES.toMillis(t));
169 : jsr166 1.10 assertEquals(1000L*t,
170 : jsr166 1.23 SECONDS.toMillis(t));
171 : jsr166 1.10 assertEquals(t,
172 : jsr166 1.23 MILLISECONDS.toMillis(t));
173 : jsr166 1.10 assertEquals(t,
174 : jsr166 1.23 MICROSECONDS.toMillis(t*1000L));
175 : jsr166 1.10 assertEquals(t,
176 : jsr166 1.23 NANOSECONDS.toMillis(t*1000000L));
177 : dl 1.3 }
178 :     }
179 :    
180 : dl 1.4 /**
181 : dl 1.5 * toSeconds correctly converts sample values in different units to
182 :     * seconds
183 : dl 1.4 */
184 : dl 1.3 public void testToSeconds() {
185 : dl 1.9 for (long t = 0; t < 88888; ++t) {
186 : jsr166 1.10 assertEquals(t*60*60*24,
187 : jsr166 1.23 DAYS.toSeconds(t));
188 : jsr166 1.10 assertEquals(t*60*60,
189 : jsr166 1.23 HOURS.toSeconds(t));
190 : jsr166 1.10 assertEquals(t*60,
191 : jsr166 1.23 MINUTES.toSeconds(t));
192 : jsr166 1.10 assertEquals(t,
193 : jsr166 1.23 SECONDS.toSeconds(t));
194 : jsr166 1.10 assertEquals(t,
195 : jsr166 1.23 MILLISECONDS.toSeconds(t*1000L));
196 : jsr166 1.10 assertEquals(t,
197 : jsr166 1.23 MICROSECONDS.toSeconds(t*1000000L));
198 : jsr166 1.10 assertEquals(t,
199 : jsr166 1.23 NANOSECONDS.toSeconds(t*1000000000L));
200 : dl 1.9 }
201 :     }
202 : dl 1.3
203 : dl 1.9 /**
204 :     * toMinutes correctly converts sample values in different units to
205 :     * minutes
206 :     */
207 :     public void testToMinutes() {
208 :     for (long t = 0; t < 88888; ++t) {
209 : jsr166 1.10 assertEquals(t*60*24,
210 : jsr166 1.23 DAYS.toMinutes(t));
211 : jsr166 1.10 assertEquals(t*60,
212 : jsr166 1.23 HOURS.toMinutes(t));
213 : jsr166 1.10 assertEquals(t,
214 : jsr166 1.23 MINUTES.toMinutes(t));
215 : jsr166 1.10 assertEquals(t,
216 : jsr166 1.23 SECONDS.toMinutes(t*60));
217 : jsr166 1.10 assertEquals(t,
218 : jsr166 1.23 MILLISECONDS.toMinutes(t*1000L*60));
219 : jsr166 1.10 assertEquals(t,
220 : jsr166 1.23 MICROSECONDS.toMinutes(t*1000000L*60));
221 : jsr166 1.10 assertEquals(t,
222 : jsr166 1.23 NANOSECONDS.toMinutes(t*1000000000L*60));
223 : dl 1.9 }
224 :     }
225 :    
226 :     /**
227 :     * toHours correctly converts sample values in different units to
228 :     * hours
229 :     */
230 :     public void testToHours() {
231 :     for (long t = 0; t < 88888; ++t) {
232 : jsr166 1.10 assertEquals(t*24,
233 : jsr166 1.23 DAYS.toHours(t));
234 : jsr166 1.10 assertEquals(t,
235 : jsr166 1.23 HOURS.toHours(t));
236 : jsr166 1.10 assertEquals(t,
237 : jsr166 1.23 MINUTES.toHours(t*60));
238 : jsr166 1.10 assertEquals(t,
239 : jsr166 1.23 SECONDS.toHours(t*60*60));
240 : jsr166 1.10 assertEquals(t,
241 : jsr166 1.23 MILLISECONDS.toHours(t*1000L*60*60));
242 : jsr166 1.10 assertEquals(t,
243 : jsr166 1.23 MICROSECONDS.toHours(t*1000000L*60*60));
244 : jsr166 1.10 assertEquals(t,
245 : jsr166 1.23 NANOSECONDS.toHours(t*1000000000L*60*60));
246 : dl 1.9 }
247 :     }
248 :    
249 :     /**
250 :     * toDays correctly converts sample values in different units to
251 :     * days
252 :     */
253 :     public void testToDays() {
254 :     for (long t = 0; t < 88888; ++t) {
255 : jsr166 1.10 assertEquals(t,
256 : jsr166 1.23 DAYS.toDays(t));
257 : jsr166 1.10 assertEquals(t,
258 : jsr166 1.23 HOURS.toDays(t*24));
259 : jsr166 1.10 assertEquals(t,
260 : jsr166 1.23 MINUTES.toDays(t*60*24));
261 : jsr166 1.10 assertEquals(t,
262 : jsr166 1.23 SECONDS.toDays(t*60*60*24));
263 : jsr166 1.10 assertEquals(t,
264 : jsr166 1.23 MILLISECONDS.toDays(t*1000L*60*60*24));
265 : jsr166 1.10 assertEquals(t,
266 : jsr166 1.23 MICROSECONDS.toDays(t*1000000L*60*60*24));
267 : jsr166 1.10 assertEquals(t,
268 : jsr166 1.23 NANOSECONDS.toDays(t*1000000000L*60*60*24));
269 : dl 1.1 }
270 :     }
271 :    
272 : dl 1.4 /**
273 : jsr166 1.10 * convert saturates positive too-large values to Long.MAX_VALUE
274 : dl 1.5 * and negative to LONG.MIN_VALUE
275 : dl 1.4 */
276 : dl 1.1 public void testConvertSaturate() {
277 :     assertEquals(Long.MAX_VALUE,
278 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
279 : dl 1.1 assertEquals(Long.MIN_VALUE,
280 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
281 : dl 1.9 assertEquals(Long.MAX_VALUE,
282 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
283 : dl 1.9 assertEquals(Long.MIN_VALUE,
284 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
285 : dl 1.9 assertEquals(Long.MAX_VALUE,
286 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
287 : dl 1.9 assertEquals(Long.MIN_VALUE,
288 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
289 : dl 1.9 assertEquals(Long.MAX_VALUE,
290 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
291 : dl 1.9 assertEquals(Long.MIN_VALUE,
292 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
293 : jsr166 1.28
294 :     for (TimeUnit x : TimeUnit.values())
295 :     for (TimeUnit y : TimeUnit.values()) {
296 :     long ratio = x.toNanos(1) / y.toNanos(1);
297 :     if (ratio > 1) {
298 :     assertEquals(ratio, y.convert(1, x));
299 :     assertEquals(1, x.convert(ratio, y));
300 :     long max = Long.MAX_VALUE/ratio;
301 :     assertEquals(max * ratio, y.convert(max, x));
302 :     assertEquals(-max * ratio, y.convert(-max, x));
303 :     assertEquals(max, x.convert(max * ratio, y));
304 :     assertEquals(-max, x.convert(-max * ratio, y));
305 :     assertEquals(Long.MAX_VALUE, y.convert(max + 1, x));
306 :     assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x));
307 :     assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x));
308 :     assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x));
309 :     assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x));
310 :     }
311 :     }
312 : dl 1.1 }
313 :    
314 : dl 1.4 /**
315 : jsr166 1.10 * toNanos saturates positive too-large values to Long.MAX_VALUE
316 : dl 1.5 * and negative to LONG.MIN_VALUE
317 : dl 1.4 */
318 : dl 1.1 public void testToNanosSaturate() {
319 : jsr166 1.13 assertEquals(Long.MAX_VALUE,
320 : jsr166 1.23 MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
321 : jsr166 1.13 assertEquals(Long.MIN_VALUE,
322 : jsr166 1.23 MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
323 : jsr166 1.28
324 :     for (TimeUnit x : TimeUnit.values()) {
325 :     long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1);
326 :     if (ratio > 1) {
327 :     long max = Long.MAX_VALUE/ratio;
328 :     for (long z : new long[] {0, 1, -1, max, -max})
329 :     assertEquals(z * ratio, x.toNanos(z));
330 :     assertEquals(Long.MAX_VALUE, x.toNanos(max + 1));
331 :     assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1));
332 :     assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE));
333 :     assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE));
334 :     assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1));
335 :     if (max < Integer.MAX_VALUE) {
336 :     assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE));
337 :     assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE));
338 :     }
339 :     }
340 :     }
341 :     }
342 :    
343 :     /**
344 :     * toMicros saturates positive too-large values to Long.MAX_VALUE
345 :     * and negative to LONG.MIN_VALUE
346 :     */
347 :     public void testToMicrosSaturate() {
348 :     for (TimeUnit x : TimeUnit.values()) {
349 :     long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1);
350 :     if (ratio > 1) {
351 :     long max = Long.MAX_VALUE/ratio;
352 :     for (long z : new long[] {0, 1, -1, max, -max})
353 :     assertEquals(z * ratio, x.toMicros(z));
354 :     assertEquals(Long.MAX_VALUE, x.toMicros(max + 1));
355 :     assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1));
356 :     assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE));
357 :     assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE));
358 :     assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1));
359 :     if (max < Integer.MAX_VALUE) {
360 :     assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE));
361 :     assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE));
362 :     }
363 :     }
364 :     }
365 :     }
366 :    
367 :     /**
368 :     * toMillis saturates positive too-large values to Long.MAX_VALUE
369 :     * and negative to LONG.MIN_VALUE
370 :     */
371 :     public void testToMillisSaturate() {
372 :     for (TimeUnit x : TimeUnit.values()) {
373 :     long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1);
374 :     if (ratio > 1) {
375 :     long max = Long.MAX_VALUE/ratio;
376 :     for (long z : new long[] {0, 1, -1, max, -max})
377 :     assertEquals(z * ratio, x.toMillis(z));
378 :     assertEquals(Long.MAX_VALUE, x.toMillis(max + 1));
379 :     assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1));
380 :     assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE));
381 :     assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE));
382 :     assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1));
383 :     if (max < Integer.MAX_VALUE) {
384 :     assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE));
385 :     assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE));
386 :     }
387 :     }
388 :     }
389 :     }
390 :    
391 :     /**
392 :     * toSeconds saturates positive too-large values to Long.MAX_VALUE
393 :     * and negative to LONG.MIN_VALUE
394 :     */
395 :     public void testToSecondsSaturate() {
396 :     for (TimeUnit x : TimeUnit.values()) {
397 :     long ratio = x.toNanos(1) / SECONDS.toNanos(1);
398 :     if (ratio > 1) {
399 :     long max = Long.MAX_VALUE/ratio;
400 :     for (long z : new long[] {0, 1, -1, max, -max})
401 :     assertEquals(z * ratio, x.toSeconds(z));
402 :     assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1));
403 :     assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1));
404 :     assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE));
405 :     assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE));
406 :     assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1));
407 :     if (max < Integer.MAX_VALUE) {
408 :     assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE));
409 :     assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE));
410 :     }
411 :     }
412 :     }
413 :     }
414 :    
415 :     /**
416 :     * toMinutes saturates positive too-large values to Long.MAX_VALUE
417 :     * and negative to LONG.MIN_VALUE
418 :     */
419 :     public void testToMinutesSaturate() {
420 :     for (TimeUnit x : TimeUnit.values()) {
421 :     long ratio = x.toNanos(1) / MINUTES.toNanos(1);
422 :     if (ratio > 1) {
423 :     long max = Long.MAX_VALUE/ratio;
424 :     for (long z : new long[] {0, 1, -1, max, -max})
425 :     assertEquals(z * ratio, x.toMinutes(z));
426 :     assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1));
427 :     assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1));
428 :     assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE));
429 :     assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE));
430 :     assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1));
431 :     }
432 :     }
433 :     }
434 :    
435 :     /**
436 :     * toHours saturates positive too-large values to Long.MAX_VALUE
437 :     * and negative to LONG.MIN_VALUE
438 :     */
439 :     public void testToHoursSaturate() {
440 :     for (TimeUnit x : TimeUnit.values()) {
441 :     long ratio = x.toNanos(1) / HOURS.toNanos(1);
442 :     if (ratio > 1) {
443 :     long max = Long.MAX_VALUE/ratio;
444 :     for (long z : new long[] {0, 1, -1, max, -max})
445 :     assertEquals(z * ratio, x.toHours(z));
446 :     assertEquals(Long.MAX_VALUE, x.toHours(max + 1));
447 :     assertEquals(Long.MIN_VALUE, x.toHours(-max - 1));
448 :     assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE));
449 :     assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE));
450 :     assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1));
451 :     }
452 :     }
453 : dl 1.1 }
454 :    
455 : dl 1.4 /**
456 : jsr166 1.20 * toString returns name of unit
457 : dl 1.4 */
458 : dl 1.1 public void testToString() {
459 : jsr166 1.23 assertEquals("SECONDS", SECONDS.toString());
460 : dl 1.1 }
461 :    
462 : jsr166 1.20 /**
463 :     * name returns name of unit
464 :     */
465 :     public void testName() {
466 : jsr166 1.23 assertEquals("SECONDS", SECONDS.name());
467 : jsr166 1.20 }
468 : jsr166 1.10
469 : dl 1.1 /**
470 : jsr166 1.17 * Timed wait without holding lock throws
471 :     * IllegalMonitorStateException
472 : dl 1.1 */
473 : jsr166 1.21 public void testTimedWait_IllegalMonitorException() {
474 :     Thread t = newStartedThread(new CheckedRunnable() {
475 : jsr166 1.13 public void realRun() throws InterruptedException {
476 :     Object o = new Object();
477 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
478 : jsr166 1.21
479 : jsr166 1.13 try {
480 : jsr166 1.21 tu.timedWait(o, LONG_DELAY_MS);
481 : jsr166 1.13 threadShouldThrow();
482 : jsr166 1.21 } catch (IllegalMonitorStateException success) {}
483 :     }});
484 : dl 1.1
485 : jsr166 1.21 awaitTermination(t);
486 : dl 1.1 }
487 : jsr166 1.10
488 : dl 1.1 /**
489 : dl 1.5 * timedWait throws InterruptedException when interrupted
490 : dl 1.4 */
491 : jsr166 1.21 public void testTimedWait_Interruptible() {
492 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
493 :     Thread t = newStartedThread(new CheckedRunnable() {
494 : jsr166 1.14 public void realRun() throws InterruptedException {
495 : jsr166 1.13 Object o = new Object();
496 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
497 : jsr166 1.13
498 : jsr166 1.21 Thread.currentThread().interrupt();
499 :     try {
500 :     synchronized (o) {
501 :     tu.timedWait(o, LONG_DELAY_MS);
502 :     }
503 :     shouldThrow();
504 :     } catch (InterruptedException success) {}
505 :     assertFalse(Thread.interrupted());
506 :    
507 :     pleaseInterrupt.countDown();
508 :     try {
509 :     synchronized (o) {
510 :     tu.timedWait(o, LONG_DELAY_MS);
511 :     }
512 :     shouldThrow();
513 :     } catch (InterruptedException success) {}
514 :     assertFalse(Thread.interrupted());
515 : jsr166 1.13 }});
516 : jsr166 1.21
517 :     await(pleaseInterrupt);
518 :     assertThreadStaysAlive(t);
519 : jsr166 1.13 t.interrupt();
520 : jsr166 1.21 awaitTermination(t);
521 : dl 1.1 }
522 : jsr166 1.10
523 : dl 1.1 /**
524 : dl 1.5 * timedJoin throws InterruptedException when interrupted
525 : dl 1.4 */
526 : jsr166 1.21 public void testTimedJoin_Interruptible() {
527 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
528 :     final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
529 : jsr166 1.13 public void realRun() throws InterruptedException {
530 : jsr166 1.21 Thread.sleep(LONG_DELAY_MS);
531 : jsr166 1.13 }});
532 : jsr166 1.21 final Thread t = newStartedThread(new CheckedRunnable() {
533 : jsr166 1.14 public void realRun() throws InterruptedException {
534 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
535 : jsr166 1.21 Thread.currentThread().interrupt();
536 :     try {
537 :     tu.timedJoin(s, LONG_DELAY_MS);
538 :     shouldThrow();
539 :     } catch (InterruptedException success) {}
540 :     assertFalse(Thread.interrupted());
541 :    
542 :     pleaseInterrupt.countDown();
543 :     try {
544 :     tu.timedJoin(s, LONG_DELAY_MS);
545 :     shouldThrow();
546 :     } catch (InterruptedException success) {}
547 :     assertFalse(Thread.interrupted());
548 : jsr166 1.18 }});
549 : jsr166 1.21
550 :     await(pleaseInterrupt);
551 :     assertThreadStaysAlive(t);
552 : jsr166 1.13 t.interrupt();
553 : jsr166 1.21 awaitTermination(t);
554 : jsr166 1.13 s.interrupt();
555 : jsr166 1.21 awaitTermination(s);
556 : dl 1.1 }
557 : jsr166 1.10
558 : dl 1.1 /**
559 : jsr166 1.17 * timedSleep throws InterruptedException when interrupted
560 : dl 1.4 */
561 : jsr166 1.21 public void testTimedSleep_Interruptible() {
562 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
563 :     Thread t = newStartedThread(new CheckedRunnable() {
564 : jsr166 1.14 public void realRun() throws InterruptedException {
565 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
566 : jsr166 1.21 Thread.currentThread().interrupt();
567 :     try {
568 :     tu.sleep(LONG_DELAY_MS);
569 :     shouldThrow();
570 :     } catch (InterruptedException success) {}
571 :     assertFalse(Thread.interrupted());
572 :    
573 :     pleaseInterrupt.countDown();
574 :     try {
575 :     tu.sleep(LONG_DELAY_MS);
576 :     shouldThrow();
577 :     } catch (InterruptedException success) {}
578 :     assertFalse(Thread.interrupted());
579 : jsr166 1.13 }});
580 : dl 1.1
581 : jsr166 1.21 await(pleaseInterrupt);
582 :     assertThreadStaysAlive(t);
583 : jsr166 1.13 t.interrupt();
584 : jsr166 1.21 awaitTermination(t);
585 : dl 1.1 }
586 : dl 1.2
587 : dl 1.4 /**
588 : jsr166 1.13 * a deserialized serialized unit is the same instance
589 : dl 1.4 */
590 : jsr166 1.13 public void testSerialization() throws Exception {
591 : jsr166 1.27 for (TimeUnit x : TimeUnit.values())
592 :     assertSame(x, serialClone(x));
593 : dl 1.2 }
594 :    
595 : jsr166 1.26 /**
596 :     * tests for toChronoUnit.
597 :     */
598 :     public void testToChronoUnit() throws Exception {
599 :     assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit());
600 :     assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit());
601 :     assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit());
602 :     assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
603 :     assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
604 :     assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit());
605 :     assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit());
606 :    
607 :     // Every TimeUnit has a defined ChronoUnit equivalent
608 :     for (TimeUnit x : TimeUnit.values())
609 :     assertSame(x, TimeUnit.of(x.toChronoUnit()));
610 :     }
611 :    
612 :     /**
613 :     * tests for TimeUnit.of(ChronoUnit).
614 :     */
615 :     public void testTimeUnitOf() throws Exception {
616 :     assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS));
617 :     assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS));
618 :     assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS));
619 :     assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS));
620 :     assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES));
621 :     assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS));
622 :     assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS));
623 :    
624 :     assertThrows(NullPointerException.class,
625 :     () -> TimeUnit.of((ChronoUnit)null));
626 :    
627 :     // ChronoUnits either round trip to their TimeUnit
628 :     // equivalents, or throw IllegalArgumentException.
629 :     for (ChronoUnit cu : ChronoUnit.values()) {
630 :     final TimeUnit tu;
631 :     try {
632 :     tu = TimeUnit.of(cu);
633 :     } catch (IllegalArgumentException acceptable) {
634 :     continue;
635 :     }
636 :     assertSame(cu, tu.toChronoUnit());
637 :     }
638 :     }
639 :    
640 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8