--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2003/09/20 18:20:07 1.4 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2003/12/27 19:26:42 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. */ @@ -35,14 +36,14 @@ public class ArrayBlockingQueueTest exte } /** - * + * A new queue has the indicated capacity */ public void testConstructor1() { assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); } /** - * + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { @@ -53,10 +54,9 @@ public class ArrayBlockingQueueTest exte } /** - * + * Initializing from null Collection throws NPE */ public void testConstructor3() { - try { ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); shouldThrow(); @@ -65,7 +65,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * Initializing from Collection of null elements throws NPE */ public void testConstructor4() { try { @@ -77,7 +77,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { try { @@ -91,7 +91,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * Initializing from too large collection throws IAE */ public void testConstructor6() { try { @@ -105,7 +105,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * Queue contains all elements of collection used to initialize */ public void testConstructor7() { try { @@ -120,7 +120,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * Queue transitions from empty to full when elements added */ public void testEmptyFull() { ArrayBlockingQueue q = new ArrayBlockingQueue(2); @@ -135,7 +135,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -152,7 +152,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * offer(null) throws NPE */ public void testOfferNull() { try { @@ -163,7 +163,18 @@ public class ArrayBlockingQueueTest exte } /** - * + * add(null) throws NPE + */ + public void testAddNull() { + try { + ArrayBlockingQueue q = new ArrayBlockingQueue(1); + q.add(null); + shouldThrow(); + } catch (NullPointerException success) { } + } + + /** + * Offer succeeds if not full; fails if full */ public void testOffer() { ArrayBlockingQueue q = new ArrayBlockingQueue(1); @@ -172,7 +183,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * add succeeds if not full; throws ISE if full */ public void testAdd() { try { @@ -187,7 +198,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * addAll(null) throws NPE */ public void testAddAll1() { try { @@ -197,8 +208,22 @@ public class ArrayBlockingQueueTest exte } catch (NullPointerException success) {} } + /** - * + * addAll(this) throws IAE + */ + public void testAddAllSelf() { + try { + ArrayBlockingQueue q = populatedQueue(SIZE); + q.addAll(q); + shouldThrow(); + } + catch (IllegalArgumentException success) {} + } + + + /** + * addAll of a collection with null elements throws NPE */ public void testAddAll2() { try { @@ -210,7 +235,8 @@ public class ArrayBlockingQueueTest exte catch (NullPointerException success) {} } /** - * + * addAll of a collection with any null elements throws NPE after + * possibly adding some elements */ public void testAddAll3() { try { @@ -224,7 +250,7 @@ public class ArrayBlockingQueueTest exte catch (NullPointerException success) {} } /** - * + * addAll throws ISE if not enough room */ public void testAddAll4() { try { @@ -238,7 +264,7 @@ public class ArrayBlockingQueueTest exte catch (IllegalStateException success) {} } /** - * + * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { try { @@ -256,7 +282,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * put(null) throws NPE */ public void testPutNull() { try { @@ -272,7 +298,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * all elements successfully put are contained */ public void testPut() { try { @@ -290,7 +316,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * put blocks interruptibly if full */ public void testBlockingPut() { Thread t = new Thread(new Runnable() { @@ -320,7 +346,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * put blocks waiting for take when full */ public void testPutWithTake() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); @@ -354,7 +380,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * timed offer times out if full and elements not taken */ public void testTimedOffer() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); @@ -381,7 +407,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * take retrieves elements in FIFO order */ public void testTake() { try { @@ -395,7 +421,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * take blocks interruptibly when empty */ public void testTakeFromEmpty() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); @@ -418,7 +444,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() { Thread t = new Thread(new Runnable() { @@ -446,7 +472,7 @@ public class ArrayBlockingQueueTest exte /** - * + * poll succeeds unless empty */ public void testPoll() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -457,7 +483,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * timed pool with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() { try { @@ -472,7 +498,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * timed pool with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() { try { @@ -487,7 +513,8 @@ public class ArrayBlockingQueueTest exte } /** - * + * Interrupted timed poll throws InterruptedException instead of + * returning timeout status */ public void testInterruptedTimedPoll() { Thread t = new Thread(new Runnable() { @@ -513,7 +540,8 @@ public class ArrayBlockingQueueTest exte } /** - * + * timed poll before a delayed offer fails; after offer succeeds; + * on interruption throws */ public void testTimedPollWithOffer() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); @@ -540,7 +568,7 @@ public class ArrayBlockingQueueTest exte /** - * + * peek returns next element, or null if empty */ public void testPeek() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -554,7 +582,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * element returns next element, or throws NSEE if empty */ public void testElement() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -570,7 +598,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * remove removes next element, or throws NSEE if empty */ public void testRemove() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -585,7 +613,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * remove(x) removes x and returns true if present */ public void testRemoveElement() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -600,7 +628,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * contains(x) reports true when elements added but not yet removed */ public void testContains() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -612,7 +640,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * clear removes all elements */ public void testClear() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -627,7 +655,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -641,7 +669,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -660,7 +688,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * removeAll(c) removes only those elements of c and reports true if changed */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { @@ -675,9 +703,8 @@ public class ArrayBlockingQueueTest exte } } - /** - * + * toArray contains all elements */ public void testToArray() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -691,7 +718,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * toArray(a) contains all elements */ public void testToArray2() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -704,9 +731,32 @@ public class ArrayBlockingQueueTest exte unexpectedException(); } } + + /** + * toArray(null) throws NPE + */ + public void testToArray_BadArg() { + try { + ArrayBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(null); + shouldThrow(); + } catch(NullPointerException success){} + } + + /** + * toArray with incompatable array type throws CCE + */ + public void testToArray1_BadArg() { + try { + ArrayBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch(ArrayStoreException success){} + } + /** - * + * iterator iterates through all elements */ public void testIterator() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -721,7 +771,26 @@ public class ArrayBlockingQueueTest exte } /** - * + * iterator.remove removes current element + */ + public void testIteratorRemove () { + final ArrayBlockingQueue q = new ArrayBlockingQueue(3); + q.add(two); + q.add(one); + q.add(three); + + Iterator it = q.iterator(); + it.next(); + it.remove(); + + it = q.iterator(); + assertEquals(it.next(), one); + assertEquals(it.next(), three); + assertFalse(it.hasNext()); + } + + /** + * iterator ordering is FIFO */ public void testIteratorOrdering() { final ArrayBlockingQueue q = new ArrayBlockingQueue(3); @@ -740,7 +809,7 @@ public class ArrayBlockingQueueTest exte } /** - * + * Modifications do not cause iterators to fail */ public void testWeaklyConsistentIteration () { final ArrayBlockingQueue q = new ArrayBlockingQueue(3); @@ -756,13 +825,12 @@ public class ArrayBlockingQueueTest exte catch (ConcurrentModificationException e) { unexpectedException(); } - assertEquals(0, q.size()); } /** - * + * toString contains toStrings of elements */ public void testToString() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -774,17 +842,13 @@ public class ArrayBlockingQueueTest exte /** - * + * offer transfers elements across Executor tasks */ public void testOfferInExecutor() { - final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - q.add(one); q.add(two); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { public void run() { threadAssertFalse(q.offer(three)); @@ -815,14 +879,11 @@ public class ArrayBlockingQueueTest exte } /** - * + * poll retrieves elements across Executor threads */ public void testPollInExecutor() { - final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { public void run() { threadAssertNull(q.poll()); @@ -849,11 +910,10 @@ public class ArrayBlockingQueueTest exte }); joinPool(executor); - } /** - * + * A deserialized serialized queue has same elements in same order */ public void testSerialization() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -875,5 +935,110 @@ public class ArrayBlockingQueueTest exte } } + /** + * drainTo(null) throws NPE + */ + public void testDrainToNull() { + ArrayBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this) throws IAE + */ + public void testDrainToSelf() { + ArrayBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(q); + shouldThrow(); + } catch(IllegalArgumentException success) { + } + } + + /** + * drainTo(c) empties queue into another collection c + */ + public void testDrainTo() { + ArrayBlockingQueue q = populatedQueue(SIZE); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + } + + /** + * drainTo empties full queue, unblocking a waiting put. + */ + public void testDrainToWithActivePut() { + final ArrayBlockingQueue q = populatedQueue(SIZE); + Thread t = new Thread(new Runnable() { + public void run() { + try { + q.put(new Integer(SIZE+1)); + } catch (InterruptedException ie){ + threadUnexpectedException(); + } + } + }); + try { + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + t.join(); + assertTrue(q.size() + l.size() == SIZE+1); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * drainTo(null, n) throws NPE + */ + public void testDrainToNullN() { + ArrayBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null, 0); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this, n) throws IAE + */ + public void testDrainToSelfN() { + ArrayBlockingQueue 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) { + ArrayBlockingQueue 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); + for (int j = 0; j < k; ++j) + assertEquals(l.get(j), new Integer(j)); + } + } + }