--- jsr166/src/test/tck/TimeUnitTest.java 2003/09/14 20:42:41 1.3 +++ jsr166/src/test/tck/TimeUnitTest.java 2003/12/29 19:05:40 1.7 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ @@ -19,6 +20,9 @@ public class TimeUnitTest extends JSR166 return new TestSuite(TimeUnitTest.class); } + /** + * convert correctly converts sample values across the four units + */ public void testConvert() { for (long t = 0; t < 10; ++t) { assertEquals(t, @@ -72,6 +76,10 @@ public class TimeUnitTest extends JSR166 } } + /** + * toNanos correctly converts sample values in different units to + * nanoseconds + */ public void testToNanos() { for (long t = 0; t < 10; ++t) { assertEquals(1000000000 * t, @@ -86,6 +94,10 @@ public class TimeUnitTest extends JSR166 } } + /** + * toMicros correctly converts sample values in different units to + * microseconds + */ public void testToMicros() { for (long t = 0; t < 10; ++t) { assertEquals(1000000 * t, @@ -100,6 +112,10 @@ public class TimeUnitTest extends JSR166 } } + /** + * toMillis correctly converts sample values in different units to + * milliseconds + */ public void testToMillis() { for (long t = 0; t < 10; ++t) { assertEquals(1000 * t, @@ -114,6 +130,10 @@ public class TimeUnitTest extends JSR166 } } + /** + * toSeconds correctly converts sample values in different units to + * seconds + */ public void testToSeconds() { for (long t = 0; t < 10; ++t) { assertEquals(t, @@ -129,6 +149,10 @@ public class TimeUnitTest extends JSR166 } + /** + * convert saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ public void testConvertSaturate() { assertEquals(Long.MAX_VALUE, TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, @@ -136,18 +160,23 @@ public class TimeUnitTest extends JSR166 assertEquals(Long.MIN_VALUE, TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, TimeUnit.SECONDS)); - } + /** + * toNanos saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ public void testToNanosSaturate() { assertEquals(Long.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); assertEquals(Long.MIN_VALUE, TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); - } + /** + * toString returns string containing common name of unit + */ public void testToString() { String s = TimeUnit.SECONDS.toString(); assertTrue(s.indexOf("econd") >= 0); @@ -167,10 +196,10 @@ public class TimeUnitTest extends JSR166 TimeUnit tu = TimeUnit.MILLISECONDS; try { tu.timedWait(o,LONG_DELAY_MS); - fail("should throw"); + threadShouldThrow(); } catch (InterruptedException ie) { - fail("should not throw IE here"); + threadUnexpectedException(); } catch(IllegalMonitorStateException success) { } @@ -183,15 +212,12 @@ public class TimeUnitTest extends JSR166 t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } /** - * timedWait will throw InterruptedException. - * Thread t waits on timedWait while the main thread interrupts it. - * Note: This does not throw IllegalMonitorException since timeWait - * is synchronized on o + * timedWait throws InterruptedException when interrupted */ public void testTimedWait() { Thread t = new Thread(new Runnable() { @@ -203,11 +229,11 @@ public class TimeUnitTest extends JSR166 synchronized(o) { tu.timedWait(o,MEDIUM_DELAY_MS); } - fail("should throw"); + threadShouldThrow(); } catch(InterruptedException success) {} catch(IllegalMonitorStateException failure) { - fail("should not throw"); + threadUnexpectedException(); } } }); @@ -217,14 +243,13 @@ public class TimeUnitTest extends JSR166 t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } /** - * timedJoin will throw InterruptedException. - * Thread t waits on timedJoin while the main thread interrupts it. + * timedJoin throws InterruptedException when interrupted */ public void testTimedJoin() { Thread t = new Thread(new Runnable() { @@ -233,14 +258,14 @@ public class TimeUnitTest extends JSR166 try { Thread s = new Thread(new Runnable() { public void run() { - try{ + try { Thread.sleep(MEDIUM_DELAY_MS); - }catch(InterruptedException success){} + } catch(InterruptedException success){} } }); s.start(); tu.timedJoin(s,MEDIUM_DELAY_MS); - fail("should throw"); + threadShouldThrow(); } catch(Exception e) {} } @@ -251,13 +276,12 @@ public class TimeUnitTest extends JSR166 t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } /** - * timedSleep will throw InterruptedException. - * Thread t waits on timedSleep while the main thread interrupts it. + * timedSleep throws InterruptedException when interrupted */ public void testTimedSleep() { //created a new thread with anonymous runnable @@ -267,7 +291,7 @@ public class TimeUnitTest extends JSR166 TimeUnit tu = TimeUnit.MILLISECONDS; try { tu.sleep(MEDIUM_DELAY_MS); - fail("should throw"); + threadShouldThrow(); } catch(InterruptedException success) {} } @@ -278,10 +302,13 @@ public class TimeUnitTest extends JSR166 t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } + /** + * a deserialized serialized unit is equal + */ public void testSerialization() { TimeUnit q = TimeUnit.MILLISECONDS; @@ -298,7 +325,7 @@ public class TimeUnitTest extends JSR166 assertEquals(q.toString(), r.toString()); } catch(Exception e){ e.printStackTrace(); - fail("unexpected exception"); + unexpectedException(); } }