ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TimeUnitTest.java (file contents):
Revision 1.23 by jsr166, Tue Sep 24 16:48:52 2013 UTC vs.
Revision 1.29 by jsr166, Fri Mar 25 04:53:28 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
9 > 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 > import java.time.temporal.ChronoUnit;
18   import java.util.concurrent.CountDownLatch;
19   import java.util.concurrent.TimeUnit;
20 < import static java.util.concurrent.TimeUnit.*;
20 >
21 > import junit.framework.Test;
22 > import junit.framework.TestSuite;
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() {
# Line 87 | Line 97 | public class TimeUnitTest extends JSR166
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      /**
# Line 271 | Line 290 | public class TimeUnitTest extends JSR166
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 +                    if (max < Long.MAX_VALUE) {
306 +                        assertEquals(Long.MAX_VALUE, y.convert(max + 1, x));
307 +                        assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x));
308 +                        assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x));
309 +                    }
310 +                    assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x));
311 +                    assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x));
312 +                }
313 +            }
314      }
315  
316      /**
# Line 282 | Line 322 | public class TimeUnitTest extends JSR166
322                       MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
323          assertEquals(Long.MIN_VALUE,
324                       MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
325 +
326 +        for (TimeUnit x : TimeUnit.values()) {
327 +            long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1);
328 +            if (ratio >= 1) {
329 +                long max = Long.MAX_VALUE/ratio;
330 +                for (long z : new long[] {0, 1, -1, max, -max})
331 +                    assertEquals(z * ratio, x.toNanos(z));
332 +                if (max < Long.MAX_VALUE) {
333 +                    assertEquals(Long.MAX_VALUE, x.toNanos(max + 1));
334 +                    assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1));
335 +                    assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1));
336 +                }
337 +                assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE));
338 +                assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE));
339 +                if (max < Integer.MAX_VALUE) {
340 +                    assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE));
341 +                    assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE));
342 +                }
343 +            }
344 +        }
345 +    }
346 +
347 +    /**
348 +     * toMicros saturates positive too-large values to Long.MAX_VALUE
349 +     * and negative to LONG.MIN_VALUE
350 +     */
351 +    public void testToMicrosSaturate() {
352 +        for (TimeUnit x : TimeUnit.values()) {
353 +            long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1);
354 +            if (ratio >= 1) {
355 +                long max = Long.MAX_VALUE/ratio;
356 +                for (long z : new long[] {0, 1, -1, max, -max})
357 +                    assertEquals(z * ratio, x.toMicros(z));
358 +                if (max < Long.MAX_VALUE) {
359 +                    assertEquals(Long.MAX_VALUE, x.toMicros(max + 1));
360 +                    assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1));
361 +                    assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1));
362 +                }
363 +                assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE));
364 +                assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE));
365 +                if (max < Integer.MAX_VALUE) {
366 +                    assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE));
367 +                    assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE));
368 +                }
369 +            }
370 +        }
371 +    }
372 +
373 +    /**
374 +     * toMillis saturates positive too-large values to Long.MAX_VALUE
375 +     * and negative to LONG.MIN_VALUE
376 +     */
377 +    public void testToMillisSaturate() {
378 +        for (TimeUnit x : TimeUnit.values()) {
379 +            long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1);
380 +            if (ratio >= 1) {
381 +                long max = Long.MAX_VALUE/ratio;
382 +                for (long z : new long[] {0, 1, -1, max, -max})
383 +                    assertEquals(z * ratio, x.toMillis(z));
384 +                if (max < Long.MAX_VALUE) {
385 +                    assertEquals(Long.MAX_VALUE, x.toMillis(max + 1));
386 +                    assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1));
387 +                    assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1));
388 +                }
389 +                assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE));
390 +                assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE));
391 +                if (max < Integer.MAX_VALUE) {
392 +                    assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE));
393 +                    assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE));
394 +                }
395 +            }
396 +        }
397 +    }
398 +
399 +    /**
400 +     * toSeconds saturates positive too-large values to Long.MAX_VALUE
401 +     * and negative to LONG.MIN_VALUE
402 +     */
403 +    public void testToSecondsSaturate() {
404 +        for (TimeUnit x : TimeUnit.values()) {
405 +            long ratio = x.toNanos(1) / SECONDS.toNanos(1);
406 +            if (ratio >= 1) {
407 +                long max = Long.MAX_VALUE/ratio;
408 +                for (long z : new long[] {0, 1, -1, max, -max})
409 +                    assertEquals(z * ratio, x.toSeconds(z));
410 +                if (max < Long.MAX_VALUE) {
411 +                    assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1));
412 +                    assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1));
413 +                    assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1));
414 +                }
415 +                assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE));
416 +                assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE));
417 +                if (max < Integer.MAX_VALUE) {
418 +                    assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE));
419 +                    assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE));
420 +                }
421 +            }
422 +        }
423 +    }
424 +
425 +    /**
426 +     * toMinutes saturates positive too-large values to Long.MAX_VALUE
427 +     * and negative to LONG.MIN_VALUE
428 +     */
429 +    public void testToMinutesSaturate() {
430 +        for (TimeUnit x : TimeUnit.values()) {
431 +            long ratio = x.toNanos(1) / MINUTES.toNanos(1);
432 +            if (ratio > 1) {
433 +                long max = Long.MAX_VALUE/ratio;
434 +                for (long z : new long[] {0, 1, -1, max, -max})
435 +                    assertEquals(z * ratio, x.toMinutes(z));
436 +                assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1));
437 +                assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1));
438 +                assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE));
439 +                assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE));
440 +                assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1));
441 +            }
442 +        }
443 +    }
444 +
445 +    /**
446 +     * toHours saturates positive too-large values to Long.MAX_VALUE
447 +     * and negative to LONG.MIN_VALUE
448 +     */
449 +    public void testToHoursSaturate() {
450 +        for (TimeUnit x : TimeUnit.values()) {
451 +            long ratio = x.toNanos(1) / HOURS.toNanos(1);
452 +            if (ratio >= 1) {
453 +                long max = Long.MAX_VALUE/ratio;
454 +                for (long z : new long[] {0, 1, -1, max, -max})
455 +                    assertEquals(z * ratio, x.toHours(z));
456 +                if (max < Long.MAX_VALUE) {
457 +                    assertEquals(Long.MAX_VALUE, x.toHours(max + 1));
458 +                    assertEquals(Long.MIN_VALUE, x.toHours(-max - 1));
459 +                    assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1));
460 +                }
461 +                assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE));
462 +                assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE));
463 +            }
464 +        }
465      }
466  
467      /**
# Line 420 | Line 600 | public class TimeUnitTest extends JSR166
600       * a deserialized serialized unit is the same instance
601       */
602      public void testSerialization() throws Exception {
603 <        TimeUnit x = MILLISECONDS;
604 <        assertSame(x, serialClone(x));
603 >        for (TimeUnit x : TimeUnit.values())
604 >            assertSame(x, serialClone(x));
605 >    }
606 >
607 >    /**
608 >     * tests for toChronoUnit.
609 >     */
610 >    public void testToChronoUnit() throws Exception {
611 >        assertSame(ChronoUnit.NANOS,   NANOSECONDS.toChronoUnit());
612 >        assertSame(ChronoUnit.MICROS,  MICROSECONDS.toChronoUnit());
613 >        assertSame(ChronoUnit.MILLIS,  MILLISECONDS.toChronoUnit());
614 >        assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
615 >        assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
616 >        assertSame(ChronoUnit.HOURS,   HOURS.toChronoUnit());
617 >        assertSame(ChronoUnit.DAYS,    DAYS.toChronoUnit());
618 >
619 >        // Every TimeUnit has a defined ChronoUnit equivalent
620 >        for (TimeUnit x : TimeUnit.values())
621 >            assertSame(x, TimeUnit.of(x.toChronoUnit()));
622 >    }
623 >
624 >    /**
625 >     * tests for TimeUnit.of(ChronoUnit).
626 >     */
627 >    public void testTimeUnitOf() throws Exception {
628 >        assertSame(NANOSECONDS,  TimeUnit.of(ChronoUnit.NANOS));
629 >        assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS));
630 >        assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS));
631 >        assertSame(SECONDS,      TimeUnit.of(ChronoUnit.SECONDS));
632 >        assertSame(MINUTES,      TimeUnit.of(ChronoUnit.MINUTES));
633 >        assertSame(HOURS,        TimeUnit.of(ChronoUnit.HOURS));
634 >        assertSame(DAYS,         TimeUnit.of(ChronoUnit.DAYS));
635 >
636 >        assertThrows(NullPointerException.class,
637 >                     () -> TimeUnit.of((ChronoUnit)null));
638 >
639 >        // ChronoUnits either round trip to their TimeUnit
640 >        // equivalents, or throw IllegalArgumentException.
641 >        for (ChronoUnit cu : ChronoUnit.values()) {
642 >            final TimeUnit tu;
643 >            try {
644 >                tu = TimeUnit.of(cu);
645 >            } catch (IllegalArgumentException acceptable) {
646 >                continue;
647 >            }
648 >            assertSame(cu, tu.toChronoUnit());
649 >        }
650      }
651  
652   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines