--- jsr166/src/test/tck/TimeUnitTest.java 2003/08/31 19:24:56 1.1 +++ jsr166/src/test/tck/TimeUnitTest.java 2003/09/20 18:20:08 1.4 @@ -8,11 +8,9 @@ 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()); } @@ -21,6 +19,9 @@ public class TimeUnitTest extends TestCa return new TestSuite(TimeUnitTest.class); } + /** + * + */ public void testConvert() { for (long t = 0; t < 10; ++t) { assertEquals(t, @@ -74,6 +75,9 @@ public class TimeUnitTest extends TestCa } } + /** + * + */ public void testToNanos() { for (long t = 0; t < 10; ++t) { assertEquals(1000000000 * t, @@ -84,11 +88,65 @@ 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)); } } + /** + * + */ public void testConvertSaturate() { assertEquals(Long.MAX_VALUE, TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, @@ -99,6 +157,9 @@ public class TimeUnitTest extends TestCa } + /** + * + */ public void testToNanosSaturate() { assertEquals(Long.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); @@ -108,17 +169,23 @@ public class TimeUnitTest extends TestCa } + /** + * + */ public void testToString() { String s = TimeUnit.SECONDS.toString(); 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() { @@ -126,11 +193,11 @@ public class TimeUnitTest extends TestCa Object o = new Object(); TimeUnit tu = TimeUnit.MILLISECONDS; try { - tu.timedWait(o,40000); - fail("should throw"); + tu.timedWait(o,LONG_DELAY_MS); + threadShouldThrow(); } catch (InterruptedException ie) { - fail("should not throw IE here"); + threadUnexpectedException(); } catch(IllegalMonitorStateException success) { } @@ -139,20 +206,23 @@ public class TimeUnitTest extends TestCa }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } /** - * 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 */ + /** + * + */ public void testTimedWait() { Thread t = new Thread(new Runnable() { public void run() { @@ -161,31 +231,34 @@ 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"); + threadShouldThrow(); } catch(InterruptedException success) {} catch(IllegalMonitorStateException failure) { - fail("should not throw"); + threadUnexpectedException(); } } }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } /** - * 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() { Thread t = new Thread(new Runnable() { public void run() { @@ -193,32 +266,35 @@ public class TimeUnitTest extends TestCa try { Thread s = new Thread(new Runnable() { public void run() { - try{ - Thread.sleep(1000); - }catch(InterruptedException success){} + try { + Thread.sleep(MEDIUM_DELAY_MS); + } catch(InterruptedException success){} } }); s.start(); - tu.timedJoin(s,1000); - fail("should throw"); + tu.timedJoin(s,MEDIUM_DELAY_MS); + threadShouldThrow(); } catch(Exception e) {} } }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } /** - * 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() { //created a new thread with anonymous runnable @@ -226,19 +302,43 @@ public class TimeUnitTest extends TestCa public void run() { TimeUnit tu = TimeUnit.MILLISECONDS; try { - tu.sleep(1000); - fail("should throw"); + tu.sleep(MEDIUM_DELAY_MS); + threadShouldThrow(); } catch(InterruptedException success) {} } }); t.start(); try { - Thread.sleep(100); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch(Exception e) { - fail("Unexpected exception"); + unexpectedException(); } } + + /** + * + */ + public void testSerialization() { + TimeUnit q = TimeUnit.MILLISECONDS; + + try { + ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); + ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); + out.writeObject(q); + out.close(); + + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); + TimeUnit r = (TimeUnit)in.readObject(); + + assertEquals(q.toString(), r.toString()); + } catch(Exception e){ + e.printStackTrace(); + unexpectedException(); + } + } + }