summaryrefslogtreecommitdiffstats
path: root/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2015-04-28 11:40:00 +0100
committerNarayan Kamath <narayan@google.com>2015-04-30 12:00:08 +0100
commit8e9a0e92906742b17eb08d7fb83cca91965f9b8e (patch)
tree790a5161ed9c8648a8dacc94c36b5db53f8da4eb /jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
parentedf43d27e240d82106f39ae91404963c23987234 (diff)
downloadlibcore-8e9a0e92906742b17eb08d7fb83cca91965f9b8e.zip
libcore-8e9a0e92906742b17eb08d7fb83cca91965f9b8e.tar.gz
libcore-8e9a0e92906742b17eb08d7fb83cca91965f9b8e.tar.bz2
Update JSR166 tck tests.
The following tests have been omitted because they are unsupported : - Atomic8Test.java - CompletableFutureTest.java - ConcurrentHashMap8Test.java - DoubleAccumulatorTest.java - DoubleAdderTest.java - ForkJoinPool8Test.java - ForkJoinTask8Test.java - LongAccumulatorTest.java - LongAdderTest.java - SplittableRandomTest.java - StampedLockTest.java - ThreadLocalRandom8Test.java - ThreadPoolExecutor9Test.java A package declaration has been added to all files (package jsr166;) and the base-class JSR166Test has been changed not to rely on features that aren't available on android / junit in the android environment. We also avoid using junit4 style TestSuite declarations. While the CTS test runner handles them properly usually, it trips over itself when it encounters a failure and tries to run each test in an invidual process and fails each test for no good reason (needs investigation on the CTS side of things) bug: 20628776 (cherry picked from commit 5da8b2b3cac51f0f592a5e1941bd84eade9648cd) Change-Id: If35be0881ad8da4c604ce6683149b15c1a85f289
Diffstat (limited to 'jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java')
-rw-r--r--jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java118
1 files changed, 87 insertions, 31 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
index b999496..247c90e 100644
--- a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
@@ -8,9 +8,10 @@
package jsr166;
-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;
@@ -20,10 +21,36 @@ import java.util.concurrent.BlockingQueue;
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 {
+ // android-note: These tests have been moved into their own separate
+ // classes to work around CTS issues.
+ //
+ // public static class Fair extends BlockingQueueTest {
+ // protected BlockingQueue emptyCollection() {
+ // return new ArrayBlockingQueue(SIZE, true);
+ // }
+ // }
+ //
+ // public static class NonFair extends BlockingQueueTest {
+ // protected BlockingQueue emptyCollection() {
+ // return new ArrayBlockingQueue(SIZE, false);
+ // }
+ // }
+ //
+ // public static void main(String[] args) {
+ // main(suite(), args);
+ // }
+ //
+ // public static Test suite() {
+ // return newTestSuite(ArrayBlockingQueueTest.class,
+ // new Fair().testSuite(),
+ // new NonFair().testSuite());
+ // }
+
/**
* Returns a new queue of given size containing consecutive
* Integers 0 ... n.
@@ -86,7 +113,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
ints[i] = i;
Collection<Integer> elements = Arrays.asList(ints);
try {
- new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
+ new ArrayBlockingQueue(SIZE, false, elements);
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -137,16 +164,16 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
* 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));
}
}
@@ -163,12 +190,12 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
* 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) {}
@@ -178,8 +205,8 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
+ ArrayBlockingQueue q = populatedQueue(SIZE);
try {
- ArrayBlockingQueue q = populatedQueue(SIZE);
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -190,11 +217,11 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
* 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) {}
@@ -204,11 +231,11 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
* 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) {}
@@ -235,9 +262,9 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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());
}
@@ -565,8 +592,8 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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));
}
}
}
@@ -676,9 +703,24 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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());
}
/**
@@ -872,8 +914,22 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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<Object>(10),
+ populatedQueue(2),
+ };
+
+ for (Collection<?> q : qs) {
+ assertFalse(q.contains(null));
+ assertFalse(q.remove(null));
+ }
+ }
}