--- jsr166/src/test/tck/DelayQueueTest.java 2003/09/25 11:02:41 1.5 +++ jsr166/src/test/tck/DelayQueueTest.java 2004/06/01 12:54:09 1.10 @@ -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. */ import junit.framework.*; @@ -21,14 +22,14 @@ public class DelayQueueTest extends JSR1 private static final int NOCAP = Integer.MAX_VALUE; /** - * A delayed implmentation for testing. + * A delayed implementation for testing. * Most tests use Pseudodelays, where delays are all elapsed * (so, no blocking solely for delays) but are still ordered */ static class PDelay implements Delayed { int pseudodelay; PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } - public int compareTo(Object y) { + public int compareTo(PDelay y) { int i = pseudodelay; int j = ((PDelay)y).pseudodelay; if (i < j) return -1; @@ -36,7 +37,7 @@ public class DelayQueueTest extends JSR1 return 0; } - public int compareTo(PDelay y) { + public int compareTo(Delayed y) { int i = pseudodelay; int j = ((PDelay)y).pseudodelay; if (i < j) return -1; @@ -73,7 +74,7 @@ public class DelayQueueTest extends JSR1 NanoDelay(long i) { trigger = System.nanoTime() + i; } - public int compareTo(Object y) { + public int compareTo(NanoDelay y) { long i = trigger; long j = ((NanoDelay)y).trigger; if (i < j) return -1; @@ -81,7 +82,7 @@ public class DelayQueueTest extends JSR1 return 0; } - public int compareTo(NanoDelay y) { + public int compareTo(Delayed y) { long i = trigger; long j = ((NanoDelay)y).trigger; if (i < j) return -1; @@ -232,6 +233,17 @@ public class DelayQueueTest extends JSR1 } /** + * add(null) throws NPE + */ + public void testAddNull() { + try { + DelayQueue q = new DelayQueue(); + q.add(null); + shouldThrow(); + } catch (NullPointerException success) { } + } + + /** * offer non-null succeeds */ public void testOffer() { @@ -262,6 +274,20 @@ public class DelayQueueTest extends JSR1 } catch (NullPointerException success) {} } + + + /** + * addAll(this) throws IAE + */ + public void testAddAllSelf() { + try { + DelayQueue q = populatedQueue(SIZE); + q.addAll(q); + shouldThrow(); + } + catch (IllegalArgumentException success) {} + } + /** * addAll of a collection with null elements throws NPE */ @@ -724,6 +750,29 @@ public class DelayQueueTest extends JSR1 unexpectedException(); } } + + + /** + * toArray(null) throws NPE + */ + public void testToArray_BadArg() { + try { + DelayQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(null); + shouldThrow(); + } catch(NullPointerException success){} + } + + /** + * toArray with incompatible array type throws CCE + */ + public void testToArray1_BadArg() { + try { + DelayQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch(ArrayStoreException success){} + } /** * iterator iterates through all elements @@ -804,7 +853,7 @@ public class DelayQueueTest extends JSR1 /** - * Dekayed actions do not occur until their delay elapses + * Delayed actions do not occur until their delay elapses */ public void testDelay() { DelayQueue q = new DelayQueue(); @@ -832,4 +881,101 @@ public class DelayQueueTest extends JSR1 } } + + /** + * drainTo(null) throws NPE + */ + public void testDrainToNull() { + DelayQueue q = populatedQueue(SIZE); + try { + q.drainTo(null); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this) throws IAE + */ + public void testDrainToSelf() { + DelayQueue q = populatedQueue(SIZE); + try { + q.drainTo(q); + shouldThrow(); + } catch(IllegalArgumentException success) { + } + } + + /** + * drainTo(c) empties queue into another collection c + */ + public void testDrainTo() { + DelayQueue q = populatedQueue(SIZE); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), SIZE); + } + + /** + * drainTo empties queue + */ + public void testDrainToWithActivePut() { + final DelayQueue q = populatedQueue(SIZE); + Thread t = new Thread(new Runnable() { + public void run() { + q.put(new PDelay(SIZE+1)); + } + }); + try { + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + t.join(); + assertTrue(q.size() + l.size() >= SIZE); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * drainTo(null, n) throws NPE + */ + public void testDrainToNullN() { + DelayQueue q = populatedQueue(SIZE); + try { + q.drainTo(null, 0); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this, n) throws IAE + */ + public void testDrainToSelfN() { + DelayQueue q = populatedQueue(SIZE); + try { + q.drainTo(q, 0); + shouldThrow(); + } catch(IllegalArgumentException success) { + } + } + + /** + * drainTo(c, n) empties first max {n, size} elements of queue into c + */ + public void testDrainToN() { + for (int i = 0; i < SIZE + 2; ++i) { + DelayQueue q = populatedQueue(SIZE); + ArrayList l = new ArrayList(); + q.drainTo(l, i); + int k = (i < SIZE)? i : SIZE; + assertEquals(q.size(), SIZE-k); + assertEquals(l.size(), k); + } + } + + }