--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/20 18:20:08 1.4 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2004/01/07 01:13:50 1.9 @@ -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.*; @@ -37,14 +38,16 @@ public class LinkedBlockingQueueTest ext } /** - * + * A new queue has the indicated capacity, or Integer.MAX_VALUE if + * none given */ public void testConstructor1() { assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity()); + assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity()); } /** - * + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { @@ -55,10 +58,9 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from null Collection throws NPE */ public void testConstructor3() { - try { LinkedBlockingQueue q = new LinkedBlockingQueue(null); shouldThrow(); @@ -67,7 +69,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from Collection of null elements throws NPE */ public void testConstructor4() { try { @@ -79,7 +81,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { try { @@ -93,7 +95,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Queue contains all elements of collection used to initialize */ public void testConstructor6() { try { @@ -108,7 +110,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Queue transitions from empty to full when elements added */ public void testEmptyFull() { LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -123,7 +125,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -140,7 +142,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * offer(null) throws NPE */ public void testOfferNull() { try { @@ -151,7 +153,18 @@ public class LinkedBlockingQueueTest ext } /** - * + * add(null) throws NPE + */ + public void testAddNull() { + try { + LinkedBlockingQueue q = new LinkedBlockingQueue(1); + q.add(null); + shouldThrow(); + } catch (NullPointerException success) { } + } + + /** + * Offer succeeds if not full; fails if full */ public void testOffer() { LinkedBlockingQueue q = new LinkedBlockingQueue(1); @@ -160,7 +173,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * add succeeds if not full; throws ISE if full */ public void testAdd() { try { @@ -175,7 +188,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * addAll(null) throws NPE */ public void testAddAll1() { try { @@ -185,8 +198,21 @@ public class LinkedBlockingQueueTest ext } catch (NullPointerException success) {} } + /** - * + * addAll(this) throws IAE + */ + public void testAddAllSelf() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + q.addAll(q); + shouldThrow(); + } + catch (IllegalArgumentException success) {} + } + + /** + * addAll of a collection with null elements throws NPE */ public void testAddAll2() { try { @@ -198,7 +224,8 @@ public class LinkedBlockingQueueTest ext catch (NullPointerException success) {} } /** - * + * addAll of a collection with any null elements throws NPE after + * possibly adding some elements */ public void testAddAll3() { try { @@ -212,7 +239,7 @@ public class LinkedBlockingQueueTest ext catch (NullPointerException success) {} } /** - * + * addAll throws ISE if not enough room */ public void testAddAll4() { try { @@ -226,7 +253,7 @@ public class LinkedBlockingQueueTest ext catch (IllegalStateException success) {} } /** - * + * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { try { @@ -244,7 +271,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put(null) throws NPE */ public void testPutNull() { try { @@ -260,7 +287,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * all elements successfully put are contained */ public void testPut() { try { @@ -278,7 +305,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks interruptibly if full */ public void testBlockingPut() { Thread t = new Thread(new Runnable() { @@ -308,7 +335,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks waiting for take when full */ public void testPutWithTake() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -342,7 +369,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed offer times out if full and elements not taken */ public void testTimedOffer() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -369,7 +396,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * take retrieves elements in FIFO order */ public void testTake() { try { @@ -383,7 +410,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * take blocks interruptibly when empty */ public void testTakeFromEmpty() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -406,7 +433,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() { Thread t = new Thread(new Runnable() { @@ -434,7 +461,7 @@ public class LinkedBlockingQueueTest ext /** - * + * poll succeeds unless empty */ public void testPoll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -445,7 +472,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed pool with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() { try { @@ -460,7 +487,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed pool with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() { try { @@ -475,7 +502,8 @@ public class LinkedBlockingQueueTest ext } /** - * + * Interrupted timed poll throws InterruptedException instead of + * returning timeout status */ public void testInterruptedTimedPoll() { Thread t = new Thread(new Runnable() { @@ -501,7 +529,8 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed poll before a delayed offer fails; after offer succeeds; + * on interruption throws */ public void testTimedPollWithOffer() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -526,9 +555,8 @@ public class LinkedBlockingQueueTest ext } } - /** - * + * peek returns next element, or null if empty */ public void testPeek() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -542,7 +570,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * element returns next element, or throws NSEE if empty */ public void testElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -558,7 +586,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remove removes next element, or throws NSEE if empty */ public void testRemove() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -573,7 +601,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remove(x) removes x and returns true if present */ public void testRemoveElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -588,7 +616,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * contains(x) reports true when elements added but not yet removed */ public void testContains() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -600,7 +628,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * clear removes all elements */ public void testClear() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -615,7 +643,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -629,7 +657,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -648,7 +676,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * removeAll(c) removes only those elements of c and reports true if changed */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { @@ -663,9 +691,8 @@ public class LinkedBlockingQueueTest ext } } - /** - * + * toArray contains all elements */ public void testToArray() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -679,7 +706,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * toArray(a) contains all elements */ public void testToArray2() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -692,9 +719,32 @@ public class LinkedBlockingQueueTest ext unexpectedException(); } } + + /** + * toArray(null) throws NPE + */ + public void testToArray_BadArg() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(null); + shouldThrow(); + } catch(NullPointerException success){} + } + + /** + * toArray with incompatible array type throws CCE + */ + public void testToArray1_BadArg() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch(ArrayStoreException success){} + } + /** - * + * iterator iterates through all elements */ public void testIterator() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -709,38 +759,50 @@ public class LinkedBlockingQueueTest ext } /** - * + * iterator.remove removes current element */ - public void testIteratorOrdering() { - + public void testIteratorRemove () { final LinkedBlockingQueue q = new LinkedBlockingQueue(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 LinkedBlockingQueue q = new LinkedBlockingQueue(3); q.add(one); q.add(two); q.add(three); - assertEquals(0, q.remainingCapacity()); - int k = 0; for (Iterator it = q.iterator(); it.hasNext();) { int i = ((Integer)(it.next())).intValue(); assertEquals(++k, i); } - assertEquals(3, k); } /** - * + * Modifications do not cause iterators to fail */ public void testWeaklyConsistentIteration () { - final LinkedBlockingQueue q = new LinkedBlockingQueue(3); - q.add(one); q.add(two); q.add(three); - try { for (Iterator it = q.iterator(); it.hasNext();) { q.remove(); @@ -750,13 +812,12 @@ public class LinkedBlockingQueueTest ext catch (ConcurrentModificationException e) { unexpectedException(); } - assertEquals(0, q.size()); } /** - * + * toString contains toStrings of elements */ public void testToString() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -768,17 +829,13 @@ public class LinkedBlockingQueueTest ext /** - * + * offer transfers elements across Executor tasks */ public void testOfferInExecutor() { - final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - q.add(one); q.add(two); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { public void run() { threadAssertFalse(q.offer(three)); @@ -805,18 +862,14 @@ public class LinkedBlockingQueueTest ext }); joinPool(executor); - } /** - * + * poll retrieves elements across Executor threads */ public void testPollInExecutor() { - final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { public void run() { threadAssertNull(q.poll()); @@ -846,7 +899,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * A deserialized serialized queue has same elements in same order */ public void testSerialization() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -868,4 +921,109 @@ public class LinkedBlockingQueueTest ext } } + /** + * drainTo(null) throws NPE + */ + public void testDrainToNull() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this) throws IAE + */ + public void testDrainToSelf() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(q); + shouldThrow(); + } catch(IllegalArgumentException success) { + } + } + + /** + * drainTo(c) empties queue into another collection c + */ + public void testDrainTo() { + LinkedBlockingQueue 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 LinkedBlockingQueue 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); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * drainTo(null, n) throws NPE + */ + public void testDrainToNullN() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null, 0); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this, n) throws IAE + */ + public void testDrainToSelfN() { + LinkedBlockingQueue 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) { + LinkedBlockingQueue 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)); + } + } + }