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.25 by jsr166, Sat Nov 26 07:39:02 2011 UTC vs.
Revision 1.27 by jsr166, Wed Nov 30 05:42:38 2011 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;
14   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 26 | Line 30 | public class CopyOnWriteArrayListTest ex
30      }
31  
32      static CopyOnWriteArrayList<Integer> populatedArray(int n) {
33 <        CopyOnWriteArrayList<Integer> a
30 <            = new CopyOnWriteArrayList<Integer>();
33 >        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
34          assertTrue(a.isEmpty());
35 <        for (int i = 0; i < n; ++i)
35 >        for (int i = 0; i < n; i++)
36              a.add(i);
37          assertFalse(a.isEmpty());
38          assertEquals(n, a.size());
39          return a;
40      }
41  
42 +    static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) {
43 +        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
44 +        assertTrue(a.isEmpty());
45 +        for (int i = 0; i < elements.length; i++)
46 +            a.add(elements[i]);
47 +        assertFalse(a.isEmpty());
48 +        assertEquals(elements.length, a.size());
49 +        return a;
50 +    }
51 +
52      /**
53       * a new list is empty
54       */
# Line 227 | 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 255 | 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 349 | Line 382 | public class CopyOnWriteArrayListTest ex
382      }
383  
384      /**
385 <     * toArray returns an Object array containing all elements from the list
385 >     * toArray() returns an Object array containing all elements from
386 >     * the list in insertion order
387       */
388      public void testToArray() {
389 <        CopyOnWriteArrayList full = populatedArray(3);
390 <        Object[] o = full.toArray();
391 <        assertEquals(3, o.length);
392 <        assertEquals(0, o[0]);
393 <        assertEquals(1, o[1]);
394 <        assertEquals(2, o[2]);
389 >        Object[] a = new CopyOnWriteArrayList().toArray();
390 >        assertTrue(Arrays.equals(new Object[0], a));
391 >        assertSame(Object[].class, a.getClass());
392 >
393 >        Integer[] elements = new Integer[SIZE];
394 >        for (int i = 0; i < SIZE; i++)
395 >            elements[i] = i;
396 >        Collections.shuffle(Arrays.asList(elements));
397 >        Collection<Integer> full = populatedArray(elements);
398 >
399 >        assertTrue(Arrays.equals(elements, full.toArray()));
400 >        assertSame(Object[].class, full.toArray().getClass());
401      }
402  
403      /**
404 <     * toArray returns an Integer array containing all elements from
405 <     * the list
404 >     * toArray(Integer array) returns an Integer array containing all
405 >     * elements from the list in insertion order
406       */
407      public void testToArray2() {
408 <        final int size = 3;
409 <        CopyOnWriteArrayList<Integer> full = populatedArray(size);
410 <        Integer[] ints = new Integer[size];
411 <        assertSame(ints, full.toArray(ints));
412 <        Iterator<Integer> it = full.iterator();
413 <        for (int i = 0; i < size; i++)
414 <            assertSame(ints[i], it.next());
415 <        assertFalse(it.hasNext());
408 >        Collection empty = new CopyOnWriteArrayList();
409 >        Integer[] a;
410 >
411 >        a = new Integer[0];
412 >        assertSame(a, empty.toArray(a));
413 >
414 >        a = new Integer[SIZE/2];
415 >        Arrays.fill(a, 42);
416 >        assertSame(a, empty.toArray(a));
417 >        assertNull(a[0]);
418 >        for (int i = 1; i < a.length; i++)
419 >            assertEquals(42, (int) a[i]);
420 >
421 >        Integer[] elements = new Integer[SIZE];
422 >        for (int i = 0; i < SIZE; i++)
423 >            elements[i] = i;
424 >        Collections.shuffle(Arrays.asList(elements));
425 >        Collection<Integer> full = populatedArray(elements);
426 >
427 >        Arrays.fill(a, 42);
428 >        assertTrue(Arrays.equals(elements, full.toArray(a)));
429 >        for (int i = 0; i < a.length; i++)
430 >            assertEquals(42, (int) a[i]);
431 >        assertSame(Integer[].class, full.toArray(a).getClass());
432 >
433 >        a = new Integer[SIZE];
434 >        Arrays.fill(a, 42);
435 >        assertSame(a, full.toArray(a));
436 >        assertTrue(Arrays.equals(elements, a));
437 >
438 >        a = new Integer[2*SIZE];
439 >        Arrays.fill(a, 42);
440 >        assertSame(a, full.toArray(a));
441 >        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
442 >        assertNull(a[SIZE]);
443 >        for (int i = SIZE + 1; i < a.length; i++)
444 >            assertEquals(42, (int) a[i]);
445      }
446  
447      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines