14 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
|
17 |
+ |
import java.time.temporal.ChronoUnit; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.TimeUnit; |
20 |
|
|
23 |
|
|
24 |
|
public class TimeUnitTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
26 |
< |
junit.textui.TestRunner.run(suite()); |
26 |
> |
main(suite(), args); |
27 |
|
} |
28 |
|
|
29 |
|
public static Test suite() { |
97 |
|
assertEquals(t, |
98 |
|
NANOSECONDS.convert(t, NANOSECONDS)); |
99 |
|
} |
100 |
+ |
|
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 |
|
} |
110 |
|
|
111 |
|
/** |
290 |
|
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); |
291 |
|
assertEquals(Long.MIN_VALUE, |
292 |
|
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); |
293 |
+ |
|
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 |
|
} |
313 |
|
|
314 |
|
/** |
320 |
|
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
321 |
|
assertEquals(Long.MIN_VALUE, |
322 |
|
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
323 |
+ |
|
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 |
|
} |
454 |
|
|
455 |
|
/** |
588 |
|
* a deserialized serialized unit is the same instance |
589 |
|
*/ |
590 |
|
public void testSerialization() throws Exception { |
591 |
< |
TimeUnit x = MILLISECONDS; |
592 |
< |
assertSame(x, serialClone(x)); |
591 |
> |
for (TimeUnit x : TimeUnit.values()) |
592 |
> |
assertSame(x, serialClone(x)); |
593 |
> |
} |
594 |
> |
|
595 |
> |
/** |
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 |
|
} |