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.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.*;
9 > import java.util.ArrayList;
10   import java.util.Arrays;
11   import java.util.Collection;
12 import java.util.Collections;
12   import java.util.Iterator;
13   import java.util.LinkedList;
14   import java.util.List;
15   import java.util.ListIterator;
16 < import java.util.Vector;
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() {
# Line 60 | 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 72 | 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)
# Line 80 | Line 82 | public class CopyOnWriteArrayListTest ex
82      }
83  
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();
88 <        v.add(three);
89 <        v.add(four);
90 <        v.add(five);
91 <        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 98 | 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
105 <        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 177 | 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());
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 238 | 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 266 | 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 315 | 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 333 | 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);
339 <        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 371 | Line 431 | public class CopyOnWriteArrayListTest ex
431          Integer[] elements = new Integer[SIZE];
432          for (int i = 0; i < SIZE; i++)
433              elements[i] = i;
434 <        Collections.shuffle(Arrays.asList(elements));
434 >        shuffle(elements);
435          Collection<Integer> full = populatedArray(elements);
436  
437          assertTrue(Arrays.equals(elements, full.toArray()));
# Line 389 | Line 449 | public class CopyOnWriteArrayListTest ex
449          a = new Integer[0];
450          assertSame(a, empty.toArray(a));
451  
452 <        a = new Integer[SIZE/2];
452 >        a = new Integer[SIZE / 2];
453          Arrays.fill(a, 42);
454          assertSame(a, empty.toArray(a));
455          assertNull(a[0]);
# Line 399 | Line 459 | public class CopyOnWriteArrayListTest ex
459          Integer[] elements = new Integer[SIZE];
460          for (int i = 0; i < SIZE; i++)
461              elements[i] = i;
462 <        Collections.shuffle(Arrays.asList(elements));
462 >        shuffle(elements);
463          Collection<Integer> full = populatedArray(elements);
464  
465          Arrays.fill(a, 42);
# Line 413 | Line 473 | public class CopyOnWriteArrayListTest ex
473          assertSame(a, full.toArray(a));
474          assertTrue(Arrays.equals(elements, a));
475  
476 <        a = new Integer[2*SIZE];
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)));
# Line 452 | 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 {
456            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
457            c.add("zfasdfsdf");
458            c.add("asdadasd");
519              c.toArray(new Long[5]);
520              shouldThrow();
521          } catch (ArrayStoreException success) {}
# Line 465 | 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 633 | 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      /**
# Line 647 | Line 739 | public class CopyOnWriteArrayListTest ex
739          List x = populatedArray(SIZE);
740          List y = serialClone(x);
741  
742 <        assertTrue(x != y);
742 >        assertNotSame(x, y);
743          assertEquals(x.size(), y.size());
744          assertEquals(x.toString(), y.toString());
745          assertTrue(Arrays.equals(x.toArray(), y.toArray()));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines