--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/20 18:20:08 1.4 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/25 11:02:41 1.5 @@ -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,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Offer succeeds if not full; fails if full */ public void testOffer() { LinkedBlockingQueue q = new LinkedBlockingQueue(1); @@ -160,7 +161,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * add succeeds if not full; throws ISE if full */ public void testAdd() { try { @@ -175,7 +176,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * addAll(null) throws NPE */ public void testAddAll1() { try { @@ -186,7 +187,7 @@ public class LinkedBlockingQueueTest ext catch (NullPointerException success) {} } /** - * + * addAll of a collection with null elements throws NPE */ public void testAddAll2() { try { @@ -198,7 +199,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 +214,7 @@ public class LinkedBlockingQueueTest ext catch (NullPointerException success) {} } /** - * + * addAll throws ISE if not enough room */ public void testAddAll4() { try { @@ -226,7 +228,7 @@ public class LinkedBlockingQueueTest ext catch (IllegalStateException success) {} } /** - * + * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { try { @@ -244,7 +246,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put(null) throws NPE */ public void testPutNull() { try { @@ -260,7 +262,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * all elements successfully put are contained */ public void testPut() { try { @@ -278,7 +280,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks interruptibly if full */ public void testBlockingPut() { Thread t = new Thread(new Runnable() { @@ -308,7 +310,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks waiting for take when full */ public void testPutWithTake() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -342,7 +344,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 +371,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * take retrieves elements in FIFO order */ public void testTake() { try { @@ -383,7 +385,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * take blocks interruptibly when empty */ public void testTakeFromEmpty() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -406,7 +408,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 +436,7 @@ public class LinkedBlockingQueueTest ext /** - * + * poll succeeds unless empty */ public void testPoll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -445,7 +447,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed pool with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() { try { @@ -460,7 +462,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed pool with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() { try { @@ -475,7 +477,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 +504,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 +530,8 @@ public class LinkedBlockingQueueTest ext } } - /** - * + * peek returns next element, or null if empty */ public void testPeek() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -542,7 +545,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * element returns next element, or throws NSEE if empty */ public void testElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -558,7 +561,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remove removes next element, or throws NSEE if empty */ public void testRemove() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -573,7 +576,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remove(x) removes x and returns true if present */ public void testRemoveElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -588,7 +591,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 +603,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * clear removes all elements */ public void testClear() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -615,7 +618,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 +632,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 +651,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 +666,8 @@ public class LinkedBlockingQueueTest ext } } - /** - * + * toArray contains all elements */ public void testToArray() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -679,7 +681,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * toArray(a) contains all elements */ public void testToArray2() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -694,7 +696,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * iterator iterates through all elements */ public void testIterator() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -709,38 +711,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 +764,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 +781,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 +814,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 +851,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * A deserialized serialized queue has same elements in same order */ public void testSerialization() { LinkedBlockingQueue q = populatedQueue(SIZE);