--- jsr166/src/test/tck/DelayQueueTest.java 2003/09/20 18:20:07 1.4 +++ 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. - * Most Q/BQ tests use Pseudodelays, where delays are all elapsed + * 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; @@ -129,14 +130,14 @@ public class DelayQueueTest extends JSR1 } /** - * + * A new queue has unbounded capacity */ public void testConstructor1() { assertEquals(NOCAP, new DelayQueue().remainingCapacity()); } /** - * + * Initializing from null Collection throws NPE */ public void testConstructor3() { try { @@ -147,7 +148,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * Initializing from Collection of null elements throws NPE */ public void testConstructor4() { try { @@ -159,7 +160,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { try { @@ -173,7 +174,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * Queue contains all elements of collection used to initialize */ public void testConstructor6() { try { @@ -188,7 +189,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * isEmpty is true before add, false after */ public void testEmpty() { DelayQueue q = new DelayQueue(); @@ -203,7 +204,8 @@ public class DelayQueueTest extends JSR1 } /** - * + * remainingCapacity does not change when elementa added or removed, + * but size does */ public void testRemainingCapacity() { DelayQueue q = populatedQueue(SIZE); @@ -220,7 +222,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * offer(null) throws NPE */ public void testOfferNull() { try { @@ -231,7 +233,18 @@ 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() { DelayQueue q = new DelayQueue(); @@ -240,7 +253,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * add succeeds */ public void testAdd() { DelayQueue q = new DelayQueue(); @@ -251,7 +264,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * addAll(null) throws NPE */ public void testAddAll1() { try { @@ -261,8 +274,22 @@ 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 */ public void testAddAll2() { try { @@ -274,7 +301,8 @@ public class DelayQueueTest extends JSR1 catch (NullPointerException success) {} } /** - * + * addAll of a collection with any null elements throws NPE after + * possibly adding some elements */ public void testAddAll3() { try { @@ -289,7 +317,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * Queue contains all elements of successful addAll */ public void testAddAll5() { try { @@ -307,7 +335,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * put(null) throws NPE */ public void testPutNull() { try { @@ -320,7 +348,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * all elements successfully put are contained */ public void testPut() { try { @@ -337,7 +365,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * put doesn't block waiting for take */ public void testPutWithTake() { final DelayQueue q = new DelayQueue(); @@ -370,7 +398,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * timed offer does not time out */ public void testTimedOffer() { final DelayQueue q = new DelayQueue(); @@ -396,7 +424,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * take retrieves elements in priority order */ public void testTake() { try { @@ -410,7 +438,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * take blocks interruptibly when empty */ public void testTakeFromEmpty() { final DelayQueue q = new DelayQueue(); @@ -433,7 +461,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() { Thread t = new Thread(new Runnable() { @@ -461,7 +489,7 @@ public class DelayQueueTest extends JSR1 /** - * + * poll succeeds unless empty */ public void testPoll() { DelayQueue q = populatedQueue(SIZE); @@ -472,7 +500,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * timed pool with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() { try { @@ -487,7 +515,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * timed pool with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() { try { @@ -502,7 +530,8 @@ public class DelayQueueTest extends JSR1 } /** - * + * Interrupted timed poll throws InterruptedException instead of + * returning timeout status */ public void testInterruptedTimedPoll() { Thread t = new Thread(new Runnable() { @@ -528,7 +557,8 @@ public class DelayQueueTest extends JSR1 } /** - * + * timed poll before a delayed offer fails; after offer succeeds; + * on interruption throws */ public void testTimedPollWithOffer() { final DelayQueue q = new DelayQueue(); @@ -555,7 +585,7 @@ public class DelayQueueTest extends JSR1 /** - * + * peek returns next element, or null if empty */ public void testPeek() { DelayQueue q = populatedQueue(SIZE); @@ -569,7 +599,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * element returns next element, or throws NSEE if empty */ public void testElement() { DelayQueue q = populatedQueue(SIZE); @@ -585,7 +615,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * remove removes next element, or throws NSEE if empty */ public void testRemove() { DelayQueue q = populatedQueue(SIZE); @@ -600,7 +630,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * remove(x) removes x and returns true if present */ public void testRemoveElement() { DelayQueue q = populatedQueue(SIZE); @@ -615,7 +645,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * contains(x) reports true when elements added but not yet removed */ public void testContains() { DelayQueue q = populatedQueue(SIZE); @@ -627,7 +657,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * clear removes all elements */ public void testClear() { DelayQueue q = populatedQueue(SIZE); @@ -642,7 +672,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { DelayQueue q = populatedQueue(SIZE); @@ -656,7 +686,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { DelayQueue q = populatedQueue(SIZE); @@ -675,7 +705,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * removeAll(c) removes only those elements of c and reports true if changed */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { @@ -691,7 +721,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * toArray contains all elements */ public void testToArray() { DelayQueue q = populatedQueue(SIZE); @@ -706,7 +736,7 @@ public class DelayQueueTest extends JSR1 } /** - * + * toArray(a) contains all elements */ public void testToArray2() { DelayQueue q = populatedQueue(SIZE); @@ -720,9 +750,32 @@ 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 */ public void testIterator() { DelayQueue q = populatedQueue(SIZE); @@ -736,20 +789,16 @@ public class DelayQueueTest extends JSR1 } /** - * + * iterator.remove removes current element */ public void testIteratorRemove () { - final DelayQueue q = new DelayQueue(); - q.add(new PDelay(2)); q.add(new PDelay(1)); q.add(new PDelay(3)); - Iterator it = q.iterator(); it.next(); it.remove(); - it = q.iterator(); assertEquals(it.next(), new PDelay(2)); assertEquals(it.next(), new PDelay(3)); @@ -758,7 +807,7 @@ public class DelayQueueTest extends JSR1 /** - * + * toString contains toStrings of elements */ public void testToString() { DelayQueue q = populatedQueue(SIZE); @@ -769,14 +818,11 @@ public class DelayQueueTest extends JSR1 } /** - * + * offer transfers elements across Executor tasks */ public void testPollInExecutor() { - final DelayQueue q = new DelayQueue(); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { public void run() { threadAssertNull(q.poll()); @@ -801,14 +847,13 @@ public class DelayQueueTest extends JSR1 } } }); - joinPool(executor); } /** - * + * Delayed actions do not occur until their delay elapses */ public void testDelay() { DelayQueue q = new DelayQueue(); @@ -836,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); + } + } + + }