--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2011/05/27 20:07:24 1.47 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2014/12/31 19:21:20 1.59 @@ -6,23 +6,33 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.io.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Queue; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; + +import junit.framework.Test; public class ArrayBlockingQueueTest extends JSR166TestCase { public static class Fair extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new ArrayBlockingQueue(20, true); + return new ArrayBlockingQueue(SIZE, true); } } public static class NonFair extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new ArrayBlockingQueue(20, false); + return new ArrayBlockingQueue(SIZE, false); } } @@ -37,7 +47,7 @@ public class ArrayBlockingQueueTest exte } /** - * Create a queue of given size containing consecutive + * Returns a new queue of given size containing consecutive * Integers 0 ... n. */ private ArrayBlockingQueue populatedQueue(int n) { @@ -63,7 +73,7 @@ public class ArrayBlockingQueueTest exte */ public void testConstructor2() { try { - ArrayBlockingQueue q = new ArrayBlockingQueue(0); + new ArrayBlockingQueue(0); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -73,7 +83,7 @@ public class ArrayBlockingQueueTest exte */ public void testConstructor3() { try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); + new ArrayBlockingQueue(1, true, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -82,9 +92,9 @@ public class ArrayBlockingQueueTest exte * Initializing from Collection of null elements throws NPE */ public void testConstructor4() { + Collection elements = Arrays.asList(new Integer[SIZE]); try { - Integer[] ints = new Integer[SIZE]; - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE, false, elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -93,11 +103,12 @@ public class ArrayBlockingQueueTest exte * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = i; + Collection elements = Arrays.asList(ints); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -106,11 +117,12 @@ public class ArrayBlockingQueueTest exte * Initializing from too large collection throws IAE */ public void testConstructor6() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = i; + Collection elements = Arrays.asList(ints); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE - 1, false, elements); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -121,8 +133,9 @@ public class ArrayBlockingQueueTest exte public void testConstructor7() { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); + ints[i] = i; + Collection elements = Arrays.asList(ints); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements); for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], q.poll()); } @@ -160,28 +173,6 @@ public class ArrayBlockingQueueTest exte } /** - * offer(null) throws NPE - */ - public void testOfferNull() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * 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() { @@ -206,17 +197,6 @@ public class ArrayBlockingQueueTest exte } /** - * addAll(null) throws NPE - */ - public void testAddAll1() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - q.addAll(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll(this) throws IAE */ public void testAddAllSelf() { @@ -228,18 +208,6 @@ public class ArrayBlockingQueueTest exte } /** - * addAll of a collection with null elements throws NPE - */ - public void testAddAll2() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements */ @@ -284,17 +252,6 @@ public class ArrayBlockingQueueTest exte } /** - * put(null) throws NPE - */ - public void testPutNull() throws InterruptedException { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - q.put(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * all elements successfully put are contained */ public void testPut() throws InterruptedException { @@ -367,14 +324,14 @@ public class ArrayBlockingQueueTest exte }}); 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()); } /** @@ -560,21 +517,6 @@ public class ArrayBlockingQueueTest exte } /** - * remove(x) removes x and returns true if present - */ - public void testRemoveElement() { - ArrayBlockingQueue q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - } - for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); - } - assertTrue(q.isEmpty()); - } - - /** * contains(x) reports true when elements added but not yet removed */ public void testContains() { @@ -651,37 +593,92 @@ public class ArrayBlockingQueueTest exte } } - /** - * toArray contains all elements in FIFO order - */ - public void testToArray() { - ArrayBlockingQueue q = populatedQueue(SIZE); + void checkToArray(ArrayBlockingQueue q) { + int size = q.size(); Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.poll()); + assertEquals(size, o.length); + Iterator it = q.iterator(); + for (int i = 0; i < size; i++) { + Integer x = (Integer) it.next(); + assertEquals((Integer)o[0] + i, (int) x); + assertSame(o[i], x); + } } /** - * toArray(a) contains all elements in FIFO order + * toArray() contains all elements in FIFO order */ - public void testToArray2() { - ArrayBlockingQueue q = populatedQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - Integer[] array = q.toArray(ints); - assertSame(ints, array); - for (int i = 0; i < ints.length; i++) - assertSame(ints[i], q.poll()); + public void testToArray() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + for (int i = 0; i < SIZE; i++) { + checkToArray(q); + q.add(i); + } + // Provoke wraparound + for (int i = 0; i < SIZE; i++) { + checkToArray(q); + assertEquals(i, q.poll()); + checkToArray(q); + q.add(SIZE+i); + } + for (int i = 0; i < SIZE; i++) { + checkToArray(q); + assertEquals(SIZE+i, q.poll()); + } + } + + void checkToArray2(ArrayBlockingQueue q) { + int size = q.size(); + Integer[] a1 = size == 0 ? null : new Integer[size-1]; + Integer[] a2 = new Integer[size]; + Integer[] a3 = new Integer[size+2]; + if (size > 0) Arrays.fill(a1, 42); + Arrays.fill(a2, 42); + Arrays.fill(a3, 42); + Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); + Integer[] b2 = (Integer[]) q.toArray(a2); + Integer[] b3 = (Integer[]) q.toArray(a3); + assertSame(a2, b2); + assertSame(a3, b3); + Iterator it = q.iterator(); + for (int i = 0; i < size; i++) { + Integer x = (Integer) it.next(); + assertSame(b1[i], x); + assertEquals(b1[0] + i, (int) x); + assertSame(b2[i], x); + assertSame(b3[i], x); + } + assertNull(a3[size]); + assertEquals(42, (int) a3[size+1]); + if (size > 0) { + assertNotSame(a1, b1); + assertEquals(size, b1.length); + for (int i = 0; i < a1.length; i++) { + assertEquals(42, (int) a1[i]); + } + } } /** - * toArray(null) throws NullPointerException + * toArray(a) contains all elements in FIFO order */ - public void testToArray_NullArg() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} + public void testToArray2() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + for (int i = 0; i < SIZE; i++) { + checkToArray2(q); + q.add(i); + } + // Provoke wraparound + for (int i = 0; i < SIZE; i++) { + checkToArray2(q); + assertEquals(i, q.poll()); + checkToArray2(q); + q.add(SIZE+i); + } + for (int i = 0; i < SIZE; i++) { + checkToArray2(q); + assertEquals(SIZE+i, q.poll()); + } } /** @@ -824,41 +821,18 @@ public class ArrayBlockingQueueTest exte * A deserialized serialized queue has same elements in same order */ public void testSerialization() throws Exception { - ArrayBlockingQueue q = populatedQueue(SIZE); - - 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)); - ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); - } - - /** - * drainTo(null) throws NPE - */ - public void testDrainToNull() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch (NullPointerException success) {} - } + Queue x = populatedQueue(SIZE); + Queue y = serialClone(x); - /** - * drainTo(this) throws IAE - */ - public void testDrainToSelf() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertNotSame(x, y); + 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()); } /** @@ -868,8 +842,8 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(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); @@ -879,8 +853,8 @@ public class ArrayBlockingQueueTest exte 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)); } @@ -906,28 +880,6 @@ public class ArrayBlockingQueueTest exte } /** - * 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 min(n, size) elements of queue into c */ public void testDrainToN() { @@ -938,12 +890,26 @@ public class ArrayBlockingQueueTest exte 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() { + Collection[] qs = { + new ArrayBlockingQueue(10), + populatedQueue(2), + }; + + for (Collection q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + } }