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.20 by jsr166, Fri May 27 19:09:55 2011 UTC vs.
Revision 1.39 by jsr166, Wed Aug 24 22:22:39 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
9 > import java.util.ArrayList;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Iterator;
13 > import java.util.LinkedList;
14 > import java.util.List;
15 > import java.util.ListIterator;
16 > import java.util.NoSuchElementException;
17 > import java.util.concurrent.CopyOnWriteArrayList;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class CopyOnWriteArrayListTest extends JSR166TestCase {
23  
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27  
28      public static Test suite() {
29          return new TestSuite(CopyOnWriteArrayListTest.class);
30      }
31  
32 <    static CopyOnWriteArrayList populatedArray(int n) {
33 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
32 >    static CopyOnWriteArrayList<Integer> populatedArray(int n) {
33 >        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
34          assertTrue(a.isEmpty());
35 <        for (int i = 0; i < n; ++i)
36 <            a.add(new Integer(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
# Line 45 | Line 62 | public class CopyOnWriteArrayListTest ex
62       */
63      public void testConstructor2() {
64          Integer[] ints = new Integer[SIZE];
65 <        for (int i = 0; i < SIZE-1; ++i)
65 >        for (int i = 0; i < SIZE - 1; ++i)
66              ints[i] = new Integer(i);
67          CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
68          for (int i = 0; i < SIZE; ++i)
# Line 57 | Line 74 | public class CopyOnWriteArrayListTest ex
74       */
75      public void testConstructor3() {
76          Integer[] ints = new Integer[SIZE];
77 <        for (int i = 0; i < SIZE-1; ++i)
77 >        for (int i = 0; i < SIZE - 1; ++i)
78              ints[i] = new Integer(i);
79          CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
80          for (int i = 0; i < SIZE; ++i)
81              assertEquals(ints[i], a.get(i));
82      }
83  
67
84      /**
85 <     * addAll adds each element from the given collection
85 >     * addAll adds each element from the given collection, including duplicates
86       */
87      public void testAddAll() {
88          CopyOnWriteArrayList full = populatedArray(3);
89 <        Vector v = new Vector();
74 <        v.add(three);
75 <        v.add(four);
76 <        v.add(five);
77 <        full.addAll(v);
89 >        assertTrue(full.addAll(Arrays.asList(three, four, five)));
90          assertEquals(6, full.size());
91 +        assertTrue(full.addAll(Arrays.asList(three, four, five)));
92 +        assertEquals(9, full.size());
93      }
94  
95      /**
# Line 84 | Line 98 | public class CopyOnWriteArrayListTest ex
98       */
99      public void testAddAllAbsent() {
100          CopyOnWriteArrayList full = populatedArray(3);
101 <        Vector v = new Vector();
102 <        v.add(three);
103 <        v.add(four);
104 <        v.add(one); // will not add this element
91 <        full.addAllAbsent(v);
101 >        // "one" is duplicate and will not be added
102 >        assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one)));
103 >        assertEquals(5, full.size());
104 >        assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one)));
105          assertEquals(5, full.size());
106      }
107  
# Line 119 | Line 132 | public class CopyOnWriteArrayListTest ex
132          assertEquals(0, full.size());
133      }
134  
122
135      /**
136       * Cloned list is equal
137       */
# Line 164 | Line 176 | public class CopyOnWriteArrayListTest ex
176          CopyOnWriteArrayList b = populatedArray(3);
177          assertTrue(a.equals(b));
178          assertTrue(b.equals(a));
179 +        assertTrue(a.containsAll(b));
180 +        assertTrue(b.containsAll(a));
181          assertEquals(a.hashCode(), b.hashCode());
182          a.add(m1);
183          assertFalse(a.equals(b));
184          assertFalse(b.equals(a));
185 +        assertTrue(a.containsAll(b));
186 +        assertFalse(b.containsAll(a));
187          b.add(m1);
188          assertTrue(a.equals(b));
189          assertTrue(b.equals(a));
190 +        assertTrue(a.containsAll(b));
191 +        assertTrue(b.containsAll(a));
192          assertEquals(a.hashCode(), b.hashCode());
175    }
193  
194 +        assertFalse(a.equals(null));
195 +    }
196  
197      /**
198 <     * containsAll returns true for collection with subset of elements
198 >     * containsAll returns true for collections with subset of elements
199       */
200      public void testContainsAll() {
201          CopyOnWriteArrayList full = populatedArray(3);
202 <        Vector v = new Vector();
203 <        v.add(one);
204 <        v.add(two);
205 <        assertTrue(full.containsAll(v));
206 <        v.add(six);
207 <        assertFalse(full.containsAll(v));
202 >        assertTrue(full.containsAll(Arrays.asList()));
203 >        assertTrue(full.containsAll(Arrays.asList(one)));
204 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
205 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
206 >        assertFalse(full.containsAll(Arrays.asList(six)));
207 >
208 >        try {
209 >            full.containsAll(null);
210 >            shouldThrow();
211 >        } catch (NullPointerException success) {}
212      }
213  
214      /**
# Line 226 | Line 249 | public class CopyOnWriteArrayListTest ex
249      }
250  
251      /**
252 <     * iterator() returns an iterator containing the elements of the list
252 >     * iterator() returns an iterator containing the elements of the
253 >     * list in insertion order
254       */
255      public void testIterator() {
256 <        CopyOnWriteArrayList full = populatedArray(SIZE);
257 <        Iterator i = full.iterator();
258 <        int j;
259 <        for (j = 0; i.hasNext(); j++)
260 <            assertEquals(j, i.next());
261 <        assertEquals(SIZE, j);
256 >        Collection empty = new CopyOnWriteArrayList();
257 >        assertFalse(empty.iterator().hasNext());
258 >        try {
259 >            empty.iterator().next();
260 >            shouldThrow();
261 >        } catch (NoSuchElementException success) {}
262 >
263 >        Integer[] elements = new Integer[SIZE];
264 >        for (int i = 0; i < SIZE; i++)
265 >            elements[i] = i;
266 >        shuffle(elements);
267 >        Collection<Integer> full = populatedArray(elements);
268 >
269 >        Iterator it = full.iterator();
270 >        for (int j = 0; j < SIZE; j++) {
271 >            assertTrue(it.hasNext());
272 >            assertEquals(elements[j], it.next());
273 >        }
274 >        assertIteratorExhausted(it);
275 >    }
276 >
277 >    /**
278 >     * iterator of empty collection has no elements
279 >     */
280 >    public void testEmptyIterator() {
281 >        Collection c = new CopyOnWriteArrayList();
282 >        assertIteratorExhausted(c.iterator());
283      }
284  
285      /**
# Line 254 | Line 299 | public class CopyOnWriteArrayListTest ex
299       * toString contains toString of elements
300       */
301      public void testToString() {
302 +        assertEquals("[]", new CopyOnWriteArrayList().toString());
303          CopyOnWriteArrayList full = populatedArray(3);
304          String s = full.toString();
305 <        for (int i = 0; i < 3; ++i) {
305 >        for (int i = 0; i < 3; ++i)
306              assertTrue(s.contains(String.valueOf(i)));
307 <        }
307 >        assertEquals(new ArrayList(full).toString(),
308 >                     full.toString());
309      }
310  
311      /**
# Line 275 | Line 322 | public class CopyOnWriteArrayListTest ex
322      /**
323       * lastIndexOf returns the index from the given starting point
324       */
325 <    public void testlastIndexOf2() {
325 >    public void testLastIndexOf2() {
326          CopyOnWriteArrayList full = populatedArray(3);
327          full.add(one);
328          full.add(three);
# Line 303 | Line 350 | public class CopyOnWriteArrayListTest ex
350          ListIterator i = full.listIterator(1);
351          int j;
352          for (j = 0; i.hasNext(); j++)
353 <            assertEquals(j+1, i.next());
353 >            assertEquals(j + 1, i.next());
354          assertEquals(2, j);
355      }
356  
357      /**
358 <     * remove removes and returns the object at the given index
358 >     * remove(int) removes and returns the object at the given index
359       */
360 <    public void testRemove() {
361 <        CopyOnWriteArrayList full = populatedArray(3);
362 <        assertEquals(2, full.remove(2));
363 <        assertEquals(2, full.size());
360 >    public void testRemove_int() {
361 >        int SIZE = 3;
362 >        for (int i = 0; i < SIZE; i++) {
363 >            CopyOnWriteArrayList full = populatedArray(SIZE);
364 >            assertEquals(i, full.remove(i));
365 >            assertEquals(SIZE - 1, full.size());
366 >            assertFalse(full.contains(new Integer(i)));
367 >        }
368 >    }
369 >
370 >    /**
371 >     * remove(Object) removes the object if found and returns true
372 >     */
373 >    public void testRemove_Object() {
374 >        int SIZE = 3;
375 >        for (int i = 0; i < SIZE; i++) {
376 >            CopyOnWriteArrayList full = populatedArray(SIZE);
377 >            assertFalse(full.remove(new Integer(-42)));
378 >            assertTrue(full.remove(new Integer(i)));
379 >            assertEquals(SIZE - 1, full.size());
380 >            assertFalse(full.contains(new Integer(i)));
381 >        }
382 >        CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
383 >        assertTrue(x.remove(new Integer(6)));
384 >        assertEquals(x, Arrays.asList(4, 5));
385 >        assertTrue(x.remove(new Integer(4)));
386 >        assertEquals(x, Arrays.asList(5));
387 >        assertTrue(x.remove(new Integer(5)));
388 >        assertEquals(x, Arrays.asList());
389 >        assertFalse(x.remove(new Integer(5)));
390      }
391  
392      /**
# Line 321 | Line 394 | public class CopyOnWriteArrayListTest ex
394       */
395      public void testRemoveAll() {
396          CopyOnWriteArrayList full = populatedArray(3);
397 <        Vector v = new Vector();
398 <        v.add(one);
399 <        v.add(two);
327 <        full.removeAll(v);
397 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
398 >        assertEquals(1, full.size());
399 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
400          assertEquals(1, full.size());
401      }
402  
# Line 348 | Line 420 | public class CopyOnWriteArrayListTest ex
420      }
421  
422      /**
423 <     * toArray returns an Object array containing all elements from the list
423 >     * toArray() returns an Object array containing all elements from
424 >     * the list in insertion order
425       */
426      public void testToArray() {
427 <        CopyOnWriteArrayList full = populatedArray(3);
428 <        Object[] o = full.toArray();
429 <        assertEquals(3, o.length);
430 <        assertEquals(0, o[0]);
431 <        assertEquals(1, o[1]);
432 <        assertEquals(2, o[2]);
427 >        Object[] a = new CopyOnWriteArrayList().toArray();
428 >        assertTrue(Arrays.equals(new Object[0], a));
429 >        assertSame(Object[].class, a.getClass());
430 >
431 >        Integer[] elements = new Integer[SIZE];
432 >        for (int i = 0; i < SIZE; i++)
433 >            elements[i] = i;
434 >        shuffle(elements);
435 >        Collection<Integer> full = populatedArray(elements);
436 >
437 >        assertTrue(Arrays.equals(elements, full.toArray()));
438 >        assertSame(Object[].class, full.toArray().getClass());
439      }
440  
441      /**
442 <     * toArray returns an Integer array containing all elements from
443 <     * the list
442 >     * toArray(Integer array) returns an Integer array containing all
443 >     * elements from the list in insertion order
444       */
445      public void testToArray2() {
446 <        CopyOnWriteArrayList full = populatedArray(3);
447 <        Integer[] i = new Integer[3];
369 <        i = (Integer[])full.toArray(i);
370 <        assertEquals(3, i.length);
371 <        assertEquals(0, i[0].intValue());
372 <        assertEquals(1, i[1].intValue());
373 <        assertEquals(2, i[2].intValue());
374 <    }
446 >        Collection empty = new CopyOnWriteArrayList();
447 >        Integer[] a;
448  
449 +        a = new Integer[0];
450 +        assertSame(a, empty.toArray(a));
451 +
452 +        a = new Integer[SIZE / 2];
453 +        Arrays.fill(a, 42);
454 +        assertSame(a, empty.toArray(a));
455 +        assertNull(a[0]);
456 +        for (int i = 1; i < a.length; i++)
457 +            assertEquals(42, (int) a[i]);
458 +
459 +        Integer[] elements = new Integer[SIZE];
460 +        for (int i = 0; i < SIZE; i++)
461 +            elements[i] = i;
462 +        shuffle(elements);
463 +        Collection<Integer> full = populatedArray(elements);
464 +
465 +        Arrays.fill(a, 42);
466 +        assertTrue(Arrays.equals(elements, full.toArray(a)));
467 +        for (int i = 0; i < a.length; i++)
468 +            assertEquals(42, (int) a[i]);
469 +        assertSame(Integer[].class, full.toArray(a).getClass());
470 +
471 +        a = new Integer[SIZE];
472 +        Arrays.fill(a, 42);
473 +        assertSame(a, full.toArray(a));
474 +        assertTrue(Arrays.equals(elements, a));
475 +
476 +        a = new Integer[2 * SIZE];
477 +        Arrays.fill(a, 42);
478 +        assertSame(a, full.toArray(a));
479 +        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
480 +        assertNull(a[SIZE]);
481 +        for (int i = SIZE + 1; i < a.length; i++)
482 +            assertEquals(42, (int) a[i]);
483 +    }
484  
485      /**
486       * sublists contains elements at indexes offset from their base
# Line 390 | Line 498 | public class CopyOnWriteArrayListTest ex
498          }
499  
500          List s = a.subList(2, 5);
501 <        assertEquals(s.size(), 3);
501 >        assertEquals(3, s.size());
502          s.set(2, m1);
503          assertEquals(a.get(4), m1);
504          s.clear();
505 <        assertEquals(a.size(), 7);
505 >        assertEquals(7, a.size());
506      }
507  
508      // Exception tests
# Line 404 | Line 512 | public class CopyOnWriteArrayListTest ex
512       * can not store the objects inside the list
513       */
514      public void testToArray_ArrayStoreException() {
515 +        CopyOnWriteArrayList c = new CopyOnWriteArrayList();
516 +        c.add("zfasdfsdf");
517 +        c.add("asdadasd");
518          try {
408            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
409            c.add("zfasdfsdf");
410            c.add("asdadasd");
519              c.toArray(new Long[5]);
520              shouldThrow();
521          } catch (ArrayStoreException success) {}
# Line 417 | Line 525 | public class CopyOnWriteArrayListTest ex
525       * get throws an IndexOutOfBoundsException on a negative index
526       */
527      public void testGet1_IndexOutOfBoundsException() {
528 <        try {
529 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
530 <            c.get(-1);
531 <            shouldThrow();
532 <        } catch (IndexOutOfBoundsException success) {}
528 >        CopyOnWriteArrayList c = populatedArray(5);
529 >        List[] lists = { c, c.subList(1, c.size() - 1) };
530 >        for (List list : lists) {
531 >            try {
532 >                list.get(-1);
533 >                shouldThrow();
534 >            } catch (IndexOutOfBoundsException success) {}
535 >        }
536      }
537  
538      /**
539       * get throws an IndexOutOfBoundsException on a too high index
540       */
541      public void testGet2_IndexOutOfBoundsException() {
542 <        try {
543 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
544 <            c.add("asdasd");
545 <            c.add("asdad");
546 <            c.get(100);
547 <            shouldThrow();
548 <        } catch (IndexOutOfBoundsException success) {}
542 >        CopyOnWriteArrayList c = populatedArray(5);
543 >        List[] lists = { c, c.subList(1, c.size() - 1) };
544 >        for (List list : lists) {
545 >            try {
546 >                list.get(list.size());
547 >                shouldThrow();
548 >            } catch (IndexOutOfBoundsException success) {}
549 >        }
550      }
551  
552      /**
553       * set throws an IndexOutOfBoundsException on a negative index
554       */
555      public void testSet1_IndexOutOfBoundsException() {
556 <        try {
557 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
558 <            c.set(-1,"qwerty");
559 <            shouldThrow();
560 <        } catch (IndexOutOfBoundsException success) {}
556 >        CopyOnWriteArrayList c = populatedArray(5);
557 >        List[] lists = { c, c.subList(1, c.size() - 1) };
558 >        for (List list : lists) {
559 >            try {
560 >                list.set(-1, "qwerty");
561 >                shouldThrow();
562 >            } catch (IndexOutOfBoundsException success) {}
563 >        }
564      }
565  
566      /**
567       * set throws an IndexOutOfBoundsException on a too high index
568       */
569      public void testSet2() {
570 <        try {
571 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
572 <            c.add("asdasd");
573 <            c.add("asdad");
574 <            c.set(100, "qwerty");
575 <            shouldThrow();
576 <        } catch (IndexOutOfBoundsException success) {}
570 >        CopyOnWriteArrayList c = populatedArray(5);
571 >        List[] lists = { c, c.subList(1, c.size() - 1) };
572 >        for (List list : lists) {
573 >            try {
574 >                list.set(list.size(), "qwerty");
575 >                shouldThrow();
576 >            } catch (IndexOutOfBoundsException success) {}
577 >        }
578      }
579  
580      /**
581       * add throws an IndexOutOfBoundsException on a negative index
582       */
583      public void testAdd1_IndexOutOfBoundsException() {
584 <        try {
585 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
586 <            c.add(-1,"qwerty");
587 <            shouldThrow();
588 <        } catch (IndexOutOfBoundsException success) {}
584 >        CopyOnWriteArrayList c = populatedArray(5);
585 >        List[] lists = { c, c.subList(1, c.size() - 1) };
586 >        for (List list : lists) {
587 >            try {
588 >                list.add(-1, "qwerty");
589 >                shouldThrow();
590 >            } catch (IndexOutOfBoundsException success) {}
591 >        }
592      }
593  
594      /**
595       * add throws an IndexOutOfBoundsException on a too high index
596       */
597      public void testAdd2_IndexOutOfBoundsException() {
598 <        try {
599 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
600 <            c.add("asdasd");
601 <            c.add("asdasdasd");
602 <            c.add(100, "qwerty");
603 <            shouldThrow();
604 <        } catch (IndexOutOfBoundsException success) {}
598 >        CopyOnWriteArrayList c = populatedArray(5);
599 >        List[] lists = { c, c.subList(1, c.size() - 1) };
600 >        for (List list : lists) {
601 >            try {
602 >                list.add(list.size() + 1, "qwerty");
603 >                shouldThrow();
604 >            } catch (IndexOutOfBoundsException success) {}
605 >        }
606      }
607  
608      /**
609       * remove throws an IndexOutOfBoundsException on a negative index
610       */
611      public void testRemove1_IndexOutOfBounds() {
612 <        try {
613 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
614 <            c.remove(-1);
615 <            shouldThrow();
616 <        } catch (IndexOutOfBoundsException success) {}
612 >        CopyOnWriteArrayList c = populatedArray(5);
613 >        List[] lists = { c, c.subList(1, c.size() - 1) };
614 >        for (List list : lists) {
615 >            try {
616 >                list.remove(-1);
617 >                shouldThrow();
618 >            } catch (IndexOutOfBoundsException success) {}
619 >        }
620      }
621  
622      /**
623       * remove throws an IndexOutOfBoundsException on a too high index
624       */
625      public void testRemove2_IndexOutOfBounds() {
626 <        try {
627 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
628 <            c.add("asdasd");
629 <            c.add("adasdasd");
630 <            c.remove(100);
631 <            shouldThrow();
632 <        } catch (IndexOutOfBoundsException success) {}
626 >        CopyOnWriteArrayList c = populatedArray(5);
627 >        List[] lists = { c, c.subList(1, c.size() - 1) };
628 >        for (List list : lists) {
629 >            try {
630 >                list.remove(list.size());
631 >                shouldThrow();
632 >            } catch (IndexOutOfBoundsException success) {}
633 >        }
634      }
635  
636      /**
637       * addAll throws an IndexOutOfBoundsException on a negative index
638       */
639      public void testAddAll1_IndexOutOfBoundsException() {
640 <        try {
641 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
642 <            c.addAll(-1,new LinkedList());
643 <            shouldThrow();
644 <        } catch (IndexOutOfBoundsException success) {}
640 >        CopyOnWriteArrayList c = populatedArray(5);
641 >        List[] lists = { c, c.subList(1, c.size() - 1) };
642 >        for (List list : lists) {
643 >            try {
644 >                list.addAll(-1, new LinkedList());
645 >                shouldThrow();
646 >            } catch (IndexOutOfBoundsException success) {}
647 >        }
648      }
649  
650      /**
651       * addAll throws an IndexOutOfBoundsException on a too high index
652       */
653      public void testAddAll2_IndexOutOfBoundsException() {
654 <        try {
655 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
656 <            c.add("asdasd");
657 <            c.add("asdasdasd");
658 <            c.addAll(100, new LinkedList());
659 <            shouldThrow();
660 <        } catch (IndexOutOfBoundsException success) {}
654 >        CopyOnWriteArrayList c = populatedArray(5);
655 >        List[] lists = { c, c.subList(1, c.size() - 1) };
656 >        for (List list : lists) {
657 >            try {
658 >                list.addAll(list.size() + 1, new LinkedList());
659 >                shouldThrow();
660 >            } catch (IndexOutOfBoundsException success) {}
661 >        }
662      }
663  
664      /**
665       * listIterator throws an IndexOutOfBoundsException on a negative index
666       */
667      public void testListIterator1_IndexOutOfBoundsException() {
668 <        try {
669 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
670 <            c.listIterator(-1);
671 <            shouldThrow();
672 <        } catch (IndexOutOfBoundsException success) {}
668 >        CopyOnWriteArrayList c = populatedArray(5);
669 >        List[] lists = { c, c.subList(1, c.size() - 1) };
670 >        for (List list : lists) {
671 >            try {
672 >                list.listIterator(-1);
673 >                shouldThrow();
674 >            } catch (IndexOutOfBoundsException success) {}
675 >        }
676      }
677  
678      /**
679       * listIterator throws an IndexOutOfBoundsException on a too high index
680       */
681      public void testListIterator2_IndexOutOfBoundsException() {
682 <        try {
683 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
684 <            c.add("adasd");
685 <            c.add("asdasdas");
686 <            c.listIterator(100);
687 <            shouldThrow();
688 <        } catch (IndexOutOfBoundsException success) {}
682 >        CopyOnWriteArrayList c = populatedArray(5);
683 >        List[] lists = { c, c.subList(1, c.size() - 1) };
684 >        for (List list : lists) {
685 >            try {
686 >                list.listIterator(list.size() + 1);
687 >                shouldThrow();
688 >            } catch (IndexOutOfBoundsException success) {}
689 >        }
690      }
691  
692      /**
693       * subList throws an IndexOutOfBoundsException on a negative index
694       */
695      public void testSubList1_IndexOutOfBoundsException() {
696 <        try {
697 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
698 <            c.subList(-1,100);
699 <            shouldThrow();
700 <        } catch (IndexOutOfBoundsException success) {}
696 >        CopyOnWriteArrayList c = populatedArray(5);
697 >        List[] lists = { c, c.subList(1, c.size() - 1) };
698 >        for (List list : lists) {
699 >            try {
700 >                list.subList(-1, list.size());
701 >                shouldThrow();
702 >            } catch (IndexOutOfBoundsException success) {}
703 >        }
704      }
705  
706      /**
707       * subList throws an IndexOutOfBoundsException on a too high index
708       */
709      public void testSubList2_IndexOutOfBoundsException() {
710 <        try {
711 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
712 <            c.add("asdasd");
713 <            c.subList(1,100);
714 <            shouldThrow();
715 <        } catch (IndexOutOfBoundsException success) {}
710 >        CopyOnWriteArrayList c = populatedArray(5);
711 >        List[] lists = { c, c.subList(1, c.size() - 1) };
712 >        for (List list : lists) {
713 >            try {
714 >                list.subList(0, list.size() + 1);
715 >                shouldThrow();
716 >            } catch (IndexOutOfBoundsException success) {}
717 >        }
718      }
719  
720      /**
# Line 585 | Line 722 | public class CopyOnWriteArrayListTest ex
722       * is lower then the first
723       */
724      public void testSubList3_IndexOutOfBoundsException() {
725 <        try {
726 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
727 <            c.subList(3,1);
728 <            shouldThrow();
729 <        } catch (IndexOutOfBoundsException success) {}
725 >        CopyOnWriteArrayList c = populatedArray(5);
726 >        List[] lists = { c, c.subList(1, c.size() - 1) };
727 >        for (List list : lists) {
728 >            try {
729 >                list.subList(list.size() - 1, 1);
730 >                shouldThrow();
731 >            } catch (IndexOutOfBoundsException success) {}
732 >        }
733      }
734  
735      /**
736       * a deserialized serialized list is equal
737       */
738      public void testSerialization() throws Exception {
739 <        CopyOnWriteArrayList q = populatedArray(SIZE);
739 >        List x = populatedArray(SIZE);
740 >        List y = serialClone(x);
741  
742 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
743 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
744 <        out.writeObject(q);
745 <        out.close();
746 <
747 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
748 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
749 <        CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
750 <        assertEquals(q.size(), r.size());
751 <        assertTrue(q.equals(r));
752 <        assertTrue(r.equals(q));
742 >        assertNotSame(x, y);
743 >        assertEquals(x.size(), y.size());
744 >        assertEquals(x.toString(), y.toString());
745 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
746 >        assertEquals(x, y);
747 >        assertEquals(y, x);
748 >        while (!x.isEmpty()) {
749 >            assertFalse(y.isEmpty());
750 >            assertEquals(x.remove(0), y.remove(0));
751 >        }
752 >        assertTrue(y.isEmpty());
753      }
754  
755   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines