summaryrefslogtreecommitdiff
path: root/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java')
-rw-r--r--jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java102
1 files changed, 54 insertions, 48 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java
index feb283fd9b..2810802325 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());
}