--- jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2009/11/21 02:07:26 1.11 +++ jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2011/11/29 20:49:39 1.23 @@ -1,34 +1,48 @@ /* * 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/licenses/publicdomain + * 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.io.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; +import java.util.Vector; +import java.util.concurrent.CopyOnWriteArraySet; public class CopyOnWriteArraySetTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(CopyOnWriteArraySetTest.class); } - 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(new Integer(i)); + for (int i = 0; i < n; i++) + a.add(i); assertFalse(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 */ @@ -49,9 +63,8 @@ public class CopyOnWriteArraySetTest ext assertTrue(a.contains(ints[i])); } - /** - * addAll adds each element from the given collection + * addAll adds each element from the given collection */ public void testAddAll() { CopyOnWriteArraySet full = populatedSet(3); @@ -64,8 +77,8 @@ public class CopyOnWriteArraySetTest ext } /** - * addAll adds each element from the given collection that did not - * already exist in the set + * addAll adds each element from the given collection that did not + * already exist in the set */ public void testAddAll2() { CopyOnWriteArraySet full = populatedSet(3); @@ -78,7 +91,7 @@ public class CopyOnWriteArraySetTest ext } /** - * add will not add the element if it already exists in the set + * add will not add the element if it already exists in the set */ public void testAdd2() { CopyOnWriteArraySet full = populatedSet(3); @@ -87,8 +100,7 @@ public class CopyOnWriteArraySetTest ext } /** - * add adds the element when it does not exist - * in the set + * add adds the element when it does not exist in the set */ public void testAdd3() { CopyOnWriteArraySet full = populatedSet(3); @@ -97,7 +109,7 @@ public class CopyOnWriteArraySetTest ext } /** - * clear removes all elements from the set + * clear removes all elements from the set */ public void testClear() { CopyOnWriteArraySet full = populatedSet(3); @@ -106,7 +118,7 @@ public class CopyOnWriteArraySetTest ext } /** - * contains returns true for added elements + * contains returns true for added elements */ public void testContains() { CopyOnWriteArraySet full = populatedSet(3); @@ -132,9 +144,8 @@ public class CopyOnWriteArraySetTest ext assertEquals(a.hashCode(), b.hashCode()); } - /** - * containsAll returns true for collections with subset of elements + * containsAll returns true for collections with subset of elements */ public void testContainsAll() { CopyOnWriteArraySet full = populatedSet(3); @@ -147,7 +158,7 @@ public class CopyOnWriteArraySetTest ext } /** - * isEmpty is true when empty, else false + * isEmpty is true when empty, else false */ public void testIsEmpty() { CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); @@ -157,29 +168,28 @@ public class CopyOnWriteArraySetTest ext } /** - * iterator() returns an iterator containing the elements of the set + * iterator() returns an iterator containing the elements of the set */ public void testIterator() { CopyOnWriteArraySet full = populatedSet(3); Iterator i = full.iterator(); int j; for (j = 0; i.hasNext(); j++) - assertEquals(j, ((Integer)i.next()).intValue()); + assertEquals(j, i.next()); assertEquals(3, j); } /** * iterator remove is unsupported */ - public void testIteratorRemove () { + public void testIteratorRemove() { CopyOnWriteArraySet full = populatedSet(3); Iterator it = full.iterator(); it.next(); try { it.remove(); shouldThrow(); - } - catch (UnsupportedOperationException success) {} + } catch (UnsupportedOperationException success) {} } /** @@ -189,13 +199,12 @@ public class CopyOnWriteArraySetTest ext CopyOnWriteArraySet full = populatedSet(3); String s = full.toString(); for (int i = 0; i < 3; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } - /** - * removeAll removes all elements from the given collection + * removeAll removes all elements from the given collection */ public void testRemoveAll() { CopyOnWriteArraySet full = populatedSet(3); @@ -206,7 +215,6 @@ public class CopyOnWriteArraySetTest ext assertEquals(1, full.size()); } - /** * remove removes an element */ @@ -218,7 +226,7 @@ public class CopyOnWriteArraySetTest ext } /** - * size returns the number of elements + * size returns the number of elements */ public void testSize() { CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); @@ -228,35 +236,71 @@ public class CopyOnWriteArraySetTest ext } /** - * toArray returns an Object array containing all elements from the set + * toArray() returns an Object array containing all elements from + * the set in insertion order */ public void testToArray() { - CopyOnWriteArraySet full = populatedSet(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()); + 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); + + assertTrue(Arrays.equals(elements, full.toArray())); + assertSame(Object[].class, full.toArray().getClass()); } /** - * toArray returns an Integer array containing all elements from - * the set + * toArray(Integer array) returns an Integer array containing all + * elements from the set in insertion order */ public void testToArray2() { - CopyOnWriteArraySet full = populatedSet(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()); - } + 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; + Collections.shuffle(Arrays.asList(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()); + + 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]); + } /** - * toArray throws an ArrayStoreException when the given array can - * not store the objects inside the set + * toArray throws an ArrayStoreException when the given array can + * not store the objects inside the set */ public void testToArray_ArrayStoreException() { try { @@ -265,30 +309,22 @@ public class CopyOnWriteArraySetTest ext c.add("asdadasd"); c.toArray(new Long[5]); shouldThrow(); - } catch (ArrayStoreException e) {} + } catch (ArrayStoreException success) {} } /** * A deserialized serialized set is equal */ - public void testSerialization() { - CopyOnWriteArraySet q = populatedSet(SIZE); - - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); - assertEquals(q.size(), r.size()); - assertTrue(q.equals(r)); - assertTrue(r.equals(q)); - } catch (Exception e) { - unexpectedException(); - } + public void testSerialization() throws Exception { + Set x = populatedSet(SIZE); + Set y = serialClone(x); + + assertTrue(x != y); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertTrue(Arrays.equals(x.toArray(), y.toArray())); + assertEquals(x, y); + assertEquals(y, x); } }