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.47 by jsr166, Mon Mar 26 21:28:22 2018 UTC vs.
Revision 1.48 by jsr166, Tue Apr 3 00:58:28 2018 UTC

# Line 9 | Line 9
9   import java.util.ArrayList;
10   import java.util.Arrays;
11   import java.util.Collection;
12 + import java.util.Collections;
13   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 + import java.util.concurrent.ThreadLocalRandom;
19  
20   import junit.framework.Test;
21  
# Line 43 | Line 44 | public class CopyOnWriteArrayListTest ex
44                  CollectionTest.testSuite(new SubListImplementation()));
45      }
46  
47 <    static CopyOnWriteArrayList<Integer> populatedArray(int n) {
48 <        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
49 <        assertTrue(a.isEmpty());
47 >    static CopyOnWriteArrayList<Integer> populatedList(int n) {
48 >        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
49 >        assertTrue(list.isEmpty());
50          for (int i = 0; i < n; i++)
51 <            a.add(i);
52 <        assertFalse(a.isEmpty());
53 <        assertEquals(n, a.size());
54 <        return a;
51 >            list.add(i);
52 >        assertEquals(n <= 0, list.isEmpty());
53 >        assertEquals(n, list.size());
54 >        return list;
55      }
56  
57 <    static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) {
58 <        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
59 <        assertTrue(a.isEmpty());
57 >    static CopyOnWriteArrayList<Integer> populatedList(Integer[] elements) {
58 >        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
59 >        assertTrue(list.isEmpty());
60          for (Integer element : elements)
61 <            a.add(element);
62 <        assertFalse(a.isEmpty());
63 <        assertEquals(elements.length, a.size());
64 <        return a;
61 >            list.add(element);
62 >        assertFalse(list.isEmpty());
63 >        assertEquals(elements.length, list.size());
64 >        return list;
65      }
66  
67      /**
68       * a new list is empty
69       */
70      public void testConstructor() {
71 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
72 <        assertTrue(a.isEmpty());
71 >        List list = new CopyOnWriteArrayList();
72 >        assertTrue(list.isEmpty());
73      }
74  
75      /**
76       * new list contains all elements of initializing array
77       */
78      public void testConstructor2() {
79 <        Integer[] ints = new Integer[SIZE];
79 >        Integer[] elts = new Integer[SIZE];
80          for (int i = 0; i < SIZE - 1; ++i)
81 <            ints[i] = new Integer(i);
82 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
81 >            elts[i] = i;
82 >        List list = new CopyOnWriteArrayList(elts);
83          for (int i = 0; i < SIZE; ++i)
84 <            assertEquals(ints[i], a.get(i));
84 >            assertEquals(elts[i], list.get(i));
85      }
86  
87      /**
88       * new list contains all elements of initializing collection
89       */
90      public void testConstructor3() {
91 <        Integer[] ints = new Integer[SIZE];
91 >        Integer[] elts = new Integer[SIZE];
92          for (int i = 0; i < SIZE - 1; ++i)
93 <            ints[i] = new Integer(i);
94 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
93 >            elts[i] = i;
94 >        List list = new CopyOnWriteArrayList(Arrays.asList(elts));
95          for (int i = 0; i < SIZE; ++i)
96 <            assertEquals(ints[i], a.get(i));
96 >            assertEquals(elts[i], list.get(i));
97      }
98  
99      /**
100       * addAll adds each element from the given collection, including duplicates
101       */
102      public void testAddAll() {
103 <        CopyOnWriteArrayList full = populatedArray(3);
104 <        assertTrue(full.addAll(Arrays.asList(three, four, five)));
105 <        assertEquals(6, full.size());
106 <        assertTrue(full.addAll(Arrays.asList(three, four, five)));
107 <        assertEquals(9, full.size());
103 >        List list = populatedList(3);
104 >        assertTrue(list.addAll(Arrays.asList(three, four, five)));
105 >        assertEquals(6, list.size());
106 >        assertTrue(list.addAll(Arrays.asList(three, four, five)));
107 >        assertEquals(9, list.size());
108      }
109  
110      /**
# Line 111 | Line 112 | public class CopyOnWriteArrayListTest ex
112       * already exist in the List
113       */
114      public void testAddAllAbsent() {
115 <        CopyOnWriteArrayList full = populatedArray(3);
115 >        CopyOnWriteArrayList list = populatedList(3);
116          // "one" is duplicate and will not be added
117 <        assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one)));
118 <        assertEquals(5, full.size());
119 <        assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one)));
120 <        assertEquals(5, full.size());
117 >        assertEquals(2, list.addAllAbsent(Arrays.asList(three, four, one)));
118 >        assertEquals(5, list.size());
119 >        assertEquals(0, list.addAllAbsent(Arrays.asList(three, four, one)));
120 >        assertEquals(5, list.size());
121      }
122  
123      /**
124       * addIfAbsent will not add the element if it already exists in the list
125       */
126      public void testAddIfAbsent() {
127 <        CopyOnWriteArrayList full = populatedArray(SIZE);
128 <        full.addIfAbsent(one);
129 <        assertEquals(SIZE, full.size());
127 >        CopyOnWriteArrayList list = populatedList(SIZE);
128 >        list.addIfAbsent(one);
129 >        assertEquals(SIZE, list.size());
130      }
131  
132      /**
133       * addIfAbsent adds the element when it does not exist in the list
134       */
135      public void testAddIfAbsent2() {
136 <        CopyOnWriteArrayList full = populatedArray(SIZE);
137 <        full.addIfAbsent(three);
138 <        assertTrue(full.contains(three));
136 >        CopyOnWriteArrayList list = populatedList(SIZE);
137 >        list.addIfAbsent(three);
138 >        assertTrue(list.contains(three));
139      }
140  
141      /**
142       * clear removes all elements from the list
143       */
144      public void testClear() {
145 <        CopyOnWriteArrayList full = populatedArray(SIZE);
146 <        full.clear();
147 <        assertEquals(0, full.size());
145 >        List list = populatedList(SIZE);
146 >        list.clear();
147 >        assertEquals(0, list.size());
148      }
149  
150      /**
151       * Cloned list is equal
152       */
153      public void testClone() {
154 <        CopyOnWriteArrayList l1 = populatedArray(SIZE);
154 >        CopyOnWriteArrayList l1 = populatedList(SIZE);
155          CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
156          assertEquals(l1, l2);
157          l1.clear();
# Line 161 | Line 162 | public class CopyOnWriteArrayListTest ex
162       * contains is true for added elements
163       */
164      public void testContains() {
165 <        CopyOnWriteArrayList full = populatedArray(3);
166 <        assertTrue(full.contains(one));
167 <        assertFalse(full.contains(five));
165 >        List list = populatedList(3);
166 >        assertTrue(list.contains(one));
167 >        assertFalse(list.contains(five));
168      }
169  
170      /**
171       * adding at an index places it in the indicated index
172       */
173      public void testAddIndex() {
174 <        CopyOnWriteArrayList full = populatedArray(3);
175 <        full.add(0, m1);
176 <        assertEquals(4, full.size());
177 <        assertEquals(m1, full.get(0));
178 <        assertEquals(zero, full.get(1));
179 <
180 <        full.add(2, m2);
181 <        assertEquals(5, full.size());
182 <        assertEquals(m2, full.get(2));
183 <        assertEquals(two, full.get(4));
174 >        List list = populatedList(3);
175 >        list.add(0, m1);
176 >        assertEquals(4, list.size());
177 >        assertEquals(m1, list.get(0));
178 >        assertEquals(zero, list.get(1));
179 >
180 >        list.add(2, m2);
181 >        assertEquals(5, list.size());
182 >        assertEquals(m2, list.get(2));
183 >        assertEquals(two, list.get(4));
184      }
185  
186      /**
187       * lists with same elements are equal and have same hashCode
188       */
189      public void testEquals() {
190 <        CopyOnWriteArrayList a = populatedArray(3);
191 <        CopyOnWriteArrayList b = populatedArray(3);
190 >        List a = populatedList(3);
191 >        List b = populatedList(3);
192          assertTrue(a.equals(b));
193          assertTrue(b.equals(a));
194          assertTrue(a.containsAll(b));
# Line 212 | Line 213 | public class CopyOnWriteArrayListTest ex
213       * containsAll returns true for collections with subset of elements
214       */
215      public void testContainsAll() {
216 <        CopyOnWriteArrayList full = populatedArray(3);
217 <        assertTrue(full.containsAll(Arrays.asList()));
218 <        assertTrue(full.containsAll(Arrays.asList(one)));
219 <        assertTrue(full.containsAll(Arrays.asList(one, two)));
220 <        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
221 <        assertFalse(full.containsAll(Arrays.asList(six)));
216 >        List list = populatedList(3);
217 >        assertTrue(list.containsAll(Arrays.asList()));
218 >        assertTrue(list.containsAll(Arrays.asList(one)));
219 >        assertTrue(list.containsAll(Arrays.asList(one, two)));
220 >        assertFalse(list.containsAll(Arrays.asList(one, two, six)));
221 >        assertFalse(list.containsAll(Arrays.asList(six)));
222  
223          try {
224 <            full.containsAll(null);
224 >            list.containsAll(null);
225              shouldThrow();
226          } catch (NullPointerException success) {}
227      }
# Line 229 | Line 230 | public class CopyOnWriteArrayListTest ex
230       * get returns the value at the given index
231       */
232      public void testGet() {
233 <        CopyOnWriteArrayList full = populatedArray(3);
234 <        assertEquals(0, full.get(0));
233 >        List list = populatedList(3);
234 >        assertEquals(0, list.get(0));
235      }
236  
237      /**
# Line 239 | Line 240 | public class CopyOnWriteArrayListTest ex
240       * contain the element
241       */
242      public void testIndexOf() {
243 <        CopyOnWriteArrayList list = populatedArray(3);
243 >        List list = populatedList(3);
244          assertEquals(-1, list.indexOf(-42));
245          int size = list.size();
246          for (int i = 0; i < size; i++) {
# Line 266 | Line 267 | public class CopyOnWriteArrayListTest ex
267       * or returns -1 if the element is not found
268       */
269      public void testIndexOf2() {
270 <        CopyOnWriteArrayList list = populatedArray(3);
270 >        CopyOnWriteArrayList list = populatedList(3);
271          int size = list.size();
272          assertEquals(-1, list.indexOf(-42, 0));
273  
# Line 296 | Line 297 | public class CopyOnWriteArrayListTest ex
297       * isEmpty returns true when empty, else false
298       */
299      public void testIsEmpty() {
300 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
300 <        CopyOnWriteArrayList full = populatedArray(SIZE);
300 >        List empty = new CopyOnWriteArrayList();
301          assertTrue(empty.isEmpty());
302 +        assertTrue(empty.subList(0, 0).isEmpty());
303 +
304 +        List full = populatedList(SIZE);
305          assertFalse(full.isEmpty());
306 +        assertTrue(full.subList(0, 0).isEmpty());
307 +        assertTrue(full.subList(SIZE, SIZE).isEmpty());
308      }
309  
310      /**
# Line 318 | Line 323 | public class CopyOnWriteArrayListTest ex
323          for (int i = 0; i < SIZE; i++)
324              elements[i] = i;
325          shuffle(elements);
326 <        Collection<Integer> full = populatedArray(elements);
326 >        Collection<Integer> full = populatedList(elements);
327  
328          Iterator it = full.iterator();
329          for (int j = 0; j < SIZE; j++) {
# Line 340 | Line 345 | public class CopyOnWriteArrayListTest ex
345       * iterator.remove throws UnsupportedOperationException
346       */
347      public void testIteratorRemove() {
348 <        CopyOnWriteArrayList full = populatedArray(SIZE);
349 <        Iterator it = full.iterator();
348 >        CopyOnWriteArrayList list = populatedList(SIZE);
349 >        Iterator it = list.iterator();
350          it.next();
351          try {
352              it.remove();
# Line 354 | Line 359 | public class CopyOnWriteArrayListTest ex
359       */
360      public void testToString() {
361          assertEquals("[]", new CopyOnWriteArrayList().toString());
362 <        CopyOnWriteArrayList full = populatedArray(3);
363 <        String s = full.toString();
362 >        List list = populatedList(3);
363 >        String s = list.toString();
364          for (int i = 0; i < 3; ++i)
365              assertTrue(s.contains(String.valueOf(i)));
366 <        assertEquals(new ArrayList(full).toString(),
367 <                     full.toString());
366 >        assertEquals(new ArrayList(list).toString(),
367 >                     list.toString());
368      }
369  
370      /**
# Line 368 | Line 373 | public class CopyOnWriteArrayListTest ex
373       * contain the element
374       */
375      public void testLastIndexOf1() {
376 <        CopyOnWriteArrayList list = populatedArray(3);
376 >        List list = populatedList(3);
377          assertEquals(-1, list.lastIndexOf(-42));
378          int size = list.size();
379          for (int i = 0; i < size; i++) {
# Line 394 | Line 399 | public class CopyOnWriteArrayListTest ex
399       * returns -1 if the element is not found
400       */
401      public void testLastIndexOf2() {
402 <        CopyOnWriteArrayList list = populatedArray(3);
402 >        CopyOnWriteArrayList list = populatedList(3);
403  
404          // we might expect IOOBE, but spec says otherwise
405          assertEquals(-1, list.lastIndexOf(0, -1));
# Line 424 | Line 429 | public class CopyOnWriteArrayListTest ex
429       * listIterator traverses all elements
430       */
431      public void testListIterator1() {
432 <        CopyOnWriteArrayList full = populatedArray(SIZE);
433 <        ListIterator i = full.listIterator();
432 >        List list = populatedList(SIZE);
433 >        ListIterator i = list.listIterator();
434          int j;
435          for (j = 0; i.hasNext(); j++)
436              assertEquals(j, i.next());
# Line 436 | Line 441 | public class CopyOnWriteArrayListTest ex
441       * listIterator only returns those elements after the given index
442       */
443      public void testListIterator2() {
444 <        CopyOnWriteArrayList full = populatedArray(3);
445 <        ListIterator i = full.listIterator(1);
444 >        List list = populatedList(3);
445 >        ListIterator i = list.listIterator(1);
446          int j;
447          for (j = 0; i.hasNext(); j++)
448              assertEquals(j + 1, i.next());
# Line 450 | Line 455 | public class CopyOnWriteArrayListTest ex
455      public void testRemove_int() {
456          int SIZE = 3;
457          for (int i = 0; i < SIZE; i++) {
458 <            CopyOnWriteArrayList full = populatedArray(SIZE);
459 <            assertEquals(i, full.remove(i));
460 <            assertEquals(SIZE - 1, full.size());
461 <            assertFalse(full.contains(new Integer(i)));
458 >            List list = populatedList(SIZE);
459 >            assertEquals(i, list.remove(i));
460 >            assertEquals(SIZE - 1, list.size());
461 >            assertFalse(list.contains(new Integer(i)));
462          }
463      }
464  
# Line 463 | Line 468 | public class CopyOnWriteArrayListTest ex
468      public void testRemove_Object() {
469          int SIZE = 3;
470          for (int i = 0; i < SIZE; i++) {
471 <            CopyOnWriteArrayList full = populatedArray(SIZE);
472 <            assertFalse(full.remove(new Integer(-42)));
473 <            assertTrue(full.remove(new Integer(i)));
474 <            assertEquals(SIZE - 1, full.size());
475 <            assertFalse(full.contains(new Integer(i)));
471 >            List list = populatedList(SIZE);
472 >            assertFalse(list.remove(new Integer(-42)));
473 >            assertTrue(list.remove(new Integer(i)));
474 >            assertEquals(SIZE - 1, list.size());
475 >            assertFalse(list.contains(new Integer(i)));
476          }
477          CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
478          assertTrue(x.remove(new Integer(6)));
# Line 483 | Line 488 | public class CopyOnWriteArrayListTest ex
488       * removeAll removes all elements from the given collection
489       */
490      public void testRemoveAll() {
491 <        CopyOnWriteArrayList full = populatedArray(3);
492 <        assertTrue(full.removeAll(Arrays.asList(one, two)));
493 <        assertEquals(1, full.size());
494 <        assertFalse(full.removeAll(Arrays.asList(one, two)));
495 <        assertEquals(1, full.size());
491 >        List list = populatedList(3);
492 >        assertTrue(list.removeAll(Arrays.asList(one, two)));
493 >        assertEquals(1, list.size());
494 >        assertFalse(list.removeAll(Arrays.asList(one, two)));
495 >        assertEquals(1, list.size());
496      }
497  
498      /**
499       * set changes the element at the given index
500       */
501      public void testSet() {
502 <        CopyOnWriteArrayList full = populatedArray(3);
503 <        assertEquals(2, full.set(2, four));
504 <        assertEquals(4, full.get(2));
502 >        List list = populatedList(3);
503 >        assertEquals(2, list.set(2, four));
504 >        assertEquals(4, list.get(2));
505      }
506  
507      /**
508       * size returns the number of elements
509       */
510      public void testSize() {
511 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
507 <        CopyOnWriteArrayList full = populatedArray(SIZE);
508 <        assertEquals(SIZE, full.size());
511 >        List empty = new CopyOnWriteArrayList();
512          assertEquals(0, empty.size());
513 +        assertEquals(0, empty.subList(0, 0).size());
514 +
515 +        List full = populatedList(SIZE);
516 +        assertEquals(SIZE, full.size());
517 +        assertEquals(0, full.subList(0, 0).size());
518 +        assertEquals(0, full.subList(SIZE, SIZE).size());
519      }
520  
521      /**
# Line 522 | Line 531 | public class CopyOnWriteArrayListTest ex
531          for (int i = 0; i < SIZE; i++)
532              elements[i] = i;
533          shuffle(elements);
534 <        Collection<Integer> full = populatedArray(elements);
534 >        Collection<Integer> full = populatedList(elements);
535  
536          assertTrue(Arrays.equals(elements, full.toArray()));
537          assertSame(Object[].class, full.toArray().getClass());
# Line 550 | Line 559 | public class CopyOnWriteArrayListTest ex
559          for (int i = 0; i < SIZE; i++)
560              elements[i] = i;
561          shuffle(elements);
562 <        Collection<Integer> full = populatedArray(elements);
562 >        Collection<Integer> full = populatedList(elements);
563  
564          Arrays.fill(a, 42);
565          assertTrue(Arrays.equals(elements, full.toArray(a)));
# Line 576 | Line 585 | public class CopyOnWriteArrayListTest ex
585       * sublists contains elements at indexes offset from their base
586       */
587      public void testSubList() {
588 <        CopyOnWriteArrayList a = populatedArray(10);
588 >        List a = populatedList(10);
589          assertTrue(a.subList(1,1).isEmpty());
590          for (int j = 0; j < 9; ++j) {
591              for (int i = j ; i < 10; ++i) {
# Line 607 | Line 616 | public class CopyOnWriteArrayListTest ex
616       * can not store the objects inside the list
617       */
618      public void testToArray_ArrayStoreException() {
619 <        CopyOnWriteArrayList c = new CopyOnWriteArrayList();
620 <        c.add("zfasdfsdf");
621 <        c.add("asdadasd");
622 <        try {
623 <            c.toArray(new Long[5]);
624 <            shouldThrow();
625 <        } catch (ArrayStoreException success) {}
626 <    }
618 <
619 <    /**
620 <     * get throws an IndexOutOfBoundsException on a negative index
621 <     */
622 <    public void testGet1_IndexOutOfBoundsException() {
623 <        CopyOnWriteArrayList c = populatedArray(5);
624 <        List[] lists = { c, c.subList(1, c.size() - 1) };
625 <        for (List list : lists) {
626 <            try {
627 <                list.get(-1);
628 <                shouldThrow();
629 <            } catch (IndexOutOfBoundsException success) {}
630 <        }
631 <    }
632 <
633 <    /**
634 <     * get throws an IndexOutOfBoundsException on a too high index
635 <     */
636 <    public void testGet2_IndexOutOfBoundsException() {
637 <        CopyOnWriteArrayList c = populatedArray(5);
638 <        List[] lists = { c, c.subList(1, c.size() - 1) };
639 <        for (List list : lists) {
640 <            try {
641 <                list.get(list.size());
642 <                shouldThrow();
643 <            } catch (IndexOutOfBoundsException success) {}
644 <        }
645 <    }
646 <
647 <    /**
648 <     * set throws an IndexOutOfBoundsException on a negative index
649 <     */
650 <    public void testSet1_IndexOutOfBoundsException() {
651 <        CopyOnWriteArrayList c = populatedArray(5);
652 <        List[] lists = { c, c.subList(1, c.size() - 1) };
653 <        for (List list : lists) {
654 <            try {
655 <                list.set(-1, "qwerty");
656 <                shouldThrow();
657 <            } catch (IndexOutOfBoundsException success) {}
658 <        }
659 <    }
660 <
661 <    /**
662 <     * set throws an IndexOutOfBoundsException on a too high index
663 <     */
664 <    public void testSet2() {
665 <        CopyOnWriteArrayList c = populatedArray(5);
666 <        List[] lists = { c, c.subList(1, c.size() - 1) };
667 <        for (List list : lists) {
668 <            try {
669 <                list.set(list.size(), "qwerty");
670 <                shouldThrow();
671 <            } catch (IndexOutOfBoundsException success) {}
672 <        }
673 <    }
674 <
675 <    /**
676 <     * add throws an IndexOutOfBoundsException on a negative index
677 <     */
678 <    public void testAdd1_IndexOutOfBoundsException() {
679 <        CopyOnWriteArrayList c = populatedArray(5);
680 <        List[] lists = { c, c.subList(1, c.size() - 1) };
681 <        for (List list : lists) {
682 <            try {
683 <                list.add(-1, "qwerty");
684 <                shouldThrow();
685 <            } catch (IndexOutOfBoundsException success) {}
686 <        }
687 <    }
688 <
689 <    /**
690 <     * add throws an IndexOutOfBoundsException on a too high index
691 <     */
692 <    public void testAdd2_IndexOutOfBoundsException() {
693 <        CopyOnWriteArrayList c = populatedArray(5);
694 <        List[] lists = { c, c.subList(1, c.size() - 1) };
695 <        for (List list : lists) {
696 <            try {
697 <                list.add(list.size() + 1, "qwerty");
698 <                shouldThrow();
699 <            } catch (IndexOutOfBoundsException success) {}
700 <        }
701 <    }
702 <
703 <    /**
704 <     * remove throws an IndexOutOfBoundsException on a negative index
705 <     */
706 <    public void testRemove1_IndexOutOfBounds() {
707 <        CopyOnWriteArrayList c = populatedArray(5);
708 <        List[] lists = { c, c.subList(1, c.size() - 1) };
709 <        for (List list : lists) {
710 <            try {
711 <                list.remove(-1);
712 <                shouldThrow();
713 <            } catch (IndexOutOfBoundsException success) {}
714 <        }
715 <    }
716 <
717 <    /**
718 <     * remove throws an IndexOutOfBoundsException on a too high index
719 <     */
720 <    public void testRemove2_IndexOutOfBounds() {
721 <        CopyOnWriteArrayList c = populatedArray(5);
722 <        List[] lists = { c, c.subList(1, c.size() - 1) };
723 <        for (List list : lists) {
724 <            try {
725 <                list.remove(list.size());
726 <                shouldThrow();
727 <            } catch (IndexOutOfBoundsException success) {}
728 <        }
729 <    }
730 <
731 <    /**
732 <     * addAll throws an IndexOutOfBoundsException on a negative index
733 <     */
734 <    public void testAddAll1_IndexOutOfBoundsException() {
735 <        CopyOnWriteArrayList c = populatedArray(5);
736 <        List[] lists = { c, c.subList(1, c.size() - 1) };
737 <        for (List list : lists) {
738 <            try {
739 <                list.addAll(-1, new LinkedList());
740 <                shouldThrow();
741 <            } catch (IndexOutOfBoundsException success) {}
742 <        }
743 <    }
744 <
745 <    /**
746 <     * addAll throws an IndexOutOfBoundsException on a too high index
747 <     */
748 <    public void testAddAll2_IndexOutOfBoundsException() {
749 <        CopyOnWriteArrayList c = populatedArray(5);
750 <        List[] lists = { c, c.subList(1, c.size() - 1) };
751 <        for (List list : lists) {
752 <            try {
753 <                list.addAll(list.size() + 1, new LinkedList());
754 <                shouldThrow();
755 <            } catch (IndexOutOfBoundsException success) {}
756 <        }
757 <    }
758 <
759 <    /**
760 <     * listIterator throws an IndexOutOfBoundsException on a negative index
761 <     */
762 <    public void testListIterator1_IndexOutOfBoundsException() {
763 <        CopyOnWriteArrayList c = populatedArray(5);
764 <        List[] lists = { c, c.subList(1, c.size() - 1) };
765 <        for (List list : lists) {
766 <            try {
767 <                list.listIterator(-1);
768 <                shouldThrow();
769 <            } catch (IndexOutOfBoundsException success) {}
770 <        }
771 <    }
772 <
773 <    /**
774 <     * listIterator throws an IndexOutOfBoundsException on a too high index
775 <     */
776 <    public void testListIterator2_IndexOutOfBoundsException() {
777 <        CopyOnWriteArrayList c = populatedArray(5);
778 <        List[] lists = { c, c.subList(1, c.size() - 1) };
779 <        for (List list : lists) {
780 <            try {
781 <                list.listIterator(list.size() + 1);
782 <                shouldThrow();
783 <            } catch (IndexOutOfBoundsException success) {}
784 <        }
619 >        List list = new CopyOnWriteArrayList();
620 >        // Integers are not auto-converted to Longs
621 >        list.add(86);
622 >        list.add(99);
623 >        assertThrows(
624 >            ArrayStoreException.class,
625 >            () -> list.toArray(new Long[0]),
626 >            () -> list.toArray(new Long[5]));
627      }
628  
629 <    /**
630 <     * subList throws an IndexOutOfBoundsException on a negative index
631 <     */
632 <    public void testSubList1_IndexOutOfBoundsException() {
633 <        CopyOnWriteArrayList c = populatedArray(5);
634 <        List[] lists = { c, c.subList(1, c.size() - 1) };
635 <        for (List list : lists) {
636 <            try {
637 <                list.subList(-1, list.size());
638 <                shouldThrow();
639 <            } catch (IndexOutOfBoundsException success) {}
640 <        }
629 >    void testIndexOutOfBoundsException(List list) {
630 >        int size = list.size();
631 >        assertThrows(
632 >            IndexOutOfBoundsException.class,
633 >            () -> list.get(-1),
634 >            () -> list.get(size),
635 >            () -> list.set(-1, "qwerty"),
636 >            () -> list.set(size, "qwerty"),
637 >            () -> list.add(-1, "qwerty"),
638 >            () -> list.add(size + 1, "qwerty"),
639 >            () -> list.remove(-1),
640 >            () -> list.remove(size),
641 >            () -> list.addAll(-1, Collections.emptyList()),
642 >            () -> list.addAll(size + 1, Collections.emptyList()),
643 >            () -> list.listIterator(-1),
644 >            () -> list.listIterator(size + 1),
645 >            () -> list.subList(-1, size),
646 >            () -> list.subList(0, size + 1));
647 >
648 >        // Conversely, operations that must not throw
649 >        list.addAll(0, Collections.emptyList());
650 >        list.addAll(size, Collections.emptyList());
651 >        list.add(0, "qwerty");
652 >        list.add(list.size(), "qwerty");
653 >        list.get(0);
654 >        list.get(list.size() - 1);
655 >        list.set(0, "azerty");
656 >        list.set(list.size() - 1, "azerty");
657 >        list.listIterator(0);
658 >        list.listIterator(list.size());
659 >        list.subList(0, list.size());
660 >        list.remove(list.size() - 1);
661      }
662  
663      /**
664 <     * subList throws an IndexOutOfBoundsException on a too high index
665 <     */
666 <    public void testSubList2_IndexOutOfBoundsException() {
667 <        CopyOnWriteArrayList c = populatedArray(5);
668 <        List[] lists = { c, c.subList(1, c.size() - 1) };
669 <        for (List list : lists) {
808 <            try {
809 <                list.subList(0, list.size() + 1);
810 <                shouldThrow();
811 <            } catch (IndexOutOfBoundsException success) {}
812 <        }
813 <    }
664 >     * IndexOutOfBoundsException is thrown when specified
665 >     */
666 >    public void testIndexOutOfBoundsException() {
667 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
668 >        List x = populatedList(rnd.nextInt(5));
669 >        testIndexOutOfBoundsException(x);
670  
671 <    /**
672 <     * subList throws IndexOutOfBoundsException when the second index
673 <     * is lower then the first
674 <     */
675 <    public void testSubList3_IndexOutOfBoundsException() {
676 <        CopyOnWriteArrayList c = populatedArray(5);
677 <        List[] lists = { c, c.subList(1, c.size() - 1) };
822 <        for (List list : lists) {
823 <            try {
824 <                list.subList(list.size() - 1, 1);
825 <                shouldThrow();
826 <            } catch (IndexOutOfBoundsException success) {}
827 <        }
671 >        int start = rnd.nextInt(x.size() + 1);
672 >        int end = rnd.nextInt(start, x.size() + 1);
673 >        assertThrows(
674 >            IndexOutOfBoundsException.class,
675 >            () -> x.subList(start, start - 1));
676 >        List subList = x.subList(start, end);
677 >        testIndexOutOfBoundsException(x);
678      }
679  
680      /**
681       * a deserialized/reserialized list equals original
682       */
683      public void testSerialization() throws Exception {
684 <        List x = populatedArray(SIZE);
684 >        List x = populatedList(SIZE);
685          List y = serialClone(x);
686  
687          assertNotSame(x, y);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines