--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2015/10/05 23:06:12 1.69 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2016/11/05 23:38:21 1.75 @@ -19,6 +19,7 @@ import java.util.concurrent.BlockingQueu import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; +import java.util.concurrent.ThreadLocalRandom; import junit.framework.Test; @@ -26,13 +27,13 @@ public class ArrayBlockingQueueTest exte public static class Fair extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new ArrayBlockingQueue(SIZE, true); + return populatedQueue(0, SIZE, 2 * SIZE, true); } } public static class NonFair extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new ArrayBlockingQueue(SIZE, false); + return populatedQueue(0, SIZE, 2 * SIZE, false); } } @@ -41,23 +42,53 @@ public class ArrayBlockingQueueTest exte } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return ArrayBlockingQueue.class; } + public Collection emptyCollection() { + boolean fair = ThreadLocalRandom.current().nextBoolean(); + return populatedQueue(0, SIZE, 2 * SIZE, fair); + } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(ArrayBlockingQueueTest.class, new Fair().testSuite(), - new NonFair().testSuite()); + new NonFair().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** * Returns a new queue of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ - private ArrayBlockingQueue populatedQueue(int n) { - ArrayBlockingQueue q = new ArrayBlockingQueue(n); + static ArrayBlockingQueue populatedQueue(int n) { + return populatedQueue(n, n, n, false); + } + + /** + * Returns a new queue of given size containing consecutive + * Integers 0 ... n - 1, with given capacity range and fairness. + */ + static ArrayBlockingQueue populatedQueue( + int size, int minCapacity, int maxCapacity, boolean fair) { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + int capacity = rnd.nextInt(minCapacity, maxCapacity + 1); + ArrayBlockingQueue q = new ArrayBlockingQueue<>(capacity); assertTrue(q.isEmpty()); - for (int i = 0; i < n; i++) - assertTrue(q.offer(new Integer(i))); - assertFalse(q.isEmpty()); - assertEquals(0, q.remainingCapacity()); - assertEquals(n, q.size()); + // shuffle circular array elements so they wrap + { + int n = rnd.nextInt(capacity); + for (int i = 0; i < n; i++) q.add(42); + for (int i = 0; i < n; i++) q.remove(); + } + for (int i = 0; i < size; i++) + assertTrue(q.offer((Integer) i)); + assertEquals(size == 0, q.isEmpty()); + assertEquals(capacity - size, q.remainingCapacity()); + assertEquals(size, q.size()); + if (size > 0) + assertEquals((Integer) 0, q.peek()); return q; } @@ -451,15 +482,13 @@ public class ArrayBlockingQueueTest exte 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 startTime = System.nanoTime(); - assertEquals(i, (int) q.poll(2*LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); } - long startTime = System.nanoTime(); aboutToWait.countDown(); try { - q.poll(2*LONG_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) { assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); @@ -467,9 +496,9 @@ public class ArrayBlockingQueueTest exte }}); await(aboutToWait); - waitForThreadToEnterWaitState(t, LONG_DELAY_MS); + waitForThreadToEnterWaitState(t); t.interrupt(); - awaitTermination(t, LONG_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -593,103 +622,75 @@ public class ArrayBlockingQueueTest exte } } - void checkToArray(ArrayBlockingQueue q) { + void checkToArray(ArrayBlockingQueue q) { int size = q.size(); - Object[] o = q.toArray(); - assertEquals(size, o.length); + Object[] a1 = q.toArray(); + assertEquals(size, a1.length); + Integer[] a2 = q.toArray(new Integer[0]); + assertEquals(size, a2.length); + Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]); + assertEquals(size, a3.length); + Integer[] a4 = new Integer[size]; + assertSame(a4, q.toArray(a4)); + Integer[] a5 = new Integer[size + 1]; + Arrays.fill(a5, 42); + assertSame(a5, q.toArray(a5)); + Integer[] a6 = new Integer[size + 2]; + Arrays.fill(a6, 42); + assertSame(a6, q.toArray(a6)); + Object[][] as = { a1, a2, a3, a4, a5, a6 }; + for (Object[] a : as) { + if (a.length > size) assertNull(a[size]); + if (a.length > size + 1) assertEquals(42, a[size + 1]); + } Iterator it = q.iterator(); + Integer s = q.peek(); for (int i = 0; i < size; i++) { Integer x = (Integer) it.next(); - assertEquals((Integer)o[0] + i, (int) x); - assertSame(o[i], x); + assertEquals(s + i, (int) x); + for (Object[] a : as) + assertSame(a1[i], x); } } /** - * toArray() contains all elements in FIFO order + * toArray() and toArray(a) contain all elements in FIFO order */ public void testToArray() { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - for (int i = 0; i < SIZE; i++) { + final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final int size = rnd.nextInt(6); + final int capacity = Math.max(1, size + rnd.nextInt(size + 1)); + ArrayBlockingQueue q = new ArrayBlockingQueue<>(capacity); + for (int i = 0; i < size; i++) { checkToArray(q); q.add(i); } // Provoke wraparound - for (int i = 0; i < SIZE; i++) { + int added = size * 2; + for (int i = 0; i < added; i++) { checkToArray(q); - assertEquals(i, q.poll()); - checkToArray(q); - q.add(SIZE + i); + assertEquals((Integer) i, q.poll()); + 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 = 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()); + checkToArray(q); + assertEquals((Integer) (added + i), q.poll()); } } /** * toArray(incompatible array type) throws ArrayStoreException */ - public void testToArray1_BadArg() { + public void testToArray_incompatibleArrayType() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} + try { + q.toArray(new String[0]); + shouldThrow(); + } catch (ArrayStoreException success) {} } /**