--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2011/05/30 22:43:20 1.40 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2017/05/13 21:47:12 1.70 @@ -4,20 +4,23 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.Arrays; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Deque; import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Queue; import java.util.concurrent.BlockingDeque; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingDeque; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.io.*; + +import junit.framework.Test; public class LinkedBlockingDequeTest extends JSR166TestCase { @@ -29,25 +32,33 @@ public class LinkedBlockingDequeTest ext public static class Bounded extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new LinkedBlockingDeque(20); + return new LinkedBlockingDeque(SIZE); } } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return LinkedBlockingDeque.class; } + public Collection emptyCollection() { return new LinkedBlockingDeque(); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(LinkedBlockingDequeTest.class, new Unbounded().testSuite(), - new Bounded().testSuite()); + new Bounded().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** - * Create a deque of given size containing consecutive - * Integers 0 ... n. + * Returns a new deque of given size containing consecutive + * Integers 0 ... n - 1. */ - private LinkedBlockingDeque populatedDeque(int n) { + private static LinkedBlockingDeque populatedDeque(int n) { LinkedBlockingDeque q = new LinkedBlockingDeque(n); assertTrue(q.isEmpty()); @@ -56,6 +67,8 @@ public class LinkedBlockingDequeTest ext assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); assertEquals(n, q.size()); + assertEquals((Integer) 0, q.peekFirst()); + assertEquals((Integer) (n - 1), q.peekLast()); return q; } @@ -79,7 +92,7 @@ public class LinkedBlockingDequeTest ext public void testSize() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); q.removeFirst(); } for (int i = 0; i < SIZE; ++i) { @@ -144,7 +157,7 @@ public class LinkedBlockingDequeTest ext */ public void testPollLast() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = SIZE-1; i >= 0; --i) { + for (int i = SIZE - 1; i >= 0; --i) { assertEquals(i, q.pollLast()); } assertNull(q.pollLast()); @@ -183,7 +196,7 @@ public class LinkedBlockingDequeTest ext */ public void testPeekLast() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = SIZE-1; i >= 0; --i) { + for (int i = SIZE - 1; i >= 0; --i) { assertEquals(i, q.peekLast()); assertEquals(i, q.pollLast()); assertTrue(q.peekLast() == null || @@ -213,7 +226,7 @@ public class LinkedBlockingDequeTest ext */ public void testLastElement() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = SIZE-1; i >= 0; --i) { + for (int i = SIZE - 1; i >= 0; --i) { assertEquals(i, q.getLast()); assertEquals(i, q.pollLast()); } @@ -273,12 +286,12 @@ public class LinkedBlockingDequeTest ext */ public void testRemoveFirstOccurrence() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); - assertFalse(q.removeFirstOccurrence(new Integer(i+1))); + assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); } assertTrue(q.isEmpty()); } @@ -288,12 +301,12 @@ public class LinkedBlockingDequeTest ext */ public void testRemoveLastOccurrence() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); - assertFalse(q.removeLastOccurrence(new Integer(i+1))); + assertFalse(q.removeLastOccurrence(new Integer(i + 1))); } assertTrue(q.isEmpty()); } @@ -364,7 +377,7 @@ public class LinkedBlockingDequeTest ext */ public void testConstructor5() { Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = i; Collection elements = Arrays.asList(ints); try { @@ -404,16 +417,16 @@ public class LinkedBlockingDequeTest ext * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - LinkedBlockingDeque q = populatedDeque(SIZE); + BlockingQueue q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); - q.remove(); + assertEquals(SIZE, q.size() + q.remainingCapacity()); + assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.remainingCapacity()); - assertEquals(i, q.size()); - q.add(new Integer(i)); + assertEquals(SIZE - i, q.remainingCapacity()); + assertEquals(SIZE, q.size() + q.remainingCapacity()); + assertTrue(q.add(i)); } } @@ -421,8 +434,8 @@ public class LinkedBlockingDequeTest ext * push(null) throws NPE */ public void testPushNull() { + LinkedBlockingDeque q = new LinkedBlockingDeque(1); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); q.push(null); shouldThrow(); } catch (NullPointerException success) {} @@ -432,14 +445,14 @@ public class LinkedBlockingDequeTest ext * push succeeds if not full; throws ISE if full */ public void testPush() { + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer x = new Integer(i); + q.push(x); + assertEquals(x, q.peek()); + } + assertEquals(0, q.remainingCapacity()); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.push(I); - assertEquals(I, q.peek()); - } - assertEquals(0, q.remainingCapacity()); q.push(new Integer(SIZE)); shouldThrow(); } catch (IllegalStateException success) {} @@ -510,7 +523,7 @@ public class LinkedBlockingDequeTest ext public void testAddAll3() { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i); Collection elements = Arrays.asList(ints); try { @@ -555,9 +568,9 @@ public class LinkedBlockingDequeTest ext public void testPut() throws InterruptedException { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); + Integer x = new Integer(i); + q.put(x); + assertTrue(q.contains(x)); } assertEquals(0, q.remainingCapacity()); } @@ -622,14 +635,14 @@ public class LinkedBlockingDequeTest ext }}); await(pleaseTake); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); assertEquals(0, q.take()); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); } /** @@ -676,9 +689,7 @@ public class LinkedBlockingDequeTest ext final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.take()); - } + for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); Thread.currentThread().interrupt(); try { @@ -696,7 +707,7 @@ public class LinkedBlockingDequeTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -748,25 +759,23 @@ public class LinkedBlockingDequeTest ext final CountDownLatch aboutToWait = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { + long startTime = System.nanoTime(); for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } - long t0 = System.nanoTime(); aboutToWait.countDown(); try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(aboutToWait); + waitForThreadToEnterWaitState(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -787,9 +796,9 @@ public class LinkedBlockingDequeTest ext public void testPutFirst() throws InterruptedException { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putFirst(I); - assertTrue(q.contains(I)); + Integer x = new Integer(i); + q.putFirst(x); + assertTrue(q.contains(x)); } assertEquals(0, q.remainingCapacity()); } @@ -854,14 +863,14 @@ public class LinkedBlockingDequeTest ext }}); await(pleaseTake); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); assertEquals(capacity - 1, q.take()); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); } /** @@ -917,7 +926,7 @@ public class LinkedBlockingDequeTest ext }}); await(threadStarted); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -958,7 +967,7 @@ public class LinkedBlockingDequeTest ext }}); await(threadStarted); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -990,9 +999,7 @@ public class LinkedBlockingDequeTest ext final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.takeFirst()); - } + for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst()); Thread.currentThread().interrupt(); try { @@ -1010,7 +1017,7 @@ public class LinkedBlockingDequeTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -1047,27 +1054,29 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollFirst() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); + long startTime = System.nanoTime(); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); } Thread.currentThread().interrupt(); try { - q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); + q.pollFirst(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); pleaseInterrupt.countDown(); try { - q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); + q.pollFirst(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); @@ -1134,9 +1143,9 @@ public class LinkedBlockingDequeTest ext public void testPutLast() throws InterruptedException { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putLast(I); - assertTrue(q.contains(I)); + Integer x = new Integer(i); + q.putLast(x); + assertTrue(q.contains(x)); } assertEquals(0, q.remainingCapacity()); } @@ -1201,14 +1210,14 @@ public class LinkedBlockingDequeTest ext }}); await(pleaseTake); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); assertEquals(0, q.take()); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); } /** @@ -1243,7 +1252,7 @@ public class LinkedBlockingDequeTest ext public void testTakeLast() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, q.takeLast()); + assertEquals(SIZE - i - 1, q.takeLast()); } } @@ -1255,9 +1264,8 @@ public class LinkedBlockingDequeTest ext final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, q.takeLast()); - } + for (int i = 0; i < SIZE; i++) + assertEquals(SIZE - i - 1, q.takeLast()); Thread.currentThread().interrupt(); try { @@ -1275,7 +1283,7 @@ public class LinkedBlockingDequeTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -1286,7 +1294,7 @@ public class LinkedBlockingDequeTest ext public void testTimedPollLast0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); + assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); } assertNull(q.pollLast(0, MILLISECONDS)); } @@ -1298,7 +1306,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { long startTime = System.nanoTime(); - assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } long startTime = System.nanoTime(); @@ -1312,12 +1320,14 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollLast() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); + long startTime = System.nanoTime(); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(SIZE - i - 1, + q.pollLast(LONG_DELAY_MS, MILLISECONDS)); } Thread.currentThread().interrupt(); @@ -1333,12 +1343,15 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); + checkEmpty(q); } /** @@ -1371,6 +1384,8 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); barrier.await(); @@ -1400,27 +1415,6 @@ public class LinkedBlockingDequeTest ext } /** - * remove(x) removes x and returns true if present - */ - public void testRemoveElement() { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.contains(i)); - assertTrue(q.remove(i)); - assertFalse(q.contains(i)); - assertTrue(q.contains(i-1)); - } - for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.contains(i)); - assertTrue(q.remove(i)); - assertFalse(q.contains(i)); - assertFalse(q.remove(i+1)); - assertFalse(q.contains(i+1)); - } - assertTrue(q.isEmpty()); - } - - /** * contains(x) reports true when elements added but not yet removed */ public void testContains() { @@ -1476,7 +1470,7 @@ public class LinkedBlockingDequeTest ext assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -1489,10 +1483,10 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(SIZE); LinkedBlockingDeque p = populatedDeque(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.remove()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.remove()); + assertFalse(q.contains(x)); } } } @@ -1500,7 +1494,7 @@ public class LinkedBlockingDequeTest ext /** * toArray contains all elements in FIFO order */ - public void testToArray() throws InterruptedException{ + public void testToArray() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); Object[] o = q.toArray(); for (int i = 0; i < o.length; i++) @@ -1536,9 +1530,26 @@ public class LinkedBlockingDequeTest ext public void testIterator() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) + assertTrue(q.contains(it.next())); + assertEquals(i, SIZE); + assertIteratorExhausted(it); + + it = q.iterator(); + for (i = 0; it.hasNext(); i++) assertEquals(it.next(), q.take()); - } + assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + Deque c = new LinkedBlockingDeque(); + assertIteratorExhausted(c.iterator()); + assertIteratorExhausted(c.descendingIterator()); } /** @@ -1671,23 +1682,23 @@ public class LinkedBlockingDequeTest ext final LinkedBlockingDeque q = new LinkedBlockingDeque(2); q.add(one); q.add(two); - ExecutorService executor = Executors.newFixedThreadPool(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertFalse(q.offer(three)); - threadsStarted.await(); - assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); - assertEquals(0, q.remainingCapacity()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertSame(one, q.take()); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.offer(three)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + }}); + } } /** @@ -1696,41 +1707,40 @@ public class LinkedBlockingDequeTest ext public void testPollInExecutor() { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll()); - threadsStarted.await(); - assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); - checkEmpty(q); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** * A deserialized serialized deque has same elements in same order */ public void testSerialization() throws Exception { - LinkedBlockingDeque q = populatedDeque(SIZE); + Queue x = populatedDeque(SIZE); + Queue y = serialClone(x); - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); + assertNotSame(y, x); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertTrue(Arrays.equals(x.toArray(), y.toArray())); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + assertEquals(x.remove(), y.remove()); + } + assertTrue(y.isEmpty()); } /** @@ -1740,8 +1750,8 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), SIZE); + assertEquals(0, q.size()); + assertEquals(SIZE, l.size()); for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i)); q.add(zero); @@ -1751,8 +1761,8 @@ public class LinkedBlockingDequeTest ext assertTrue(q.contains(one)); l.clear(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), 2); + assertEquals(0, q.size()); + assertEquals(2, l.size()); for (int i = 0; i < 2; ++i) assertEquals(l.get(i), new Integer(i)); } @@ -1764,7 +1774,7 @@ public class LinkedBlockingDequeTest ext final LinkedBlockingDeque q = populatedDeque(SIZE); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.put(new Integer(SIZE+1)); + q.put(new Integer(SIZE + 1)); }}); t.start(); @@ -1788,11 +1798,28 @@ public class LinkedBlockingDequeTest ext ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; - assertEquals(l.size(), k); - assertEquals(q.size(), SIZE-k); + assertEquals(k, l.size()); + assertEquals(SIZE - k, q.size()); for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); - while (q.poll() != null) ; + do {} while (q.poll() != null); + } + } + + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Deque[] qs = { + new LinkedBlockingDeque(), + populatedDeque(2), + }; + + for (Deque q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + assertFalse(q.removeFirstOccurrence(null)); + assertFalse(q.removeLastOccurrence(null)); } }