--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2011/11/26 05:19:17 1.51 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2015/02/28 19:59:23 1.64 @@ -6,9 +6,10 @@ * Pat Fisher, Mike Judd. */ -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.Iterator; import java.util.NoSuchElementException; @@ -18,7 +19,8 @@ import java.util.concurrent.BlockingQueu import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; public class ArrayBlockingQueueTest extends JSR166TestCase { @@ -45,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) { @@ -106,7 +108,7 @@ public class ArrayBlockingQueueTest exte ints[i] = i; Collection elements = Arrays.asList(ints); try { - new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE, false, elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -157,16 +159,16 @@ public class ArrayBlockingQueueTest exte * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - ArrayBlockingQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(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, q.size() + q.remainingCapacity()); + assertTrue(q.add(i)); } } @@ -183,12 +185,12 @@ public class ArrayBlockingQueueTest exte * add succeeds if not full; throws ISE if full */ public void testAdd() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertTrue(q.add(new Integer(i))); + } + assertEquals(0, q.remainingCapacity()); try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertTrue(q.add(new Integer(i))); - } - assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); shouldThrow(); } catch (IllegalStateException success) {} @@ -198,8 +200,8 @@ public class ArrayBlockingQueueTest exte * addAll(this) throws IAE */ public void testAddAllSelf() { + ArrayBlockingQueue q = populatedQueue(SIZE); try { - ArrayBlockingQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -210,11 +212,11 @@ public class ArrayBlockingQueueTest exte * possibly adding some elements */ public void testAddAll3() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i); try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -224,11 +226,11 @@ public class ArrayBlockingQueueTest exte * addAll throws ISE if not enough room */ public void testAddAll4() { + ArrayBlockingQueue q = new ArrayBlockingQueue(1); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (IllegalStateException success) {} @@ -255,9 +257,9 @@ public class ArrayBlockingQueueTest exte public void testPut() throws InterruptedException { ArrayBlockingQueue q = new ArrayBlockingQueue(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()); } @@ -585,32 +587,98 @@ public class ArrayBlockingQueueTest exte assertTrue(q.removeAll(p)); 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)); } } } + void checkToArray(ArrayBlockingQueue q) { + int size = q.size(); + Object[] o = q.toArray(); + 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 contains all elements in FIFO order + * toArray() contains all elements in FIFO order */ public void testToArray() { - ArrayBlockingQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.poll()); + 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(a) 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()); + 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()); + } } /** @@ -630,9 +698,24 @@ public class ArrayBlockingQueueTest exte public void testIterator() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(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() { + assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator()); } /** @@ -756,7 +839,7 @@ public class ArrayBlockingQueueTest exte Queue x = populatedQueue(SIZE); Queue y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); assertEquals(x.toString(), y.toString()); assertTrue(Arrays.equals(x.toArray(), y.toArray())); @@ -822,12 +905,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)); + } + } }