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.28 by jsr166, Fri Mar 18 16:55:09 2016 UTC vs.
Revision 1.33 by jsr166, Sun Aug 11 22:29:27 2019 UTC

# Line 14 | Line 14 | import static java.util.concurrent.TimeU
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  
# Line 294 | Line 293 | public class TimeUnitTest extends JSR166
293          for (TimeUnit x : TimeUnit.values())
294              for (TimeUnit y : TimeUnit.values()) {
295                  long ratio = x.toNanos(1) / y.toNanos(1);
296 <                if (ratio > 1) {
296 >                if (ratio >= 1) {
297                      assertEquals(ratio, y.convert(1, x));
298                      assertEquals(1, x.convert(ratio, y));
299                      long max = Long.MAX_VALUE/ratio;
# Line 302 | Line 301 | public class TimeUnitTest extends JSR166
301                      assertEquals(-max * ratio, y.convert(-max, x));
302                      assertEquals(max, x.convert(max * ratio, y));
303                      assertEquals(-max, x.convert(-max * ratio, y));
304 <                    assertEquals(Long.MAX_VALUE, y.convert(max + 1, x));
305 <                    assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x));
304 >                    if (max < Long.MAX_VALUE) {
305 >                        assertEquals(Long.MAX_VALUE, y.convert(max + 1, x));
306 >                        assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x));
307 >                        assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x));
308 >                    }
309                      assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x));
310                      assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x));
309                    assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x));
311                  }
312              }
313      }
# Line 323 | Line 324 | public class TimeUnitTest extends JSR166
324  
325          for (TimeUnit x : TimeUnit.values()) {
326              long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1);
327 <            if (ratio > 1) {
327 >            if (ratio >= 1) {
328                  long max = Long.MAX_VALUE/ratio;
329                  for (long z : new long[] {0, 1, -1, max, -max})
330                      assertEquals(z * ratio, x.toNanos(z));
331 <                assertEquals(Long.MAX_VALUE, x.toNanos(max + 1));
332 <                assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1));
331 >                if (max < Long.MAX_VALUE) {
332 >                    assertEquals(Long.MAX_VALUE, x.toNanos(max + 1));
333 >                    assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1));
334 >                    assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1));
335 >                }
336                  assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE));
337                  assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE));
334                assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1));
338                  if (max < Integer.MAX_VALUE) {
339                      assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE));
340                      assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE));
# Line 347 | Line 350 | public class TimeUnitTest extends JSR166
350      public void testToMicrosSaturate() {
351          for (TimeUnit x : TimeUnit.values()) {
352              long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1);
353 <            if (ratio > 1) {
353 >            if (ratio >= 1) {
354                  long max = Long.MAX_VALUE/ratio;
355                  for (long z : new long[] {0, 1, -1, max, -max})
356                      assertEquals(z * ratio, x.toMicros(z));
357 <                assertEquals(Long.MAX_VALUE, x.toMicros(max + 1));
358 <                assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1));
357 >                if (max < Long.MAX_VALUE) {
358 >                    assertEquals(Long.MAX_VALUE, x.toMicros(max + 1));
359 >                    assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1));
360 >                    assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1));
361 >                }
362                  assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE));
363                  assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE));
358                assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1));
364                  if (max < Integer.MAX_VALUE) {
365                      assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE));
366                      assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE));
# Line 371 | Line 376 | public class TimeUnitTest extends JSR166
376      public void testToMillisSaturate() {
377          for (TimeUnit x : TimeUnit.values()) {
378              long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1);
379 <            if (ratio > 1) {
379 >            if (ratio >= 1) {
380                  long max = Long.MAX_VALUE/ratio;
381                  for (long z : new long[] {0, 1, -1, max, -max})
382                      assertEquals(z * ratio, x.toMillis(z));
383 <                assertEquals(Long.MAX_VALUE, x.toMillis(max + 1));
384 <                assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1));
383 >                if (max < Long.MAX_VALUE) {
384 >                    assertEquals(Long.MAX_VALUE, x.toMillis(max + 1));
385 >                    assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1));
386 >                    assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1));
387 >                }
388                  assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE));
389                  assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE));
382                assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1));
390                  if (max < Integer.MAX_VALUE) {
391                      assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE));
392                      assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE));
# Line 395 | Line 402 | public class TimeUnitTest extends JSR166
402      public void testToSecondsSaturate() {
403          for (TimeUnit x : TimeUnit.values()) {
404              long ratio = x.toNanos(1) / SECONDS.toNanos(1);
405 <            if (ratio > 1) {
405 >            if (ratio >= 1) {
406                  long max = Long.MAX_VALUE/ratio;
407                  for (long z : new long[] {0, 1, -1, max, -max})
408                      assertEquals(z * ratio, x.toSeconds(z));
409 <                assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1));
410 <                assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1));
409 >                if (max < Long.MAX_VALUE) {
410 >                    assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1));
411 >                    assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1));
412 >                    assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1));
413 >                }
414                  assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE));
415                  assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE));
406                assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1));
416                  if (max < Integer.MAX_VALUE) {
417                      assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE));
418                      assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE));
# Line 439 | Line 448 | public class TimeUnitTest extends JSR166
448      public void testToHoursSaturate() {
449          for (TimeUnit x : TimeUnit.values()) {
450              long ratio = x.toNanos(1) / HOURS.toNanos(1);
451 <            if (ratio > 1) {
451 >            if (ratio >= 1) {
452                  long max = Long.MAX_VALUE/ratio;
453                  for (long z : new long[] {0, 1, -1, max, -max})
454                      assertEquals(z * ratio, x.toHours(z));
455 <                assertEquals(Long.MAX_VALUE, x.toHours(max + 1));
456 <                assertEquals(Long.MIN_VALUE, x.toHours(-max - 1));
455 >                if (max < Long.MAX_VALUE) {
456 >                    assertEquals(Long.MAX_VALUE, x.toHours(max + 1));
457 >                    assertEquals(Long.MIN_VALUE, x.toHours(-max - 1));
458 >                    assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1));
459 >                }
460                  assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE));
461                  assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE));
450                assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1));
462              }
463          }
464      }
# Line 456 | Line 467 | public class TimeUnitTest extends JSR166
467       * toString returns name of unit
468       */
469      public void testToString() {
470 +        assertEquals("NANOSECONDS", NANOSECONDS.toString());
471 +        assertEquals("MICROSECONDS", MICROSECONDS.toString());
472 +        assertEquals("MILLISECONDS", MILLISECONDS.toString());
473          assertEquals("SECONDS", SECONDS.toString());
474 +        assertEquals("MINUTES", MINUTES.toString());
475 +        assertEquals("HOURS", HOURS.toString());
476 +        assertEquals("DAYS", DAYS.toString());
477      }
478  
479      /**
480       * name returns name of unit
481       */
482      public void testName() {
483 <        assertEquals("SECONDS", SECONDS.name());
483 >        for (TimeUnit x : TimeUnit.values())
484 >            assertEquals(x.toString(), x.name());
485      }
486  
487      /**
# Line 473 | Line 491 | public class TimeUnitTest extends JSR166
491      public void testTimedWait_IllegalMonitorException() {
492          Thread t = newStartedThread(new CheckedRunnable() {
493              public void realRun() throws InterruptedException {
494 +                long startTime = System.nanoTime();
495                  Object o = new Object();
477                TimeUnit tu = MILLISECONDS;
496  
497                  try {
498 <                    tu.timedWait(o, LONG_DELAY_MS);
498 >                    MILLISECONDS.timedWait(o, LONG_DELAY_MS);
499                      threadShouldThrow();
500                  } catch (IllegalMonitorStateException success) {}
501 +
502 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
503              }});
504  
505          awaitTermination(t);
# Line 492 | Line 512 | public class TimeUnitTest extends JSR166
512          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
513          Thread t = newStartedThread(new CheckedRunnable() {
514              public void realRun() throws InterruptedException {
515 +                long startTime = System.nanoTime();
516                  Object o = new Object();
496                TimeUnit tu = MILLISECONDS;
517  
518                  Thread.currentThread().interrupt();
519                  try {
520                      synchronized (o) {
521 <                        tu.timedWait(o, LONG_DELAY_MS);
521 >                        MILLISECONDS.timedWait(o, LONG_DELAY_MS);
522                      }
523                      shouldThrow();
524                  } catch (InterruptedException success) {}
# Line 507 | Line 527 | public class TimeUnitTest extends JSR166
527                  pleaseInterrupt.countDown();
528                  try {
529                      synchronized (o) {
530 <                        tu.timedWait(o, LONG_DELAY_MS);
530 >                        MILLISECONDS.timedWait(o, LONG_DELAY_MS);
531                      }
532                      shouldThrow();
533                  } catch (InterruptedException success) {}
534                  assertFalse(Thread.interrupted());
535 +
536 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
537              }});
538  
539          await(pleaseInterrupt);
540 <        assertThreadStaysAlive(t);
540 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
541          t.interrupt();
542          awaitTermination(t);
543      }
# Line 531 | Line 553 | public class TimeUnitTest extends JSR166
553              }});
554          final Thread t = newStartedThread(new CheckedRunnable() {
555              public void realRun() throws InterruptedException {
556 <                TimeUnit tu = MILLISECONDS;
556 >                long startTime = System.nanoTime();
557 >
558                  Thread.currentThread().interrupt();
559                  try {
560 <                    tu.timedJoin(s, LONG_DELAY_MS);
560 >                    MILLISECONDS.timedJoin(s, LONG_DELAY_MS);
561                      shouldThrow();
562                  } catch (InterruptedException success) {}
563                  assertFalse(Thread.interrupted());
564  
565                  pleaseInterrupt.countDown();
566                  try {
567 <                    tu.timedJoin(s, LONG_DELAY_MS);
567 >                    MILLISECONDS.timedJoin(s, LONG_DELAY_MS);
568                      shouldThrow();
569                  } catch (InterruptedException success) {}
570                  assertFalse(Thread.interrupted());
571 +
572 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
573              }});
574  
575          await(pleaseInterrupt);
576 <        assertThreadStaysAlive(t);
576 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
577          t.interrupt();
578          awaitTermination(t);
579          s.interrupt();
# Line 562 | Line 587 | public class TimeUnitTest extends JSR166
587          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
588          Thread t = newStartedThread(new CheckedRunnable() {
589              public void realRun() throws InterruptedException {
590 <                TimeUnit tu = MILLISECONDS;
590 >                long startTime = System.nanoTime();
591 >
592                  Thread.currentThread().interrupt();
593                  try {
594 <                    tu.sleep(LONG_DELAY_MS);
594 >                    MILLISECONDS.sleep(LONG_DELAY_MS);
595                      shouldThrow();
596                  } catch (InterruptedException success) {}
597                  assertFalse(Thread.interrupted());
598  
599                  pleaseInterrupt.countDown();
600                  try {
601 <                    tu.sleep(LONG_DELAY_MS);
601 >                    MILLISECONDS.sleep(LONG_DELAY_MS);
602                      shouldThrow();
603                  } catch (InterruptedException success) {}
604                  assertFalse(Thread.interrupted());
605 +
606 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
607              }});
608  
609          await(pleaseInterrupt);
610 <        assertThreadStaysAlive(t);
610 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
611          t.interrupt();
612          awaitTermination(t);
613      }
614  
615      /**
616 <     * a deserialized serialized unit is the same instance
616 >     * a deserialized/reserialized unit is the same instance
617       */
618      public void testSerialization() throws Exception {
619          for (TimeUnit x : TimeUnit.values())
620              assertSame(x, serialClone(x));
621      }
622  
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
623   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines