diff options
author | Narayan Kamath <narayan@google.com> | 2015-04-28 11:40:00 +0100 |
---|---|---|
committer | Narayan Kamath <narayan@google.com> | 2015-04-30 12:00:08 +0100 |
commit | 8e9a0e92906742b17eb08d7fb83cca91965f9b8e (patch) | |
tree | 790a5161ed9c8648a8dacc94c36b5db53f8da4eb | |
parent | edf43d27e240d82106f39ae91404963c23987234 (diff) | |
download | libcore-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
68 files changed, 2776 insertions, 1226 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java b/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java index ba4cc66..9e83de2 100644 --- a/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java +++ b/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java @@ -8,14 +8,38 @@ package jsr166; -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicBoolean; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.security.*; + +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.AbstractExecutorService; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import junit.framework.Test; +import junit.framework.TestSuite; public class AbstractExecutorServiceTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * A no-frills implementation of AbstractExecutorService, designed @@ -42,11 +66,10 @@ public class AbstractExecutorServiceTest extends JSR166TestCase { public void testExecuteRunnable() throws Exception { ExecutorService e = new DirectExecutorService(); final AtomicBoolean done = new AtomicBoolean(false); - CheckedRunnable task = new CheckedRunnable() { + Future<?> future = e.submit(new CheckedRunnable() { public void realRun() { done.set(true); - }}; - Future<?> future = e.submit(task); + }}); assertNull(future.get()); assertNull(future.get(0, MILLISECONDS)); assertTrue(done.get()); @@ -149,8 +172,8 @@ public class AbstractExecutorServiceTest extends JSR166TestCase { * execute(null runnable) throws NPE */ public void testExecuteNullRunnable() { + ExecutorService e = new DirectExecutorService(); try { - ExecutorService e = new DirectExecutorService(); e.submit((Runnable) null); shouldThrow(); } catch (NullPointerException success) {} @@ -160,8 +183,8 @@ public class AbstractExecutorServiceTest extends JSR166TestCase { * submit(null callable) throws NPE */ public void testSubmitNullCallable() { + ExecutorService e = new DirectExecutorService(); try { - ExecutorService e = new DirectExecutorService(); e.submit((Callable) null); shouldThrow(); } catch (NullPointerException success) {} diff --git a/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java b/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java index e74fc3c..2aa7326 100644 --- a/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java @@ -8,13 +8,24 @@ package jsr166; -import junit.framework.*; import java.util.AbstractQueue; import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AbstractQueueTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class Succeed extends AbstractQueue<Integer> { public boolean offer(Integer x) { @@ -110,8 +121,8 @@ public class AbstractQueueTest extends JSR166TestCase { * addAll(null) throws NPE */ public void testAddAll1() { + Succeed q = new Succeed(); try { - Succeed q = new Succeed(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -121,8 +132,8 @@ public class AbstractQueueTest extends JSR166TestCase { * addAll(this) throws IAE */ public void testAddAllSelf() { + Succeed q = new Succeed(); try { - Succeed q = new Succeed(); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -132,9 +143,9 @@ public class AbstractQueueTest extends JSR166TestCase { * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + Succeed q = new Succeed(); + Integer[] ints = new Integer[SIZE]; try { - Succeed q = new Succeed(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -145,11 +156,11 @@ public class AbstractQueueTest extends JSR166TestCase { * possibly adding some elements */ public void testAddAll3() { + Succeed q = new Succeed(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i); try { - Succeed q = new Succeed(); - 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) {} @@ -159,11 +170,11 @@ public class AbstractQueueTest extends JSR166TestCase { * addAll throws ISE if an add fails */ public void testAddAll4() { + Fail q = new Fail(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); try { - Fail q = new Fail(); - 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) {} diff --git a/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java b/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java index d957e61..8604d86 100644 --- a/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java +++ b/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java @@ -8,13 +8,29 @@ package jsr166; -import junit.framework.*; -import java.util.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * A simple mutex class, adapted from the class javadoc. Exclusive @@ -83,7 +99,7 @@ public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { } public boolean tryReleaseShared(long ignore) { - setState(1 << 62); + setState(1L << 62); return true; } } @@ -191,7 +207,7 @@ public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { new HashSet<Thread>(Arrays.asList(threads))); } - enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** * Awaits condition using the specified AwaitMethod. @@ -214,6 +230,8 @@ public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { case awaitUntil: assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); break; + default: + throw new AssertionError(); } } @@ -1184,7 +1202,6 @@ public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { public void testTryAcquireSharedNanos_Timeout() { final BooleanLatch l = new BooleanLatch(); final BooleanLatch observedQueued = new BooleanLatch(); - final long timeoutMillis = timeoutMillis(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(l.isSignalled()); @@ -1206,4 +1223,28 @@ public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { assertFalse(l.isSignalled()); } + /** + * awaitNanos/timed await with 0 wait times out immediately + */ + public void testAwait_Zero() throws InterruptedException { + final Mutex sync = new Mutex(); + final ConditionObject c = sync.newCondition(); + sync.acquire(); + assertTrue(c.awaitNanos(0L) <= 0); + assertFalse(c.await(0L, NANOSECONDS)); + sync.release(); + } + + /** + * awaitNanos/timed await with maximum negative wait times does not underflow + */ + public void testAwait_NegativeInfinity() throws InterruptedException { + final Mutex sync = new Mutex(); + final ConditionObject c = sync.newCondition(); + sync.acquire(); + assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); + assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); + sync.release(); + } + } diff --git a/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java b/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java index b9dab06..b3c4110 100644 --- a/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java +++ b/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java @@ -8,13 +8,29 @@ package jsr166; -import junit.framework.*; -import java.util.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AbstractQueuedSynchronizerTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * A simple mutex class, adapted from the class javadoc. Exclusive @@ -194,7 +210,7 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase { new HashSet<Thread>(Arrays.asList(threads))); } - enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** * Awaits condition using the specified AwaitMethod. @@ -217,6 +233,8 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase { case awaitUntil: assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); break; + default: + throw new AssertionError(); } } @@ -1187,7 +1205,6 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase { public void testTryAcquireSharedNanos_Timeout() { final BooleanLatch l = new BooleanLatch(); final BooleanLatch observedQueued = new BooleanLatch(); - final long timeoutMillis = timeoutMillis(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(l.isSignalled()); @@ -1209,4 +1226,28 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase { assertFalse(l.isSignalled()); } + /** + * awaitNanos/timed await with 0 wait times out immediately + */ + public void testAwait_Zero() throws InterruptedException { + final Mutex sync = new Mutex(); + final ConditionObject c = sync.newCondition(); + sync.acquire(); + assertTrue(c.awaitNanos(0L) <= 0); + assertFalse(c.await(0L, NANOSECONDS)); + sync.release(); + } + + /** + * awaitNanos/timed await with maximum negative wait times does not underflow + */ + public void testAwait_NegativeInfinity() throws InterruptedException { + final Mutex sync = new Mutex(); + final ConditionObject c = sync.newCondition(); + sync.acquire(); + assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); + assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); + sync.release(); + } + } diff --git a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueNotFairTest.java b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueNonFairTest.java index ddb1c5d..7bd920f 100644 --- a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueNotFairTest.java +++ b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueNonFairTest.java @@ -11,7 +11,7 @@ package jsr166; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; -public class ArrayBlockingQueueNotFairTest extends BlockingQueueTest { +public class ArrayBlockingQueueNonFairTest extends BlockingQueueTest { protected BlockingQueue emptyCollection() { return new ArrayBlockingQueue(SIZE, false); 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)); + } + } } diff --git a/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java b/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java index d18a560..16290e9 100644 --- a/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java +++ b/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java @@ -6,9 +6,8 @@ package jsr166; -import junit.framework.*; -import java.util.Arrays; import java.util.ArrayDeque; +import java.util.Arrays; import java.util.Collection; import java.util.Deque; import java.util.Iterator; @@ -16,7 +15,19 @@ import java.util.NoSuchElementException; import java.util.Queue; import java.util.Random; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ArrayDequeTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns a new deque of given size containing consecutive @@ -44,7 +55,7 @@ public class ArrayDequeTest extends JSR166TestCase { */ public void testConstructor3() { try { - ArrayDeque q = new ArrayDeque((Collection)null); + new ArrayDeque((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -55,7 +66,7 @@ public class ArrayDequeTest extends JSR166TestCase { public void testConstructor4() { try { Integer[] ints = new Integer[SIZE]; - ArrayDeque q = new ArrayDeque(Arrays.asList(ints)); + new ArrayDeque(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -68,7 +79,7 @@ public class ArrayDequeTest extends JSR166TestCase { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); - ArrayDeque q = new ArrayDeque(Arrays.asList(ints)); + new ArrayDeque(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -386,13 +397,13 @@ public class ArrayDequeTest extends JSR166TestCase { */ public void testRemoveElement() { ArrayDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -525,10 +536,10 @@ public class ArrayDequeTest extends JSR166TestCase { */ public void testRemoveFirstOccurrence() { ArrayDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); assertFalse(q.removeFirstOccurrence(new Integer(i+1))); } @@ -540,10 +551,10 @@ public class ArrayDequeTest extends JSR166TestCase { */ public void testRemoveLastOccurrence() { ArrayDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); assertFalse(q.removeLastOccurrence(new Integer(i+1))); } @@ -735,13 +746,21 @@ public class ArrayDequeTest extends JSR166TestCase { */ public void testIterator() { ArrayDeque q = populatedDeque(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + Deque c = new ArrayDeque(); + assertIteratorExhausted(c.iterator()); + assertIteratorExhausted(c.descendingIterator()); } /** @@ -885,4 +904,21 @@ public class ArrayDequeTest extends JSR166TestCase { assertTrue(y.isEmpty()); } + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Deque<?>[] qs = { + new ArrayDeque<Object>(), + populatedDeque(2), + }; + + for (Deque<?> q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + assertFalse(q.removeFirstOccurrence(null)); + assertFalse(q.removeLastOccurrence(null)); + } + } + } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java b/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java index 7a50120..bfe3fc6 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java @@ -8,10 +8,21 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicBoolean; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicBooleanTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * constructor initializes to given value @@ -91,11 +102,11 @@ public class AtomicBooleanTest extends JSR166TestCase { */ public void testWeakCompareAndSet() { AtomicBoolean ai = new AtomicBoolean(true); - while (!ai.weakCompareAndSet(true, false)); + do {} while (!ai.weakCompareAndSet(true, false)); assertFalse(ai.get()); - while (!ai.weakCompareAndSet(false, false)); + do {} while (!ai.weakCompareAndSet(false, false)); assertFalse(ai.get()); - while (!ai.weakCompareAndSet(false, true)); + do {} while (!ai.weakCompareAndSet(false, true)); assertTrue(ai.get()); } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java b/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java index e81a107..670b9ce 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java @@ -8,11 +8,22 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.concurrent.atomic.AtomicIntegerArray; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicIntegerArrayTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * constructor creates array of given size with all elements zero @@ -29,7 +40,7 @@ public class AtomicIntegerArrayTest extends JSR166TestCase { public void testConstructor2NPE() { try { int[] a = null; - AtomicIntegerArray aa = new AtomicIntegerArray(a); + new AtomicIntegerArray(a); shouldThrow(); } catch (NullPointerException success) {} } @@ -157,10 +168,10 @@ public class AtomicIntegerArrayTest extends JSR166TestCase { AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; i++) { aa.set(i, 1); - while (!aa.weakCompareAndSet(i, 1, 2)); - while (!aa.weakCompareAndSet(i, 2, -4)); + do {} while (!aa.weakCompareAndSet(i, 1, 2)); + do {} while (!aa.weakCompareAndSet(i, 2, -4)); assertEquals(-4, aa.get(i)); - while (!aa.weakCompareAndSet(i, -4, 7)); + do {} while (!aa.weakCompareAndSet(i, -4, 7)); assertEquals(7, aa.get(i)); } } @@ -267,8 +278,6 @@ public class AtomicIntegerArrayTest extends JSR166TestCase { } } - static final int COUNTDOWN = 100000; - class Counter extends CheckedRunnable { final AtomicIntegerArray aa; volatile int counts; @@ -297,8 +306,9 @@ public class AtomicIntegerArrayTest extends JSR166TestCase { */ public void testCountingInMultipleThreads() throws InterruptedException { final AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + int countdown = 10000; for (int i = 0; i < SIZE; i++) - aa.set(i, COUNTDOWN); + aa.set(i, countdown); Counter c1 = new Counter(aa); Counter c2 = new Counter(aa); Thread t1 = new Thread(c1); @@ -307,7 +317,7 @@ public class AtomicIntegerArrayTest extends JSR166TestCase { t2.start(); t1.join(); t2.join(); - assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); + assertEquals(c1.counts+c2.counts, SIZE * countdown); } /** diff --git a/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java b/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java index f0c1ae6..ef75b46 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java @@ -8,13 +8,24 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { volatile int x = 0; int w; long z; + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) { return AtomicIntegerFieldUpdater.newUpdater @@ -127,10 +138,10 @@ public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; a = updaterFor("x"); x = 1; - while (!a.weakCompareAndSet(this, 1, 2)); - while (!a.weakCompareAndSet(this, 2, -4)); + do {} while (!a.weakCompareAndSet(this, 1, 2)); + do {} while (!a.weakCompareAndSet(this, 2, -4)); assertEquals(-4, a.get(this)); - while (!a.weakCompareAndSet(this, -4, 7)); + do {} while (!a.weakCompareAndSet(this, -4, 7)); assertEquals(7, a.get(this)); } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java b/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java index 2afaa73..cf73810 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java @@ -8,10 +8,21 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicInteger; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicIntegerTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } final int[] VALUES = { Integer.MIN_VALUE, -1, 0, 1, 42, Integer.MAX_VALUE, @@ -96,10 +107,10 @@ public class AtomicIntegerTest extends JSR166TestCase { */ public void testWeakCompareAndSet() { AtomicInteger ai = new AtomicInteger(1); - while (!ai.weakCompareAndSet(1, 2)); - while (!ai.weakCompareAndSet(2, -4)); + do {} while (!ai.weakCompareAndSet(1, 2)); + do {} while (!ai.weakCompareAndSet(2, -4)); assertEquals(-4, ai.get()); - while (!ai.weakCompareAndSet(-4, 7)); + do {} while (!ai.weakCompareAndSet(-4, 7)); assertEquals(7, ai.get()); } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java b/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java index 53be5dc..08df01e 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java @@ -8,11 +8,22 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.concurrent.atomic.AtomicLongArray; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicLongArrayTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * constructor creates array of given size with all elements zero @@ -29,7 +40,7 @@ public class AtomicLongArrayTest extends JSR166TestCase { public void testConstructor2NPE() { try { long[] a = null; - AtomicLongArray aa = new AtomicLongArray(a); + new AtomicLongArray(a); shouldThrow(); } catch (NullPointerException success) {} } @@ -157,10 +168,10 @@ public class AtomicLongArrayTest extends JSR166TestCase { AtomicLongArray aa = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; i++) { aa.set(i, 1); - while (!aa.weakCompareAndSet(i, 1, 2)); - while (!aa.weakCompareAndSet(i, 2, -4)); + do {} while (!aa.weakCompareAndSet(i, 1, 2)); + do {} while (!aa.weakCompareAndSet(i, 2, -4)); assertEquals(-4, aa.get(i)); - while (!aa.weakCompareAndSet(i, -4, 7)); + do {} while (!aa.weakCompareAndSet(i, -4, 7)); assertEquals(7, aa.get(i)); } } @@ -267,8 +278,6 @@ public class AtomicLongArrayTest extends JSR166TestCase { } } - static final long COUNTDOWN = 100000; - class Counter extends CheckedRunnable { final AtomicLongArray aa; volatile long counts; @@ -297,8 +306,9 @@ public class AtomicLongArrayTest extends JSR166TestCase { */ public void testCountingInMultipleThreads() throws InterruptedException { final AtomicLongArray aa = new AtomicLongArray(SIZE); + long countdown = 10000; for (int i = 0; i < SIZE; i++) - aa.set(i, COUNTDOWN); + aa.set(i, countdown); Counter c1 = new Counter(aa); Counter c2 = new Counter(aa); Thread t1 = new Thread(c1); @@ -307,7 +317,7 @@ public class AtomicLongArrayTest extends JSR166TestCase { t2.start(); t1.join(); t2.join(); - assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); + assertEquals(c1.counts+c2.counts, SIZE * countdown); } /** diff --git a/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java b/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java index c9374e0..204f814 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java @@ -8,14 +8,26 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicLongFieldUpdater; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicLongFieldUpdaterTest extends JSR166TestCase { volatile long x = 0; int z; long w; + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) { return AtomicLongFieldUpdater.newUpdater (AtomicLongFieldUpdaterTest.class, fieldName); @@ -127,10 +139,10 @@ public class AtomicLongFieldUpdaterTest extends JSR166TestCase { AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; a = updaterFor("x"); x = 1; - while (!a.weakCompareAndSet(this, 1, 2)); - while (!a.weakCompareAndSet(this, 2, -4)); + do {} while (!a.weakCompareAndSet(this, 1, 2)); + do {} while (!a.weakCompareAndSet(this, 2, -4)); assertEquals(-4, a.get(this)); - while (!a.weakCompareAndSet(this, -4, 7)); + do {} while (!a.weakCompareAndSet(this, -4, 7)); assertEquals(7, a.get(this)); } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java b/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java index d300367..b9c1722 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java @@ -8,10 +8,21 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicLong; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicLongTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } final long[] VALUES = { Long.MIN_VALUE, @@ -98,10 +109,10 @@ public class AtomicLongTest extends JSR166TestCase { */ public void testWeakCompareAndSet() { AtomicLong ai = new AtomicLong(1); - while (!ai.weakCompareAndSet(1, 2)); - while (!ai.weakCompareAndSet(2, -4)); + do {} while (!ai.weakCompareAndSet(1, 2)); + do {} while (!ai.weakCompareAndSet(2, -4)); assertEquals(-4, ai.get()); - while (!ai.weakCompareAndSet(-4, 7)); + do {} while (!ai.weakCompareAndSet(-4, 7)); assertEquals(7, ai.get()); } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java b/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java index fd1f2f1..61b6b1b 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java @@ -8,10 +8,21 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicMarkableReference; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicMarkableReferenceTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * constructor initializes to given reference and mark @@ -135,11 +146,11 @@ public class AtomicMarkableReferenceTest extends JSR166TestCase { assertFalse(ai.isMarked()); assertFalse(mark[0]); - while (!ai.weakCompareAndSet(one, two, false, false)); + do {} while (!ai.weakCompareAndSet(one, two, false, false)); assertSame(two, ai.get(mark)); assertFalse(mark[0]); - while (!ai.weakCompareAndSet(two, m3, false, true)); + do {} while (!ai.weakCompareAndSet(two, m3, false, true)); assertSame(m3, ai.get(mark)); assertTrue(mark[0]); } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java b/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java index 0a4f3d9..1df2f9f 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java @@ -8,11 +8,22 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.concurrent.atomic.AtomicReferenceArray; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicReferenceArrayTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * constructor creates array of given size with all elements null @@ -30,7 +41,7 @@ public class AtomicReferenceArrayTest extends JSR166TestCase { public void testConstructor2NPE() { try { Integer[] a = null; - AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a); + new AtomicReferenceArray<Integer>(a); shouldThrow(); } catch (NullPointerException success) {} } @@ -165,10 +176,10 @@ public class AtomicReferenceArrayTest extends JSR166TestCase { AtomicReferenceArray aa = new AtomicReferenceArray(SIZE); for (int i = 0; i < SIZE; i++) { aa.set(i, one); - while (!aa.weakCompareAndSet(i, one, two)); - while (!aa.weakCompareAndSet(i, two, m4)); + do {} while (!aa.weakCompareAndSet(i, one, two)); + do {} while (!aa.weakCompareAndSet(i, two, m4)); assertSame(m4, aa.get(i)); - while (!aa.weakCompareAndSet(i, m4, seven)); + do {} while (!aa.weakCompareAndSet(i, m4, seven)); assertSame(seven, aa.get(i)); } } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java b/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java index 271c7b7..4b0d946 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java @@ -8,15 +8,27 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { volatile Integer x = null; Object z; Integer w; volatile int i; + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) { return AtomicReferenceFieldUpdater.newUpdater (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName); @@ -68,7 +80,7 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { * get returns the last value set or assigned */ public void testGetSet() { - AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; + AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; assertSame(one, a.get(this)); @@ -82,7 +94,7 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { * get returns the last value lazySet by same thread */ public void testGetLazySet() { - AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; + AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; assertSame(one, a.get(this)); @@ -96,7 +108,7 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet() { - AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; + AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; assertTrue(a.compareAndSet(this, one, two)); @@ -114,7 +126,7 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { */ public void testCompareAndSetInMultipleThreads() throws Exception { x = one; - final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; + final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); Thread t = new Thread(new CheckedRunnable() { @@ -135,13 +147,13 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { * to expected */ public void testWeakCompareAndSet() { - AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; + AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; - while (!a.weakCompareAndSet(this, one, two)); - while (!a.weakCompareAndSet(this, two, m4)); + do {} while (!a.weakCompareAndSet(this, one, two)); + do {} while (!a.weakCompareAndSet(this, two, m4)); assertSame(m4, a.get(this)); - while (!a.weakCompareAndSet(this, m4, seven)); + do {} while (!a.weakCompareAndSet(this, m4, seven)); assertSame(seven, a.get(this)); } @@ -149,7 +161,7 @@ public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { * getAndSet returns previous value and sets to given value */ public void testGetAndSet() { - AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; + AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; assertSame(one, a.getAndSet(this, zero)); diff --git a/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java b/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java index 8032546..457182f 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java @@ -8,10 +8,21 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicReference; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicReferenceTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * constructor initializes to given value @@ -92,10 +103,10 @@ public class AtomicReferenceTest extends JSR166TestCase { */ public void testWeakCompareAndSet() { AtomicReference ai = new AtomicReference(one); - while (!ai.weakCompareAndSet(one, two)); - while (!ai.weakCompareAndSet(two, m4)); + do {} while (!ai.weakCompareAndSet(one, two)); + do {} while (!ai.weakCompareAndSet(two, m4)); assertSame(m4, ai.get()); - while (!ai.weakCompareAndSet(m4, seven)); + do {} while (!ai.weakCompareAndSet(m4, seven)); assertSame(seven, ai.get()); } diff --git a/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java b/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java index 3e6445e..b3ff06a 100644 --- a/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java +++ b/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java @@ -8,10 +8,21 @@ package jsr166; -import junit.framework.*; import java.util.concurrent.atomic.AtomicStampedReference; +import junit.framework.Test; +import junit.framework.TestSuite; + public class AtomicStampedReferenceTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * constructor initializes to given reference and stamp @@ -135,11 +146,11 @@ public class AtomicStampedReferenceTest extends JSR166TestCase { assertEquals(0, ai.getStamp()); assertEquals(0, mark[0]); - while (!ai.weakCompareAndSet(one, two, 0, 0)); + do {} while (!ai.weakCompareAndSet(one, two, 0, 0)); assertSame(two, ai.get(mark)); assertEquals(0, mark[0]); - while (!ai.weakCompareAndSet(two, m3, 0, 1)); + do {} while (!ai.weakCompareAndSet(two, m3, 0, 1)); assertSame(m3, ai.get(mark)); assertEquals(1, mark[0]); } diff --git a/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java index 1ed7559..db0f03d 100644 --- a/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java @@ -9,14 +9,17 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Queue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; +import junit.framework.TestSuite; /** * Contains "contract" tests applicable to all BlockingQueue implementations. @@ -32,6 +35,15 @@ public abstract class BlockingQueueTest extends JSR166TestCase { * instances. */ + /** Like suite(), but non-static */ + // android-note: Explicitly instantiated. + // + // public Test testSuite() { + // // TODO: filter the returned tests using the configuration + // // information provided by the subclass via protected methods. + // return new TestSuite(this.getClass()); + // } + //---------------------------------------------------------------- // Configuration methods //---------------------------------------------------------------- @@ -335,7 +347,7 @@ public abstract class BlockingQueueTest extends JSR166TestCase { checkEmpty(q); for (int i = 0; i < size; i++) q.add(elts[i] = makeElement(i)); - for (int i = 1; i < size; i+=2) { + for (int i = 1; i < size; i += 2) { for (int pass = 0; pass < 2; pass++) { assertEquals((pass == 0), q.contains(elts[i])); assertEquals((pass == 0), q.remove(elts[i])); @@ -347,7 +359,7 @@ public abstract class BlockingQueueTest extends JSR166TestCase { } if (size > 0) assertTrue(q.contains(elts[0])); - for (int i = size-2; i >= 0; i-=2) { + for (int i = size-2; i >= 0; i -= 2) { assertTrue(q.contains(elts[i])); assertFalse(q.contains(elts[i+1])); assertTrue(q.remove(elts[i])); diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java index 5d9e2ac..4650f41 100644 --- a/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java +++ b/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java @@ -8,17 +8,36 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.Map; +import java.util.Random; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentHashMapTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns a new map from Integers 1-5 to Strings "A"-"E". */ - private static ConcurrentHashMap map5() { - ConcurrentHashMap map = new ConcurrentHashMap(5); + private static ConcurrentHashMap<Integer, String> map5() { + ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5); assertTrue(map.isEmpty()); map.put(one, "A"); map.put(two, "B"); @@ -30,12 +49,15 @@ public class ConcurrentHashMapTest extends JSR166TestCase { return map; } + /** Re-implement Integer.compare for old java versions */ + static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } + // classes for testing Comparable fallbacks static class BI implements Comparable<BI> { private final int value; BI(int value) { this.value = value; } public int compareTo(BI other) { - return Integer.compare(value, other.value); + return compare(value, other.value); } public boolean equals(Object x) { return (x instanceof BI) && ((BI)x).value == value; @@ -59,12 +81,9 @@ public class ConcurrentHashMapTest extends JSR166TestCase { static class LexicographicList<E extends Comparable<E>> extends ArrayList<E> implements Comparable<LexicographicList<E>> { - static long total; - static long n; LexicographicList(Collection<E> c) { super(c); } LexicographicList(E e) { super(Collections.singleton(e)); } public int compareTo(LexicographicList<E> other) { - long start = System.currentTimeMillis(); int common = Math.min(size(), other.size()); int r = 0; for (int i = 0; i < common; i++) { @@ -72,25 +91,40 @@ public class ConcurrentHashMapTest extends JSR166TestCase { break; } if (r == 0) - r = Integer.compare(size(), other.size()); - total += System.currentTimeMillis() - start; - n++; + r = compare(size(), other.size()); return r; } private static final long serialVersionUID = 0; } + static class CollidingObject { + final String value; + CollidingObject(final String value) { this.value = value; } + public int hashCode() { return this.value.hashCode() & 1; } + public boolean equals(final Object obj) { + return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value); + } + } + + static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> { + ComparableCollidingObject(final String value) { super(value); } + public int compareTo(final ComparableCollidingObject o) { + return value.compareTo(o.value); + } + } + /** * Inserted elements that are subclasses of the same Comparable * class are found. */ public void testComparableFamily() { + int size = 500; // makes measured test run time -> 60ms ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<BI, Boolean>(); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < size; i++) { assertTrue(m.put(new CI(i), true) == null); } - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < size; i++) { assertTrue(m.containsKey(new CI(i))); assertTrue(m.containsKey(new DI(i))); } @@ -100,24 +134,25 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * Elements of classes with erased generic type parameters based * on Comparable can be inserted and found. */ - public void testGenericComparable() { - ConcurrentHashMap<Object, Boolean> m = - new ConcurrentHashMap<Object, Boolean>(); - for (int i = 0; i < 1000; i++) { - BI bi = new BI(i); - BS bs = new BS(String.valueOf(i)); - LexicographicList<BI> bis = new LexicographicList<BI>(bi); - LexicographicList<BS> bss = new LexicographicList<BS>(bs); - assertTrue(m.putIfAbsent(bis, true) == null); - assertTrue(m.containsKey(bis)); - if (m.putIfAbsent(bss, true) == null) - assertTrue(m.containsKey(bss)); - assertTrue(m.containsKey(bis)); - } - for (int i = 0; i < 1000; i++) { - assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); - } - } + public void testGenericComparable() { + int size = 120; // makes measured test run time -> 60ms + ConcurrentHashMap<Object, Boolean> m = + new ConcurrentHashMap<Object, Boolean>(); + for (int i = 0; i < size; i++) { + BI bi = new BI(i); + BS bs = new BS(String.valueOf(i)); + LexicographicList<BI> bis = new LexicographicList<BI>(bi); + LexicographicList<BS> bss = new LexicographicList<BS>(bs); + assertTrue(m.putIfAbsent(bis, true) == null); + assertTrue(m.containsKey(bis)); + if (m.putIfAbsent(bss, true) == null) + assertTrue(m.containsKey(bss)); + assertTrue(m.containsKey(bis)); + } + for (int i = 0; i < size; i++) { + assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); + } + } /** * Elements of non-comparable classes equal to those of classes @@ -125,19 +160,55 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * inserted and found. */ public void testGenericComparable2() { + int size = 500; // makes measured test run time -> 60ms ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<Object, Boolean>(); - for (int i = 0; i < 1000; i++) { - m.put(new ArrayList(Collections.singleton(new BI(i))), true); + for (int i = 0; i < size; i++) { + m.put(Collections.singletonList(new BI(i)), true); } - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < size; i++) { LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); assertTrue(m.containsKey(bis)); } } /** + * Mixtures of instances of comparable and non-comparable classes + * can be inserted and found. + */ + public void testMixedComparable() { + int size = 1200; // makes measured test run time -> 35ms + ConcurrentHashMap<Object, Object> map = + new ConcurrentHashMap<Object, Object>(); + Random rng = new Random(); + for (int i = 0; i < size; i++) { + Object x; + switch (rng.nextInt(4)) { + case 0: + x = new Object(); + break; + case 1: + x = new CollidingObject(Integer.toString(i)); + break; + default: + x = new ComparableCollidingObject(Integer.toString(i)); + } + assertNull(map.put(x, x)); + } + int count = 0; + for (Object k : map.keySet()) { + assertEquals(map.get(k), k); + ++count; + } + assertEquals(count, size); + assertEquals(map.size(), size); + for (Object k : map.keySet()) { + assertEquals(map.put(k, k), k); + } + } + + /** * clear removes all pairs */ public void testClear() { @@ -160,6 +231,17 @@ public class ConcurrentHashMapTest extends JSR166TestCase { } /** + * hashCode() equals sum of each key.hashCode ^ value.hashCode + */ + public void testHashCode() { + ConcurrentHashMap<Integer,String> map = map5(); + int sum = 0; + for (Map.Entry<Integer,String> e : map.entrySet()) + sum += e.getKey().hashCode() ^ e.getValue().hashCode(); + assertEquals(sum, map.hashCode()); + } + + /** * contains returns true for contained value */ public void testContains() { @@ -210,6 +292,7 @@ public class ConcurrentHashMapTest extends JSR166TestCase { assertEquals("A", (String)map.get(one)); ConcurrentHashMap empty = new ConcurrentHashMap(); assertNull(map.get("anything")); + assertNull(empty.get("anything")); } /** @@ -443,41 +526,81 @@ public class ConcurrentHashMapTest extends JSR166TestCase { // Exception tests /** - * Cannot create with negative capacity + * Cannot create with only negative capacity */ public void testConstructor1() { try { - new ConcurrentHashMap(-1,0,1); + new ConcurrentHashMap(-1); shouldThrow(); } catch (IllegalArgumentException success) {} } /** - * Cannot create with negative concurrency level - */ + * Constructor (initialCapacity, loadFactor) throws + * IllegalArgumentException if either argument is negative + */ public void testConstructor2() { try { - new ConcurrentHashMap(1,0,-1); + new ConcurrentHashMap(-1, .75f); + shouldThrow(); + } catch (IllegalArgumentException success) {} + + try { + new ConcurrentHashMap(16, -1); shouldThrow(); } catch (IllegalArgumentException success) {} } /** - * Cannot create with only negative capacity + * Constructor (initialCapacity, loadFactor, concurrencyLevel) + * throws IllegalArgumentException if any argument is negative */ public void testConstructor3() { try { - new ConcurrentHashMap(-1); + new ConcurrentHashMap(-1, .75f, 1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + + try { + new ConcurrentHashMap(16, -1, 1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + + try { + new ConcurrentHashMap(16, .75f, -1); shouldThrow(); } catch (IllegalArgumentException success) {} } /** + * ConcurrentHashMap(map) throws NullPointerException if the given + * map is null + */ + public void testConstructor4() { + try { + new ConcurrentHashMap(null); + shouldThrow(); + } catch (NullPointerException success) {} + } + + /** + * ConcurrentHashMap(map) creates a new map with the same mappings + * as the given map + */ + public void testConstructor5() { + ConcurrentHashMap map1 = map5(); + ConcurrentHashMap map2 = new ConcurrentHashMap(map5()); + assertTrue(map2.equals(map1)); + map2.put(one, "F"); + assertFalse(map2.equals(map1)); + } + + /** * get(null) throws NPE */ public void testGet_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -487,8 +610,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * containsKey(null) throws NPE */ public void testContainsKey_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -498,8 +621,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * containsValue(null) throws NPE */ public void testContainsValue_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsValue(null); shouldThrow(); } catch (NullPointerException success) {} @@ -509,8 +632,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * contains(null) throws NPE */ public void testContains_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.contains(null); shouldThrow(); } catch (NullPointerException success) {} @@ -520,8 +643,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -531,8 +654,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * put(x, null) throws NPE */ public void testPut2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("whatever", null); shouldThrow(); } catch (NullPointerException success) {} @@ -542,8 +665,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * putIfAbsent(null, x) throws NPE */ public void testPutIfAbsent1_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -553,8 +676,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * replace(null, x) throws NPE */ public void testReplace_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -564,8 +687,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * replace(null, x, y) throws NPE */ public void testReplaceValue_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, one, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -575,8 +698,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * putIfAbsent(x, null) throws NPE */ public void testPutIfAbsent2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent("whatever", null); shouldThrow(); } catch (NullPointerException success) {} @@ -586,8 +709,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * replace(x, null) throws NPE */ public void testReplace2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null); shouldThrow(); } catch (NullPointerException success) {} @@ -597,8 +720,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * replace(x, null, y) throws NPE */ public void testReplaceValue2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null, "A"); shouldThrow(); } catch (NullPointerException success) {} @@ -608,8 +731,8 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * replace(x, y, null) throws NPE */ public void testReplaceValue3_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", one, null); shouldThrow(); } catch (NullPointerException success) {} @@ -619,9 +742,9 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * remove(null) throws NPE */ public void testRemove1_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} @@ -631,9 +754,9 @@ public class ConcurrentHashMapTest extends JSR166TestCase { * remove(null, x) throws NPE */ public void testRemove2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java index f5b8318..c445957 100644 --- a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java +++ b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java @@ -8,17 +8,30 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.Collection; +import java.util.Deque; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; import java.util.Random; import java.util.concurrent.ConcurrentLinkedDeque; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentLinkedDequeTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + /** * Returns a new deque of given size containing consecutive * Integers 0 ... n. @@ -46,7 +59,7 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { */ public void testConstructor3() { try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null); + new ConcurrentLinkedDeque((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -57,7 +70,7 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { public void testConstructor4() { try { Integer[] ints = new Integer[SIZE]; - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); + new ConcurrentLinkedDeque(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -70,7 +83,7 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); + new ConcurrentLinkedDeque(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -428,13 +441,13 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { */ public void testRemoveElement() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -538,10 +551,10 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { */ public void testRemoveFirstOccurrence() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); assertFalse(q.removeFirstOccurrence(new Integer(i+1))); } @@ -553,10 +566,10 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { */ public void testRemoveLastOccurrence() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); assertFalse(q.removeLastOccurrence(new Integer(i+1))); } @@ -632,8 +645,8 @@ public class ConcurrentLinkedDequeTest 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)); } } } @@ -687,13 +700,21 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { */ public void testIterator() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + Deque c = new ConcurrentLinkedDeque(); + assertIteratorExhausted(c.iterator()); + assertIteratorExhausted(c.descendingIterator()); } /** @@ -855,4 +876,30 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase { assertTrue(y.isEmpty()); } + /** + * contains(null) always return false. + * remove(null) always throws NullPointerException. + */ + public void testNeverContainsNull() { + Deque<?>[] qs = { + new ConcurrentLinkedDeque<Object>(), + populatedDeque(2), + }; + + for (Deque<?> q : qs) { + assertFalse(q.contains(null)); + try { + assertFalse(q.remove(null)); + shouldThrow(); + } catch (NullPointerException success) {} + try { + assertFalse(q.removeFirstOccurrence(null)); + shouldThrow(); + } catch (NullPointerException success) {} + try { + assertFalse(q.removeLastOccurrence(null)); + shouldThrow(); + } catch (NullPointerException success) {} + } + } } diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java index 7924034..d3f5b1f 100644 --- a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java @@ -8,7 +8,6 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; @@ -16,8 +15,21 @@ import java.util.NoSuchElementException; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentLinkedQueueTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + /** * Returns a new queue of given size containing consecutive * Integers 0 ... n. @@ -44,7 +56,7 @@ public class ConcurrentLinkedQueueTest extends JSR166TestCase { */ public void testConstructor3() { try { - ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); + new ConcurrentLinkedQueue((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -55,7 +67,7 @@ public class ConcurrentLinkedQueueTest extends JSR166TestCase { public void testConstructor4() { try { Integer[] ints = new Integer[SIZE]; - ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); + new ConcurrentLinkedQueue(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -68,7 +80,7 @@ public class ConcurrentLinkedQueueTest extends JSR166TestCase { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); - ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); + new ConcurrentLinkedQueue(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -279,13 +291,13 @@ public class ConcurrentLinkedQueueTest extends JSR166TestCase { */ public void testRemoveElement() { ConcurrentLinkedQueue q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -364,8 +376,8 @@ public class ConcurrentLinkedQueueTest 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)); } } } @@ -419,13 +431,19 @@ public class ConcurrentLinkedQueueTest extends JSR166TestCase { */ public void testIterator() { ConcurrentLinkedQueue q = populatedQueue(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new ConcurrentLinkedQueue().iterator()); } /** @@ -508,4 +526,18 @@ public class ConcurrentLinkedQueueTest extends JSR166TestCase { assertTrue(y.isEmpty()); } + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection<?>[] qs = { + new ConcurrentLinkedQueue<Object>(), + populatedQueue(2), + }; + + for (Collection<?> q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + } } diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java index 4359287..0aadd23 100644 --- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java +++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java @@ -6,11 +6,32 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.BitSet; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.NavigableSet; +import java.util.NoSuchElementException; +import java.util.Random; +import java.util.Set; import java.util.concurrent.ConcurrentSkipListMap; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentSkipListMapTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns a new map from Integers 1-5 to Strings "A"-"E". @@ -678,8 +699,8 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -689,8 +710,8 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -700,8 +721,8 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * containsValue(null) throws NPE */ public void testContainsValue_NullPointerException() { + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); c.containsValue(null); shouldThrow(); } catch (NullPointerException success) {} @@ -711,8 +732,8 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -722,8 +743,8 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * putIfAbsent(null, x) throws NPE */ public void testPutIfAbsent1_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.putIfAbsent(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -733,8 +754,8 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * replace(null, x) throws NPE */ public void testReplace_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.replace(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -744,8 +765,8 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * replace(null, x, y) throws NPE */ public void testReplaceValue_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.replace(null, one, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -755,9 +776,9 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * remove(null) throws NPE */ public void testRemove1_NullPointerException() { + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); + c.put("sadsdf", "asdads"); try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); - c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} @@ -767,9 +788,9 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { * remove(null, x) throws NPE */ public void testRemove2_NullPointerException() { + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); + c.put("sadsdf", "asdads"); try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); - c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -1014,7 +1035,7 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { // Add entries till we're back to original size while (map.size() < size) { int key = min + rnd.nextInt(rangeSize); - assertTrue(key >= min && key<= max); + assertTrue(key >= min && key <= max); put(map, key); } } @@ -1039,7 +1060,7 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase { // Add entries till we're back to original size while (map.size() < size) { int key = min - 5 + rnd.nextInt(rangeSize + 10); - if (key >= min && key<= max) { + if (key >= min && key <= max) { put(map, key); } else { try { diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java index 1fd3c5f..41f8835 100644 --- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java +++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java @@ -6,7 +6,6 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.BitSet; import java.util.Collection; @@ -19,7 +18,19 @@ import java.util.Set; import java.util.SortedSet; import java.util.concurrent.ConcurrentSkipListSet; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentSkipListSetTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { @@ -35,9 +46,9 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { ConcurrentSkipListSet<Integer> q = new ConcurrentSkipListSet<Integer>(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i -= 2) assertTrue(q.add(new Integer(i))); - for (int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.add(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(n, q.size()); @@ -71,7 +82,7 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { */ public void testConstructor3() { try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); + new ConcurrentSkipListSet((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -82,7 +93,7 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { public void testConstructor4() { try { Integer[] ints = new Integer[SIZE]; - ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); + new ConcurrentSkipListSet(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -95,7 +106,7 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); - ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); + new ConcurrentSkipListSet(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -160,8 +171,8 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { * add(null) throws NPE */ public void testAddNull() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -189,9 +200,8 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { * Add of non-Comparable throws CCE */ public void testAddNonComparable() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -202,8 +212,8 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { * addAll(null) throws NPE */ public void testAddAll1() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -213,9 +223,9 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + Integer[] ints = new Integer[SIZE]; try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -226,11 +236,11 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { * possibly adding some elements */ public void testAddAll3() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - 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) {} @@ -278,13 +288,13 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { */ public void testRemoveElement() { ConcurrentSkipListSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -363,8 +373,8 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } @@ -467,27 +477,21 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { */ public void testIterator() { ConcurrentSkipListSet q = populatedSet(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); } /** * iterator of empty set has no elements */ public void testEmptyIterator() { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - int i = 0; - Iterator it = q.iterator(); - while (it.hasNext()) { - assertTrue(q.contains(it.next())); - ++i; - } - assertEquals(0, i); + NavigableSet s = new ConcurrentSkipListSet(); + assertIteratorExhausted(s.iterator()); + assertIteratorExhausted(s.descendingSet().iterator()); } /** @@ -726,7 +730,7 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { // Add entries till we're back to original size while (set.size() < size) { int element = min + rnd.nextInt(rangeSize); - assertTrue(element >= min && element<= max); + assertTrue(element >= min && element <= max); put(set, element, bs); } } @@ -752,7 +756,7 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase { // Add entries till we're back to original size while (set.size() < size) { int element = min - 5 + rnd.nextInt(rangeSize + 10); - if (element >= min && element<= max) { + if (element >= min && element <= max) { put(set, element, bs); } else { try { diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java index 7247657..5315bcb 100644 --- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java +++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java @@ -6,12 +6,30 @@ package jsr166; -import junit.framework.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Set; +import java.util.SortedMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; -import java.util.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ConcurrentSkipListSubMapTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns a new map from Integers 1-5 to Strings "A"-"E". diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java index 43c1759..f1c4aae 100644 --- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java +++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java @@ -6,20 +6,26 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; -import java.util.BitSet; -import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.NavigableSet; -import java.util.NoSuchElementException; -import java.util.Random; -import java.util.Set; import java.util.SortedSet; import java.util.concurrent.ConcurrentSkipListSet; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentSkipListSubSetTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { @@ -36,9 +42,9 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { new ConcurrentSkipListSet<Integer>(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i -= 2) assertTrue(q.add(new Integer(i))); - for (int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.add(new Integer(i))); assertTrue(q.add(new Integer(-n))); assertTrue(q.add(new Integer(n))); @@ -134,8 +140,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * add(null) throws NPE */ public void testAddNull() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -162,9 +168,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * Add of non-Comparable throws CCE */ public void testAddNonComparable() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -175,8 +180,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * addAll(null) throws NPE */ public void testAddAll1() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -186,9 +191,9 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + NavigableSet q = set0(); + Integer[] ints = new Integer[SIZE]; try { - NavigableSet q = set0(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -199,11 +204,11 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * possibly adding some elements */ public void testAddAll3() { + NavigableSet q = set0(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i+SIZE); try { - NavigableSet q = set0(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -240,13 +245,13 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { */ public void testRemoveElement() { NavigableSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -325,8 +330,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } @@ -430,27 +435,19 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { */ public void testIterator() { NavigableSet q = populatedSet(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); } /** * iterator of empty set has no elements */ public void testEmptyIterator() { - NavigableSet q = set0(); - int i = 0; - Iterator it = q.iterator(); - while (it.hasNext()) { - assertTrue(q.contains(it.next())); - ++i; - } - assertEquals(0, i); + assertIteratorExhausted(set0().iterator()); } /** @@ -639,8 +636,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * add(null) throws NPE */ public void testDescendingAddNull() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -667,9 +664,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * Add of non-Comparable throws CCE */ public void testDescendingAddNonComparable() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -680,8 +676,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * addAll(null) throws NPE */ public void testDescendingAddAll1() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -691,9 +687,9 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * addAll of a collection with null elements throws NPE */ public void testDescendingAddAll2() { + NavigableSet q = dset0(); + Integer[] ints = new Integer[SIZE]; try { - NavigableSet q = dset0(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -704,11 +700,11 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { * possibly adding some elements */ public void testDescendingAddAll3() { + NavigableSet q = dset0(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i+SIZE); try { - NavigableSet q = dset0(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -745,10 +741,10 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { */ public void testDescendingRemoveElement() { NavigableSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.remove(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2 ) { assertTrue(q.remove(new Integer(i))); assertFalse(q.remove(new Integer(i+1))); } @@ -824,8 +820,8 @@ public class ConcurrentSkipListSubSetTest extends JSR166TestCase { assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } diff --git a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java index 6bef8be..658268a 100644 --- a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java +++ b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java @@ -8,7 +8,6 @@ package jsr166; -import junit.framework.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -18,11 +17,23 @@ import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; -import java.util.Vector; import java.util.concurrent.CopyOnWriteArrayList; +import junit.framework.Test; +import junit.framework.TestSuite; + public class CopyOnWriteArrayListTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + static CopyOnWriteArrayList<Integer> populatedArray(int n) { CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); assertTrue(a.isEmpty()); @@ -76,16 +87,14 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { } /** - * addAll adds each element from the given collection + * addAll adds each element from the given collection, including duplicates */ public void testAddAll() { CopyOnWriteArrayList full = populatedArray(3); - Vector v = new Vector(); - v.add(three); - v.add(four); - v.add(five); - full.addAll(v); + assertTrue(full.addAll(Arrays.asList(three, four, five))); assertEquals(6, full.size()); + assertTrue(full.addAll(Arrays.asList(three, four, five))); + assertEquals(9, full.size()); } /** @@ -94,11 +103,10 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { */ public void testAddAllAbsent() { CopyOnWriteArrayList full = populatedArray(3); - Vector v = new Vector(); - v.add(three); - v.add(four); - v.add(one); // will not add this element - full.addAllAbsent(v); + // "one" is duplicate and will not be added + assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); + assertEquals(5, full.size()); + assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); assertEquals(5, full.size()); } @@ -188,12 +196,11 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { */ public void testContainsAll() { CopyOnWriteArrayList full = populatedArray(3); - Vector v = new Vector(); - v.add(one); - v.add(two); - assertTrue(full.containsAll(v)); - v.add(six); - assertFalse(full.containsAll(v)); + assertTrue(full.containsAll(Arrays.asList())); + assertTrue(full.containsAll(Arrays.asList(one))); + assertTrue(full.containsAll(Arrays.asList(one, two))); + assertFalse(full.containsAll(Arrays.asList(one, two, six))); + assertFalse(full.containsAll(Arrays.asList(six))); } /** @@ -256,11 +263,15 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { assertTrue(it.hasNext()); assertEquals(elements[j], it.next()); } - assertFalse(it.hasNext()); - try { - it.next(); - shouldThrow(); - } catch (NoSuchElementException success) {} + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + Collection c = new CopyOnWriteArrayList(); + assertIteratorExhausted(c.iterator()); } /** @@ -375,10 +386,9 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { */ public void testRemoveAll() { CopyOnWriteArrayList full = populatedArray(3); - Vector v = new Vector(); - v.add(one); - v.add(two); - full.removeAll(v); + assertTrue(full.removeAll(Arrays.asList(one, two))); + assertEquals(1, full.size()); + assertFalse(full.removeAll(Arrays.asList(one, two))); assertEquals(1, full.size()); } @@ -494,10 +504,10 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { * can not store the objects inside the list */ public void testToArray_ArrayStoreException() { + CopyOnWriteArrayList c = new CopyOnWriteArrayList(); + c.add("zfasdfsdf"); + c.add("asdadasd"); try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("zfasdfsdf"); - c.add("asdadasd"); c.toArray(new Long[5]); shouldThrow(); } catch (ArrayStoreException success) {} @@ -507,167 +517,196 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { * get throws an IndexOutOfBoundsException on a negative index */ public void testGet1_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.get(-1); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.get(-1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * get throws an IndexOutOfBoundsException on a too high index */ public void testGet2_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("asdasd"); - c.add("asdad"); - c.get(100); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.get(list.size()); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * set throws an IndexOutOfBoundsException on a negative index */ public void testSet1_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.set(-1,"qwerty"); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.set(-1, "qwerty"); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * set throws an IndexOutOfBoundsException on a too high index */ public void testSet2() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("asdasd"); - c.add("asdad"); - c.set(100, "qwerty"); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.set(list.size(), "qwerty"); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * add throws an IndexOutOfBoundsException on a negative index */ public void testAdd1_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add(-1,"qwerty"); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.add(-1, "qwerty"); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * add throws an IndexOutOfBoundsException on a too high index */ public void testAdd2_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("asdasd"); - c.add("asdasdasd"); - c.add(100, "qwerty"); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.add(list.size() + 1, "qwerty"); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * remove throws an IndexOutOfBoundsException on a negative index */ public void testRemove1_IndexOutOfBounds() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.remove(-1); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.remove(-1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * remove throws an IndexOutOfBoundsException on a too high index */ public void testRemove2_IndexOutOfBounds() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("asdasd"); - c.add("adasdasd"); - c.remove(100); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.remove(list.size()); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * addAll throws an IndexOutOfBoundsException on a negative index */ public void testAddAll1_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.addAll(-1,new LinkedList()); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.addAll(-1, new LinkedList()); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * addAll throws an IndexOutOfBoundsException on a too high index */ public void testAddAll2_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("asdasd"); - c.add("asdasdasd"); - c.addAll(100, new LinkedList()); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.addAll(list.size() + 1, new LinkedList()); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * listIterator throws an IndexOutOfBoundsException on a negative index */ public void testListIterator1_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.listIterator(-1); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.listIterator(-1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * listIterator throws an IndexOutOfBoundsException on a too high index */ public void testListIterator2_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("adasd"); - c.add("asdasdas"); - c.listIterator(100); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.listIterator(list.size() + 1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * subList throws an IndexOutOfBoundsException on a negative index */ public void testSubList1_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.subList(-1,100); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.subList(-1, list.size()); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** * subList throws an IndexOutOfBoundsException on a too high index */ public void testSubList2_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.add("asdasd"); - c.subList(1,100); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.subList(0, list.size() + 1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** @@ -675,11 +714,14 @@ public class CopyOnWriteArrayListTest extends JSR166TestCase { * is lower then the first */ public void testSubList3_IndexOutOfBoundsException() { - try { - CopyOnWriteArrayList c = new CopyOnWriteArrayList(); - c.subList(3,1); - shouldThrow(); - } catch (IndexOutOfBoundsException success) {} + CopyOnWriteArrayList c = populatedArray(5); + List[] lists = { c, c.subList(1, c.size() - 1) }; + for (List list : lists) { + try { + list.subList(list.size() - 1, 1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + } } /** diff --git a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java index feb283f..2810802 100644 --- a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java +++ b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java @@ -8,7 +8,6 @@ package jsr166; -import junit.framework.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -16,17 +15,28 @@ import java.util.Collections; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; -import java.util.Vector; import java.util.concurrent.CopyOnWriteArraySet; +import junit.framework.Test; +import junit.framework.TestSuite; + public class CopyOnWriteArraySetTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static CopyOnWriteArraySet<Integer> populatedSet(int n) { CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); assertTrue(a.isEmpty()); for (int i = 0; i < n; i++) a.add(i); - assertFalse(a.isEmpty()); + assertEquals(n == 0, a.isEmpty()); assertEquals(n, a.size()); return a; } @@ -62,29 +72,25 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { } /** - * addAll adds each element from the given collection + * addAll adds each non-duplicate element from the given collection */ public void testAddAll() { - CopyOnWriteArraySet full = populatedSet(3); - Vector v = new Vector(); - v.add(three); - v.add(four); - v.add(five); - full.addAll(v); + Set full = populatedSet(3); + assertTrue(full.addAll(Arrays.asList(three, four, five))); + assertEquals(6, full.size()); + assertFalse(full.addAll(Arrays.asList(three, four, five))); assertEquals(6, full.size()); } /** - * addAll adds each element from the given collection that did not - * already exist in the set + * addAll adds each non-duplicate element from the given collection */ public void testAddAll2() { - CopyOnWriteArraySet full = populatedSet(3); - Vector v = new Vector(); - v.add(three); - v.add(four); - v.add(one); // will not add this element - full.addAll(v); + Set full = populatedSet(3); + // "one" is duplicate and will not be added + assertTrue(full.addAll(Arrays.asList(three, four, one))); + assertEquals(5, full.size()); + assertFalse(full.addAll(Arrays.asList(three, four, one))); assertEquals(5, full.size()); } @@ -92,7 +98,7 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { * add will not add the element if it already exists in the set */ public void testAdd2() { - CopyOnWriteArraySet full = populatedSet(3); + Set full = populatedSet(3); full.add(one); assertEquals(3, full.size()); } @@ -101,7 +107,7 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { * add adds the element when it does not exist in the set */ public void testAdd3() { - CopyOnWriteArraySet full = populatedSet(3); + Set full = populatedSet(3); full.add(three); assertTrue(full.contains(three)); } @@ -110,16 +116,17 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { * clear removes all elements from the set */ public void testClear() { - CopyOnWriteArraySet full = populatedSet(3); + Collection full = populatedSet(3); full.clear(); assertEquals(0, full.size()); + assertTrue(full.isEmpty()); } /** * contains returns true for added elements */ public void testContains() { - CopyOnWriteArraySet full = populatedSet(3); + Collection full = populatedSet(3); assertTrue(full.contains(one)); assertFalse(full.contains(five)); } @@ -146,23 +153,20 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { * containsAll returns true for collections with subset of elements */ public void testContainsAll() { - CopyOnWriteArraySet full = populatedSet(3); - Vector v = new Vector(); - v.add(one); - v.add(two); - assertTrue(full.containsAll(v)); - v.add(six); - assertFalse(full.containsAll(v)); + Collection full = populatedSet(3); + assertTrue(full.containsAll(Arrays.asList())); + assertTrue(full.containsAll(Arrays.asList(one))); + assertTrue(full.containsAll(Arrays.asList(one, two))); + assertFalse(full.containsAll(Arrays.asList(one, two, six))); + assertFalse(full.containsAll(Arrays.asList(six))); } /** * isEmpty is true when empty, else false */ public void testIsEmpty() { - CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); - CopyOnWriteArraySet full = populatedSet(3); - assertTrue(empty.isEmpty()); - assertFalse(full.isEmpty()); + assertTrue(populatedSet(0).isEmpty()); + assertFalse(populatedSet(3).isEmpty()); } /** @@ -188,18 +192,21 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { assertTrue(it.hasNext()); assertEquals(elements[j], it.next()); } - assertFalse(it.hasNext()); - try { - it.next(); - shouldThrow(); - } catch (NoSuchElementException success) {} + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new CopyOnWriteArraySet().iterator()); } /** * iterator remove is unsupported */ public void testIteratorRemove() { - CopyOnWriteArraySet full = populatedSet(3); + Collection full = populatedSet(3); Iterator it = full.iterator(); it.next(); try { @@ -213,7 +220,7 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { */ public void testToString() { assertEquals("[]", new CopyOnWriteArraySet().toString()); - CopyOnWriteArraySet full = populatedSet(3); + Collection full = populatedSet(3); String s = full.toString(); for (int i = 0; i < 3; ++i) assertTrue(s.contains(String.valueOf(i))); @@ -225,11 +232,10 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { * removeAll removes all elements from the given collection */ public void testRemoveAll() { - CopyOnWriteArraySet full = populatedSet(3); - Vector v = new Vector(); - v.add(one); - v.add(two); - full.removeAll(v); + Set full = populatedSet(3); + assertTrue(full.removeAll(Arrays.asList(one, two))); + assertEquals(1, full.size()); + assertFalse(full.removeAll(Arrays.asList(one, two))); assertEquals(1, full.size()); } @@ -237,7 +243,7 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { * remove removes an element */ public void testRemove() { - CopyOnWriteArraySet full = populatedSet(3); + Collection full = populatedSet(3); full.remove(one); assertFalse(full.contains(one)); assertEquals(2, full.size()); @@ -247,8 +253,8 @@ public class CopyOnWriteArraySetTest extends JSR166TestCase { * size returns the number of elements */ public void testSize() { - CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); - CopyOnWriteArraySet full = populatedSet(3); + Collection empty = new CopyOnWriteArraySet(); + Collection full = populatedSet(3); assertEquals(3, full.size()); assertEquals(0, empty.size()); } diff --git a/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java b/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java index bc2aecf..da1ebb4 100644 --- a/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java +++ b/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java @@ -8,12 +8,23 @@ package jsr166; -import junit.framework.*; -import java.util.*; -import java.util.concurrent.CountDownLatch; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.concurrent.CountDownLatch; + +import junit.framework.Test; +import junit.framework.TestSuite; + public class CountDownLatchTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * negative constructor argument throws IAE diff --git a/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java b/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java index 2f8665b..80d7b3b 100644 --- a/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java +++ b/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java @@ -6,25 +6,34 @@ package jsr166; -import java.util.concurrent.ExecutionException; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.HashSet; import java.util.concurrent.CancellationException; +import java.util.concurrent.CountedCompleter; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; -import java.util.concurrent.CountedCompleter; -import java.util.concurrent.ForkJoinWorkerThread; -import java.util.concurrent.RecursiveAction; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import java.util.concurrent.atomic.AtomicReference; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.HashSet; -import junit.framework.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class CountedCompleterTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + // Runs with "mainPool" use > 1 thread. singletonPool tests use 1 static final int mainPoolSize = Math.max(2, Runtime.getRuntime().availableProcessors()); @@ -193,8 +202,8 @@ public class CountedCompleterTest extends JSR166TestCase { try { a.invoke(); shouldThrow(); - } catch (Throwable ex) { - assertSame(t, ex); + } catch (Throwable success) { + assertSame(t, success); } } @@ -494,8 +503,9 @@ public class CountedCompleterTest extends JSR166TestCase { // Invocation tests use some interdependent task classes // to better test propagation etc - - // Version of Fibonacci with different classes for left vs right forks + /** + * Version of Fibonacci with different classes for left vs right forks + */ abstract class CCF extends CheckedCC { int number; int rnumber; @@ -1131,7 +1141,7 @@ public class CountedCompleterTest extends JSR166TestCase { } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { ForkJoinTask a = new CheckedRecursiveAction() { @@ -1796,7 +1806,7 @@ public class CountedCompleterTest extends JSR166TestCase { } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollectionSingleton() { ForkJoinTask a = new CheckedRecursiveAction() { diff --git a/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java b/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java index 3239030..a9d8c54 100644 --- a/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java +++ b/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java @@ -8,16 +8,27 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; +import junit.framework.TestSuite; public class CyclicBarrierTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } private volatile int countAction; private class MyAction implements Runnable { diff --git a/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java b/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java index dc221ab..7619b48 100644 --- a/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java @@ -8,9 +8,11 @@ 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; import java.util.concurrent.BlockingQueue; @@ -20,10 +22,33 @@ import java.util.concurrent.DelayQueue; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import junit.framework.Test; + +// android-changed: Extend BlockingQueueTest directly. public class DelayQueueTest extends BlockingQueueTest { + // android-changed: Extend BlockingQueueTest directly instead of creating + // an inner class and its associated suite. + // + // public static class Generic extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new DelayQueue(); + // } + // protected PDelay makeElement(int i) { + // return new PDelay(i); + // } + // } + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // + // public static Test suite() { + // return newTestSuite(DelayQueueTest.class, + // new Generic().testSuite()); + // } + protected BlockingQueue emptyCollection() { return new DelayQueue(); } @@ -32,8 +57,6 @@ public class DelayQueueTest extends BlockingQueueTest { return new PDelay(i); } - private static final int NOCAP = Integer.MAX_VALUE; - /** * A delayed implementation for testing. * Most tests use Pseudodelays, where delays are all elapsed @@ -115,12 +138,12 @@ public class DelayQueueTest extends BlockingQueueTest { private DelayQueue<PDelay> populatedQueue(int n) { DelayQueue<PDelay> q = new DelayQueue<PDelay>(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i -= 2) assertTrue(q.offer(new PDelay(i))); - for (int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new PDelay(i))); assertFalse(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(n, q.size()); return q; } @@ -129,7 +152,7 @@ public class DelayQueueTest extends BlockingQueueTest { * A new queue has unbounded capacity */ public void testConstructor1() { - assertEquals(NOCAP, new DelayQueue().remainingCapacity()); + assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity()); } /** @@ -137,7 +160,7 @@ public class DelayQueueTest extends BlockingQueueTest { */ public void testConstructor3() { try { - DelayQueue q = new DelayQueue(null); + new DelayQueue(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -148,7 +171,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testConstructor4() { try { PDelay[] ints = new PDelay[SIZE]; - DelayQueue q = new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -161,7 +184,7 @@ public class DelayQueueTest extends BlockingQueueTest { PDelay[] ints = new PDelay[SIZE]; for (int i = 0; i < SIZE-1; ++i) ints[i] = new PDelay(i); - DelayQueue q = new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -184,7 +207,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testEmpty() { DelayQueue q = new DelayQueue(); assertTrue(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(new PDelay(1)); assertFalse(q.isEmpty()); q.add(new PDelay(2)); @@ -194,20 +217,19 @@ public class DelayQueueTest extends BlockingQueueTest { } /** - * remainingCapacity does not change when elements added or removed, - * but size does + * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - DelayQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(SIZE-i, q.size()); - q.remove(); + assertTrue(q.remove() instanceof PDelay); } for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(i, q.size()); - q.add(new PDelay(i)); + assertTrue(q.add(new PDelay(i))); } } @@ -278,9 +300,9 @@ public class DelayQueueTest extends BlockingQueueTest { public void testPut() { DelayQueue q = new DelayQueue(); for (int i = 0; i < SIZE; ++i) { - PDelay I = new PDelay(i); - q.put(I); - assertTrue(q.contains(I)); + PDelay x = new PDelay(i); + q.put(x); + assertTrue(q.contains(x)); } assertEquals(SIZE, q.size()); } @@ -324,7 +346,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testTake() throws InterruptedException { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.take())); + assertEquals(new PDelay(i), q.take()); } } @@ -367,7 +389,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testPoll() { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll())); + assertEquals(new PDelay(i), q.poll()); } assertNull(q.poll()); } @@ -378,7 +400,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testTimedPoll0() throws InterruptedException { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); + assertEquals(new PDelay(i), q.poll(0, MILLISECONDS)); } assertNull(q.poll(0, MILLISECONDS)); } @@ -390,7 +412,7 @@ public class DelayQueueTest extends BlockingQueueTest { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { long startTime = System.nanoTime(); - assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); + assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } long startTime = System.nanoTime(); @@ -439,8 +461,8 @@ public class DelayQueueTest extends BlockingQueueTest { public void testPeek() { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.peek())); - assertEquals(new PDelay(i), ((PDelay)q.poll())); + assertEquals(new PDelay(i), q.peek()); + assertEquals(new PDelay(i), q.poll()); if (q.isEmpty()) assertNull(q.peek()); else @@ -455,7 +477,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testElement() { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.element())); + assertEquals(new PDelay(i), q.element()); q.poll(); } try { @@ -470,7 +492,7 @@ public class DelayQueueTest extends BlockingQueueTest { public void testRemove() { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.remove())); + assertEquals(new PDelay(i), q.remove()); } try { q.remove(); @@ -498,7 +520,7 @@ public class DelayQueueTest extends BlockingQueueTest { q.clear(); assertTrue(q.isEmpty()); assertEquals(0, q.size()); - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); PDelay x = new PDelay(1); q.add(x); assertFalse(q.isEmpty()); @@ -550,8 +572,8 @@ public class DelayQueueTest extends BlockingQueueTest { assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { - PDelay I = (PDelay)(p.remove()); - assertFalse(q.contains(I)); + PDelay x = (PDelay)(p.remove()); + assertFalse(q.contains(x)); } } } @@ -603,6 +625,14 @@ public class DelayQueueTest extends BlockingQueueTest { ++i; } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new DelayQueue().iterator()); } /** @@ -763,4 +793,12 @@ public class DelayQueueTest extends BlockingQueueTest { } } + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection<?> q = populatedQueue(SIZE); + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } } diff --git a/jsr166-tests/src/test/java/jsr166/EntryTest.java b/jsr166-tests/src/test/java/jsr166/EntryTest.java index 4387a53..d141a84 100644 --- a/jsr166-tests/src/test/java/jsr166/EntryTest.java +++ b/jsr166-tests/src/test/java/jsr166/EntryTest.java @@ -6,10 +6,22 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import java.util.AbstractMap; +import java.util.Map; + +import junit.framework.Test; +import junit.framework.TestSuite; public class EntryTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static final String k1 = "1"; static final String v1 = "a"; diff --git a/jsr166-tests/src/test/java/jsr166/ExchangerTest.java b/jsr166-tests/src/test/java/jsr166/ExchangerTest.java index b0f325e..172fccd 100644 --- a/jsr166-tests/src/test/java/jsr166/ExchangerTest.java +++ b/jsr166-tests/src/test/java/jsr166/ExchangerTest.java @@ -8,15 +8,27 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.Exchanger; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ExchangerTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + /** * exchange exchanges objects across two threads */ diff --git a/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java b/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java index eced0ba..e988cc6 100644 --- a/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java +++ b/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java @@ -8,30 +8,40 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorCompletionService; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.RunnableFuture; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.concurrent.atomic.AtomicBoolean; -import java.security.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ExecutorCompletionServiceTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Creating a new ECS with null Executor throw NPE */ public void testConstructorNPE() { try { - ExecutorCompletionService ecs = new ExecutorCompletionService(null); + new ExecutorCompletionService(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -42,7 +52,7 @@ public class ExecutorCompletionServiceTest extends JSR166TestCase { public void testConstructorNPE2() { try { ExecutorService e = Executors.newCachedThreadPool(); - ExecutorCompletionService ecs = new ExecutorCompletionService(e, null); + new ExecutorCompletionService(e, null); shouldThrow(); } catch (NullPointerException success) {} } diff --git a/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java b/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java index 18c0975..2518e63 100644 --- a/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java +++ b/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java @@ -8,13 +8,36 @@ package jsr166; -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.security.*; + +import java.security.AccessControlContext; +import java.security.AccessControlException; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ExecutorsTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * A newCachedThreadPool can execute runnables @@ -267,7 +290,6 @@ public class ExecutorsTest extends JSR166TestCase { for (final ExecutorService executor : executors) { threads.add(newStartedThread(new CheckedRunnable() { public void realRun() { - long startTime = System.nanoTime(); Future future = executor.submit(sleeper); assertFutureTimesOut(future); }})); @@ -328,7 +350,7 @@ public class ExecutorsTest extends JSR166TestCase { public void realRun() throws Exception { final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); - // final AccessControlContext thisacc = AccessController.getContext(); // Android removed + final AccessControlContext thisacc = AccessController.getContext(); Runnable r = new CheckedRunnable() { public void realRun() { Thread current = Thread.currentThread(); @@ -343,7 +365,7 @@ public class ExecutorsTest extends JSR166TestCase { String name = current.getName(); assertTrue(name.endsWith("thread-1")); assertSame(thisccl, current.getContextClassLoader()); - // assertEquals(thisacc, AccessController.getContext()); // Android removed + assertEquals(thisacc, AccessController.getContext()); done.countDown(); }}; ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); diff --git a/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java b/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java index 8416198..09a3511 100644 --- a/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java +++ b/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java @@ -6,34 +6,42 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.AbstractExecutorService; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.Callable; -import java.util.concurrent.Future; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; -import java.util.concurrent.CancellationException; -import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.Future; import java.util.concurrent.RecursiveTask; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import java.security.AccessControlException; -import java.security.Policy; -import java.security.PrivilegedAction; -import java.security.PrivilegedExceptionAction; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinPoolTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /* * Testing coverage notes: @@ -103,7 +111,7 @@ public class ForkJoinPoolTest extends JSR166TestCase { static final class FibTask extends RecursiveTask<Integer> { final int number; FibTask(int n) { number = n; } - public Integer compute() { + protected Integer compute() { int n = number; if (n <= 1) return n; @@ -131,7 +139,7 @@ public class ForkJoinPoolTest extends JSR166TestCase { this.locker = locker; this.lock = lock; } - public Integer compute() { + protected Integer compute() { int n; LockingFibTask f1 = null; LockingFibTask f2 = null; @@ -414,11 +422,10 @@ public class ForkJoinPoolTest extends JSR166TestCase { ExecutorService e = new ForkJoinPool(1); try { final AtomicBoolean done = new AtomicBoolean(false); - CheckedRunnable task = new CheckedRunnable() { + Future<?> future = e.submit(new CheckedRunnable() { public void realRun() { done.set(true); - }}; - Future<?> future = e.submit(task); + }}); assertNull(future.get()); assertNull(future.get(0, MILLISECONDS)); assertTrue(done.get()); diff --git a/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java b/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java index 080fd9c..3c1fcb7 100644 --- a/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java +++ b/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java @@ -6,22 +6,33 @@ package jsr166; -import java.util.concurrent.ExecutionException; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.HashSet; import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; -import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.HashSet; -import junit.framework.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinTaskTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + // Runs with "mainPool" use > 1 thread. singletonPool tests use 1 static final int mainPoolSize = Math.max(2, Runtime.getRuntime().availableProcessors()); @@ -913,7 +924,7 @@ public class ForkJoinTaskTest extends JSR166TestCase { } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { @@ -1580,7 +1591,7 @@ public class ForkJoinTaskTest extends JSR166TestCase { } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollectionSingleton() { RecursiveAction a = new CheckedRecursiveAction() { @@ -1602,4 +1613,21 @@ public class ForkJoinTaskTest extends JSR166TestCase { testInvokeOnPool(singletonPool(), a); } + /** + * ForkJoinTask.quietlyComplete returns when task completes + * normally without setting a value. The most recent value + * established by setRawResult(V) (or null by default) is returned + * from invoke. + */ + public void testQuietlyComplete() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.quietlyComplete(); + assertEquals(8, f.number); + checkCompletedNormally(f); + }}; + testInvokeOnPool(mainPool(), a); + } + } diff --git a/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java b/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java index baab79e..a5d8c46 100644 --- a/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java +++ b/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java @@ -8,21 +8,39 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class FutureTaskTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + void checkIsDone(Future<?> f) { assertTrue(f.isDone()); assertFalse(f.cancel(false)); @@ -122,7 +140,7 @@ public class FutureTaskTest extends JSR166TestCase { pf.set(new Object()); pf.setException(new Error()); for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { - pf.cancel(true); + pf.cancel(mayInterruptIfRunning); } } @@ -473,8 +491,8 @@ public class FutureTaskTest extends JSR166TestCase { final PublicFutureTask task = new PublicFutureTask(new Runnable() { public void run() { + pleaseCancel.countDown(); try { - pleaseCancel.countDown(); delay(LONG_DELAY_MS); threadShouldThrow(); } catch (InterruptedException success) { @@ -796,4 +814,31 @@ public class FutureTaskTest extends JSR166TestCase { } } + /** + * timed get with most negative timeout works correctly (i.e. no + * underflow bug) + */ + public void testGet_NegativeInfinityTimeout() throws Exception { + final ExecutorService pool = Executors.newFixedThreadPool(10); + final Runnable nop = new Runnable() { public void run() {}}; + final FutureTask<Void> task = new FutureTask<>(nop, null); + final List<Future<?>> futures = new ArrayList<>(); + Runnable r = new Runnable() { public void run() { + for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { + try { + task.get(timeout, NANOSECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) {threadUnexpectedException(fail);}}}}; + for (int i = 0; i < 10; i++) + futures.add(pool.submit(r)); + try { + joinPool(pool); + for (Future<?> future : futures) + checkCompletedNormally(future, null); + } finally { + task.run(); // last resort to help terminate + } + } + } diff --git a/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java b/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java index d9bf255..d07d936 100644 --- a/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java +++ b/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java @@ -8,31 +8,49 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.Method; +import java.security.CodeSource; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.security.Policy; +import java.security.ProtectionDomain; +import java.security.SecurityPermission; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.Enumeration; +import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.PropertyPermission; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.RecursiveAction; +import java.util.concurrent.RecursiveTask; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import java.security.CodeSource; -import java.security.Permission; -import java.security.PermissionCollection; -import java.security.Permissions; -import java.security.Policy; -import java.security.ProtectionDomain; -import java.security.SecurityPermission; +import java.util.regex.Pattern; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; /** * Base class for JSR166 Junit TCK tests. Defines some constants, @@ -108,6 +126,7 @@ import java.security.SecurityPermission; * </ul> */ public class JSR166TestCase extends TestCase { + // Delays for timing-dependent tests, in milliseconds. protected static final boolean expensiveTests = false; @@ -116,7 +135,6 @@ public class JSR166TestCase extends TestCase { public static long MEDIUM_DELAY_MS; public static long LONG_DELAY_MS; - /** * Returns the shortest timed delay. This could * be reimplemented to use for example a Property. @@ -204,7 +222,7 @@ public class JSR166TestCase extends TestCase { } /** - * Find missing try { ... } finally { joinPool(e); } + * Finds missing try { ... } finally { joinPool(e); } */ void checkForkJoinPoolThreadLeaks() throws InterruptedException { Thread[] survivors = new Thread[5]; @@ -302,11 +320,11 @@ public class JSR166TestCase extends TestCase { public void threadAssertEquals(Object x, Object y) { try { assertEquals(x, y); - } catch (AssertionFailedError t) { - threadRecordFailure(t); - throw t; - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (AssertionFailedError fail) { + threadRecordFailure(fail); + throw fail; + } catch (Throwable fail) { + threadUnexpectedException(fail); } } @@ -318,9 +336,9 @@ public class JSR166TestCase extends TestCase { public void threadAssertSame(Object x, Object y) { try { assertSame(x, y); - } catch (AssertionFailedError t) { - threadRecordFailure(t); - throw t; + } catch (AssertionFailedError fail) { + threadRecordFailure(fail); + throw fail; } } @@ -385,16 +403,23 @@ public class JSR166TestCase extends TestCase { void joinPool(ExecutorService exec) { try { exec.shutdown(); - assertTrue("ExecutorService did not terminate in a timely manner", - exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); + if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) + fail("ExecutorService " + exec + + " did not terminate in a timely manner"); } catch (SecurityException ok) { // Allowed in case test doesn't have privs - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { fail("Unexpected InterruptedException"); } } /** + * A debugging tool to print all stack traces, as jstack does. + */ + static void printAllStackTraces() { + } + + /** * Checks that thread does not terminate within the default * millisecond delay of {@code timeoutMillis()}. */ @@ -410,7 +435,7 @@ public class JSR166TestCase extends TestCase { // No need to optimize the failing case via Thread.join. delay(millis); assertTrue(thread.isAlive()); - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { fail("Unexpected InterruptedException"); } } @@ -432,7 +457,7 @@ public class JSR166TestCase extends TestCase { delay(millis); for (Thread thread : threads) assertTrue(thread.isAlive()); - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { fail("Unexpected InterruptedException"); } } @@ -454,8 +479,8 @@ public class JSR166TestCase extends TestCase { future.get(timeoutMillis, MILLISECONDS); shouldThrow(); } catch (TimeoutException success) { - } catch (Exception e) { - threadUnexpectedException(e); + } catch (Exception fail) { + threadUnexpectedException(fail); } finally { future.cancel(true); } assertTrue(millisElapsedSince(startTime) >= timeoutMillis); } @@ -499,24 +524,53 @@ public class JSR166TestCase extends TestCase { public static final Integer m6 = new Integer(-6); public static final Integer m10 = new Integer(-10); - /** - * android-changed - * Android does not use a SecurityManager. This will simply execute - * the runnable ingoring permisions. + * Runs Runnable r with a security policy that permits precisely + * the specified permissions. If there is no current security + * manager, the runnable is run twice, both with and without a + * security manager. We require that any security manager permit + * getPolicy/setPolicy. */ public void runWithPermissions(Runnable r, Permission... permissions) { - r.run(); + SecurityManager sm = System.getSecurityManager(); + if (sm == null) { + r.run(); + } + runWithSecurityManagerWithPermissions(r, permissions); } /** - * android-changed - * Android does not use a SecurityManager. This will simply execute - * the runnable ingoring permisions. + * Runs Runnable r with a security policy that permits precisely + * the specified permissions. If there is no current security + * manager, a temporary one is set for the duration of the + * Runnable. We require that any security manager permit + * getPolicy/setPolicy. */ public void runWithSecurityManagerWithPermissions(Runnable r, Permission... permissions) { - r.run(); + SecurityManager sm = System.getSecurityManager(); + if (sm == null) { + Policy savedPolicy = Policy.getPolicy(); + try { + Policy.setPolicy(permissivePolicy()); + System.setSecurityManager(new SecurityManager()); + runWithSecurityManagerWithPermissions(r, permissions); + } finally { + System.setSecurityManager(null); + Policy.setPolicy(savedPolicy); + } + } else { + Policy savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(permissions); + Policy.setPolicy(policy); + + try { + r.run(); + } finally { + policy.addPermission(new SecurityPermission("setPolicy")); + Policy.setPolicy(savedPolicy); + } + } } /** @@ -582,10 +636,10 @@ public class JSR166TestCase extends TestCase { void sleep(long millis) { try { delay(millis); - } catch (InterruptedException ie) { + } catch (InterruptedException fail) { AssertionFailedError afe = new AssertionFailedError("Unexpected InterruptedException"); - afe.initCause(ie); + afe.initCause(fail); throw afe; } } @@ -623,12 +677,42 @@ public class JSR166TestCase extends TestCase { /** * Returns the number of milliseconds since time given by * startNanoTime, which must have been previously returned from a - * call to {@link System.nanoTime()}. + * call to {@link System#nanoTime()}. */ - long millisElapsedSince(long startNanoTime) { + static long millisElapsedSince(long startNanoTime) { return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); } +// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { +// long startTime = System.nanoTime(); +// try { +// r.run(); +// } catch (Throwable fail) { threadUnexpectedException(fail); } +// if (millisElapsedSince(startTime) > timeoutMillis/2) +// throw new AssertionFailedError("did not return promptly"); +// } + +// void assertTerminatesPromptly(Runnable r) { +// assertTerminatesPromptly(LONG_DELAY_MS/2, r); +// } + + /** + * Checks that timed f.get() returns the expected value, and does not + * wait for the timeout to elapse before returning. + */ + <T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { + long startTime = System.nanoTime(); + try { + assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + if (millisElapsedSince(startTime) > timeoutMillis/2) + throw new AssertionFailedError("timed get did not return promptly"); + } + + <T> void checkTimedGet(Future<T> f, T expectedValue) { + checkTimedGet(f, expectedValue, LONG_DELAY_MS); + } + /** * Returns a new started daemon Thread running the given runnable. */ @@ -647,8 +731,8 @@ public class JSR166TestCase extends TestCase { void awaitTermination(Thread t, long timeoutMillis) { try { t.join(timeoutMillis); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); + } catch (InterruptedException fail) { + threadUnexpectedException(fail); } finally { if (t.getState() != Thread.State.TERMINATED) { t.interrupt(); @@ -674,8 +758,8 @@ public class JSR166TestCase extends TestCase { public final void run() { try { realRun(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } } @@ -729,8 +813,8 @@ public class JSR166TestCase extends TestCase { threadShouldThrow("InterruptedException"); } catch (InterruptedException success) { threadAssertFalse(Thread.interrupted()); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } } @@ -741,8 +825,8 @@ public class JSR166TestCase extends TestCase { public final T call() { try { return realCall(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return null; } } @@ -759,8 +843,8 @@ public class JSR166TestCase extends TestCase { return result; } catch (InterruptedException success) { threadAssertFalse(Thread.interrupted()); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } return null; } @@ -800,16 +884,16 @@ public class JSR166TestCase extends TestCase { public void await(CountDownLatch latch) { try { assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } public void await(Semaphore semaphore) { try { assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } @@ -1003,8 +1087,8 @@ public class JSR166TestCase extends TestCase { @Override protected final void compute() { try { realCompute(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); } } } @@ -1018,8 +1102,8 @@ public class JSR166TestCase extends TestCase { @Override protected final T compute() { try { return realCompute(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return null; } } @@ -1043,12 +1127,12 @@ public class JSR166TestCase extends TestCase { public int await() { try { return super.await(2 * LONG_DELAY_MS, MILLISECONDS); - } catch (TimeoutException e) { + } catch (TimeoutException timedOut) { throw new AssertionFailedError("timed out"); - } catch (Exception e) { + } catch (Exception fail) { AssertionFailedError afe = - new AssertionFailedError("Unexpected exception: " + e); - afe.initCause(e); + new AssertionFailedError("Unexpected exception: " + fail); + afe.initCause(fail); throw afe; } } @@ -1076,9 +1160,7 @@ public class JSR166TestCase extends TestCase { q.remove(); shouldThrow(); } catch (NoSuchElementException success) {} - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } void assertSerialEquals(Object x, Object y) { @@ -1097,8 +1179,8 @@ public class JSR166TestCase extends TestCase { oos.flush(); oos.close(); return bos.toByteArray(); - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return new byte[0]; } } @@ -1111,8 +1193,8 @@ public class JSR166TestCase extends TestCase { T clone = (T) ois.readObject(); assertSame(o.getClass(), clone.getClass()); return clone; - } catch (Throwable t) { - threadUnexpectedException(t); + } catch (Throwable fail) { + threadUnexpectedException(fail); return null; } } @@ -1137,4 +1219,12 @@ public class JSR166TestCase extends TestCase { shouldThrow(expectedExceptionClass.getName()); } } + + public void assertIteratorExhausted(Iterator<?> it) { + try { + it.next(); + shouldThrow(); + } catch (NoSuchElementException success) {} + assertFalse(it.hasNext()); + } } diff --git a/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java b/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java index 4016f6d..62802bb 100644 --- a/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java +++ b/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java @@ -6,10 +6,12 @@ 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.Deque; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; @@ -19,10 +21,36 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingDeque; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; public class LinkedBlockingDequeTest extends JSR166TestCase { + // android-note: These tests have been moved into their own separate + // classes to work around CTS issues. + // + // public static class Unbounded extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedBlockingDeque(); + // } + // } + // + // public static class Bounded extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedBlockingDeque(SIZE); + // } + // } + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // + // public static Test suite() { + // return newTestSuite(LinkedBlockingDequeTest.class, + // new Unbounded().testSuite(), + // new Bounded().testSuite()); + // } + /** * Returns a new deque of given size containing consecutive * Integers 0 ... n. @@ -253,10 +281,10 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { */ public void testRemoveFirstOccurrence() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); assertFalse(q.removeFirstOccurrence(new Integer(i+1))); } @@ -268,10 +296,10 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { */ public void testRemoveLastOccurrence() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); assertFalse(q.removeLastOccurrence(new Integer(i+1))); } @@ -384,16 +412,16 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - LinkedBlockingDeque q = populatedDeque(SIZE); + BlockingQueue q = populatedDeque(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)); } } @@ -415,9 +443,9 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.push(I); - assertEquals(I, q.peek()); + Integer x = new Integer(i); + q.push(x); + assertEquals(x, q.peek()); } assertEquals(0, q.remainingCapacity()); q.push(new Integer(SIZE)); @@ -535,9 +563,9 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testPut() throws InterruptedException { LinkedBlockingDeque q = new LinkedBlockingDeque(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()); } @@ -767,9 +795,9 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testPutFirst() throws InterruptedException { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putFirst(I); - assertTrue(q.contains(I)); + Integer x = new Integer(i); + q.putFirst(x); + assertTrue(q.contains(x)); } assertEquals(0, q.remainingCapacity()); } @@ -1114,9 +1142,9 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testPutLast() throws InterruptedException { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putLast(I); - assertTrue(q.contains(I)); + Integer x = new Integer(i); + q.putLast(x); + assertTrue(q.contains(x)); } assertEquals(0, q.remainingCapacity()); } @@ -1450,8 +1478,8 @@ public class LinkedBlockingDequeTest 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)); } } } @@ -1495,9 +1523,26 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testIterator() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(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() { + Deque c = new LinkedBlockingDeque(); + assertIteratorExhausted(c.iterator()); + assertIteratorExhausted(c.descendingIterator()); } /** @@ -1750,7 +1795,24 @@ public class LinkedBlockingDequeTest 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() { + Deque<?>[] qs = { + new LinkedBlockingDeque<Object>(), + populatedDeque(2), + }; + + for (Deque<?> q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + assertFalse(q.removeFirstOccurrence(null)); + assertFalse(q.removeLastOccurrence(null)); } } diff --git a/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java index fe0b871..bd37b2a 100644 --- a/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java @@ -8,22 +8,49 @@ 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; import java.util.Queue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.concurrent.LinkedBlockingQueue; + +import junit.framework.Test; public class LinkedBlockingQueueTest extends JSR166TestCase { + // android-note: These tests have been moved into their own separate + // classes to work around CTS issues. + // + // public static class Unbounded extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedBlockingQueue(); + // } + // } + // + // public static class Bounded extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedBlockingQueue(SIZE); + // } + // } + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // + // public static Test suite() { + // return newTestSuite(LinkedBlockingQueueTest.class, + // new Unbounded().testSuite(), + // new Bounded().testSuite()); + // } + /** * Returns a new queue of given size containing consecutive * Integers 0 ... n. @@ -126,16 +153,16 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - LinkedBlockingQueue 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)); } } @@ -225,9 +252,9 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { public void testPut() throws InterruptedException { LinkedBlockingQueue q = new LinkedBlockingQueue(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()); } @@ -567,8 +594,8 @@ public class LinkedBlockingQueueTest 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)); } } } @@ -612,9 +639,24 @@ public class LinkedBlockingQueueTest extends JSR166TestCase { public void testIterator() throws InterruptedException { LinkedBlockingQueue 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 LinkedBlockingQueue().iterator()); } /** @@ -805,7 +847,22 @@ public class LinkedBlockingQueueTest 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 LinkedBlockingQueue<Object>(), + populatedQueue(2), + }; + + for (Collection<?> q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); } } diff --git a/jsr166-tests/src/test/java/jsr166/LinkedListTest.java b/jsr166-tests/src/test/java/jsr166/LinkedListTest.java index 5b09100..9d9481d 100644 --- a/jsr166-tests/src/test/java/jsr166/LinkedListTest.java +++ b/jsr166-tests/src/test/java/jsr166/LinkedListTest.java @@ -8,14 +8,25 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.NoSuchElementException; +import junit.framework.Test; +import junit.framework.TestSuite; + public class LinkedListTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns a new queue of given size containing consecutive @@ -43,7 +54,7 @@ public class LinkedListTest extends JSR166TestCase { */ public void testConstructor3() { try { - LinkedList q = new LinkedList((Collection)null); + new LinkedList((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -230,13 +241,13 @@ public class LinkedListTest extends JSR166TestCase { */ public void testRemoveElement() { LinkedList q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove((Integer)i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove((Integer)i)); assertFalse(q.contains(i)); @@ -315,8 +326,8 @@ public class LinkedListTest 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)); } } } @@ -372,13 +383,19 @@ public class LinkedListTest extends JSR166TestCase { */ public void testIterator() { LinkedList q = populatedQueue(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new LinkedList().iterator()); } /** @@ -599,10 +616,10 @@ public class LinkedListTest extends JSR166TestCase { */ public void testRemoveFirstOccurrence() { LinkedList q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); assertFalse(q.removeFirstOccurrence(new Integer(i+1))); } @@ -614,10 +631,10 @@ public class LinkedListTest extends JSR166TestCase { */ public void testRemoveLastOccurrence() { LinkedList q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); assertFalse(q.removeLastOccurrence(new Integer(i+1))); } diff --git a/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java b/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java index 94427df..f5893ec 100644 --- a/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java @@ -7,9 +7,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.List; @@ -20,12 +21,30 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedTransferQueue; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import junit.framework.Test; @SuppressWarnings({"unchecked", "rawtypes"}) +// android-changed: Extend BlockingQueueTest directly. public class LinkedTransferQueueTest extends BlockingQueueTest { + // android-changed: Extend BlockingQueueTest directly. + // + // public static class Generic extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedTransferQueue(); + // } + // } + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // + // public static Test suite() { + // return newTestSuite(LinkedTransferQueueTest.class, + // new Generic().testSuite()); + // } + protected BlockingQueue emptyCollection() { return new LinkedTransferQueue(); } @@ -105,16 +124,16 @@ public class LinkedTransferQueueTest extends BlockingQueueTest { * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - LinkedTransferQueue<Integer> q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(SIZE - i, q.size()); - q.remove(); + assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(i, q.size()); - q.add(i); + assertTrue(q.add(i)); } } @@ -490,11 +509,24 @@ public class LinkedTransferQueueTest extends BlockingQueueTest { public void testIterator() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); - int i = 0; - while (it.hasNext()) { - assertEquals(it.next(), i++); - } + 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 LinkedTransferQueue().iterator()); } /** @@ -687,8 +719,7 @@ public class LinkedTransferQueueTest extends BlockingQueueTest { assertEquals(SIZE - k, q.size()); for (int j = 0; j < k; ++j) assertEquals(j, l.get(j)); - while (q.poll() != null) - ; + do {} while (q.poll() != null); } } @@ -1004,4 +1035,19 @@ public class LinkedTransferQueueTest extends BlockingQueueTest { assertFalse(q.isEmpty()); return q; } + + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection<?>[] qs = { + new LinkedTransferQueue<Object>(), + populatedQueue(2), + }; + + for (Collection<?> q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + } } diff --git a/jsr166-tests/src/test/java/jsr166/LockSupportTest.java b/jsr166-tests/src/test/java/jsr166/LockSupportTest.java index 051de35..8347b08 100644 --- a/jsr166-tests/src/test/java/jsr166/LockSupportTest.java +++ b/jsr166-tests/src/test/java/jsr166/LockSupportTest.java @@ -9,13 +9,25 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.LockSupport; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; +import junit.framework.TestSuite; public class LockSupportTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns the blocker object used by tests in this file. diff --git a/jsr166-tests/src/test/java/jsr166/PhaserTest.java b/jsr166-tests/src/test/java/jsr166/PhaserTest.java index 3889c1f..42d72f4 100644 --- a/jsr166-tests/src/test/java/jsr166/PhaserTest.java +++ b/jsr166-tests/src/test/java/jsr166/PhaserTest.java @@ -7,19 +7,30 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Phaser; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Phaser; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import junit.framework.Test; +import junit.framework.TestSuite; + public class PhaserTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + private static final int maxParties = 65535; /** Checks state of unterminated phaser. */ diff --git a/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java index 89d6d24..64c3b3a 100644 --- a/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java @@ -8,24 +8,49 @@ 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.Comparator; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; -import java.util.concurrent.PriorityBlockingQueue; 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 java.util.concurrent.PriorityBlockingQueue; + +import junit.framework.Test; public class PriorityBlockingQueueTest extends JSR166TestCase { - private static final int NOCAP = Integer.MAX_VALUE; + // android-note: These tests have been moved into their own separate + // classes to work around CTS issues. + // + // public static class Generic extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new PriorityBlockingQueue(); + // } + // } + // + // public static class InitialCapacity extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new PriorityBlockingQueue(SIZE); + // } + // } + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // + // public static Test suite() { + // return newTestSuite(PriorityBlockingQueueTest.class, + // new Generic().testSuite(), + // new InitialCapacity().testSuite()); + // } /** Sample Comparator */ static class MyReverseComparator implements Comparator { @@ -42,12 +67,12 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { PriorityBlockingQueue<Integer> q = new PriorityBlockingQueue<Integer>(n); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i -= 2) assertTrue(q.offer(new Integer(i))); - for (int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(n, q.size()); return q; } @@ -56,7 +81,8 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { * A new queue has unbounded capacity */ public void testConstructor1() { - assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); + assertEquals(Integer.MAX_VALUE, + new PriorityBlockingQueue(SIZE).remainingCapacity()); } /** @@ -137,7 +163,7 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { public void testEmpty() { PriorityBlockingQueue q = new PriorityBlockingQueue(2); assertTrue(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); q.add(two); @@ -147,20 +173,19 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { } /** - * remainingCapacity does not change when elements added or removed, - * but size does + * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - PriorityBlockingQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); - q.remove(); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); + assertEquals(SIZE - i, q.size()); + assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(i, q.size()); - q.add(new Integer(i)); + assertTrue(q.add(i)); } } @@ -177,9 +202,8 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { * Offer of non-Comparable throws CCE */ public void testOfferNonComparable() { + PriorityBlockingQueue q = new PriorityBlockingQueue(1); try { - PriorityBlockingQueue q = new PriorityBlockingQueue(1); - q.offer(new Object()); q.offer(new Object()); q.offer(new Object()); shouldThrow(); @@ -244,9 +268,9 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { public void testPut() { PriorityBlockingQueue q = new PriorityBlockingQueue(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(SIZE, q.size()); } @@ -508,8 +532,8 @@ public class PriorityBlockingQueueTest 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)); } } } @@ -554,13 +578,19 @@ public class PriorityBlockingQueueTest extends JSR166TestCase { */ public void testIterator() { PriorityBlockingQueue q = populatedQueue(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new PriorityBlockingQueue().iterator()); } /** @@ -692,7 +722,22 @@ public class PriorityBlockingQueueTest 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 PriorityBlockingQueue<Object>(), + populatedQueue(2), + }; + + for (Collection<?> q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); } } diff --git a/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java b/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java index 2b237dd..88cdd37 100644 --- a/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java @@ -8,7 +8,6 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; @@ -17,7 +16,19 @@ import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Queue; +import junit.framework.Test; +import junit.framework.TestSuite; + public class PriorityQueueTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { @@ -32,9 +43,9 @@ public class PriorityQueueTest extends JSR166TestCase { private PriorityQueue<Integer> populatedQueue(int n) { PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i -= 2) assertTrue(q.offer(new Integer(i))); - for (int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(n, q.size()); @@ -53,7 +64,7 @@ public class PriorityQueueTest extends JSR166TestCase { */ public void testConstructor2() { try { - PriorityQueue q = new PriorityQueue(0); + new PriorityQueue(0); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -63,7 +74,7 @@ public class PriorityQueueTest extends JSR166TestCase { */ public void testConstructor3() { try { - PriorityQueue q = new PriorityQueue((Collection)null); + new PriorityQueue((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -74,7 +85,7 @@ public class PriorityQueueTest extends JSR166TestCase { public void testConstructor4() { try { Integer[] ints = new Integer[SIZE]; - PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); + new PriorityQueue(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -87,7 +98,7 @@ public class PriorityQueueTest extends JSR166TestCase { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); - PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); + new PriorityQueue(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -183,9 +194,8 @@ public class PriorityQueueTest extends JSR166TestCase { * Offer of non-Comparable throws CCE */ public void testOfferNonComparable() { + PriorityQueue q = new PriorityQueue(1); try { - PriorityQueue q = new PriorityQueue(1); - q.offer(new Object()); q.offer(new Object()); q.offer(new Object()); shouldThrow(); @@ -315,13 +325,13 @@ public class PriorityQueueTest extends JSR166TestCase { */ public void testRemoveElement() { PriorityQueue q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -400,8 +410,8 @@ public class PriorityQueueTest 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)); } } } @@ -435,13 +445,19 @@ public class PriorityQueueTest extends JSR166TestCase { */ public void testIterator() { PriorityQueue q = populatedQueue(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new PriorityQueue().iterator()); } /** diff --git a/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java b/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java index ad61a2e..1c3bba8 100644 --- a/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java +++ b/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java @@ -6,23 +6,35 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.Arrays; +import java.util.HashSet; import java.util.concurrent.CancellationException; -import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; +import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.Arrays; -import java.util.HashSet; + +import junit.framework.Test; +import junit.framework.TestSuite; public class RecursiveActionTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + private static ForkJoinPool mainPool() { return new ForkJoinPool(); } @@ -496,6 +508,8 @@ public class RecursiveActionTest extends JSR166TestCase { FibAction f = new FibAction(8); assertSame(f, f.fork()); helpQuiesce(); + while (!f.isDone()) // wait out race + ; assertEquals(21, f.result); assertEquals(0, getQueuedTaskCount()); checkCompletedNormally(f); @@ -581,7 +595,7 @@ public class RecursiveActionTest extends JSR166TestCase { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { - f.get(5L, TimeUnit.SECONDS); + f.get(5L, SECONDS); shouldThrow(); } catch (ExecutionException success) { Throwable cause = success.getCause(); diff --git a/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java b/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java index 48b6470..7783370 100644 --- a/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java +++ b/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java @@ -6,20 +6,31 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.HashSet; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; -import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveTask; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.HashSet; + +import junit.framework.Test; +import junit.framework.TestSuite; public class RecursiveTaskTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + private static ForkJoinPool mainPool() { return new ForkJoinPool(); } diff --git a/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java b/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java index 6fe8122..17eaf76 100644 --- a/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java +++ b/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java @@ -8,34 +8,48 @@ package jsr166; -import junit.framework.*; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.ReentrantLock; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.*; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; -public class ReentrantLockTest extends JSR166TestCase { +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; +public class ReentrantLockTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** - * A runnable calling lockInterruptibly + * A checked runnable calling lockInterruptibly */ class InterruptibleLockRunnable extends CheckedRunnable { final ReentrantLock lock; - InterruptibleLockRunnable(ReentrantLock l) { lock = l; } + InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; } public void realRun() throws InterruptedException { lock.lockInterruptibly(); } } /** - * A runnable calling lockInterruptibly that expects to be + * A checked runnable calling lockInterruptibly that expects to be * interrupted */ class InterruptedLockRunnable extends CheckedInterruptedRunnable { final ReentrantLock lock; - InterruptedLockRunnable(ReentrantLock l) { lock = l; } + InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; } public void realRun() throws InterruptedException { lock.lockInterruptibly(); } @@ -133,7 +147,7 @@ public class ReentrantLockTest extends JSR166TestCase { lock.unlock(); } - enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** * Awaits condition using the specified AwaitMethod. @@ -156,6 +170,8 @@ public class ReentrantLockTest extends JSR166TestCase { case awaitUntil: assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); break; + default: + throw new AssertionError(); } } @@ -448,9 +464,7 @@ public class ReentrantLockTest extends JSR166TestCase { barrier.await(); awaitTermination(t); assertFalse(lock.isLocked()); - } catch (Exception e) { - threadUnexpectedException(e); - } + } catch (Exception fail) { threadUnexpectedException(fail); } } /** @@ -462,9 +476,7 @@ public class ReentrantLockTest extends JSR166TestCase { final PublicReentrantLock lock = new PublicReentrantLock(fair); try { lock.lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } assertLockedByMoi(lock); Thread t = newStartedThread(new InterruptedLockRunnable(lock)); waitForQueuedThread(lock, t); @@ -525,9 +537,7 @@ public class ReentrantLockTest extends JSR166TestCase { assertTrue(nanosRemaining <= 0); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -545,9 +555,7 @@ public class ReentrantLockTest extends JSR166TestCase { assertFalse(c.await(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -566,9 +574,7 @@ public class ReentrantLockTest extends JSR166TestCase { assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** diff --git a/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java b/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java index 2be27d2..7ef8ea3 100644 --- a/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java +++ b/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java @@ -8,16 +8,31 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.concurrent.CountDownLatch; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.*; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; public class ReentrantReadWriteLockTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * A runnable calling lockInterruptibly @@ -142,7 +157,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { lock.writeLock().unlock(); } - enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; + enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } /** * Awaits condition using the specified AwaitMethod. @@ -164,6 +179,8 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { java.util.Date d = new java.util.Date(); assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); break; + default: + throw new AssertionError(); } } @@ -818,9 +835,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { new PublicReentrantReadWriteLock(fair); try { lock.writeLock().lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lockInterruptibly(); @@ -845,9 +860,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { lock.readLock().lockInterruptibly(); lock.readLock().unlock(); lock.writeLock().lockInterruptibly(); - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.readLock().lockInterruptibly(); @@ -873,7 +886,9 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { await(c, awaitMethod); shouldThrow(); } catch (IllegalMonitorStateException success) { - } catch (InterruptedException e) { threadUnexpectedException(e); } + } catch (InterruptedException fail) { + threadUnexpectedException(fail); + } assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -924,9 +939,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { assertTrue(nanosRemaining <= 0); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -945,9 +958,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { assertFalse(c.await(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** @@ -967,9 +978,7 @@ public class ReentrantReadWriteLockTest extends JSR166TestCase { assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); - } catch (InterruptedException e) { - threadUnexpectedException(e); - } + } catch (InterruptedException fail) { threadUnexpectedException(fail); } } /** diff --git a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java index b72ad02..a93feea 100644 --- a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java +++ b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java @@ -6,13 +6,42 @@ package jsr166; -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Delayed; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.RunnableScheduledFuture; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ScheduledExecutorSubclassTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class CustomTask<V> implements RunnableScheduledFuture<V> { RunnableScheduledFuture<V> task; @@ -189,17 +218,27 @@ public class ScheduledExecutorSubclassTest extends JSR166TestCase { */ public void testFixedRateSequence() throws InterruptedException { CustomExecutor p = new CustomExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); - delay(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - // By time scaling conventions, we must have at least - // an execution per SHORT delay, but no more than one SHORT more - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p); + try { + for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { + long startTime = System.nanoTime(); + int cycles = 10; + final CountDownLatch done = new CountDownLatch(cycles); + Runnable task = new CheckedRunnable() { + public void realRun() { done.countDown(); }}; + ScheduledFuture h = + p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); + done.await(); + h.cancel(true); + double normalizedTime = + (double) millisElapsedSince(startTime) / delay; + if (normalizedTime >= cycles - 1 && + normalizedTime <= cycles) + return; + } + throw new AssertionError("unexpected execution rate"); + } finally { + joinPool(p); + } } /** @@ -207,15 +246,27 @@ public class ScheduledExecutorSubclassTest extends JSR166TestCase { */ public void testFixedDelaySequence() throws InterruptedException { CustomExecutor p = new CustomExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); - delay(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p); + try { + for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { + long startTime = System.nanoTime(); + int cycles = 10; + final CountDownLatch done = new CountDownLatch(cycles); + Runnable task = new CheckedRunnable() { + public void realRun() { done.countDown(); }}; + ScheduledFuture h = + p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); + done.await(); + h.cancel(true); + double normalizedTime = + (double) millisElapsedSince(startTime) / delay; + if (normalizedTime >= cycles - 1 && + normalizedTime <= cycles) + return; + } + throw new AssertionError("unexpected execution rate"); + } finally { + joinPool(p); + } } /** diff --git a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java index 4eea2c9..a2e83d0 100644 --- a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java +++ b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java @@ -8,13 +8,37 @@ package jsr166; -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.atomic.AtomicInteger; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ScheduledExecutorTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * execute successfully executes a runnable @@ -137,17 +161,27 @@ public class ScheduledExecutorTest extends JSR166TestCase { */ public void testFixedRateSequence() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); - delay(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - // By time scaling conventions, we must have at least - // an execution per SHORT delay, but no more than one SHORT more - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p); + try { + for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { + long startTime = System.nanoTime(); + int cycles = 10; + final CountDownLatch done = new CountDownLatch(cycles); + Runnable task = new CheckedRunnable() { + public void realRun() { done.countDown(); }}; + ScheduledFuture h = + p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); + done.await(); + h.cancel(true); + double normalizedTime = + (double) millisElapsedSince(startTime) / delay; + if (normalizedTime >= cycles - 1 && + normalizedTime <= cycles) + return; + } + throw new AssertionError("unexpected execution rate"); + } finally { + joinPool(p); + } } /** @@ -155,15 +189,27 @@ public class ScheduledExecutorTest extends JSR166TestCase { */ public void testFixedDelaySequence() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); - delay(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p); + try { + for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { + long startTime = System.nanoTime(); + int cycles = 10; + final CountDownLatch done = new CountDownLatch(cycles); + Runnable task = new CheckedRunnable() { + public void realRun() { done.countDown(); }}; + ScheduledFuture h = + p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); + done.await(); + h.cancel(true); + double normalizedTime = + (double) millisElapsedSince(startTime) / delay; + if (normalizedTime >= cycles - 1 && + normalizedTime <= cycles) + return; + } + throw new AssertionError("unexpected execution rate"); + } finally { + joinPool(p); + } } /** diff --git a/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java b/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java index f303285..db4f4b4 100644 --- a/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java +++ b/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java @@ -8,14 +8,26 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.Collection; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Semaphore; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -public class SemaphoreTest extends JSR166TestCase { +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestSuite; +public class SemaphoreTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Subclass to expose protected methods */ @@ -608,7 +620,7 @@ public class SemaphoreTest extends JSR166TestCase { assertTrue(t2.isAlive()); s.release(); awaitTermination(t2); - } + } /** * toString indicates current number of permits diff --git a/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java b/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java index bd030cf..605a955 100644 --- a/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java @@ -8,22 +8,48 @@ 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; -import java.util.Queue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.SynchronousQueue; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; public class SynchronousQueueTest 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 SynchronousQueue(true); + // } + // } + // + // public static class NonFair extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new SynchronousQueue(false); + // } + // } + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // + // public static Test suite() { + // return newTestSuite(SynchronousQueueTest.class, + // new Fair().testSuite(), + // new NonFair().testSuite()); + // } + /** * Any SynchronousQueue is both empty and full */ @@ -402,7 +428,7 @@ public class SynchronousQueueTest extends JSR166TestCase { public void testToArray_null(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); try { - Object o[] = q.toArray(null); + Object[] o = q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -413,13 +439,7 @@ public class SynchronousQueueTest extends JSR166TestCase { public void testIterator() { testIterator(false); } public void testIterator_fair() { testIterator(true); } public void testIterator(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - Iterator it = q.iterator(); - assertFalse(it.hasNext()); - try { - Object x = it.next(); - shouldThrow(); - } catch (NoSuchElementException success) {} + assertIteratorExhausted(new SynchronousQueue(fair).iterator()); } /** @@ -586,4 +606,13 @@ public class SynchronousQueueTest extends JSR166TestCase { awaitTermination(t2); } + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection<?> q = new SynchronousQueue(); + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + } diff --git a/jsr166-tests/src/test/java/jsr166/SystemTest.java b/jsr166-tests/src/test/java/jsr166/SystemTest.java index 32caec2..6918374 100644 --- a/jsr166-tests/src/test/java/jsr166/SystemTest.java +++ b/jsr166-tests/src/test/java/jsr166/SystemTest.java @@ -8,9 +8,19 @@ package jsr166; -import junit.framework.*; +import junit.framework.Test; +import junit.framework.TestSuite; public class SystemTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Worst case rounding for millisecs; set for 60 cycle millis clock. diff --git a/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java b/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java index 665a2b7..4ae141d 100644 --- a/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java +++ b/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java @@ -6,14 +6,25 @@ package jsr166; -import junit.framework.*; -import java.util.*; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ThreadLocalRandomTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } + /* * Testing coverage notes: * @@ -23,15 +34,18 @@ public class ThreadLocalRandomTest extends JSR166TestCase { * across multiples of primes. */ - // + // max numbers of calls to detect getting stuck on one value static final int NCALLS = 10000; // max sampled int bound static final int MAX_INT_BOUND = (1 << 28); - // Max sampled long bound + // max sampled long bound static final long MAX_LONG_BOUND = (1L << 42); + // Number of replications for other checks + static final int REPS = 20; + /** * setSeed throws UnsupportedOperationException */ @@ -43,7 +57,7 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** - * Repeated calls to nextInt produce at least one different result + * Repeated calls to nextInt produce at least two distinct results */ public void testNextInt() { int f = ThreadLocalRandom.current().nextInt(); @@ -54,7 +68,7 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** - * Repeated calls to nextLong produce at least one different result + * Repeated calls to nextLong produce at least two distinct results */ public void testNextLong() { long f = ThreadLocalRandom.current().nextLong(); @@ -65,7 +79,7 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** - * Repeated calls to nextBoolean produce at least one different result + * Repeated calls to nextBoolean produce at least two distinct results */ public void testNextBoolean() { boolean f = ThreadLocalRandom.current().nextBoolean(); @@ -76,7 +90,7 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** - * Repeated calls to nextFloat produce at least one different result + * Repeated calls to nextFloat produce at least two distinct results */ public void testNextFloat() { float f = ThreadLocalRandom.current().nextFloat(); @@ -87,18 +101,18 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** - * Repeated calls to nextDouble produce at least one different result + * Repeated calls to nextDouble produce at least two distinct results */ public void testNextDouble() { double f = ThreadLocalRandom.current().nextDouble(); - double i = 0; + int i = 0; while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f) ++i; assertTrue(i < NCALLS); } /** - * Repeated calls to nextGaussian produce at least one different result + * Repeated calls to nextGaussian produce at least two distinct results */ public void testNextGaussian() { double f = ThreadLocalRandom.current().nextGaussian(); @@ -109,28 +123,39 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** - * nextInt(negative) throws IllegalArgumentException; + * nextInt(non-positive) throws IllegalArgumentException */ - public void testNextIntBoundedNeg() { - try { - int f = ThreadLocalRandom.current().nextInt(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + public void testNextIntBoundNonPositive() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) { + try { + rnd.nextInt(bound); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** - * nextInt(least >= bound) throws IllegalArgumentException; + * nextInt(least >= bound) throws IllegalArgumentException */ public void testNextIntBadBounds() { - try { - int f = ThreadLocalRandom.current().nextInt(17, 2); - shouldThrow(); - } catch (IllegalArgumentException success) {} + int[][] badBoundss = { + { 17, 2 }, + { -42, -42 }, + { Integer.MAX_VALUE, Integer.MIN_VALUE }, + }; + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (int[] badBounds : badBoundss) { + try { + rnd.nextInt(badBounds[0], badBounds[1]); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** * nextInt(bound) returns 0 <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextIntBounded() { // sample bound space across prime number increments @@ -150,7 +175,7 @@ public class ThreadLocalRandomTest extends JSR166TestCase { /** * nextInt(least, bound) returns least <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextIntBounded2() { for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { @@ -170,28 +195,39 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** - * nextLong(negative) throws IllegalArgumentException; + * nextLong(non-positive) throws IllegalArgumentException */ - public void testNextLongBoundedNeg() { - try { - long f = ThreadLocalRandom.current().nextLong(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + public void testNextLongBoundNonPositive() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) { + try { + rnd.nextLong(bound); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** - * nextLong(least >= bound) throws IllegalArgumentException; + * nextLong(least >= bound) throws IllegalArgumentException */ public void testNextLongBadBounds() { - try { - long f = ThreadLocalRandom.current().nextLong(17, 2); - shouldThrow(); - } catch (IllegalArgumentException success) {} + long[][] badBoundss = { + { 17L, 2L }, + { -42L, -42L }, + { Long.MAX_VALUE, Long.MIN_VALUE }, + }; + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (long[] badBounds : badBoundss) { + try { + rnd.nextLong(badBounds[0], badBounds[1]); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** * nextLong(bound) returns 0 <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextLongBounded() { for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { @@ -210,7 +246,7 @@ public class ThreadLocalRandomTest extends JSR166TestCase { /** * nextLong(least, bound) returns least <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextLongBounded2() { for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { @@ -230,8 +266,28 @@ public class ThreadLocalRandomTest extends JSR166TestCase { } /** + * nextDouble(non-positive) throws IllegalArgumentException + */ + public void testNextDoubleBoundNonPositive() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + double[] badBounds = { + 0.0d, + -17.0d, + -Double.MIN_VALUE, + Double.NEGATIVE_INFINITY, + Double.NaN, + }; + for (double bound : badBounds) { + try { + rnd.nextDouble(bound); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } + } + + /** * nextDouble(least, bound) returns least <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextDoubleBounded2() { for (double least = 0.0001; least < 1.0e20; least *= 8) { @@ -263,7 +319,7 @@ public class ThreadLocalRandomTest extends JSR166TestCase { long firstRand = 0; ThreadLocalRandom firstThreadLocalRandom = null; - final CheckedRunnable getRandomState = new CheckedRunnable() { + Runnable getRandomState = new CheckedRunnable() { public void realRun() { ThreadLocalRandom current = ThreadLocalRandom.current(); assertSame(current, ThreadLocalRandom.current()); diff --git a/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java b/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java index 885c2b2..7f5f072 100644 --- a/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java +++ b/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java @@ -8,10 +8,19 @@ package jsr166; -import junit.framework.*; -import java.util.concurrent.Semaphore; +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadLocalTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static ThreadLocal<Integer> tl = new ThreadLocal<Integer>() { public Integer initialValue() { @@ -85,7 +94,7 @@ public class ThreadLocalTest extends JSR166TestCase { */ public void testGenericITL() throws InterruptedException { final int threadCount = 10; - final int x[] = new int[threadCount]; + final int[] x = new int[threadCount]; Thread progenitor = new ITLThread(x); progenitor.start(); progenitor.join(); diff --git a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java index f16f422..5f38d39 100644 --- a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java +++ b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java @@ -8,14 +8,44 @@ package jsr166; -import junit.framework.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.RunnableFuture; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; -import java.util.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class CustomTask<V> implements RunnableFuture<V> { final Callable<V> callable; @@ -33,7 +63,7 @@ public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { CustomTask(final Runnable r, final V res) { if (r == null) throw new NullPointerException(); callable = new Callable<V>() { - public V call() throws Exception { r.run(); return res; }}; + public V call() throws Exception { r.run(); return res; }}; } public boolean isDone() { lock.lock(); try { return done; } finally { lock.unlock() ; } @@ -1244,11 +1274,10 @@ public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { CustomTPE p = new CustomTPE(); try { final CountDownLatch done = new CountDownLatch(1); - final CheckedRunnable task = new CheckedRunnable() { + p.execute(new CheckedRunnable() { public void realRun() { done.countDown(); - }}; - p.execute(task); + }}); await(p.afterCalled); assertEquals(0, done.getCount()); assertTrue(p.afterCalled()); diff --git a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java index 55f769b..52a7002 100644 --- a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java +++ b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java @@ -8,13 +8,41 @@ package jsr166; -import junit.framework.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; -import java.util.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadPoolExecutorTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class ExtendedTPE extends ThreadPoolExecutor { final CountDownLatch beforeCalled = new CountDownLatch(1); @@ -1371,11 +1399,10 @@ public class ThreadPoolExecutorTest extends JSR166TestCase { ExtendedTPE p = new ExtendedTPE(); try { final CountDownLatch done = new CountDownLatch(1); - final CheckedRunnable task = new CheckedRunnable() { + p.execute(new CheckedRunnable() { public void realRun() { done.countDown(); - }}; - p.execute(task); + }}); await(p.afterCalled); assertEquals(0, done.getCount()); assertTrue(p.afterCalled()); diff --git a/jsr166-tests/src/test/java/jsr166/ThreadTest.java b/jsr166-tests/src/test/java/jsr166/ThreadTest.java index 12c2f8a..2652201 100644 --- a/jsr166-tests/src/test/java/jsr166/ThreadTest.java +++ b/jsr166-tests/src/test/java/jsr166/ThreadTest.java @@ -8,9 +8,19 @@ package jsr166; -import junit.framework.*; +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class MyHandler implements Thread.UncaughtExceptionHandler { public void uncaughtException(Thread t, Throwable e) { @@ -28,11 +38,14 @@ public class ThreadTest extends JSR166TestCase { Thread current = Thread.currentThread(); ThreadGroup tg = current.getThreadGroup(); MyHandler eh = new MyHandler(); - assertEquals(tg, current.getUncaughtExceptionHandler()); + assertSame(tg, current.getUncaughtExceptionHandler()); current.setUncaughtExceptionHandler(eh); - assertEquals(eh, current.getUncaughtExceptionHandler()); - current.setUncaughtExceptionHandler(null); - assertEquals(tg, current.getUncaughtExceptionHandler()); + try { + assertSame(eh, current.getUncaughtExceptionHandler()); + } finally { + current.setUncaughtExceptionHandler(null); + } + assertSame(tg, current.getUncaughtExceptionHandler()); } /** @@ -40,24 +53,23 @@ public class ThreadTest extends JSR166TestCase { * setDefaultUncaughtExceptionHandler. */ public void testGetAndSetDefaultUncaughtExceptionHandler() { - // BEGIN android-remove (when running as cts the RuntimeInit will - // set a default handler) - // assertEquals(null, Thread.getDefaultUncaughtExceptionHandler()); - // END android-remove - + assertEquals(null, Thread.getDefaultUncaughtExceptionHandler()); // failure due to securityException is OK. // Would be nice to explicitly test both ways, but cannot yet. + Thread.UncaughtExceptionHandler defaultHandler + = Thread.getDefaultUncaughtExceptionHandler(); + MyHandler eh = new MyHandler(); try { - Thread current = Thread.currentThread(); - ThreadGroup tg = current.getThreadGroup(); - MyHandler eh = new MyHandler(); Thread.setDefaultUncaughtExceptionHandler(eh); - assertEquals(eh, Thread.getDefaultUncaughtExceptionHandler()); - Thread.setDefaultUncaughtExceptionHandler(null); - } - catch (SecurityException ok) { + try { + assertSame(eh, Thread.getDefaultUncaughtExceptionHandler()); + } finally { + Thread.setDefaultUncaughtExceptionHandler(defaultHandler); + } + } catch (SecurityException ok) { + assertNotNull(System.getSecurityManager()); } - assertEquals(null, Thread.getDefaultUncaughtExceptionHandler()); + assertSame(defaultHandler, Thread.getDefaultUncaughtExceptionHandler()); } // How to test actually using UEH within junit? diff --git a/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java b/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java index 7fa9e1a..2c9529b 100644 --- a/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java +++ b/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java @@ -8,11 +8,30 @@ package jsr166; -import junit.framework.*; +import static java.util.concurrent.TimeUnit.DAYS; +import static java.util.concurrent.TimeUnit.HOURS; +import static java.util.concurrent.TimeUnit.MICROSECONDS; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import junit.framework.Test; +import junit.framework.TestSuite; + public class TimeUnitTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } // (loops to 88888 check increments at all time divisions.) @@ -22,92 +41,64 @@ public class TimeUnitTest extends JSR166TestCase { public void testConvert() { for (long t = 0; t < 88888; ++t) { assertEquals(t*60*60*24, - TimeUnit.SECONDS.convert(t, - TimeUnit.DAYS)); + SECONDS.convert(t, DAYS)); assertEquals(t*60*60, - TimeUnit.SECONDS.convert(t, - TimeUnit.HOURS)); + SECONDS.convert(t, HOURS)); assertEquals(t*60, - TimeUnit.SECONDS.convert(t, - TimeUnit.MINUTES)); + SECONDS.convert(t, MINUTES)); assertEquals(t, - TimeUnit.SECONDS.convert(t, - TimeUnit.SECONDS)); + SECONDS.convert(t, SECONDS)); assertEquals(t, - TimeUnit.SECONDS.convert(1000L*t, - TimeUnit.MILLISECONDS)); + SECONDS.convert(1000L*t, MILLISECONDS)); assertEquals(t, - TimeUnit.SECONDS.convert(1000000L*t, - TimeUnit.MICROSECONDS)); + SECONDS.convert(1000000L*t, MICROSECONDS)); assertEquals(t, - TimeUnit.SECONDS.convert(1000000000L*t, - TimeUnit.NANOSECONDS)); + SECONDS.convert(1000000000L*t, NANOSECONDS)); assertEquals(1000L*t*60*60*24, - TimeUnit.MILLISECONDS.convert(t, - TimeUnit.DAYS)); + MILLISECONDS.convert(t, DAYS)); assertEquals(1000L*t*60*60, - TimeUnit.MILLISECONDS.convert(t, - TimeUnit.HOURS)); + MILLISECONDS.convert(t, HOURS)); assertEquals(1000L*t*60, - TimeUnit.MILLISECONDS.convert(t, - TimeUnit.MINUTES)); + MILLISECONDS.convert(t, MINUTES)); assertEquals(1000L*t, - TimeUnit.MILLISECONDS.convert(t, - TimeUnit.SECONDS)); + MILLISECONDS.convert(t, SECONDS)); assertEquals(t, - TimeUnit.MILLISECONDS.convert(t, - TimeUnit.MILLISECONDS)); + MILLISECONDS.convert(t, MILLISECONDS)); assertEquals(t, - TimeUnit.MILLISECONDS.convert(1000L*t, - TimeUnit.MICROSECONDS)); + MILLISECONDS.convert(1000L*t, MICROSECONDS)); assertEquals(t, - TimeUnit.MILLISECONDS.convert(1000000L*t, - TimeUnit.NANOSECONDS)); + MILLISECONDS.convert(1000000L*t, NANOSECONDS)); assertEquals(1000000L*t*60*60*24, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.DAYS)); + MICROSECONDS.convert(t, DAYS)); assertEquals(1000000L*t*60*60, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.HOURS)); + MICROSECONDS.convert(t, HOURS)); assertEquals(1000000L*t*60, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.MINUTES)); + MICROSECONDS.convert(t, MINUTES)); assertEquals(1000000L*t, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.SECONDS)); + MICROSECONDS.convert(t, SECONDS)); assertEquals(1000L*t, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.MILLISECONDS)); + MICROSECONDS.convert(t, MILLISECONDS)); assertEquals(t, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.MICROSECONDS)); + MICROSECONDS.convert(t, MICROSECONDS)); assertEquals(t, - TimeUnit.MICROSECONDS.convert(1000L*t, - TimeUnit.NANOSECONDS)); + MICROSECONDS.convert(1000L*t, NANOSECONDS)); assertEquals(1000000000L*t*60*60*24, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.DAYS)); + NANOSECONDS.convert(t, DAYS)); assertEquals(1000000000L*t*60*60, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.HOURS)); + NANOSECONDS.convert(t, HOURS)); assertEquals(1000000000L*t*60, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.MINUTES)); + NANOSECONDS.convert(t, MINUTES)); assertEquals(1000000000L*t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.SECONDS)); + NANOSECONDS.convert(t, SECONDS)); assertEquals(1000000L*t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.MILLISECONDS)); + NANOSECONDS.convert(t, MILLISECONDS)); assertEquals(1000L*t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.MICROSECONDS)); + NANOSECONDS.convert(t, MICROSECONDS)); assertEquals(t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.NANOSECONDS)); + NANOSECONDS.convert(t, NANOSECONDS)); } } @@ -118,19 +109,19 @@ public class TimeUnitTest extends JSR166TestCase { public void testToNanos() { for (long t = 0; t < 88888; ++t) { assertEquals(t*1000000000L*60*60*24, - TimeUnit.DAYS.toNanos(t)); + DAYS.toNanos(t)); assertEquals(t*1000000000L*60*60, - TimeUnit.HOURS.toNanos(t)); + HOURS.toNanos(t)); assertEquals(t*1000000000L*60, - TimeUnit.MINUTES.toNanos(t)); + MINUTES.toNanos(t)); assertEquals(1000000000L*t, - TimeUnit.SECONDS.toNanos(t)); + SECONDS.toNanos(t)); assertEquals(1000000L*t, - TimeUnit.MILLISECONDS.toNanos(t)); + MILLISECONDS.toNanos(t)); assertEquals(1000L*t, - TimeUnit.MICROSECONDS.toNanos(t)); + MICROSECONDS.toNanos(t)); assertEquals(t, - TimeUnit.NANOSECONDS.toNanos(t)); + NANOSECONDS.toNanos(t)); } } @@ -141,19 +132,19 @@ public class TimeUnitTest extends JSR166TestCase { public void testToMicros() { for (long t = 0; t < 88888; ++t) { assertEquals(t*1000000L*60*60*24, - TimeUnit.DAYS.toMicros(t)); + DAYS.toMicros(t)); assertEquals(t*1000000L*60*60, - TimeUnit.HOURS.toMicros(t)); + HOURS.toMicros(t)); assertEquals(t*1000000L*60, - TimeUnit.MINUTES.toMicros(t)); + MINUTES.toMicros(t)); assertEquals(1000000L*t, - TimeUnit.SECONDS.toMicros(t)); + SECONDS.toMicros(t)); assertEquals(1000L*t, - TimeUnit.MILLISECONDS.toMicros(t)); + MILLISECONDS.toMicros(t)); assertEquals(t, - TimeUnit.MICROSECONDS.toMicros(t)); + MICROSECONDS.toMicros(t)); assertEquals(t, - TimeUnit.NANOSECONDS.toMicros(t*1000L)); + NANOSECONDS.toMicros(t*1000L)); } } @@ -164,19 +155,19 @@ public class TimeUnitTest extends JSR166TestCase { public void testToMillis() { for (long t = 0; t < 88888; ++t) { assertEquals(t*1000L*60*60*24, - TimeUnit.DAYS.toMillis(t)); + DAYS.toMillis(t)); assertEquals(t*1000L*60*60, - TimeUnit.HOURS.toMillis(t)); + HOURS.toMillis(t)); assertEquals(t*1000L*60, - TimeUnit.MINUTES.toMillis(t)); + MINUTES.toMillis(t)); assertEquals(1000L*t, - TimeUnit.SECONDS.toMillis(t)); + SECONDS.toMillis(t)); assertEquals(t, - TimeUnit.MILLISECONDS.toMillis(t)); + MILLISECONDS.toMillis(t)); assertEquals(t, - TimeUnit.MICROSECONDS.toMillis(t*1000L)); + MICROSECONDS.toMillis(t*1000L)); assertEquals(t, - TimeUnit.NANOSECONDS.toMillis(t*1000000L)); + NANOSECONDS.toMillis(t*1000000L)); } } @@ -187,19 +178,19 @@ public class TimeUnitTest extends JSR166TestCase { public void testToSeconds() { for (long t = 0; t < 88888; ++t) { assertEquals(t*60*60*24, - TimeUnit.DAYS.toSeconds(t)); + DAYS.toSeconds(t)); assertEquals(t*60*60, - TimeUnit.HOURS.toSeconds(t)); + HOURS.toSeconds(t)); assertEquals(t*60, - TimeUnit.MINUTES.toSeconds(t)); + MINUTES.toSeconds(t)); assertEquals(t, - TimeUnit.SECONDS.toSeconds(t)); + SECONDS.toSeconds(t)); assertEquals(t, - TimeUnit.MILLISECONDS.toSeconds(t*1000L)); + MILLISECONDS.toSeconds(t*1000L)); assertEquals(t, - TimeUnit.MICROSECONDS.toSeconds(t*1000000L)); + MICROSECONDS.toSeconds(t*1000000L)); assertEquals(t, - TimeUnit.NANOSECONDS.toSeconds(t*1000000000L)); + NANOSECONDS.toSeconds(t*1000000000L)); } } @@ -210,19 +201,19 @@ public class TimeUnitTest extends JSR166TestCase { public void testToMinutes() { for (long t = 0; t < 88888; ++t) { assertEquals(t*60*24, - TimeUnit.DAYS.toMinutes(t)); + DAYS.toMinutes(t)); assertEquals(t*60, - TimeUnit.HOURS.toMinutes(t)); + HOURS.toMinutes(t)); assertEquals(t, - TimeUnit.MINUTES.toMinutes(t)); + MINUTES.toMinutes(t)); assertEquals(t, - TimeUnit.SECONDS.toMinutes(t*60)); + SECONDS.toMinutes(t*60)); assertEquals(t, - TimeUnit.MILLISECONDS.toMinutes(t*1000L*60)); + MILLISECONDS.toMinutes(t*1000L*60)); assertEquals(t, - TimeUnit.MICROSECONDS.toMinutes(t*1000000L*60)); + MICROSECONDS.toMinutes(t*1000000L*60)); assertEquals(t, - TimeUnit.NANOSECONDS.toMinutes(t*1000000000L*60)); + NANOSECONDS.toMinutes(t*1000000000L*60)); } } @@ -233,19 +224,19 @@ public class TimeUnitTest extends JSR166TestCase { public void testToHours() { for (long t = 0; t < 88888; ++t) { assertEquals(t*24, - TimeUnit.DAYS.toHours(t)); + DAYS.toHours(t)); assertEquals(t, - TimeUnit.HOURS.toHours(t)); + HOURS.toHours(t)); assertEquals(t, - TimeUnit.MINUTES.toHours(t*60)); + MINUTES.toHours(t*60)); assertEquals(t, - TimeUnit.SECONDS.toHours(t*60*60)); + SECONDS.toHours(t*60*60)); assertEquals(t, - TimeUnit.MILLISECONDS.toHours(t*1000L*60*60)); + MILLISECONDS.toHours(t*1000L*60*60)); assertEquals(t, - TimeUnit.MICROSECONDS.toHours(t*1000000L*60*60)); + MICROSECONDS.toHours(t*1000000L*60*60)); assertEquals(t, - TimeUnit.NANOSECONDS.toHours(t*1000000000L*60*60)); + NANOSECONDS.toHours(t*1000000000L*60*60)); } } @@ -256,19 +247,19 @@ public class TimeUnitTest extends JSR166TestCase { public void testToDays() { for (long t = 0; t < 88888; ++t) { assertEquals(t, - TimeUnit.DAYS.toDays(t)); + DAYS.toDays(t)); assertEquals(t, - TimeUnit.HOURS.toDays(t*24)); + HOURS.toDays(t*24)); assertEquals(t, - TimeUnit.MINUTES.toDays(t*60*24)); + MINUTES.toDays(t*60*24)); assertEquals(t, - TimeUnit.SECONDS.toDays(t*60*60*24)); + SECONDS.toDays(t*60*60*24)); assertEquals(t, - TimeUnit.MILLISECONDS.toDays(t*1000L*60*60*24)); + MILLISECONDS.toDays(t*1000L*60*60*24)); assertEquals(t, - TimeUnit.MICROSECONDS.toDays(t*1000000L*60*60*24)); + MICROSECONDS.toDays(t*1000000L*60*60*24)); assertEquals(t, - TimeUnit.NANOSECONDS.toDays(t*1000000000L*60*60*24)); + NANOSECONDS.toDays(t*1000000000L*60*60*24)); } } @@ -278,29 +269,21 @@ public class TimeUnitTest extends JSR166TestCase { */ public void testConvertSaturate() { assertEquals(Long.MAX_VALUE, - TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, - TimeUnit.SECONDS)); + NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS)); assertEquals(Long.MIN_VALUE, - TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, - TimeUnit.SECONDS)); + NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS)); assertEquals(Long.MAX_VALUE, - TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, - TimeUnit.MINUTES)); + NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES)); assertEquals(Long.MIN_VALUE, - TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, - TimeUnit.MINUTES)); + NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES)); assertEquals(Long.MAX_VALUE, - TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, - TimeUnit.HOURS)); + NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS)); assertEquals(Long.MIN_VALUE, - TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, - TimeUnit.HOURS)); + NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS)); assertEquals(Long.MAX_VALUE, - TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, - TimeUnit.DAYS)); + NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); assertEquals(Long.MIN_VALUE, - TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, - TimeUnit.DAYS)); + NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); } /** @@ -309,23 +292,23 @@ public class TimeUnitTest extends JSR166TestCase { */ public void testToNanosSaturate() { assertEquals(Long.MAX_VALUE, - TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); + MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); assertEquals(Long.MIN_VALUE, - TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); + MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); } /** * toString returns name of unit */ public void testToString() { - assertEquals("SECONDS", TimeUnit.SECONDS.toString()); + assertEquals("SECONDS", SECONDS.toString()); } /** * name returns name of unit */ public void testName() { - assertEquals("SECONDS", TimeUnit.SECONDS.name()); + assertEquals("SECONDS", SECONDS.name()); } /** @@ -336,7 +319,7 @@ public class TimeUnitTest extends JSR166TestCase { Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { Object o = new Object(); - TimeUnit tu = TimeUnit.MILLISECONDS; + TimeUnit tu = MILLISECONDS; try { tu.timedWait(o, LONG_DELAY_MS); @@ -355,7 +338,7 @@ public class TimeUnitTest extends JSR166TestCase { Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { Object o = new Object(); - TimeUnit tu = TimeUnit.MILLISECONDS; + TimeUnit tu = MILLISECONDS; Thread.currentThread().interrupt(); try { @@ -393,7 +376,7 @@ public class TimeUnitTest extends JSR166TestCase { }}); final Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - TimeUnit tu = TimeUnit.MILLISECONDS; + TimeUnit tu = MILLISECONDS; Thread.currentThread().interrupt(); try { tu.timedJoin(s, LONG_DELAY_MS); @@ -424,7 +407,7 @@ public class TimeUnitTest extends JSR166TestCase { final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - TimeUnit tu = TimeUnit.MILLISECONDS; + TimeUnit tu = MILLISECONDS; Thread.currentThread().interrupt(); try { tu.sleep(LONG_DELAY_MS); @@ -450,7 +433,7 @@ public class TimeUnitTest extends JSR166TestCase { * a deserialized serialized unit is the same instance */ public void testSerialization() throws Exception { - TimeUnit x = TimeUnit.MILLISECONDS; + TimeUnit x = MILLISECONDS; assertSame(x, serialClone(x)); } diff --git a/jsr166-tests/src/test/java/jsr166/TreeMapTest.java b/jsr166-tests/src/test/java/jsr166/TreeMapTest.java index 87baa1a..afc73de 100644 --- a/jsr166-tests/src/test/java/jsr166/TreeMapTest.java +++ b/jsr166-tests/src/test/java/jsr166/TreeMapTest.java @@ -6,10 +6,31 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import java.util.Arrays; +import java.util.BitSet; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.NavigableSet; +import java.util.NoSuchElementException; +import java.util.Random; +import java.util.Set; +import java.util.TreeMap; + +import junit.framework.Test; +import junit.framework.TestSuite; public class TreeMapTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns a new map from Integers 1-5 to Strings "A"-"E". @@ -559,8 +580,8 @@ public class TreeMapTest extends JSR166TestCase { * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + TreeMap c = map5(); try { - TreeMap c = map5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -570,8 +591,8 @@ public class TreeMapTest extends JSR166TestCase { * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + TreeMap c = map5(); try { - TreeMap c = map5(); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -581,9 +602,9 @@ public class TreeMapTest extends JSR166TestCase { * remove(null) throws NPE for nonempty map */ public void testRemove1_NullPointerException() { + TreeMap c = new TreeMap(); + c.put("sadsdf", "asdads"); try { - TreeMap c = new TreeMap(); - c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} @@ -819,7 +840,7 @@ public class TreeMapTest extends JSR166TestCase { // Add entries till we're back to original size while (map.size() < size) { int key = min + rnd.nextInt(rangeSize); - assertTrue(key >= min && key<= max); + assertTrue(key >= min && key <= max); put(map, key); } } @@ -844,7 +865,7 @@ public class TreeMapTest extends JSR166TestCase { // Add entries till we're back to original size while (map.size() < size) { int key = min - 5 + rnd.nextInt(rangeSize + 10); - if (key >= min && key<= max) { + if (key >= min && key <= max) { put(map, key); } else { try { diff --git a/jsr166-tests/src/test/java/jsr166/TreeSetTest.java b/jsr166-tests/src/test/java/jsr166/TreeSetTest.java index 2957019..a935637 100644 --- a/jsr166-tests/src/test/java/jsr166/TreeSetTest.java +++ b/jsr166-tests/src/test/java/jsr166/TreeSetTest.java @@ -6,7 +6,6 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.BitSet; import java.util.Collection; @@ -19,7 +18,19 @@ import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; +import junit.framework.Test; +import junit.framework.TestSuite; + public class TreeSetTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { @@ -39,9 +50,9 @@ public class TreeSetTest extends JSR166TestCase { private TreeSet<Integer> populatedSet(int n) { TreeSet<Integer> q = new TreeSet<Integer>(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i -= 2) assertTrue(q.add(new Integer(i))); - for (int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.add(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(n, q.size()); @@ -75,7 +86,7 @@ public class TreeSetTest extends JSR166TestCase { */ public void testConstructor3() { try { - TreeSet q = new TreeSet((Collection)null); + new TreeSet((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -86,7 +97,7 @@ public class TreeSetTest extends JSR166TestCase { public void testConstructor4() { try { Integer[] ints = new Integer[SIZE]; - TreeSet q = new TreeSet(Arrays.asList(ints)); + new TreeSet(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -99,7 +110,7 @@ public class TreeSetTest extends JSR166TestCase { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); - TreeSet q = new TreeSet(Arrays.asList(ints)); + new TreeSet(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -164,8 +175,8 @@ public class TreeSetTest extends JSR166TestCase { * add(null) throws NPE if nonempty */ public void testAddNull() { + TreeSet q = populatedSet(SIZE); try { - TreeSet q = populatedSet(SIZE); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -193,9 +204,8 @@ public class TreeSetTest extends JSR166TestCase { * Add of non-Comparable throws CCE */ public void testAddNonComparable() { + TreeSet q = new TreeSet(); try { - TreeSet q = new TreeSet(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -206,8 +216,8 @@ public class TreeSetTest extends JSR166TestCase { * addAll(null) throws NPE */ public void testAddAll1() { + TreeSet q = new TreeSet(); try { - TreeSet q = new TreeSet(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -217,9 +227,9 @@ public class TreeSetTest extends JSR166TestCase { * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + TreeSet q = new TreeSet(); + Integer[] ints = new Integer[SIZE]; try { - TreeSet q = new TreeSet(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -230,11 +240,11 @@ public class TreeSetTest extends JSR166TestCase { * possibly adding some elements */ public void testAddAll3() { + TreeSet q = new TreeSet(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i); try { - TreeSet q = new TreeSet(); - 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) {} @@ -282,13 +292,13 @@ public class TreeSetTest extends JSR166TestCase { */ public void testRemoveElement() { TreeSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -367,8 +377,8 @@ public class TreeSetTest extends JSR166TestCase { assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } @@ -472,27 +482,19 @@ public class TreeSetTest extends JSR166TestCase { */ public void testIterator() { TreeSet q = populatedSet(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); } /** * iterator of empty set has no elements */ public void testEmptyIterator() { - TreeSet q = new TreeSet(); - int i = 0; - Iterator it = q.iterator(); - while (it.hasNext()) { - assertTrue(q.contains(it.next())); - ++i; - } - assertEquals(0, i); + assertIteratorExhausted(new TreeSet().iterator()); } /** @@ -732,7 +734,7 @@ public class TreeSetTest extends JSR166TestCase { // Add entries till we're back to original size while (set.size() < size) { int element = min + rnd.nextInt(rangeSize); - assertTrue(element >= min && element<= max); + assertTrue(element >= min && element <= max); put(set, element); } } @@ -757,7 +759,7 @@ public class TreeSetTest extends JSR166TestCase { // Add entries till we're back to original size while (set.size() < size) { int element = min - 5 + rnd.nextInt(rangeSize + 10); - if (element >= min && element<= max) { + if (element >= min && element <= max) { put(set, element); } else { try { diff --git a/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java b/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java index 17201f3..18a9e37 100644 --- a/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java +++ b/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java @@ -6,10 +6,29 @@ package jsr166; -import junit.framework.*; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; + +import junit.framework.Test; +import junit.framework.TestSuite; public class TreeSubMapTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } /** * Returns a new map from Integers 1-5 to Strings "A"-"E". @@ -378,8 +397,8 @@ public class TreeSubMapTest extends JSR166TestCase { * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -389,8 +408,8 @@ public class TreeSubMapTest extends JSR166TestCase { * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -400,8 +419,8 @@ public class TreeSubMapTest extends JSR166TestCase { * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -411,8 +430,8 @@ public class TreeSubMapTest extends JSR166TestCase { * remove(null) throws NPE */ public void testRemove1_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} @@ -927,8 +946,8 @@ public class TreeSubMapTest extends JSR166TestCase { * get(null) of nonempty map throws NPE */ public void testDescendingGet_NullPointerException() { + NavigableMap c = dmap5(); try { - NavigableMap c = dmap5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -938,8 +957,8 @@ public class TreeSubMapTest extends JSR166TestCase { * put(null,x) throws NPE */ public void testDescendingPut1_NullPointerException() { + NavigableMap c = dmap5(); try { - NavigableMap c = dmap5(); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} diff --git a/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java b/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java index ba61748..5398c4e 100644 --- a/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java +++ b/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java @@ -6,16 +6,27 @@ package jsr166; -import junit.framework.*; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.NavigableSet; -import java.util.SortedSet; import java.util.Set; +import java.util.SortedSet; import java.util.TreeSet; +import junit.framework.Test; +import junit.framework.TestSuite; + public class TreeSubSetTest extends JSR166TestCase { + // android-note: Removed because the CTS runner does a bad job of + // retrying tests that have suite() declarations. + // + // public static void main(String[] args) { + // main(suite(), args); + // } + // public static Test suite() { + // return new TestSuite(...); + // } static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { @@ -31,9 +42,9 @@ public class TreeSubSetTest extends JSR166TestCase { TreeSet<Integer> q = new TreeSet<Integer>(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i -= 2) assertTrue(q.add(new Integer(i))); - for (int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.add(new Integer(i))); assertTrue(q.add(new Integer(-n))); assertTrue(q.add(new Integer(n))); @@ -126,8 +137,8 @@ public class TreeSubSetTest extends JSR166TestCase { * add(null) throws NPE */ public void testAddNull() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -154,9 +165,8 @@ public class TreeSubSetTest extends JSR166TestCase { * Add of non-Comparable throws CCE */ public void testAddNonComparable() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -167,8 +177,8 @@ public class TreeSubSetTest extends JSR166TestCase { * addAll(null) throws NPE */ public void testAddAll1() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -178,9 +188,9 @@ public class TreeSubSetTest extends JSR166TestCase { * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + NavigableSet q = set0(); + Integer[] ints = new Integer[SIZE]; try { - NavigableSet q = set0(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -191,11 +201,11 @@ public class TreeSubSetTest extends JSR166TestCase { * possibly adding some elements */ public void testAddAll3() { + NavigableSet q = set0(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i+SIZE); try { - NavigableSet q = set0(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -232,13 +242,13 @@ public class TreeSubSetTest extends JSR166TestCase { */ public void testRemoveElement() { NavigableSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -317,8 +327,8 @@ public class TreeSubSetTest extends JSR166TestCase { assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } @@ -422,27 +432,19 @@ public class TreeSubSetTest extends JSR166TestCase { */ public void testIterator() { NavigableSet q = populatedSet(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); } /** * iterator of empty set has no elements */ public void testEmptyIterator() { - NavigableSet q = set0(); - int i = 0; - Iterator it = q.iterator(); - while (it.hasNext()) { - assertTrue(q.contains(it.next())); - ++i; - } - assertEquals(0, i); + assertIteratorExhausted(set0().iterator()); } /** @@ -648,9 +650,8 @@ public class TreeSubSetTest extends JSR166TestCase { * Add of non-Comparable throws CCE */ public void testDescendingAddNonComparable() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -661,8 +662,8 @@ public class TreeSubSetTest extends JSR166TestCase { * addAll(null) throws NPE */ public void testDescendingAddAll1() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -672,9 +673,9 @@ public class TreeSubSetTest extends JSR166TestCase { * addAll of a collection with null elements throws NPE */ public void testDescendingAddAll2() { + NavigableSet q = dset0(); + Integer[] ints = new Integer[SIZE]; try { - NavigableSet q = dset0(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -685,11 +686,11 @@ public class TreeSubSetTest extends JSR166TestCase { * possibly adding some elements */ public void testDescendingAddAll3() { + NavigableSet q = dset0(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i+SIZE); try { - NavigableSet q = dset0(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -726,10 +727,10 @@ public class TreeSubSetTest extends JSR166TestCase { */ public void testDescendingRemoveElement() { NavigableSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.remove(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.remove(new Integer(i))); assertFalse(q.remove(new Integer(i+1))); } @@ -805,8 +806,8 @@ public class TreeSubSetTest extends JSR166TestCase { assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } |