--- jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2003/08/31 19:24:54 1.1 +++ jsr166/src/test/tck/CopyOnWriteArraySetTest.java 2003/12/29 19:05:40 1.7 @@ -1,25 +1,25 @@ /* - * 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/licenses/publicdomain + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import java.io.*; -public class CopyOnWriteArraySetTest extends TestCase{ - +public class CopyOnWriteArraySetTest extends JSR166TestCase { public static void main(String[] args) { junit.textui.TestRunner.run (suite()); } - public static Test suite() { return new TestSuite(CopyOnWriteArraySetTest.class); } - static CopyOnWriteArraySet fullSet(int n){ + static CopyOnWriteArraySet populatedSet(int n){ CopyOnWriteArraySet a = new CopyOnWriteArraySet(); assertTrue(a.isEmpty()); for (int i = 0; i < n; ++i) @@ -30,78 +30,103 @@ public class CopyOnWriteArraySetTest ext } /** - * Test to verify addAll correctly adds each element from the given collection + * 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 testAddAll(){ - CopyOnWriteArraySet full = fullSet(3); + 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])); + } + + + /** + * addAll adds each element from the given collection + */ + public void testAddAll() { + CopyOnWriteArraySet full = populatedSet(3); Vector v = new Vector(); - v.add(new Integer(3)); - v.add(new Integer(4)); - v.add(new Integer(5)); + v.add(three); + v.add(four); + v.add(five); full.addAll(v); 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 element from the given collection that did not + * already exist in the set */ - public void testAddAll2(){ - CopyOnWriteArraySet full = fullSet(3); + public void testAddAll2() { + CopyOnWriteArraySet full = populatedSet(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 + v.add(three); + v.add(four); + v.add(one); // will not add this element full.addAll(v); 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)); + public void testAdd2() { + CopyOnWriteArraySet 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() { + CopyOnWriteArraySet 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); + public void testClear() { + CopyOnWriteArraySet full = populatedSet(3); full.clear(); assertEquals(0, full.size()); } /** - * 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() { + CopyOnWriteArraySet 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)); assertEquals(a.hashCode(), b.hashCode()); - a.add(new Integer(-1)); + a.add(m1); assertFalse(a.equals(b)); assertFalse(b.equals(a)); - b.add(new Integer(-1)); + b.add(m1); assertTrue(a.equals(b)); assertTrue(b.equals(a)); assertEquals(a.hashCode(), b.hashCode()); @@ -109,33 +134,33 @@ public class CopyOnWriteArraySetTest ext /** - * Test to verify containsAll returns the correct values + * containsAll returns true for collections with subset of elements */ - public void testContainsAll(){ - CopyOnWriteArraySet full = fullSet(3); + public void testContainsAll() { + CopyOnWriteArraySet full = populatedSet(3); Vector v = new Vector(); - v.add(new Integer(1)); - v.add(new Integer(2)); + v.add(one); + v.add(two); assertTrue(full.containsAll(v)); - v.add(new Integer(6)); + v.add(six); assertFalse(full.containsAll(v)); } /** - * Test to verify isEmpty returns the correct values + * isEmpty is true when empty, else false */ - public void testIsEmpty(){ + public void testIsEmpty() { CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); - CopyOnWriteArraySet full = fullSet(3); + CopyOnWriteArraySet full = populatedSet(3); assertTrue(empty.isEmpty()); assertFalse(full.isEmpty()); } /** - * Test to verify iterator() returns an iterator containing the elements of the list + * iterator() returns an iterator containing the elements of the set */ - public void testIterator(){ - CopyOnWriteArraySet full = fullSet(3); + public void testIterator() { + CopyOnWriteArraySet full = populatedSet(3); Iterator i = full.iterator(); int j; for(j = 0; i.hasNext(); j++) @@ -143,19 +168,25 @@ public class CopyOnWriteArraySetTest ext assertEquals(3, j); } + /** + * iterator remove is unsupported + */ public void testIteratorRemove () { - CopyOnWriteArraySet full = fullSet(3); + CopyOnWriteArraySet full = populatedSet(3); Iterator it = full.iterator(); it.next(); try { it.remove(); - fail("should throw"); + shouldThrow(); } catch (UnsupportedOperationException success) {} } - public void testToString(){ - CopyOnWriteArraySet full = fullSet(3); + /** + * toString holds toString of elements + */ + public void testToString() { + CopyOnWriteArraySet full = populatedSet(3); String s = full.toString(); for (int i = 0; i < 3; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); @@ -164,40 +195,43 @@ public class CopyOnWriteArraySetTest ext /** - * 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); + public void testRemoveAll() { + CopyOnWriteArraySet full = populatedSet(3); Vector v = new Vector(); - v.add(new Integer(1)); - v.add(new Integer(2)); + v.add(one); + v.add(two); full.removeAll(v); assertEquals(1, full.size()); } - public void testRemove(){ - CopyOnWriteArraySet full = fullSet(3); - full.remove(new Integer(1)); - assertFalse(full.contains(new Integer(1))); + /** + * remove removes an element + */ + public void testRemove() { + CopyOnWriteArraySet 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(){ + public void testSize() { CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); - CopyOnWriteArraySet full = fullSet(3); + CopyOnWriteArraySet 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 */ - public void testToArray(){ - CopyOnWriteArraySet full = fullSet(3); + public void testToArray() { + CopyOnWriteArraySet full = populatedSet(3); Object[] o = full.toArray(); assertEquals(3, o.length); assertEquals(0, ((Integer)o[0]).intValue()); @@ -206,10 +240,11 @@ public class CopyOnWriteArraySetTest ext } /** - * test to verify toArray returns an Integer array containing all elements from the list + * toArray returns an Integer array containing all elements from + * the set */ - public void testToArray2(){ - CopyOnWriteArraySet full = fullSet(3); + public void testToArray2() { + CopyOnWriteArraySet full = populatedSet(3); Integer[] i = new Integer[3]; i = (Integer[])full.toArray(i); assertEquals(3, i.length); @@ -219,21 +254,41 @@ public class CopyOnWriteArraySetTest ext } - - // Exception tests - /** - * 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{ + public void testToArray_ArrayStoreException() { + try { CopyOnWriteArraySet c = new CopyOnWriteArraySet(); c.add("zfasdfsdf"); c.add("asdadasd"); c.toArray(new Long[5]); - fail("Object[] toArray(Object[]) should throw ArrayStoreException"); - }catch(ArrayStoreException e){} + shouldThrow(); + } catch(ArrayStoreException e){} + } + + /** + * 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(); + } } }