--- jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2014/12/31 19:05:42 1.28 +++ jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2021/01/27 01:57:24 1.43 @@ -9,41 +9,47 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; -import java.util.Vector; import java.util.concurrent.CopyOnWriteArraySet; import junit.framework.Test; -import junit.framework.TestSuite; public class CopyOnWriteArraySetTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { - return new TestSuite(CopyOnWriteArraySetTest.class); + class Implementation implements CollectionImplementation { + public Class klazz() { return CopyOnWriteArraySet.class; } + public Set emptyCollection() { return new CopyOnWriteArraySet(); } + public Object makeElement(int i) { return JSR166TestCase.itemFor(i); } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return true; } + } + return newTestSuite( + CopyOnWriteArraySetTest.class, + CollectionTest.testSuite(new Implementation())); } - static CopyOnWriteArraySet populatedSet(int n) { - CopyOnWriteArraySet a = new CopyOnWriteArraySet(); + static CopyOnWriteArraySet populatedSet(int n) { + CopyOnWriteArraySet a = new CopyOnWriteArraySet<>(); assertTrue(a.isEmpty()); for (int i = 0; i < n; i++) - a.add(i); - assertFalse(a.isEmpty()); - assertEquals(n, a.size()); + mustAdd(a, i); + mustEqual(n == 0, a.isEmpty()); + mustEqual(n, a.size()); return a; } - static CopyOnWriteArraySet populatedSet(Integer[] elements) { - CopyOnWriteArraySet a = new CopyOnWriteArraySet(); + static CopyOnWriteArraySet populatedSet(Item[] elements) { + CopyOnWriteArraySet a = new CopyOnWriteArraySet<>(); assertTrue(a.isEmpty()); for (int i = 0; i < elements.length; i++) - a.add(elements[i]); + mustAdd(a, elements[i]); assertFalse(a.isEmpty()); - assertEquals(elements.length, a.size()); + mustEqual(elements.length, a.size()); return a; } @@ -51,7 +57,7 @@ public class CopyOnWriteArraySetTest ext * Default-constructed set is empty */ public void testConstructor() { - CopyOnWriteArraySet a = new CopyOnWriteArraySet(); + CopyOnWriteArraySet a = new CopyOnWriteArraySet<>(); assertTrue(a.isEmpty()); } @@ -59,116 +65,153 @@ public class CopyOnWriteArraySetTest ext * Collection-constructed set holds all of its elements */ public void testConstructor3() { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints)); + Item[] items = defaultItems; + CopyOnWriteArraySet a = new CopyOnWriteArraySet<>(Arrays.asList(items)); for (int i = 0; i < SIZE; ++i) - assertTrue(a.contains(ints[i])); + mustContain(a, i); } /** - * 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); - assertEquals(6, full.size()); + Set full = populatedSet(3); + assertTrue(full.addAll(Arrays.asList(three, four, five))); + mustEqual(6, full.size()); + assertFalse(full.addAll(Arrays.asList(three, four, five))); + mustEqual(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); - assertEquals(5, full.size()); + Set full = populatedSet(3); + // "one" is duplicate and will not be added + assertTrue(full.addAll(Arrays.asList(three, four, one))); + mustEqual(5, full.size()); + assertFalse(full.addAll(Arrays.asList(three, four, one))); + mustEqual(5, full.size()); } /** * 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()); + mustEqual(3, full.size()); } /** * 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)); + mustContain(full, three); } /** * clear removes all elements from the set */ public void testClear() { - CopyOnWriteArraySet full = populatedSet(3); + Collection full = populatedSet(3); full.clear(); - assertEquals(0, full.size()); + mustEqual(0, full.size()); + assertTrue(full.isEmpty()); } /** * contains returns true for added elements */ public void testContains() { - CopyOnWriteArraySet full = populatedSet(3); - assertTrue(full.contains(one)); - assertFalse(full.contains(five)); + Collection full = populatedSet(3); + mustContain(full, one); + mustNotContain(full, five); } /** * Sets with equal elements are equal */ public void testEquals() { - CopyOnWriteArraySet a = populatedSet(3); - CopyOnWriteArraySet b = populatedSet(3); + CopyOnWriteArraySet a = populatedSet(3); + CopyOnWriteArraySet b = populatedSet(3); + assertTrue(a.equals(b)); + assertTrue(b.equals(a)); + assertTrue(a.containsAll(b)); + assertTrue(b.containsAll(a)); + mustEqual(a.hashCode(), b.hashCode()); + mustEqual(a.size(), b.size()); + + a.add(minusOne); + assertFalse(a.equals(b)); + assertFalse(b.equals(a)); + assertTrue(a.containsAll(b)); + assertFalse(b.containsAll(a)); + b.add(minusOne); assertTrue(a.equals(b)); assertTrue(b.equals(a)); - assertEquals(a.hashCode(), b.hashCode()); - a.add(m1); + assertTrue(a.containsAll(b)); + assertTrue(b.containsAll(a)); + mustEqual(a.hashCode(), b.hashCode()); + + Item x = a.iterator().next(); + a.remove(x); assertFalse(a.equals(b)); assertFalse(b.equals(a)); - b.add(m1); + assertFalse(a.containsAll(b)); + assertTrue(b.containsAll(a)); + a.add(x); assertTrue(a.equals(b)); assertTrue(b.equals(a)); - assertEquals(a.hashCode(), b.hashCode()); + assertTrue(a.containsAll(b)); + assertTrue(b.containsAll(a)); + mustEqual(a.hashCode(), b.hashCode()); + mustEqual(a.size(), b.size()); + + CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet<>(Arrays.asList()); + CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet<>(Arrays.asList()); + assertTrue(empty1.equals(empty1)); + assertTrue(empty1.equals(empty2)); + + assertFalse(empty1.equals(a)); + assertFalse(a.equals(empty1)); + + assertFalse(a.equals(null)); } /** * 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(full)); + 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))); + + CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet<>(Arrays.asList()); + CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet<>(Arrays.asList()); + assertTrue(empty1.containsAll(empty2)); + assertTrue(empty1.containsAll(empty1)); + assertFalse(empty1.containsAll(full)); + assertTrue(full.containsAll(empty1)); + + try { + full.containsAll(null); + shouldThrow(); + } catch (NullPointerException success) {} } /** * 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()); } /** @@ -176,37 +219,38 @@ public class CopyOnWriteArraySetTest ext * set in insertion order */ public void testIterator() { - Collection empty = new CopyOnWriteArraySet(); + Collection empty = new CopyOnWriteArraySet<>(); assertFalse(empty.iterator().hasNext()); try { empty.iterator().next(); shouldThrow(); } catch (NoSuchElementException success) {} - Integer[] elements = new Integer[SIZE]; - for (int i = 0; i < SIZE; i++) - elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); - Collection full = populatedSet(elements); + Item[] elements = seqItems(SIZE); + shuffle(elements); + Collection full = populatedSet(elements); - Iterator it = full.iterator(); + Iterator it = full.iterator(); for (int j = 0; j < SIZE; j++) { assertTrue(it.hasNext()); - assertEquals(elements[j], it.next()); + mustEqual(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); - Iterator it = full.iterator(); + Collection full = populatedSet(3); + Iterator it = full.iterator(); it.next(); try { it.remove(); @@ -218,12 +262,12 @@ public class CopyOnWriteArraySetTest ext * toString holds toString of elements */ public void testToString() { - assertEquals("[]", new CopyOnWriteArraySet().toString()); - CopyOnWriteArraySet full = populatedSet(3); + mustEqual("[]", new CopyOnWriteArraySet().toString()); + Collection full = populatedSet(3); String s = full.toString(); for (int i = 0; i < 3; ++i) assertTrue(s.contains(String.valueOf(i))); - assertEquals(new ArrayList(full).toString(), + mustEqual(new ArrayList(full).toString(), full.toString()); } @@ -231,32 +275,31 @@ public class CopyOnWriteArraySetTest ext * 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); - assertEquals(1, full.size()); + Set full = populatedSet(3); + assertTrue(full.removeAll(Arrays.asList(one, two))); + mustEqual(1, full.size()); + assertFalse(full.removeAll(Arrays.asList(one, two))); + mustEqual(1, full.size()); } /** * 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()); + mustNotContain(full, one); + mustEqual(2, full.size()); } /** * size returns the number of elements */ public void testSize() { - CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); - CopyOnWriteArraySet full = populatedSet(3); - assertEquals(3, full.size()); - assertEquals(0, empty.size()); + Collection empty = new CopyOnWriteArraySet<>(); + Collection full = populatedSet(3); + mustEqual(3, full.size()); + mustEqual(0, empty.size()); } /** @@ -264,62 +307,58 @@ public class CopyOnWriteArraySetTest ext * the set in insertion order */ public void testToArray() { - Object[] a = new CopyOnWriteArraySet().toArray(); + Object[] a = new CopyOnWriteArraySet<>().toArray(); assertTrue(Arrays.equals(new Object[0], a)); assertSame(Object[].class, a.getClass()); - Integer[] elements = new Integer[SIZE]; - for (int i = 0; i < SIZE; i++) - elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); - Collection full = populatedSet(elements); + Item[] elements = seqItems(SIZE); + shuffle(elements); + Collection full = populatedSet(elements); assertTrue(Arrays.equals(elements, full.toArray())); assertSame(Object[].class, full.toArray().getClass()); } /** - * toArray(Integer array) returns an Integer array containing all + * toArray(Item array) returns an Item array containing all * elements from the set in insertion order */ public void testToArray2() { - Collection empty = new CopyOnWriteArraySet(); - Integer[] a; + Collection empty = new CopyOnWriteArraySet<>(); + Item[] a; - a = new Integer[0]; + a = new Item[0]; assertSame(a, empty.toArray(a)); - a = new Integer[SIZE/2]; - Arrays.fill(a, 42); + a = new Item[SIZE / 2]; + Arrays.fill(a, fortytwo); assertSame(a, empty.toArray(a)); assertNull(a[0]); for (int i = 1; i < a.length; i++) - assertEquals(42, (int) a[i]); + mustEqual(42, a[i]); - Integer[] elements = new Integer[SIZE]; - for (int i = 0; i < SIZE; i++) - elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); - Collection full = populatedSet(elements); + Item[] elements = seqItems(SIZE); + shuffle(elements); + Collection full = populatedSet(elements); - Arrays.fill(a, 42); + Arrays.fill(a, fortytwo); assertTrue(Arrays.equals(elements, full.toArray(a))); for (int i = 0; i < a.length; i++) - assertEquals(42, (int) a[i]); - assertSame(Integer[].class, full.toArray(a).getClass()); + mustEqual(42, a[i]); + assertSame(Item[].class, full.toArray(a).getClass()); - a = new Integer[SIZE]; - Arrays.fill(a, 42); + a = new Item[SIZE]; + Arrays.fill(a, fortytwo); assertSame(a, full.toArray(a)); assertTrue(Arrays.equals(elements, a)); - a = new Integer[2*SIZE]; - Arrays.fill(a, 42); + a = new Item[2 * SIZE]; + Arrays.fill(a, fortytwo); assertSame(a, full.toArray(a)); assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); assertNull(a[SIZE]); for (int i = SIZE + 1; i < a.length; i++) - assertEquals(42, (int) a[i]); + mustEqual(42, a[i]); } /** @@ -327,39 +366,39 @@ public class CopyOnWriteArraySetTest ext * not store the objects inside the set */ public void testToArray_ArrayStoreException() { + CopyOnWriteArraySet c = new CopyOnWriteArraySet<>(); + c.add(one); + c.add(two); try { - CopyOnWriteArraySet c = new CopyOnWriteArraySet(); - c.add("zfasdfsdf"); - c.add("asdadasd"); c.toArray(new Long[5]); shouldThrow(); } catch (ArrayStoreException success) {} } /** - * A deserialized serialized set is equal + * A deserialized/reserialized set equals original */ public void testSerialization() throws Exception { - Set x = populatedSet(SIZE); - Set y = serialClone(x); + Set x = populatedSet(SIZE); + Set y = serialClone(x); assertNotSame(y, x); - assertEquals(x.size(), y.size()); - assertEquals(x.toString(), y.toString()); + mustEqual(x.size(), y.size()); + mustEqual(x.toString(), y.toString()); assertTrue(Arrays.equals(x.toArray(), y.toArray())); - assertEquals(x, y); - assertEquals(y, x); + mustEqual(x, y); + mustEqual(y, x); } /** * addAll is idempotent */ public void testAddAll_idempotent() throws Exception { - Set x = populatedSet(SIZE); - Set y = new CopyOnWriteArraySet(x); + Set x = populatedSet(SIZE); + Set y = new CopyOnWriteArraySet<>(x); y.addAll(x); - assertEquals(x, y); - assertEquals(y, x); + mustEqual(x, y); + mustEqual(y, x); } }