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.49 by jsr166, Tue Apr 3 05:49:43 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 34 | Line 35 | public class CopyOnWriteArrayListTest ex
35          }
36          class SubListImplementation extends Implementation {
37              public List emptyCollection() {
38 <                return super.emptyCollection().subList(0, 0);
38 >                List list = super.emptyCollection();
39 >                ThreadLocalRandom rnd = ThreadLocalRandom.current();
40 >                if (rnd.nextBoolean())
41 >                    list.add(makeElement(rnd.nextInt()));
42 >                int i = rnd.nextInt(list.size() + 1);
43 >                return list.subList(i, i);
44              }
45          }
46          return newTestSuite(
# Line 43 | Line 49 | public class CopyOnWriteArrayListTest ex
49                  CollectionTest.testSuite(new SubListImplementation()));
50      }
51  
52 <    static CopyOnWriteArrayList<Integer> populatedArray(int n) {
53 <        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
54 <        assertTrue(a.isEmpty());
52 >    static CopyOnWriteArrayList<Integer> populatedList(int n) {
53 >        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
54 >        assertTrue(list.isEmpty());
55          for (int i = 0; i < n; i++)
56 <            a.add(i);
57 <        assertFalse(a.isEmpty());
58 <        assertEquals(n, a.size());
59 <        return a;
56 >            list.add(i);
57 >        assertEquals(n <= 0, list.isEmpty());
58 >        assertEquals(n, list.size());
59 >        return list;
60      }
61  
62 <    static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) {
63 <        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
64 <        assertTrue(a.isEmpty());
62 >    static CopyOnWriteArrayList<Integer> populatedList(Integer[] elements) {
63 >        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
64 >        assertTrue(list.isEmpty());
65          for (Integer element : elements)
66 <            a.add(element);
67 <        assertFalse(a.isEmpty());
68 <        assertEquals(elements.length, a.size());
69 <        return a;
66 >            list.add(element);
67 >        assertFalse(list.isEmpty());
68 >        assertEquals(elements.length, list.size());
69 >        return list;
70      }
71  
72      /**
73       * a new list is empty
74       */
75      public void testConstructor() {
76 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
77 <        assertTrue(a.isEmpty());
76 >        List list = new CopyOnWriteArrayList();
77 >        assertTrue(list.isEmpty());
78      }
79  
80      /**
81       * new list contains all elements of initializing array
82       */
83      public void testConstructor2() {
84 <        Integer[] ints = new Integer[SIZE];
84 >        Integer[] elts = new Integer[SIZE];
85          for (int i = 0; i < SIZE - 1; ++i)
86 <            ints[i] = new Integer(i);
87 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
86 >            elts[i] = i;
87 >        List list = new CopyOnWriteArrayList(elts);
88          for (int i = 0; i < SIZE; ++i)
89 <            assertEquals(ints[i], a.get(i));
89 >            assertEquals(elts[i], list.get(i));
90      }
91  
92      /**
93       * new list contains all elements of initializing collection
94       */
95      public void testConstructor3() {
96 <        Integer[] ints = new Integer[SIZE];
96 >        Integer[] elts = new Integer[SIZE];
97          for (int i = 0; i < SIZE - 1; ++i)
98 <            ints[i] = new Integer(i);
99 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
98 >            elts[i] = i;
99 >        List list = new CopyOnWriteArrayList(Arrays.asList(elts));
100          for (int i = 0; i < SIZE; ++i)
101 <            assertEquals(ints[i], a.get(i));
101 >            assertEquals(elts[i], list.get(i));
102      }
103  
104      /**
105       * addAll adds each element from the given collection, including duplicates
106       */
107      public void testAddAll() {
108 <        CopyOnWriteArrayList full = populatedArray(3);
109 <        assertTrue(full.addAll(Arrays.asList(three, four, five)));
110 <        assertEquals(6, full.size());
111 <        assertTrue(full.addAll(Arrays.asList(three, four, five)));
112 <        assertEquals(9, full.size());
108 >        List list = populatedList(3);
109 >        assertTrue(list.addAll(Arrays.asList(three, four, five)));
110 >        assertEquals(6, list.size());
111 >        assertTrue(list.addAll(Arrays.asList(three, four, five)));
112 >        assertEquals(9, list.size());
113      }
114  
115      /**
# Line 111 | Line 117 | public class CopyOnWriteArrayListTest ex
117       * already exist in the List
118       */
119      public void testAddAllAbsent() {
120 <        CopyOnWriteArrayList full = populatedArray(3);
120 >        CopyOnWriteArrayList list = populatedList(3);
121          // "one" is duplicate and will not be added
122 <        assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one)));
123 <        assertEquals(5, full.size());
124 <        assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one)));
125 <        assertEquals(5, full.size());
122 >        assertEquals(2, list.addAllAbsent(Arrays.asList(three, four, one)));
123 >        assertEquals(5, list.size());
124 >        assertEquals(0, list.addAllAbsent(Arrays.asList(three, four, one)));
125 >        assertEquals(5, list.size());
126      }
127  
128      /**
129       * addIfAbsent will not add the element if it already exists in the list
130       */
131      public void testAddIfAbsent() {
132 <        CopyOnWriteArrayList full = populatedArray(SIZE);
133 <        full.addIfAbsent(one);
134 <        assertEquals(SIZE, full.size());
132 >        CopyOnWriteArrayList list = populatedList(SIZE);
133 >        list.addIfAbsent(one);
134 >        assertEquals(SIZE, list.size());
135      }
136  
137      /**
138       * addIfAbsent adds the element when it does not exist in the list
139       */
140      public void testAddIfAbsent2() {
141 <        CopyOnWriteArrayList full = populatedArray(SIZE);
142 <        full.addIfAbsent(three);
143 <        assertTrue(full.contains(three));
141 >        CopyOnWriteArrayList list = populatedList(SIZE);
142 >        list.addIfAbsent(three);
143 >        assertTrue(list.contains(three));
144      }
145  
146      /**
147       * clear removes all elements from the list
148       */
149      public void testClear() {
150 <        CopyOnWriteArrayList full = populatedArray(SIZE);
151 <        full.clear();
152 <        assertEquals(0, full.size());
150 >        List list = populatedList(SIZE);
151 >        list.clear();
152 >        assertEquals(0, list.size());
153      }
154  
155      /**
156       * Cloned list is equal
157       */
158      public void testClone() {
159 <        CopyOnWriteArrayList l1 = populatedArray(SIZE);
159 >        CopyOnWriteArrayList l1 = populatedList(SIZE);
160          CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
161          assertEquals(l1, l2);
162          l1.clear();
# Line 161 | Line 167 | public class CopyOnWriteArrayListTest ex
167       * contains is true for added elements
168       */
169      public void testContains() {
170 <        CopyOnWriteArrayList full = populatedArray(3);
171 <        assertTrue(full.contains(one));
172 <        assertFalse(full.contains(five));
170 >        List list = populatedList(3);
171 >        assertTrue(list.contains(one));
172 >        assertFalse(list.contains(five));
173      }
174  
175      /**
176       * adding at an index places it in the indicated index
177       */
178      public void testAddIndex() {
179 <        CopyOnWriteArrayList full = populatedArray(3);
180 <        full.add(0, m1);
181 <        assertEquals(4, full.size());
182 <        assertEquals(m1, full.get(0));
183 <        assertEquals(zero, full.get(1));
184 <
185 <        full.add(2, m2);
186 <        assertEquals(5, full.size());
187 <        assertEquals(m2, full.get(2));
188 <        assertEquals(two, full.get(4));
179 >        List list = populatedList(3);
180 >        list.add(0, m1);
181 >        assertEquals(4, list.size());
182 >        assertEquals(m1, list.get(0));
183 >        assertEquals(zero, list.get(1));
184 >
185 >        list.add(2, m2);
186 >        assertEquals(5, list.size());
187 >        assertEquals(m2, list.get(2));
188 >        assertEquals(two, list.get(4));
189      }
190  
191      /**
192       * lists with same elements are equal and have same hashCode
193       */
194      public void testEquals() {
195 <        CopyOnWriteArrayList a = populatedArray(3);
196 <        CopyOnWriteArrayList b = populatedArray(3);
195 >        List a = populatedList(3);
196 >        List b = populatedList(3);
197          assertTrue(a.equals(b));
198          assertTrue(b.equals(a));
199          assertTrue(a.containsAll(b));
# Line 212 | Line 218 | public class CopyOnWriteArrayListTest ex
218       * containsAll returns true for collections with subset of elements
219       */
220      public void testContainsAll() {
221 <        CopyOnWriteArrayList full = populatedArray(3);
222 <        assertTrue(full.containsAll(Arrays.asList()));
223 <        assertTrue(full.containsAll(Arrays.asList(one)));
224 <        assertTrue(full.containsAll(Arrays.asList(one, two)));
225 <        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
226 <        assertFalse(full.containsAll(Arrays.asList(six)));
221 >        List list = populatedList(3);
222 >        assertTrue(list.containsAll(Arrays.asList()));
223 >        assertTrue(list.containsAll(Arrays.asList(one)));
224 >        assertTrue(list.containsAll(Arrays.asList(one, two)));
225 >        assertFalse(list.containsAll(Arrays.asList(one, two, six)));
226 >        assertFalse(list.containsAll(Arrays.asList(six)));
227  
228          try {
229 <            full.containsAll(null);
229 >            list.containsAll(null);
230              shouldThrow();
231          } catch (NullPointerException success) {}
232      }
# Line 229 | Line 235 | public class CopyOnWriteArrayListTest ex
235       * get returns the value at the given index
236       */
237      public void testGet() {
238 <        CopyOnWriteArrayList full = populatedArray(3);
239 <        assertEquals(0, full.get(0));
238 >        List list = populatedList(3);
239 >        assertEquals(0, list.get(0));
240      }
241  
242      /**
# Line 239 | Line 245 | public class CopyOnWriteArrayListTest ex
245       * contain the element
246       */
247      public void testIndexOf() {
248 <        CopyOnWriteArrayList list = populatedArray(3);
248 >        List list = populatedList(3);
249          assertEquals(-1, list.indexOf(-42));
250          int size = list.size();
251          for (int i = 0; i < size; i++) {
# Line 266 | Line 272 | public class CopyOnWriteArrayListTest ex
272       * or returns -1 if the element is not found
273       */
274      public void testIndexOf2() {
275 <        CopyOnWriteArrayList list = populatedArray(3);
275 >        CopyOnWriteArrayList list = populatedList(3);
276          int size = list.size();
277          assertEquals(-1, list.indexOf(-42, 0));
278  
# Line 296 | Line 302 | public class CopyOnWriteArrayListTest ex
302       * isEmpty returns true when empty, else false
303       */
304      public void testIsEmpty() {
305 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
300 <        CopyOnWriteArrayList full = populatedArray(SIZE);
305 >        List empty = new CopyOnWriteArrayList();
306          assertTrue(empty.isEmpty());
307 +        assertTrue(empty.subList(0, 0).isEmpty());
308 +
309 +        List full = populatedList(SIZE);
310          assertFalse(full.isEmpty());
311 +        assertTrue(full.subList(0, 0).isEmpty());
312 +        assertTrue(full.subList(SIZE, SIZE).isEmpty());
313      }
314  
315      /**
# Line 318 | Line 328 | public class CopyOnWriteArrayListTest ex
328          for (int i = 0; i < SIZE; i++)
329              elements[i] = i;
330          shuffle(elements);
331 <        Collection<Integer> full = populatedArray(elements);
331 >        Collection<Integer> full = populatedList(elements);
332  
333          Iterator it = full.iterator();
334          for (int j = 0; j < SIZE; j++) {
# Line 340 | Line 350 | public class CopyOnWriteArrayListTest ex
350       * iterator.remove throws UnsupportedOperationException
351       */
352      public void testIteratorRemove() {
353 <        CopyOnWriteArrayList full = populatedArray(SIZE);
354 <        Iterator it = full.iterator();
353 >        CopyOnWriteArrayList list = populatedList(SIZE);
354 >        Iterator it = list.iterator();
355          it.next();
356          try {
357              it.remove();
# Line 354 | Line 364 | public class CopyOnWriteArrayListTest ex
364       */
365      public void testToString() {
366          assertEquals("[]", new CopyOnWriteArrayList().toString());
367 <        CopyOnWriteArrayList full = populatedArray(3);
368 <        String s = full.toString();
367 >        List list = populatedList(3);
368 >        String s = list.toString();
369          for (int i = 0; i < 3; ++i)
370              assertTrue(s.contains(String.valueOf(i)));
371 <        assertEquals(new ArrayList(full).toString(),
372 <                     full.toString());
371 >        assertEquals(new ArrayList(list).toString(),
372 >                     list.toString());
373      }
374  
375      /**
# Line 368 | Line 378 | public class CopyOnWriteArrayListTest ex
378       * contain the element
379       */
380      public void testLastIndexOf1() {
381 <        CopyOnWriteArrayList list = populatedArray(3);
381 >        List list = populatedList(3);
382          assertEquals(-1, list.lastIndexOf(-42));
383          int size = list.size();
384          for (int i = 0; i < size; i++) {
# Line 394 | Line 404 | public class CopyOnWriteArrayListTest ex
404       * returns -1 if the element is not found
405       */
406      public void testLastIndexOf2() {
407 <        CopyOnWriteArrayList list = populatedArray(3);
407 >        CopyOnWriteArrayList list = populatedList(3);
408  
409          // we might expect IOOBE, but spec says otherwise
410          assertEquals(-1, list.lastIndexOf(0, -1));
# Line 424 | Line 434 | public class CopyOnWriteArrayListTest ex
434       * listIterator traverses all elements
435       */
436      public void testListIterator1() {
437 <        CopyOnWriteArrayList full = populatedArray(SIZE);
438 <        ListIterator i = full.listIterator();
437 >        List list = populatedList(SIZE);
438 >        ListIterator i = list.listIterator();
439          int j;
440          for (j = 0; i.hasNext(); j++)
441              assertEquals(j, i.next());
# Line 436 | Line 446 | public class CopyOnWriteArrayListTest ex
446       * listIterator only returns those elements after the given index
447       */
448      public void testListIterator2() {
449 <        CopyOnWriteArrayList full = populatedArray(3);
450 <        ListIterator i = full.listIterator(1);
449 >        List list = populatedList(3);
450 >        ListIterator i = list.listIterator(1);
451          int j;
452          for (j = 0; i.hasNext(); j++)
453              assertEquals(j + 1, i.next());
# Line 450 | Line 460 | public class CopyOnWriteArrayListTest ex
460      public void testRemove_int() {
461          int SIZE = 3;
462          for (int i = 0; i < SIZE; i++) {
463 <            CopyOnWriteArrayList full = populatedArray(SIZE);
464 <            assertEquals(i, full.remove(i));
465 <            assertEquals(SIZE - 1, full.size());
466 <            assertFalse(full.contains(new Integer(i)));
463 >            List list = populatedList(SIZE);
464 >            assertEquals(i, list.remove(i));
465 >            assertEquals(SIZE - 1, list.size());
466 >            assertFalse(list.contains(new Integer(i)));
467          }
468      }
469  
# Line 463 | Line 473 | public class CopyOnWriteArrayListTest ex
473      public void testRemove_Object() {
474          int SIZE = 3;
475          for (int i = 0; i < SIZE; i++) {
476 <            CopyOnWriteArrayList full = populatedArray(SIZE);
477 <            assertFalse(full.remove(new Integer(-42)));
478 <            assertTrue(full.remove(new Integer(i)));
479 <            assertEquals(SIZE - 1, full.size());
480 <            assertFalse(full.contains(new Integer(i)));
476 >            List list = populatedList(SIZE);
477 >            assertFalse(list.remove(new Integer(-42)));
478 >            assertTrue(list.remove(new Integer(i)));
479 >            assertEquals(SIZE - 1, list.size());
480 >            assertFalse(list.contains(new Integer(i)));
481          }
482          CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
483          assertTrue(x.remove(new Integer(6)));
# Line 483 | Line 493 | public class CopyOnWriteArrayListTest ex
493       * removeAll removes all elements from the given collection
494       */
495      public void testRemoveAll() {
496 <        CopyOnWriteArrayList full = populatedArray(3);
497 <        assertTrue(full.removeAll(Arrays.asList(one, two)));
498 <        assertEquals(1, full.size());
499 <        assertFalse(full.removeAll(Arrays.asList(one, two)));
500 <        assertEquals(1, full.size());
496 >        List list = populatedList(3);
497 >        assertTrue(list.removeAll(Arrays.asList(one, two)));
498 >        assertEquals(1, list.size());
499 >        assertFalse(list.removeAll(Arrays.asList(one, two)));
500 >        assertEquals(1, list.size());
501      }
502  
503      /**
504       * set changes the element at the given index
505       */
506      public void testSet() {
507 <        CopyOnWriteArrayList full = populatedArray(3);
508 <        assertEquals(2, full.set(2, four));
509 <        assertEquals(4, full.get(2));
507 >        List list = populatedList(3);
508 >        assertEquals(2, list.set(2, four));
509 >        assertEquals(4, list.get(2));
510      }
511  
512      /**
513       * size returns the number of elements
514       */
515      public void testSize() {
516 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
507 <        CopyOnWriteArrayList full = populatedArray(SIZE);
508 <        assertEquals(SIZE, full.size());
516 >        List empty = new CopyOnWriteArrayList();
517          assertEquals(0, empty.size());
518 +        assertEquals(0, empty.subList(0, 0).size());
519 +
520 +        List full = populatedList(SIZE);
521 +        assertEquals(SIZE, full.size());
522 +        assertEquals(0, full.subList(0, 0).size());
523 +        assertEquals(0, full.subList(SIZE, SIZE).size());
524      }
525  
526      /**
# Line 522 | Line 536 | public class CopyOnWriteArrayListTest ex
536          for (int i = 0; i < SIZE; i++)
537              elements[i] = i;
538          shuffle(elements);
539 <        Collection<Integer> full = populatedArray(elements);
539 >        Collection<Integer> full = populatedList(elements);
540  
541          assertTrue(Arrays.equals(elements, full.toArray()));
542          assertSame(Object[].class, full.toArray().getClass());
# Line 550 | Line 564 | public class CopyOnWriteArrayListTest ex
564          for (int i = 0; i < SIZE; i++)
565              elements[i] = i;
566          shuffle(elements);
567 <        Collection<Integer> full = populatedArray(elements);
567 >        Collection<Integer> full = populatedList(elements);
568  
569          Arrays.fill(a, 42);
570          assertTrue(Arrays.equals(elements, full.toArray(a)));
# Line 576 | Line 590 | public class CopyOnWriteArrayListTest ex
590       * sublists contains elements at indexes offset from their base
591       */
592      public void testSubList() {
593 <        CopyOnWriteArrayList a = populatedArray(10);
593 >        List a = populatedList(10);
594          assertTrue(a.subList(1,1).isEmpty());
595          for (int j = 0; j < 9; ++j) {
596              for (int i = j ; i < 10; ++i) {
# Line 607 | Line 621 | public class CopyOnWriteArrayListTest ex
621       * can not store the objects inside the list
622       */
623      public void testToArray_ArrayStoreException() {
624 <        CopyOnWriteArrayList c = new CopyOnWriteArrayList();
625 <        c.add("zfasdfsdf");
626 <        c.add("asdadasd");
627 <        try {
628 <            c.toArray(new Long[5]);
629 <            shouldThrow();
630 <        } catch (ArrayStoreException success) {}
631 <    }
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 <        }
624 >        List list = new CopyOnWriteArrayList();
625 >        // Integers are not auto-converted to Longs
626 >        list.add(86);
627 >        list.add(99);
628 >        assertThrows(
629 >            ArrayStoreException.class,
630 >            () -> list.toArray(new Long[0]),
631 >            () -> list.toArray(new Long[5]));
632      }
633  
634 <    /**
635 <     * subList throws an IndexOutOfBoundsException on a negative index
636 <     */
637 <    public void testSubList1_IndexOutOfBoundsException() {
638 <        CopyOnWriteArrayList c = populatedArray(5);
639 <        List[] lists = { c, c.subList(1, c.size() - 1) };
640 <        for (List list : lists) {
641 <            try {
642 <                list.subList(-1, list.size());
643 <                shouldThrow();
644 <            } catch (IndexOutOfBoundsException success) {}
645 <        }
634 >    void testIndexOutOfBoundsException(List list) {
635 >        int size = list.size();
636 >        assertThrows(
637 >            IndexOutOfBoundsException.class,
638 >            () -> list.get(-1),
639 >            () -> list.get(size),
640 >            () -> list.set(-1, "qwerty"),
641 >            () -> list.set(size, "qwerty"),
642 >            () -> list.add(-1, "qwerty"),
643 >            () -> list.add(size + 1, "qwerty"),
644 >            () -> list.remove(-1),
645 >            () -> list.remove(size),
646 >            () -> list.addAll(-1, Collections.emptyList()),
647 >            () -> list.addAll(size + 1, Collections.emptyList()),
648 >            () -> list.listIterator(-1),
649 >            () -> list.listIterator(size + 1),
650 >            () -> list.subList(-1, size),
651 >            () -> list.subList(0, size + 1));
652 >
653 >        // Conversely, operations that must not throw
654 >        list.addAll(0, Collections.emptyList());
655 >        list.addAll(size, Collections.emptyList());
656 >        list.add(0, "qwerty");
657 >        list.add(list.size(), "qwerty");
658 >        list.get(0);
659 >        list.get(list.size() - 1);
660 >        list.set(0, "azerty");
661 >        list.set(list.size() - 1, "azerty");
662 >        list.listIterator(0);
663 >        list.listIterator(list.size());
664 >        list.subList(0, list.size());
665 >        list.remove(list.size() - 1);
666      }
667  
668      /**
669 <     * subList throws an IndexOutOfBoundsException on a too high index
670 <     */
671 <    public void testSubList2_IndexOutOfBoundsException() {
672 <        CopyOnWriteArrayList c = populatedArray(5);
673 <        List[] lists = { c, c.subList(1, c.size() - 1) };
674 <        for (List list : lists) {
808 <            try {
809 <                list.subList(0, list.size() + 1);
810 <                shouldThrow();
811 <            } catch (IndexOutOfBoundsException success) {}
812 <        }
813 <    }
669 >     * IndexOutOfBoundsException is thrown when specified
670 >     */
671 >    public void testIndexOutOfBoundsException() {
672 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
673 >        List x = populatedList(rnd.nextInt(5));
674 >        testIndexOutOfBoundsException(x);
675  
676 <    /**
677 <     * subList throws IndexOutOfBoundsException when the second index
678 <     * is lower then the first
679 <     */
680 <    public void testSubList3_IndexOutOfBoundsException() {
681 <        CopyOnWriteArrayList c = populatedArray(5);
682 <        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 <        }
676 >        int start = rnd.nextInt(x.size() + 1);
677 >        int end = rnd.nextInt(start, x.size() + 1);
678 >        assertThrows(
679 >            IndexOutOfBoundsException.class,
680 >            () -> x.subList(start, start - 1));
681 >        List subList = x.subList(start, end);
682 >        testIndexOutOfBoundsException(x);
683      }
684  
685      /**
686       * a deserialized/reserialized list equals original
687       */
688      public void testSerialization() throws Exception {
689 <        List x = populatedArray(SIZE);
689 >        List x = populatedList(SIZE);
690          List y = serialClone(x);
691  
692          assertNotSame(x, y);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines