ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CopyOnWriteArrayListTest.java (file contents):
Revision 1.26 by jsr166, Tue Nov 29 05:23:56 2011 UTC vs.
Revision 1.28 by jsr166, Fri Apr 12 20:09:01 2013 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 + import java.util.ArrayList;
11   import java.util.Arrays;
12   import java.util.Collection;
13   import java.util.Collections;
# Line 14 | Line 15 | import java.util.Iterator;
15   import java.util.LinkedList;
16   import java.util.List;
17   import java.util.ListIterator;
18 + import java.util.NoSuchElementException;
19   import java.util.Vector;
20   import java.util.concurrent.CopyOnWriteArrayList;
21  
# Line 238 | Line 240 | public class CopyOnWriteArrayListTest ex
240      }
241  
242      /**
243 <     * iterator() returns an iterator containing the elements of the list
243 >     * iterator() returns an iterator containing the elements of the
244 >     * list in insertion order
245       */
246      public void testIterator() {
247 <        CopyOnWriteArrayList full = populatedArray(SIZE);
248 <        Iterator i = full.iterator();
249 <        int j;
250 <        for (j = 0; i.hasNext(); j++)
251 <            assertEquals(j, i.next());
252 <        assertEquals(SIZE, j);
247 >        Collection empty = new CopyOnWriteArrayList();
248 >        assertFalse(empty.iterator().hasNext());
249 >        try {
250 >            empty.iterator().next();
251 >            shouldThrow();
252 >        } catch (NoSuchElementException success) {}
253 >
254 >        Integer[] elements = new Integer[SIZE];
255 >        for (int i = 0; i < SIZE; i++)
256 >            elements[i] = i;
257 >        Collections.shuffle(Arrays.asList(elements));
258 >        Collection<Integer> full = populatedArray(elements);
259 >
260 >        Iterator it = full.iterator();
261 >        for (int j = 0; j < SIZE; j++) {
262 >            assertTrue(it.hasNext());
263 >            assertEquals(elements[j], it.next());
264 >        }
265 >        assertFalse(it.hasNext());
266 >        try {
267 >            it.next();
268 >            shouldThrow();
269 >        } catch (NoSuchElementException success) {}
270      }
271  
272      /**
# Line 266 | Line 286 | public class CopyOnWriteArrayListTest ex
286       * toString contains toString of elements
287       */
288      public void testToString() {
289 +        assertEquals("[]", new CopyOnWriteArrayList().toString());
290          CopyOnWriteArrayList full = populatedArray(3);
291          String s = full.toString();
292 <        for (int i = 0; i < 3; ++i) {
292 >        for (int i = 0; i < 3; ++i)
293              assertTrue(s.contains(String.valueOf(i)));
294 <        }
294 >        assertEquals(new ArrayList(full).toString(),
295 >                     full.toString());
296      }
297  
298      /**
# Line 320 | Line 342 | public class CopyOnWriteArrayListTest ex
342      }
343  
344      /**
345 <     * remove removes and returns the object at the given index
345 >     * remove(int) removes and returns the object at the given index
346       */
347 <    public void testRemove() {
348 <        CopyOnWriteArrayList full = populatedArray(3);
349 <        assertEquals(2, full.remove(2));
350 <        assertEquals(2, full.size());
347 >    public void testRemove_int() {
348 >        int SIZE = 3;
349 >        for (int i = 0; i < SIZE; i++) {
350 >            CopyOnWriteArrayList full = populatedArray(SIZE);
351 >            assertEquals(i, full.remove(i));
352 >            assertEquals(SIZE - 1, full.size());
353 >            assertFalse(full.contains(new Integer(i)));
354 >        }
355 >    }
356 >
357 >    /**
358 >     * remove(Object) removes the object if found and returns true
359 >     */
360 >    public void testRemove_Object() {
361 >        int SIZE = 3;
362 >        for (int i = 0; i < SIZE; i++) {
363 >            CopyOnWriteArrayList full = populatedArray(SIZE);
364 >            assertFalse(full.remove(new Integer(-42)));
365 >            assertTrue(full.remove(new Integer(i)));
366 >            assertEquals(SIZE - 1, full.size());
367 >            assertFalse(full.contains(new Integer(i)));
368 >        }
369 >        CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
370 >        assertTrue(x.remove(new Integer(6)));
371 >        assertEquals(x, Arrays.asList(4, 5));
372 >        assertTrue(x.remove(new Integer(4)));
373 >        assertEquals(x, Arrays.asList(5));
374 >        assertTrue(x.remove(new Integer(5)));
375 >        assertEquals(x, Arrays.asList());
376 >        assertFalse(x.remove(new Integer(5)));
377      }
378  
379      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines