--- jsr166/src/test/tck/CopyOnWriteArrayListTest.java 2011/11/29 05:23:56 1.26 +++ jsr166/src/test/tck/CopyOnWriteArrayListTest.java 2013/04/12 20:09:01 1.28 @@ -7,6 +7,7 @@ */ import junit.framework.*; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -14,6 +15,7 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; +import java.util.NoSuchElementException; import java.util.Vector; import java.util.concurrent.CopyOnWriteArrayList; @@ -238,15 +240,33 @@ public class CopyOnWriteArrayListTest ex } /** - * iterator() returns an iterator containing the elements of the list + * iterator() returns an iterator containing the elements of the + * list in insertion order */ public void testIterator() { - CopyOnWriteArrayList full = populatedArray(SIZE); - Iterator i = full.iterator(); - int j; - for (j = 0; i.hasNext(); j++) - assertEquals(j, i.next()); - assertEquals(SIZE, j); + Collection empty = new CopyOnWriteArrayList(); + 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 = populatedArray(elements); + + Iterator it = full.iterator(); + for (int j = 0; j < SIZE; j++) { + assertTrue(it.hasNext()); + assertEquals(elements[j], it.next()); + } + assertFalse(it.hasNext()); + try { + it.next(); + shouldThrow(); + } catch (NoSuchElementException success) {} } /** @@ -266,11 +286,13 @@ public class CopyOnWriteArrayListTest ex * toString contains toString of elements */ public void testToString() { + assertEquals("[]", new CopyOnWriteArrayList().toString()); CopyOnWriteArrayList full = populatedArray(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()); } /** @@ -320,12 +342,38 @@ public class CopyOnWriteArrayListTest ex } /** - * remove removes and returns the object at the given index + * remove(int) removes and returns the object at the given index */ - public void testRemove() { - CopyOnWriteArrayList full = populatedArray(3); - assertEquals(2, full.remove(2)); - assertEquals(2, full.size()); + public void testRemove_int() { + int SIZE = 3; + for (int i = 0; i < SIZE; i++) { + CopyOnWriteArrayList full = populatedArray(SIZE); + assertEquals(i, full.remove(i)); + assertEquals(SIZE - 1, full.size()); + assertFalse(full.contains(new Integer(i))); + } + } + + /** + * remove(Object) removes the object if found and returns true + */ + public void testRemove_Object() { + int SIZE = 3; + for (int i = 0; i < SIZE; i++) { + CopyOnWriteArrayList full = populatedArray(SIZE); + assertFalse(full.remove(new Integer(-42))); + assertTrue(full.remove(new Integer(i))); + assertEquals(SIZE - 1, full.size()); + assertFalse(full.contains(new Integer(i))); + } + CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); + assertTrue(x.remove(new Integer(6))); + assertEquals(x, Arrays.asList(4, 5)); + assertTrue(x.remove(new Integer(4))); + assertEquals(x, Arrays.asList(5)); + assertTrue(x.remove(new Integer(5))); + assertEquals(x, Arrays.asList()); + assertFalse(x.remove(new Integer(5))); } /**