--- jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2003/08/31 19:24:54 1.1 +++ jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2018/04/05 02:20:21 1.40 @@ -1,239 +1,413 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/publicdomain/zero/1.0/ + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; -public class CopyOnWriteArraySetTest extends TestCase{ - +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 i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return true; } + } + return newTestSuite( + CopyOnWriteArraySetTest.class, + CollectionTest.testSuite(new Implementation())); } - static CopyOnWriteArraySet fullSet(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(new Integer(i)); - assertFalse(a.isEmpty()); + for (int i = 0; i < n; i++) + a.add(i); + assertEquals(n == 0, a.isEmpty()); assertEquals(n, a.size()); return a; } + static CopyOnWriteArraySet populatedSet(Integer[] elements) { + CopyOnWriteArraySet a = new CopyOnWriteArraySet<>(); + assertTrue(a.isEmpty()); + for (int i = 0; i < elements.length; i++) + a.add(elements[i]); + assertFalse(a.isEmpty()); + assertEquals(elements.length, a.size()); + return a; + } + + /** + * Default-constructed set is empty + */ + public void testConstructor() { + CopyOnWriteArraySet a = new CopyOnWriteArraySet(); + assertTrue(a.isEmpty()); + } + + /** + * 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)); + for (int i = 0; i < SIZE; ++i) + assertTrue(a.contains(ints[i])); + } + /** - * Test to verify addAll correctly adds each element from the given collection + * addAll adds each non-duplicate element from the given collection */ - public void testAddAll(){ - CopyOnWriteArraySet full = fullSet(3); - Vector v = new Vector(); - v.add(new Integer(3)); - v.add(new Integer(4)); - v.add(new Integer(5)); - full.addAll(v); - assertEquals(6, full.size()); + public void testAddAll() { + 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()); } /** - * Test to verify addAllAbsent adds each element from the given collection that did not - * already exist in the List + * addAll adds each non-duplicate element from the given collection */ - public void testAddAll2(){ - CopyOnWriteArraySet full = fullSet(3); - Vector v = new Vector(); - v.add(new Integer(3)); - v.add(new Integer(4)); - v.add(new Integer(1)); // will not add this element - full.addAll(v); - assertEquals(5, full.size()); + public void testAddAll2() { + 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()); } /** - * Test to verify addIfAbsent will not add the element if it already exists in the list + * add will not add the element if it already exists in the set */ - public void testAdd2(){ - CopyOnWriteArraySet full = fullSet(3); - full.add(new Integer(1)); - assertEquals(3, full.size()); + public void testAdd2() { + Set full = populatedSet(3); + full.add(one); + assertEquals(3, full.size()); } /** - * test to verify addIfAbsent correctly adds the element when it does not exist in the list + * add adds the element when it does not exist in the set */ - public void testAdd3(){ - CopyOnWriteArraySet full = fullSet(3); - full.add(new Integer(3)); - assertTrue(full.contains(new Integer(3))); + public void testAdd3() { + Set full = populatedSet(3); + full.add(three); + assertTrue(full.contains(three)); } /** - * Test to verify clear correctly removes all elements from the list + * clear removes all elements from the set */ - public void testClear(){ - CopyOnWriteArraySet full = fullSet(3); - full.clear(); - assertEquals(0, full.size()); + public void testClear() { + Collection full = populatedSet(3); + full.clear(); + assertEquals(0, full.size()); + assertTrue(full.isEmpty()); } /** - * Test to verify contains returns the correct values + * contains returns true for added elements */ - public void testContains(){ - CopyOnWriteArraySet full = fullSet(3); - assertTrue(full.contains(new Integer(1))); - assertFalse(full.contains(new Integer(5))); + public void testContains() { + Collection full = populatedSet(3); + assertTrue(full.contains(one)); + assertFalse(full.contains(five)); } + /** + * Sets with equal elements are equal + */ public void testEquals() { - CopyOnWriteArraySet a = fullSet(3); - CopyOnWriteArraySet b = fullSet(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)); assertEquals(a.hashCode(), b.hashCode()); - a.add(new Integer(-1)); + 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)); - b.add(new Integer(-1)); + 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)); } - /** - * Test to verify containsAll returns the correct values + * containsAll returns true for collections with subset of elements */ - public void testContainsAll(){ - CopyOnWriteArraySet full = fullSet(3); - Vector v = new Vector(); - v.add(new Integer(1)); - v.add(new Integer(2)); - assertTrue(full.containsAll(v)); - v.add(new Integer(6)); - assertFalse(full.containsAll(v)); + public void testContainsAll() { + 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) {} } /** - * Test to verify isEmpty returns the correct values + * isEmpty is true when empty, else false */ - public void testIsEmpty(){ - CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); - CopyOnWriteArraySet full = fullSet(3); - assertTrue(empty.isEmpty()); - assertFalse(full.isEmpty()); + public void testIsEmpty() { + assertTrue(populatedSet(0).isEmpty()); + assertFalse(populatedSet(3).isEmpty()); } /** - * Test to verify iterator() returns an iterator containing the elements of the list + * iterator() returns an iterator containing the elements of the + * set in insertion order */ - public void testIterator(){ - CopyOnWriteArraySet full = fullSet(3); - Iterator i = full.iterator(); - int j; - for(j = 0; i.hasNext(); j++) - assertEquals(j, ((Integer)i.next()).intValue()); - assertEquals(3, j); + public void testIterator() { + 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); } - public void testIteratorRemove () { - CopyOnWriteArraySet full = fullSet(3); + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new CopyOnWriteArraySet().iterator()); + } + + /** + * iterator remove is unsupported + */ + public void testIteratorRemove() { + Collection full = populatedSet(3); Iterator it = full.iterator(); it.next(); try { it.remove(); - fail("should throw"); - } - catch (UnsupportedOperationException success) {} + shouldThrow(); + } catch (UnsupportedOperationException success) {} } - public void testToString(){ - CopyOnWriteArraySet full = fullSet(3); + /** + * toString holds toString of elements + */ + public void testToString() { + assertEquals("[]", new CopyOnWriteArraySet().toString()); + Collection full = populatedSet(3); String s = full.toString(); - for (int i = 0; i < 3; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); - } - } - + for (int i = 0; i < 3; ++i) + assertTrue(s.contains(String.valueOf(i))); + assertEquals(new ArrayList(full).toString(), + full.toString()); + } /** - * Test to verify removeAll correctly removes all elements from the given collection + * removeAll removes all elements from the given collection */ - public void testRemoveAll(){ - CopyOnWriteArraySet full = fullSet(3); - Vector v = new Vector(); - v.add(new Integer(1)); - v.add(new Integer(2)); - full.removeAll(v); - assertEquals(1, full.size()); + public void testRemoveAll() { + 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()); } - - public void testRemove(){ - CopyOnWriteArraySet full = fullSet(3); - full.remove(new Integer(1)); - assertFalse(full.contains(new Integer(1))); - assertEquals(2, full.size()); + /** + * remove removes an element + */ + public void testRemove() { + Collection full = populatedSet(3); + full.remove(one); + assertFalse(full.contains(one)); + assertEquals(2, full.size()); } /** - * Test to verify size returns the correct values + * size returns the number of elements */ - public void testSize(){ - CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); - CopyOnWriteArraySet full = fullSet(3); - assertEquals(3, full.size()); - assertEquals(0, empty.size()); + public void testSize() { + Collection empty = new CopyOnWriteArraySet(); + Collection full = populatedSet(3); + assertEquals(3, full.size()); + assertEquals(0, empty.size()); } /** - * Test to verify toArray returns an Object array containing all elements from the list + * toArray() returns an Object array containing all elements from + * the set in insertion order */ - public void testToArray(){ - CopyOnWriteArraySet full = fullSet(3); - Object[] o = full.toArray(); - assertEquals(3, o.length); - assertEquals(0, ((Integer)o[0]).intValue()); - assertEquals(1, ((Integer)o[1]).intValue()); - assertEquals(2, ((Integer)o[2]).intValue()); + public void testToArray() { + 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; + shuffle(elements); + Collection full = populatedSet(elements); + + assertTrue(Arrays.equals(elements, full.toArray())); + assertSame(Object[].class, full.toArray().getClass()); } /** - * test to verify toArray returns an Integer array containing all elements from the list + * toArray(Integer array) returns an Integer array containing all + * elements from the set in insertion order */ - public void testToArray2(){ - CopyOnWriteArraySet full = fullSet(3); - Integer[] i = new Integer[3]; - i = (Integer[])full.toArray(i); - assertEquals(3, i.length); - assertEquals(0, i[0].intValue()); - assertEquals(1, i[1].intValue()); - assertEquals(2, i[2].intValue()); - } + public void testToArray2() { + Collection empty = new CopyOnWriteArraySet(); + Integer[] a; + + a = new Integer[0]; + assertSame(a, empty.toArray(a)); + + a = new Integer[SIZE / 2]; + Arrays.fill(a, 42); + assertSame(a, empty.toArray(a)); + assertNull(a[0]); + for (int i = 1; i < a.length; i++) + assertEquals(42, (int) a[i]); + Integer[] elements = new Integer[SIZE]; + for (int i = 0; i < SIZE; i++) + elements[i] = i; + shuffle(elements); + Collection full = populatedSet(elements); + Arrays.fill(a, 42); + 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()); - // Exception tests + a = new Integer[SIZE]; + Arrays.fill(a, 42); + assertSame(a, full.toArray(a)); + assertTrue(Arrays.equals(elements, a)); + + a = new Integer[2 * SIZE]; + Arrays.fill(a, 42); + 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]); + } /** - * Test to verify toArray throws an ArrayStoreException when the given array - * can not store the objects inside the list + * toArray throws an ArrayStoreException when the given array can + * not store the objects inside the set */ - public void testToArray_ArrayStoreException(){ - try{ - CopyOnWriteArraySet c = new CopyOnWriteArraySet(); - c.add("zfasdfsdf"); - c.add("asdadasd"); + public void testToArray_ArrayStoreException() { + CopyOnWriteArraySet c = new CopyOnWriteArraySet(); + c.add("zfasdfsdf"); + c.add("asdadasd"); + try { c.toArray(new Long[5]); - fail("Object[] toArray(Object[]) should throw ArrayStoreException"); - }catch(ArrayStoreException e){} + shouldThrow(); + } catch (ArrayStoreException success) {} + } + + /** + * A deserialized/reserialized set equals original + */ + public void testSerialization() throws Exception { + Set x = populatedSet(SIZE); + Set y = serialClone(x); + + 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); } }