--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2016/11/05 23:38:21 1.75 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2016/11/06 06:27:04 1.81 @@ -11,6 +11,7 @@ import static java.util.concurrent.TimeU import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; @@ -25,18 +26,6 @@ import junit.framework.Test; public class ArrayBlockingQueueTest extends JSR166TestCase { - public static class Fair extends BlockingQueueTest { - protected BlockingQueue emptyCollection() { - return populatedQueue(0, SIZE, 2 * SIZE, true); - } - } - - public static class NonFair extends BlockingQueueTest { - protected BlockingQueue emptyCollection() { - return populatedQueue(0, SIZE, 2 * SIZE, false); - } - } - public static void main(String[] args) { main(suite(), args); } @@ -52,10 +41,24 @@ public class ArrayBlockingQueueTest exte public boolean isConcurrent() { return true; } public boolean permitsNulls() { return false; } } - return newTestSuite(ArrayBlockingQueueTest.class, - new Fair().testSuite(), - new NonFair().testSuite(), - CollectionTest.testSuite(new Implementation())); + + return newTestSuite( + ArrayBlockingQueueTest.class, + new Fair().testSuite(), + new NonFair().testSuite(), + CollectionTest.testSuite(new Implementation())); + } + + public static class Fair extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return populatedQueue(0, SIZE, 2 * SIZE, true); + } + } + + public static class NonFair extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return populatedQueue(0, SIZE, 2 * SIZE, false); + } } /** @@ -102,17 +105,25 @@ public class ArrayBlockingQueueTest exte /** * Constructor throws IAE if capacity argument nonpositive */ - public void testConstructor2() { - try { - new ArrayBlockingQueue(0); - shouldThrow(); - } catch (IllegalArgumentException success) {} + public void testConstructor_nonPositiveCapacity() { + for (int i : new int[] { 0, -1, Integer.MIN_VALUE }) { + try { + new ArrayBlockingQueue(i); + shouldThrow(); + } catch (IllegalArgumentException success) {} + for (boolean fair : new boolean[] { true, false }) { + try { + new ArrayBlockingQueue(i, fair); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } + } } /** * Initializing from null Collection throws NPE */ - public void testConstructor3() { + public void testConstructor_nullCollection() { try { new ArrayBlockingQueue(1, true, null); shouldThrow(); @@ -147,13 +158,13 @@ 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); + public void testConstructor_collectionTooLarge() { + // just barely fits - succeeds + new ArrayBlockingQueue(SIZE, false, + Collections.nCopies(SIZE, "")); try { - new ArrayBlockingQueue(SIZE - 1, false, elements); + new ArrayBlockingQueue(SIZE - 1, false, + Collections.nCopies(SIZE, "")); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -175,12 +186,12 @@ public class ArrayBlockingQueueTest exte * Queue transitions from empty to full when elements added */ public void testEmptyFull() { - ArrayBlockingQueue q = new ArrayBlockingQueue(2); + BlockingQueue q = populatedQueue(0, 2, 2, false); assertTrue(q.isEmpty()); assertEquals(2, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); - q.add(two); + assertTrue(q.offer(two)); assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); assertFalse(q.offer(three)); @@ -190,15 +201,18 @@ public class ArrayBlockingQueueTest exte * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - BlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.remainingCapacity()); - assertEquals(SIZE, q.size() + q.remainingCapacity()); + int size = ThreadLocalRandom.current().nextInt(1, SIZE); + BlockingQueue q = populatedQueue(size, size, 2 * size, false); + int spare = q.remainingCapacity(); + int capacity = spare + size; + for (int i = 0; i < size; i++) { + assertEquals(spare + i, q.remainingCapacity()); + assertEquals(capacity, q.size() + q.remainingCapacity()); assertEquals(i, q.remove()); } - for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE - i, q.remainingCapacity()); - assertEquals(SIZE, q.size() + q.remainingCapacity()); + for (int i = 0; i < size; i++) { + assertEquals(capacity - i, q.remainingCapacity()); + assertEquals(capacity, q.size() + q.remainingCapacity()); assertTrue(q.add(i)); } } @@ -217,12 +231,10 @@ public class ArrayBlockingQueueTest exte */ public void testAdd() { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertTrue(q.add(new Integer(i))); - } + for (int i = 0; i < SIZE; i++) assertTrue(q.add((Integer) i)); assertEquals(0, q.remainingCapacity()); try { - q.add(new Integer(SIZE)); + q.add((Integer) SIZE); shouldThrow(); } catch (IllegalStateException success) {} } @@ -256,13 +268,17 @@ 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); + public void testAddAll_insufficientSpace() { + int size = ThreadLocalRandom.current().nextInt(1, SIZE); + ArrayBlockingQueue q = populatedQueue(0, size, size, false); + // Just fits: + q.addAll(populatedQueue(size, size, 2 * size, false)); + assertEquals(0, q.remainingCapacity()); + assertEquals(size, q.size()); + assertEquals(0, q.peek()); try { - q.addAll(Arrays.asList(ints)); + q = populatedQueue(0, size, size, false); + q.addAll(Collections.nCopies(size + 1, 42)); shouldThrow(); } catch (IllegalStateException success) {} } @@ -549,8 +565,10 @@ public class ArrayBlockingQueueTest exte * contains(x) reports true when elements added but not yet removed */ public void testContains() { - ArrayBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { + int size = ThreadLocalRandom.current().nextInt(1, SIZE); + ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); + assertFalse(q.contains(null)); + for (int i = 0; i < size; ++i) { assertTrue(q.contains(new Integer(i))); assertEquals(i, q.poll()); assertFalse(q.contains(new Integer(i))); @@ -561,11 +579,13 @@ public class ArrayBlockingQueueTest exte * clear removes all elements */ public void testClear() { - ArrayBlockingQueue q = populatedQueue(SIZE); + int size = ThreadLocalRandom.current().nextInt(1, 5); + ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); + int capacity = size + q.remainingCapacity(); q.clear(); assertTrue(q.isEmpty()); assertEquals(0, q.size()); - assertEquals(SIZE, q.remainingCapacity()); + assertEquals(capacity, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); assertTrue(q.contains(one)); @@ -919,8 +939,8 @@ public class ArrayBlockingQueueTest exte */ public void testNeverContainsNull() { Collection[] qs = { - new ArrayBlockingQueue(10), - populatedQueue(2), + populatedQueue(0, 1, 10, false), + populatedQueue(2, 2, 10, true), }; for (Collection q : qs) {