--- jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2011/11/29 05:23:56 1.22 +++ jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2016/08/24 22:22:39 1.37 @@ -6,18 +6,20 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; +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); @@ -28,7 +30,7 @@ public class CopyOnWriteArraySetTest ext 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; } @@ -56,7 +58,7 @@ public class CopyOnWriteArraySetTest ext */ public void testConstructor3() { Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i); CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints)); for (int i = 0; i < SIZE; ++i) @@ -64,29 +66,25 @@ public class CopyOnWriteArraySetTest ext } /** - * 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()); } @@ -94,7 +92,7 @@ public class CopyOnWriteArraySetTest ext * 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()); } @@ -103,7 +101,7 @@ public class CopyOnWriteArraySetTest ext * 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)); } @@ -112,16 +110,17 @@ public class CopyOnWriteArraySetTest ext * 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)); } @@ -134,56 +133,119 @@ public class CopyOnWriteArraySetTest ext CopyOnWriteArraySet b = populatedSet(3); assertTrue(a.equals(b)); assertTrue(b.equals(a)); + assertTrue(a.containsAll(b)); + assertTrue(b.containsAll(a)); assertEquals(a.hashCode(), b.hashCode()); + assertEquals(a.size(), b.size()); + a.add(m1); assertFalse(a.equals(b)); assertFalse(b.equals(a)); + assertTrue(a.containsAll(b)); + assertFalse(b.containsAll(a)); b.add(m1); assertTrue(a.equals(b)); assertTrue(b.equals(a)); + assertTrue(a.containsAll(b)); + assertTrue(b.containsAll(a)); + assertEquals(a.hashCode(), b.hashCode()); + + Object x = a.iterator().next(); + a.remove(x); + assertFalse(a.equals(b)); + assertFalse(b.equals(a)); + assertFalse(a.containsAll(b)); + assertTrue(b.containsAll(a)); + a.add(x); + assertTrue(a.equals(b)); + assertTrue(b.equals(a)); + assertTrue(a.containsAll(b)); + assertTrue(b.containsAll(a)); assertEquals(a.hashCode(), b.hashCode()); + assertEquals(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()); } /** - * iterator() returns an iterator containing the elements of the set + * iterator() returns an iterator containing the elements of the + * set in insertion order */ public void testIterator() { - CopyOnWriteArraySet full = populatedSet(3); - Iterator i = full.iterator(); - int j; - for (j = 0; i.hasNext(); j++) - assertEquals(j, i.next()); - assertEquals(3, j); + 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; + shuffle(elements); + Collection full = populatedSet(elements); + + Iterator it = full.iterator(); + for (int j = 0; j < SIZE; j++) { + assertTrue(it.hasNext()); + assertEquals(elements[j], it.next()); + } + 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 { @@ -196,22 +258,23 @@ public class CopyOnWriteArraySetTest ext * toString holds toString of elements */ public void testToString() { - CopyOnWriteArraySet full = populatedSet(3); + assertEquals("[]", new CopyOnWriteArraySet().toString()); + Collection full = populatedSet(3); String s = full.toString(); - for (int i = 0; i < 3; ++i) { + for (int i = 0; i < 3; ++i) assertTrue(s.contains(String.valueOf(i))); - } + assertEquals(new ArrayList(full).toString(), + full.toString()); } /** * 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()); } @@ -219,7 +282,7 @@ public class CopyOnWriteArraySetTest ext * 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()); @@ -229,8 +292,8 @@ public class CopyOnWriteArraySetTest ext * 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()); } @@ -247,7 +310,7 @@ public class CopyOnWriteArraySetTest ext Integer[] elements = new Integer[SIZE]; for (int i = 0; i < SIZE; i++) elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); + shuffle(elements); Collection full = populatedSet(elements); assertTrue(Arrays.equals(elements, full.toArray())); @@ -265,7 +328,7 @@ public class CopyOnWriteArraySetTest ext a = new Integer[0]; assertSame(a, empty.toArray(a)); - a = new Integer[SIZE/2]; + a = new Integer[SIZE / 2]; Arrays.fill(a, 42); assertSame(a, empty.toArray(a)); assertNull(a[0]); @@ -275,7 +338,7 @@ public class CopyOnWriteArraySetTest ext Integer[] elements = new Integer[SIZE]; for (int i = 0; i < SIZE; i++) elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); + shuffle(elements); Collection full = populatedSet(elements); Arrays.fill(a, 42); @@ -289,7 +352,7 @@ public class CopyOnWriteArraySetTest ext assertSame(a, full.toArray(a)); assertTrue(Arrays.equals(elements, a)); - a = new Integer[2*SIZE]; + a = new Integer[2 * SIZE]; Arrays.fill(a, 42); assertSame(a, full.toArray(a)); assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); @@ -303,10 +366,10 @@ public class CopyOnWriteArraySetTest ext * not store the objects inside the set */ public void testToArray_ArrayStoreException() { + CopyOnWriteArraySet c = new CopyOnWriteArraySet(); + c.add("zfasdfsdf"); + c.add("asdadasd"); try { - CopyOnWriteArraySet c = new CopyOnWriteArraySet(); - c.add("zfasdfsdf"); - c.add("asdadasd"); c.toArray(new Long[5]); shouldThrow(); } catch (ArrayStoreException success) {} @@ -319,8 +382,21 @@ public class CopyOnWriteArraySetTest ext Set x = populatedSet(SIZE); Set y = serialClone(x); - assertTrue(x != y); + assertNotSame(y, x); assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertTrue(Arrays.equals(x.toArray(), y.toArray())); + assertEquals(x, y); + assertEquals(y, x); + } + + /** + * addAll is idempotent + */ + public void testAddAll_idempotent() throws Exception { + Set x = populatedSet(SIZE); + Set y = new CopyOnWriteArraySet(x); + y.addAll(x); assertEquals(x, y); assertEquals(y, x); }