ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.49
Committed: Tue Apr 3 05:49:43 2018 UTC (6 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.48: +6 -1 lines
Log Message:
add more randomness to sublist testing

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.6 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.19 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.8 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.27 import java.util.ArrayList;
10 jsr166 1.22 import java.util.Arrays;
11 jsr166 1.26 import java.util.Collection;
12 jsr166 1.48 import java.util.Collections;
13 jsr166 1.22 import java.util.Iterator;
14     import java.util.List;
15     import java.util.ListIterator;
16 jsr166 1.27 import java.util.NoSuchElementException;
17 jsr166 1.22 import java.util.concurrent.CopyOnWriteArrayList;
18 jsr166 1.48 import java.util.concurrent.ThreadLocalRandom;
19 dl 1.1
20 jsr166 1.31 import junit.framework.Test;
21    
22 jsr166 1.10 public class CopyOnWriteArrayListTest extends JSR166TestCase {
23 jsr166 1.8
24 dl 1.1 public static void main(String[] args) {
25 jsr166 1.34 main(suite(), args);
26 dl 1.1 }
27    
28     public static Test suite() {
29 jsr166 1.40 class Implementation implements CollectionImplementation {
30 jsr166 1.43 public Class<?> klazz() { return CopyOnWriteArrayList.class; }
31 jsr166 1.40 public List emptyCollection() { return new CopyOnWriteArrayList(); }
32     public Object makeElement(int i) { return i; }
33     public boolean isConcurrent() { return true; }
34     public boolean permitsNulls() { return true; }
35     }
36     class SubListImplementation extends Implementation {
37     public List emptyCollection() {
38 jsr166 1.49 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 jsr166 1.40 }
45     }
46     return newTestSuite(
47     CopyOnWriteArrayListTest.class,
48 jsr166 1.41 CollectionTest.testSuite(new Implementation()),
49     CollectionTest.testSuite(new SubListImplementation()));
50 dl 1.1 }
51    
52 jsr166 1.48 static CopyOnWriteArrayList<Integer> populatedList(int n) {
53     CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
54     assertTrue(list.isEmpty());
55 jsr166 1.26 for (int i = 0; i < n; i++)
56 jsr166 1.48 list.add(i);
57     assertEquals(n <= 0, list.isEmpty());
58     assertEquals(n, list.size());
59     return list;
60 dl 1.1 }
61    
62 jsr166 1.48 static CopyOnWriteArrayList<Integer> populatedList(Integer[] elements) {
63     CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
64     assertTrue(list.isEmpty());
65 jsr166 1.46 for (Integer element : elements)
66 jsr166 1.48 list.add(element);
67     assertFalse(list.isEmpty());
68     assertEquals(elements.length, list.size());
69     return list;
70 jsr166 1.26 }
71    
72 dl 1.4 /**
73 dl 1.5 * a new list is empty
74 dl 1.4 */
75 dl 1.3 public void testConstructor() {
76 jsr166 1.48 List list = new CopyOnWriteArrayList();
77     assertTrue(list.isEmpty());
78 dl 1.3 }
79    
80 dl 1.4 /**
81 dl 1.5 * new list contains all elements of initializing array
82 dl 1.4 */
83 dl 1.3 public void testConstructor2() {
84 jsr166 1.48 Integer[] elts = new Integer[SIZE];
85 jsr166 1.35 for (int i = 0; i < SIZE - 1; ++i)
86 jsr166 1.48 elts[i] = i;
87     List list = new CopyOnWriteArrayList(elts);
88 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
89 jsr166 1.48 assertEquals(elts[i], list.get(i));
90 dl 1.3 }
91    
92 dl 1.4 /**
93 dl 1.5 * new list contains all elements of initializing collection
94 dl 1.4 */
95 dl 1.3 public void testConstructor3() {
96 jsr166 1.48 Integer[] elts = new Integer[SIZE];
97 jsr166 1.35 for (int i = 0; i < SIZE - 1; ++i)
98 jsr166 1.48 elts[i] = i;
99     List list = new CopyOnWriteArrayList(Arrays.asList(elts));
100 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
101 jsr166 1.48 assertEquals(elts[i], list.get(i));
102 dl 1.3 }
103 jsr166 1.8
104 dl 1.1 /**
105 jsr166 1.33 * addAll adds each element from the given collection, including duplicates
106 dl 1.1 */
107 dl 1.4 public void testAddAll() {
108 jsr166 1.48 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 dl 1.1 }
114    
115     /**
116 jsr166 1.18 * addAllAbsent adds each element from the given collection that did not
117     * already exist in the List
118 dl 1.1 */
119 dl 1.4 public void testAddAllAbsent() {
120 jsr166 1.48 CopyOnWriteArrayList list = populatedList(3);
121 jsr166 1.33 // "one" is duplicate and will not be added
122 jsr166 1.48 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 dl 1.1 }
127    
128     /**
129 jsr166 1.18 * addIfAbsent will not add the element if it already exists in the list
130 dl 1.1 */
131 dl 1.4 public void testAddIfAbsent() {
132 jsr166 1.48 CopyOnWriteArrayList list = populatedList(SIZE);
133     list.addIfAbsent(one);
134     assertEquals(SIZE, list.size());
135 dl 1.1 }
136    
137     /**
138 jsr166 1.18 * addIfAbsent adds the element when it does not exist in the list
139 dl 1.1 */
140 dl 1.4 public void testAddIfAbsent2() {
141 jsr166 1.48 CopyOnWriteArrayList list = populatedList(SIZE);
142     list.addIfAbsent(three);
143     assertTrue(list.contains(three));
144 dl 1.1 }
145    
146     /**
147 jsr166 1.18 * clear removes all elements from the list
148 dl 1.1 */
149 dl 1.4 public void testClear() {
150 jsr166 1.48 List list = populatedList(SIZE);
151     list.clear();
152     assertEquals(0, list.size());
153 dl 1.1 }
154    
155 dl 1.4 /**
156 jsr166 1.18 * Cloned list is equal
157 dl 1.4 */
158     public void testClone() {
159 jsr166 1.48 CopyOnWriteArrayList l1 = populatedList(SIZE);
160 jsr166 1.11 CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
161 dl 1.4 assertEquals(l1, l2);
162 jsr166 1.11 l1.clear();
163 dl 1.4 assertFalse(l1.equals(l2));
164     }
165    
166 dl 1.1 /**
167 jsr166 1.18 * contains is true for added elements
168 dl 1.1 */
169 dl 1.4 public void testContains() {
170 jsr166 1.48 List list = populatedList(3);
171     assertTrue(list.contains(one));
172     assertFalse(list.contains(five));
173 dl 1.1 }
174    
175 dl 1.4 /**
176 dl 1.7 * adding at an index places it in the indicated index
177 dl 1.4 */
178 dl 1.1 public void testAddIndex() {
179 jsr166 1.48 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 dl 1.1 }
190    
191 dl 1.4 /**
192 dl 1.5 * lists with same elements are equal and have same hashCode
193 dl 1.4 */
194 dl 1.1 public void testEquals() {
195 jsr166 1.48 List a = populatedList(3);
196     List b = populatedList(3);
197 dl 1.1 assertTrue(a.equals(b));
198     assertTrue(b.equals(a));
199 jsr166 1.37 assertTrue(a.containsAll(b));
200     assertTrue(b.containsAll(a));
201 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
202 dl 1.3 a.add(m1);
203 dl 1.1 assertFalse(a.equals(b));
204     assertFalse(b.equals(a));
205 jsr166 1.37 assertTrue(a.containsAll(b));
206     assertFalse(b.containsAll(a));
207 dl 1.3 b.add(m1);
208 dl 1.1 assertTrue(a.equals(b));
209     assertTrue(b.equals(a));
210 jsr166 1.37 assertTrue(a.containsAll(b));
211     assertTrue(b.containsAll(a));
212 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
213 jsr166 1.37
214     assertFalse(a.equals(null));
215 dl 1.1 }
216    
217     /**
218 jsr166 1.37 * containsAll returns true for collections with subset of elements
219 dl 1.1 */
220 dl 1.4 public void testContainsAll() {
221 jsr166 1.48 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 jsr166 1.37
228     try {
229 jsr166 1.48 list.containsAll(null);
230 jsr166 1.37 shouldThrow();
231     } catch (NullPointerException success) {}
232 dl 1.1 }
233    
234     /**
235 jsr166 1.18 * get returns the value at the given index
236 dl 1.1 */
237 dl 1.4 public void testGet() {
238 jsr166 1.48 List list = populatedList(3);
239     assertEquals(0, list.get(0));
240 dl 1.1 }
241    
242     /**
243 jsr166 1.47 * indexOf(Object) returns the index of the first occurrence of the
244     * specified element in this list, or -1 if this list does not
245     * contain the element
246 dl 1.1 */
247 dl 1.4 public void testIndexOf() {
248 jsr166 1.48 List list = populatedList(3);
249 jsr166 1.47 assertEquals(-1, list.indexOf(-42));
250     int size = list.size();
251     for (int i = 0; i < size; i++) {
252     assertEquals(i, list.indexOf(i));
253     assertEquals(i, list.subList(0, size).indexOf(i));
254     assertEquals(i, list.subList(0, i + 1).indexOf(i));
255     assertEquals(-1, list.subList(0, i).indexOf(i));
256     assertEquals(0, list.subList(i, size).indexOf(i));
257     assertEquals(-1, list.subList(i + 1, size).indexOf(i));
258     }
259    
260     list.add(1);
261     assertEquals(1, list.indexOf(1));
262     assertEquals(1, list.subList(0, size + 1).indexOf(1));
263     assertEquals(0, list.subList(1, size + 1).indexOf(1));
264     assertEquals(size - 2, list.subList(2, size + 1).indexOf(1));
265     assertEquals(0, list.subList(size, size + 1).indexOf(1));
266     assertEquals(-1, list.subList(size + 1, size + 1).indexOf(1));
267 dl 1.1 }
268    
269     /**
270 jsr166 1.47 * indexOf(E, int) returns the index of the first occurrence of the
271     * specified element in this list, searching forwards from index,
272     * or returns -1 if the element is not found
273 dl 1.1 */
274 dl 1.4 public void testIndexOf2() {
275 jsr166 1.48 CopyOnWriteArrayList list = populatedList(3);
276 jsr166 1.47 int size = list.size();
277     assertEquals(-1, list.indexOf(-42, 0));
278    
279     // we might expect IOOBE, but spec says otherwise
280     assertEquals(-1, list.indexOf(0, size));
281     assertEquals(-1, list.indexOf(0, Integer.MAX_VALUE));
282    
283     assertThrows(
284     IndexOutOfBoundsException.class,
285     () -> list.indexOf(0, -1),
286     () -> list.indexOf(0, Integer.MIN_VALUE));
287    
288     for (int i = 0; i < size; i++) {
289     assertEquals(i, list.indexOf(i, 0));
290     assertEquals(i, list.indexOf(i, i));
291     assertEquals(-1, list.indexOf(i, i + 1));
292     }
293    
294     list.add(1);
295     assertEquals(1, list.indexOf(1, 0));
296     assertEquals(1, list.indexOf(1, 1));
297     assertEquals(size, list.indexOf(1, 2));
298     assertEquals(size, list.indexOf(1, size));
299 dl 1.1 }
300    
301     /**
302 jsr166 1.18 * isEmpty returns true when empty, else false
303 dl 1.1 */
304 dl 1.4 public void testIsEmpty() {
305 jsr166 1.48 List empty = new CopyOnWriteArrayList();
306 jsr166 1.11 assertTrue(empty.isEmpty());
307 jsr166 1.48 assertTrue(empty.subList(0, 0).isEmpty());
308    
309     List full = populatedList(SIZE);
310 jsr166 1.11 assertFalse(full.isEmpty());
311 jsr166 1.48 assertTrue(full.subList(0, 0).isEmpty());
312     assertTrue(full.subList(SIZE, SIZE).isEmpty());
313 dl 1.1 }
314    
315     /**
316 jsr166 1.27 * iterator() returns an iterator containing the elements of the
317     * list in insertion order
318 dl 1.1 */
319 dl 1.4 public void testIterator() {
320 jsr166 1.27 Collection empty = new CopyOnWriteArrayList();
321     assertFalse(empty.iterator().hasNext());
322     try {
323     empty.iterator().next();
324     shouldThrow();
325     } catch (NoSuchElementException success) {}
326    
327     Integer[] elements = new Integer[SIZE];
328     for (int i = 0; i < SIZE; i++)
329     elements[i] = i;
330 jsr166 1.38 shuffle(elements);
331 jsr166 1.48 Collection<Integer> full = populatedList(elements);
332 jsr166 1.27
333     Iterator it = full.iterator();
334     for (int j = 0; j < SIZE; j++) {
335     assertTrue(it.hasNext());
336     assertEquals(elements[j], it.next());
337     }
338 jsr166 1.32 assertIteratorExhausted(it);
339     }
340    
341     /**
342     * iterator of empty collection has no elements
343     */
344     public void testEmptyIterator() {
345     Collection c = new CopyOnWriteArrayList();
346     assertIteratorExhausted(c.iterator());
347 dl 1.1 }
348    
349 dl 1.4 /**
350 dl 1.5 * iterator.remove throws UnsupportedOperationException
351 dl 1.4 */
352 jsr166 1.17 public void testIteratorRemove() {
353 jsr166 1.48 CopyOnWriteArrayList list = populatedList(SIZE);
354     Iterator it = list.iterator();
355 dl 1.1 it.next();
356     try {
357     it.remove();
358 dl 1.4 shouldThrow();
359 jsr166 1.12 } catch (UnsupportedOperationException success) {}
360 dl 1.1 }
361    
362 dl 1.4 /**
363 dl 1.5 * toString contains toString of elements
364 dl 1.4 */
365     public void testToString() {
366 jsr166 1.27 assertEquals("[]", new CopyOnWriteArrayList().toString());
367 jsr166 1.48 List list = populatedList(3);
368     String s = list.toString();
369 jsr166 1.27 for (int i = 0; i < 3; ++i)
370 jsr166 1.20 assertTrue(s.contains(String.valueOf(i)));
371 jsr166 1.48 assertEquals(new ArrayList(list).toString(),
372     list.toString());
373 jsr166 1.8 }
374 dl 1.1
375     /**
376 jsr166 1.47 * lastIndexOf(Object) returns the index of the last occurrence of
377     * the specified element in this list, or -1 if this list does not
378     * contain the element
379 dl 1.1 */
380 dl 1.4 public void testLastIndexOf1() {
381 jsr166 1.48 List list = populatedList(3);
382 jsr166 1.47 assertEquals(-1, list.lastIndexOf(-42));
383     int size = list.size();
384     for (int i = 0; i < size; i++) {
385     assertEquals(i, list.lastIndexOf(i));
386     assertEquals(i, list.subList(0, size).lastIndexOf(i));
387     assertEquals(i, list.subList(0, i + 1).lastIndexOf(i));
388     assertEquals(-1, list.subList(0, i).lastIndexOf(i));
389     assertEquals(0, list.subList(i, size).lastIndexOf(i));
390     assertEquals(-1, list.subList(i + 1, size).lastIndexOf(i));
391     }
392    
393     list.add(1);
394     assertEquals(size, list.lastIndexOf(1));
395     assertEquals(size, list.subList(0, size + 1).lastIndexOf(1));
396     assertEquals(1, list.subList(0, size).lastIndexOf(1));
397     assertEquals(0, list.subList(1, 2).lastIndexOf(1));
398     assertEquals(-1, list.subList(0, 1).indexOf(1));
399 dl 1.1 }
400    
401     /**
402 jsr166 1.47 * lastIndexOf(E, int) returns the index of the last occurrence of the
403     * specified element in this list, searching backwards from index, or
404     * returns -1 if the element is not found
405 dl 1.1 */
406 jsr166 1.21 public void testLastIndexOf2() {
407 jsr166 1.48 CopyOnWriteArrayList list = populatedList(3);
408 jsr166 1.47
409     // we might expect IOOBE, but spec says otherwise
410     assertEquals(-1, list.lastIndexOf(0, -1));
411    
412     int size = list.size();
413     assertThrows(
414     IndexOutOfBoundsException.class,
415     () -> list.lastIndexOf(0, size),
416     () -> list.lastIndexOf(0, Integer.MAX_VALUE));
417    
418     for (int i = 0; i < size; i++) {
419     assertEquals(i, list.lastIndexOf(i, i));
420     assertEquals(list.indexOf(i), list.lastIndexOf(i, i));
421     if (i > 0)
422     assertEquals(-1, list.lastIndexOf(i, i - 1));
423     }
424     list.add(one);
425     list.add(three);
426     assertEquals(1, list.lastIndexOf(one, 1));
427     assertEquals(1, list.lastIndexOf(one, 2));
428     assertEquals(3, list.lastIndexOf(one, 3));
429     assertEquals(3, list.lastIndexOf(one, 4));
430     assertEquals(-1, list.lastIndexOf(three, 3));
431 dl 1.1 }
432    
433     /**
434 jsr166 1.18 * listIterator traverses all elements
435 dl 1.1 */
436 dl 1.4 public void testListIterator1() {
437 jsr166 1.48 List list = populatedList(SIZE);
438     ListIterator i = list.listIterator();
439 jsr166 1.11 int j;
440     for (j = 0; i.hasNext(); j++)
441 jsr166 1.14 assertEquals(j, i.next());
442 jsr166 1.11 assertEquals(SIZE, j);
443 dl 1.1 }
444    
445     /**
446 jsr166 1.18 * listIterator only returns those elements after the given index
447 dl 1.1 */
448 dl 1.4 public void testListIterator2() {
449 jsr166 1.48 List list = populatedList(3);
450     ListIterator i = list.listIterator(1);
451 jsr166 1.11 int j;
452     for (j = 0; i.hasNext(); j++)
453 jsr166 1.36 assertEquals(j + 1, i.next());
454 jsr166 1.11 assertEquals(2, j);
455 dl 1.1 }
456    
457     /**
458 jsr166 1.28 * remove(int) removes and returns the object at the given index
459 dl 1.1 */
460 jsr166 1.28 public void testRemove_int() {
461     int SIZE = 3;
462     for (int i = 0; i < SIZE; i++) {
463 jsr166 1.48 List list = populatedList(SIZE);
464     assertEquals(i, list.remove(i));
465     assertEquals(SIZE - 1, list.size());
466     assertFalse(list.contains(new Integer(i)));
467 jsr166 1.28 }
468     }
469    
470     /**
471     * remove(Object) removes the object if found and returns true
472     */
473     public void testRemove_Object() {
474     int SIZE = 3;
475     for (int i = 0; i < SIZE; i++) {
476 jsr166 1.48 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 jsr166 1.28 }
482     CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
483     assertTrue(x.remove(new Integer(6)));
484     assertEquals(x, Arrays.asList(4, 5));
485     assertTrue(x.remove(new Integer(4)));
486     assertEquals(x, Arrays.asList(5));
487     assertTrue(x.remove(new Integer(5)));
488     assertEquals(x, Arrays.asList());
489     assertFalse(x.remove(new Integer(5)));
490 dl 1.1 }
491    
492     /**
493 jsr166 1.18 * removeAll removes all elements from the given collection
494 dl 1.1 */
495 dl 1.4 public void testRemoveAll() {
496 jsr166 1.48 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 dl 1.1 }
502    
503     /**
504 jsr166 1.18 * set changes the element at the given index
505 dl 1.1 */
506 dl 1.4 public void testSet() {
507 jsr166 1.48 List list = populatedList(3);
508     assertEquals(2, list.set(2, four));
509     assertEquals(4, list.get(2));
510 dl 1.1 }
511    
512     /**
513 jsr166 1.18 * size returns the number of elements
514 dl 1.1 */
515 dl 1.4 public void testSize() {
516 jsr166 1.48 List empty = new CopyOnWriteArrayList();
517     assertEquals(0, empty.size());
518     assertEquals(0, empty.subList(0, 0).size());
519    
520     List full = populatedList(SIZE);
521 jsr166 1.11 assertEquals(SIZE, full.size());
522 jsr166 1.48 assertEquals(0, full.subList(0, 0).size());
523     assertEquals(0, full.subList(SIZE, SIZE).size());
524 dl 1.1 }
525    
526     /**
527 jsr166 1.26 * toArray() returns an Object array containing all elements from
528     * the list in insertion order
529 dl 1.1 */
530 dl 1.4 public void testToArray() {
531 jsr166 1.26 Object[] a = new CopyOnWriteArrayList().toArray();
532     assertTrue(Arrays.equals(new Object[0], a));
533     assertSame(Object[].class, a.getClass());
534    
535     Integer[] elements = new Integer[SIZE];
536     for (int i = 0; i < SIZE; i++)
537     elements[i] = i;
538 jsr166 1.38 shuffle(elements);
539 jsr166 1.48 Collection<Integer> full = populatedList(elements);
540 jsr166 1.26
541     assertTrue(Arrays.equals(elements, full.toArray()));
542     assertSame(Object[].class, full.toArray().getClass());
543 dl 1.1 }
544    
545     /**
546 jsr166 1.26 * toArray(Integer array) returns an Integer array containing all
547     * elements from the list in insertion order
548 dl 1.1 */
549 dl 1.4 public void testToArray2() {
550 jsr166 1.26 Collection empty = new CopyOnWriteArrayList();
551     Integer[] a;
552    
553     a = new Integer[0];
554     assertSame(a, empty.toArray(a));
555    
556 jsr166 1.35 a = new Integer[SIZE / 2];
557 jsr166 1.26 Arrays.fill(a, 42);
558     assertSame(a, empty.toArray(a));
559     assertNull(a[0]);
560     for (int i = 1; i < a.length; i++)
561     assertEquals(42, (int) a[i]);
562    
563     Integer[] elements = new Integer[SIZE];
564     for (int i = 0; i < SIZE; i++)
565     elements[i] = i;
566 jsr166 1.38 shuffle(elements);
567 jsr166 1.48 Collection<Integer> full = populatedList(elements);
568 jsr166 1.26
569     Arrays.fill(a, 42);
570     assertTrue(Arrays.equals(elements, full.toArray(a)));
571     for (int i = 0; i < a.length; i++)
572     assertEquals(42, (int) a[i]);
573     assertSame(Integer[].class, full.toArray(a).getClass());
574    
575     a = new Integer[SIZE];
576     Arrays.fill(a, 42);
577     assertSame(a, full.toArray(a));
578     assertTrue(Arrays.equals(elements, a));
579    
580 jsr166 1.35 a = new Integer[2 * SIZE];
581 jsr166 1.26 Arrays.fill(a, 42);
582     assertSame(a, full.toArray(a));
583     assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
584     assertNull(a[SIZE]);
585     for (int i = SIZE + 1; i < a.length; i++)
586     assertEquals(42, (int) a[i]);
587 dl 1.1 }
588    
589 dl 1.4 /**
590 dl 1.5 * sublists contains elements at indexes offset from their base
591 dl 1.4 */
592 dl 1.1 public void testSubList() {
593 jsr166 1.48 List a = populatedList(10);
594 dl 1.1 assertTrue(a.subList(1,1).isEmpty());
595 jsr166 1.11 for (int j = 0; j < 9; ++j) {
596     for (int i = j ; i < 10; ++i) {
597     List b = a.subList(j,i);
598     for (int k = j; k < i; ++k) {
599     assertEquals(new Integer(k), b.get(k-j));
600     }
601     }
602     }
603 dl 1.1
604 jsr166 1.11 List s = a.subList(2, 5);
605 jsr166 1.24 assertEquals(3, s.size());
606 dl 1.3 s.set(2, m1);
607 dl 1.1 assertEquals(a.get(4), m1);
608 jsr166 1.11 s.clear();
609 jsr166 1.24 assertEquals(7, a.size());
610 jsr166 1.47
611     assertThrows(
612     IndexOutOfBoundsException.class,
613     () -> s.get(0),
614     () -> s.set(0, 42));
615 dl 1.1 }
616    
617     // Exception tests
618    
619     /**
620 jsr166 1.18 * toArray throws an ArrayStoreException when the given array
621     * can not store the objects inside the list
622 dl 1.1 */
623 dl 1.4 public void testToArray_ArrayStoreException() {
624 jsr166 1.48 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 dl 1.1 }
633    
634 jsr166 1.48 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 dl 1.1 }
667 jsr166 1.8
668 dl 1.1 /**
669 jsr166 1.48 * 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 dl 1.1
676 jsr166 1.48 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 dl 1.2 }
684    
685 dl 1.4 /**
686 jsr166 1.45 * a deserialized/reserialized list equals original
687 dl 1.4 */
688 jsr166 1.12 public void testSerialization() throws Exception {
689 jsr166 1.48 List x = populatedList(SIZE);
690 jsr166 1.22 List y = serialClone(x);
691 dl 1.2
692 jsr166 1.29 assertNotSame(x, y);
693 jsr166 1.22 assertEquals(x.size(), y.size());
694     assertEquals(x.toString(), y.toString());
695     assertTrue(Arrays.equals(x.toArray(), y.toArray()));
696     assertEquals(x, y);
697     assertEquals(y, x);
698     while (!x.isEmpty()) {
699     assertFalse(y.isEmpty());
700     assertEquals(x.remove(0), y.remove(0));
701     }
702     assertTrue(y.isEmpty());
703 dl 1.1 }
704 jsr166 1.8
705 dl 1.1 }