--- jsr166/src/test/tck/TimeUnitTest.java 2003/09/07 20:39:11 1.2 +++ jsr166/src/test/tck/TimeUnitTest.java 2003/09/14 20:42:41 1.3 @@ -10,10 +10,7 @@ import junit.framework.*; import java.util.concurrent.*; import java.io.*; -public class TimeUnitTest extends TestCase { - static public boolean DEBUG = false; - - +public class TimeUnitTest extends JSR166TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } @@ -85,7 +82,49 @@ public class TimeUnitTest extends TestCa assertEquals(1000 * t, TimeUnit.MICROSECONDS.toNanos(t)); assertEquals(t, - TimeUnit.SECONDS.NANOSECONDS.toNanos(t)); + TimeUnit.NANOSECONDS.toNanos(t)); + } + } + + public void testToMicros() { + for (long t = 0; t < 10; ++t) { + assertEquals(1000000 * t, + TimeUnit.SECONDS.toMicros(t)); + + assertEquals(1000 * t, + TimeUnit.MILLISECONDS.toMicros(t)); + assertEquals(t, + TimeUnit.MICROSECONDS.toMicros(t)); + assertEquals(t, + TimeUnit.NANOSECONDS.toMicros(t * 1000)); + } + } + + public void testToMillis() { + for (long t = 0; t < 10; ++t) { + assertEquals(1000 * t, + TimeUnit.SECONDS.toMillis(t)); + + assertEquals(t, + TimeUnit.MILLISECONDS.toMillis(t)); + assertEquals(t, + TimeUnit.MICROSECONDS.toMillis(t * 1000)); + assertEquals(t, + TimeUnit.NANOSECONDS.toMillis(t * 1000000)); + } + } + + public void testToSeconds() { + for (long t = 0; t < 10; ++t) { + assertEquals(t, + TimeUnit.SECONDS.toSeconds(t)); + + assertEquals(t, + TimeUnit.MILLISECONDS.toSeconds(t * 1000)); + assertEquals(t, + TimeUnit.MICROSECONDS.toSeconds(t * 1000000)); + assertEquals(t, + TimeUnit.NANOSECONDS.toSeconds(t * 1000000000)); } } @@ -114,12 +153,12 @@ public class TimeUnitTest extends TestCa assertTrue(s.indexOf("econd") >= 0); } - // Exception tests /** - * This test specifically to catch the unreported exception + * Timed wait without holding lock throws + * IllegalMonitorStateException */ - public void testTimedWaitForUnreportedIllegalMonitorException() { + public void testTimedWait_IllegalMonitorException() { //created a new thread with anonymous runnable Thread t = new Thread(new Runnable() { @@ -127,7 +166,7 @@ public class TimeUnitTest extends TestCa Object o = new Object(); TimeUnit tu = TimeUnit.MILLISECONDS; try { - tu.timedWait(o,40000); + tu.timedWait(o,LONG_DELAY_MS); fail("should throw"); } catch (InterruptedException ie) { @@ -140,7 +179,7 @@ public class TimeUnitTest extends TestCa }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) { @@ -149,7 +188,7 @@ public class TimeUnitTest extends TestCa } /** - * Test to verify that timedWait will throw InterruptedException. + * 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 @@ -162,7 +201,7 @@ public class TimeUnitTest extends TestCa TimeUnit tu = TimeUnit.MILLISECONDS; try { synchronized(o) { - tu.timedWait(o,1000); + tu.timedWait(o,MEDIUM_DELAY_MS); } fail("should throw"); } @@ -174,7 +213,7 @@ public class TimeUnitTest extends TestCa }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) { @@ -184,7 +223,7 @@ public class TimeUnitTest extends TestCa /** - * Test to verify that timedJoin will throw InterruptedException. + * timedJoin will throw InterruptedException. * Thread t waits on timedJoin while the main thread interrupts it. */ public void testTimedJoin() { @@ -195,12 +234,12 @@ public class TimeUnitTest extends TestCa Thread s = new Thread(new Runnable() { public void run() { try{ - Thread.sleep(1000); + Thread.sleep(MEDIUM_DELAY_MS); }catch(InterruptedException success){} } }); s.start(); - tu.timedJoin(s,1000); + tu.timedJoin(s,MEDIUM_DELAY_MS); fail("should throw"); } catch(Exception e) {} @@ -208,7 +247,7 @@ public class TimeUnitTest extends TestCa }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) { @@ -217,7 +256,7 @@ public class TimeUnitTest extends TestCa } /** - * Test to verify that timedSleep will throw InterruptedException. + * timedSleep will throw InterruptedException. * Thread t waits on timedSleep while the main thread interrupts it. */ public void testTimedSleep() { @@ -227,7 +266,7 @@ public class TimeUnitTest extends TestCa public void run() { TimeUnit tu = TimeUnit.MILLISECONDS; try { - tu.sleep(1000); + tu.sleep(MEDIUM_DELAY_MS); fail("should throw"); } catch(InterruptedException success) {} @@ -235,7 +274,7 @@ public class TimeUnitTest extends TestCa }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) {