--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/20 18:20:08 1.4 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/10/05 23:00:40 1.6 @@ -37,14 +37,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 +57,9 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from null Collection throws NPE */ public void testConstructor3() { - try { LinkedBlockingQueue q = new LinkedBlockingQueue(null); shouldThrow(); @@ -67,7 +68,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from Collection of null elements throws NPE */ public void testConstructor4() { try { @@ -79,7 +80,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { try { @@ -93,7 +94,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Queue contains all elements of collection used to initialize */ public void testConstructor6() { try { @@ -108,7 +109,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Queue transitions from empty to full when elements added */ public void testEmptyFull() { LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -123,7 +124,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -140,7 +141,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * offer(null) throws NPE */ public void testOfferNull() { try { @@ -151,7 +152,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 +172,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * add succeeds if not full; throws ISE if full */ public void testAdd() { try { @@ -175,7 +187,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * addAll(null) throws NPE */ public void testAddAll1() { try { @@ -185,8 +197,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 +223,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 +238,7 @@ public class LinkedBlockingQueueTest ext catch (NullPointerException success) {} } /** - * + * addAll throws ISE if not enough room */ public void testAddAll4() { try { @@ -226,7 +252,7 @@ public class LinkedBlockingQueueTest ext catch (IllegalStateException success) {} } /** - * + * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { try { @@ -244,7 +270,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put(null) throws NPE */ public void testPutNull() { try { @@ -260,7 +286,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * all elements successfully put are contained */ public void testPut() { try { @@ -278,7 +304,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks interruptibly if full */ public void testBlockingPut() { Thread t = new Thread(new Runnable() { @@ -308,7 +334,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks waiting for take when full */ public void testPutWithTake() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -342,7 +368,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 +395,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * take retrieves elements in FIFO order */ public void testTake() { try { @@ -383,7 +409,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * take blocks interruptibly when empty */ public void testTakeFromEmpty() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -406,7 +432,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 +460,7 @@ public class LinkedBlockingQueueTest ext /** - * + * poll succeeds unless empty */ public void testPoll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -445,7 +471,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed pool with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() { try { @@ -460,7 +486,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed pool with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() { try { @@ -475,7 +501,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 +528,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 +554,8 @@ public class LinkedBlockingQueueTest ext } } - /** - * + * peek returns next element, or null if empty */ public void testPeek() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -542,7 +569,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * element returns next element, or throws NSEE if empty */ public void testElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -558,7 +585,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remove removes next element, or throws NSEE if empty */ public void testRemove() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -573,7 +600,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remove(x) removes x and returns true if present */ public void testRemoveElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -588,7 +615,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 +627,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * clear removes all elements */ public void testClear() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -615,7 +642,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 +656,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 +675,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 +690,8 @@ public class LinkedBlockingQueueTest ext } } - /** - * + * toArray contains all elements */ public void testToArray() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -679,7 +705,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * toArray(a) contains all elements */ public void testToArray2() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -692,9 +718,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 incompatable 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 +758,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 +811,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 +828,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 +861,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 +898,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * A deserialized serialized queue has same elements in same order */ public void testSerialization() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -868,4 +920,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+1); + } 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)); + } + } + }