ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.47
Committed: Mon Mar 26 21:28:22 2018 UTC (6 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.46: +102 -21 lines
Log Message:
improve tests for indexOf and lastIndexOf

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.22 import java.util.Iterator;
13     import java.util.LinkedList;
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 dl 1.1
19 jsr166 1.31 import junit.framework.Test;
20    
21 jsr166 1.10 public class CopyOnWriteArrayListTest extends JSR166TestCase {
22 jsr166 1.8
23 dl 1.1 public static void main(String[] args) {
24 jsr166 1.34 main(suite(), args);
25 dl 1.1 }
26    
27     public static Test suite() {
28 jsr166 1.40 class Implementation implements CollectionImplementation {
29 jsr166 1.43 public Class<?> klazz() { return CopyOnWriteArrayList.class; }
30 jsr166 1.40 public List emptyCollection() { return new CopyOnWriteArrayList(); }
31     public Object makeElement(int i) { return i; }
32     public boolean isConcurrent() { return true; }
33     public boolean permitsNulls() { return true; }
34     }
35     class SubListImplementation extends Implementation {
36     public List emptyCollection() {
37     return super.emptyCollection().subList(0, 0);
38     }
39     }
40     return newTestSuite(
41     CopyOnWriteArrayListTest.class,
42 jsr166 1.41 CollectionTest.testSuite(new Implementation()),
43     CollectionTest.testSuite(new SubListImplementation()));
44 dl 1.1 }
45    
46 jsr166 1.25 static CopyOnWriteArrayList<Integer> populatedArray(int n) {
47 jsr166 1.42 CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
48 dl 1.1 assertTrue(a.isEmpty());
49 jsr166 1.26 for (int i = 0; i < n; i++)
50 jsr166 1.25 a.add(i);
51 dl 1.1 assertFalse(a.isEmpty());
52     assertEquals(n, a.size());
53     return a;
54     }
55    
56 jsr166 1.26 static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) {
57 jsr166 1.42 CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
58 jsr166 1.26 assertTrue(a.isEmpty());
59 jsr166 1.46 for (Integer element : elements)
60     a.add(element);
61 jsr166 1.26 assertFalse(a.isEmpty());
62     assertEquals(elements.length, a.size());
63     return a;
64     }
65    
66 dl 1.4 /**
67 dl 1.5 * a new list is empty
68 dl 1.4 */
69 dl 1.3 public void testConstructor() {
70 jsr166 1.11 CopyOnWriteArrayList a = new CopyOnWriteArrayList();
71 dl 1.3 assertTrue(a.isEmpty());
72     }
73    
74 dl 1.4 /**
75 dl 1.5 * new list contains all elements of initializing array
76 dl 1.4 */
77 dl 1.3 public void testConstructor2() {
78     Integer[] ints = new Integer[SIZE];
79 jsr166 1.35 for (int i = 0; i < SIZE - 1; ++i)
80 dl 1.3 ints[i] = new Integer(i);
81 jsr166 1.11 CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
82 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
83 dl 1.3 assertEquals(ints[i], a.get(i));
84     }
85    
86 dl 1.4 /**
87 dl 1.5 * new list contains all elements of initializing collection
88 dl 1.4 */
89 dl 1.3 public void testConstructor3() {
90     Integer[] ints = new Integer[SIZE];
91 jsr166 1.35 for (int i = 0; i < SIZE - 1; ++i)
92 dl 1.3 ints[i] = new Integer(i);
93 jsr166 1.11 CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
94 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
95 dl 1.3 assertEquals(ints[i], a.get(i));
96     }
97 jsr166 1.8
98 dl 1.1 /**
99 jsr166 1.33 * addAll adds each element from the given collection, including duplicates
100 dl 1.1 */
101 dl 1.4 public void testAddAll() {
102 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
103 jsr166 1.33 assertTrue(full.addAll(Arrays.asList(three, four, five)));
104 jsr166 1.11 assertEquals(6, full.size());
105 jsr166 1.33 assertTrue(full.addAll(Arrays.asList(three, four, five)));
106     assertEquals(9, full.size());
107 dl 1.1 }
108    
109     /**
110 jsr166 1.18 * addAllAbsent adds each element from the given collection that did not
111     * already exist in the List
112 dl 1.1 */
113 dl 1.4 public void testAddAllAbsent() {
114 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
115 jsr166 1.33 // "one" is duplicate and will not be added
116     assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one)));
117     assertEquals(5, full.size());
118     assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one)));
119 jsr166 1.11 assertEquals(5, full.size());
120 dl 1.1 }
121    
122     /**
123 jsr166 1.18 * addIfAbsent will not add the element if it already exists in the list
124 dl 1.1 */
125 dl 1.4 public void testAddIfAbsent() {
126 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
127     full.addIfAbsent(one);
128     assertEquals(SIZE, full.size());
129 dl 1.1 }
130    
131     /**
132 jsr166 1.18 * addIfAbsent adds the element when it does not exist in the list
133 dl 1.1 */
134 dl 1.4 public void testAddIfAbsent2() {
135 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
136 dl 1.3 full.addIfAbsent(three);
137     assertTrue(full.contains(three));
138 dl 1.1 }
139    
140     /**
141 jsr166 1.18 * clear removes all elements from the list
142 dl 1.1 */
143 dl 1.4 public void testClear() {
144 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
145     full.clear();
146     assertEquals(0, full.size());
147 dl 1.1 }
148    
149 dl 1.4 /**
150 jsr166 1.18 * Cloned list is equal
151 dl 1.4 */
152     public void testClone() {
153 jsr166 1.11 CopyOnWriteArrayList l1 = populatedArray(SIZE);
154     CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
155 dl 1.4 assertEquals(l1, l2);
156 jsr166 1.11 l1.clear();
157 dl 1.4 assertFalse(l1.equals(l2));
158     }
159    
160 dl 1.1 /**
161 jsr166 1.18 * contains is true for added elements
162 dl 1.1 */
163 dl 1.4 public void testContains() {
164 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
165     assertTrue(full.contains(one));
166     assertFalse(full.contains(five));
167 dl 1.1 }
168    
169 dl 1.4 /**
170 dl 1.7 * adding at an index places it in the indicated index
171 dl 1.4 */
172 dl 1.1 public void testAddIndex() {
173 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
174 dl 1.3 full.add(0, m1);
175 dl 1.1 assertEquals(4, full.size());
176 dl 1.3 assertEquals(m1, full.get(0));
177     assertEquals(zero, full.get(1));
178 dl 1.1
179 dl 1.3 full.add(2, m2);
180 dl 1.1 assertEquals(5, full.size());
181 dl 1.3 assertEquals(m2, full.get(2));
182     assertEquals(two, full.get(4));
183 dl 1.1 }
184    
185 dl 1.4 /**
186 dl 1.5 * lists with same elements are equal and have same hashCode
187 dl 1.4 */
188 dl 1.1 public void testEquals() {
189 jsr166 1.11 CopyOnWriteArrayList a = populatedArray(3);
190     CopyOnWriteArrayList b = populatedArray(3);
191 dl 1.1 assertTrue(a.equals(b));
192     assertTrue(b.equals(a));
193 jsr166 1.37 assertTrue(a.containsAll(b));
194     assertTrue(b.containsAll(a));
195 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
196 dl 1.3 a.add(m1);
197 dl 1.1 assertFalse(a.equals(b));
198     assertFalse(b.equals(a));
199 jsr166 1.37 assertTrue(a.containsAll(b));
200     assertFalse(b.containsAll(a));
201 dl 1.3 b.add(m1);
202 dl 1.1 assertTrue(a.equals(b));
203     assertTrue(b.equals(a));
204 jsr166 1.37 assertTrue(a.containsAll(b));
205     assertTrue(b.containsAll(a));
206 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
207 jsr166 1.37
208     assertFalse(a.equals(null));
209 dl 1.1 }
210    
211     /**
212 jsr166 1.37 * containsAll returns true for collections with subset of elements
213 dl 1.1 */
214 dl 1.4 public void testContainsAll() {
215 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
216 jsr166 1.33 assertTrue(full.containsAll(Arrays.asList()));
217     assertTrue(full.containsAll(Arrays.asList(one)));
218     assertTrue(full.containsAll(Arrays.asList(one, two)));
219     assertFalse(full.containsAll(Arrays.asList(one, two, six)));
220     assertFalse(full.containsAll(Arrays.asList(six)));
221 jsr166 1.37
222     try {
223     full.containsAll(null);
224     shouldThrow();
225     } catch (NullPointerException success) {}
226 dl 1.1 }
227    
228     /**
229 jsr166 1.18 * get returns the value at the given index
230 dl 1.1 */
231 dl 1.4 public void testGet() {
232 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
233 jsr166 1.14 assertEquals(0, full.get(0));
234 dl 1.1 }
235    
236     /**
237 jsr166 1.47 * indexOf(Object) returns the index of the first occurrence of the
238     * specified element in this list, or -1 if this list does not
239     * contain the element
240 dl 1.1 */
241 dl 1.4 public void testIndexOf() {
242 jsr166 1.47 CopyOnWriteArrayList list = populatedArray(3);
243     assertEquals(-1, list.indexOf(-42));
244     int size = list.size();
245     for (int i = 0; i < size; i++) {
246     assertEquals(i, list.indexOf(i));
247     assertEquals(i, list.subList(0, size).indexOf(i));
248     assertEquals(i, list.subList(0, i + 1).indexOf(i));
249     assertEquals(-1, list.subList(0, i).indexOf(i));
250     assertEquals(0, list.subList(i, size).indexOf(i));
251     assertEquals(-1, list.subList(i + 1, size).indexOf(i));
252     }
253    
254     list.add(1);
255     assertEquals(1, list.indexOf(1));
256     assertEquals(1, list.subList(0, size + 1).indexOf(1));
257     assertEquals(0, list.subList(1, size + 1).indexOf(1));
258     assertEquals(size - 2, list.subList(2, size + 1).indexOf(1));
259     assertEquals(0, list.subList(size, size + 1).indexOf(1));
260     assertEquals(-1, list.subList(size + 1, size + 1).indexOf(1));
261 dl 1.1 }
262    
263     /**
264 jsr166 1.47 * indexOf(E, int) returns the index of the first occurrence of the
265     * specified element in this list, searching forwards from index,
266     * or returns -1 if the element is not found
267 dl 1.1 */
268 dl 1.4 public void testIndexOf2() {
269 jsr166 1.47 CopyOnWriteArrayList list = populatedArray(3);
270     int size = list.size();
271     assertEquals(-1, list.indexOf(-42, 0));
272    
273     // we might expect IOOBE, but spec says otherwise
274     assertEquals(-1, list.indexOf(0, size));
275     assertEquals(-1, list.indexOf(0, Integer.MAX_VALUE));
276    
277     assertThrows(
278     IndexOutOfBoundsException.class,
279     () -> list.indexOf(0, -1),
280     () -> list.indexOf(0, Integer.MIN_VALUE));
281    
282     for (int i = 0; i < size; i++) {
283     assertEquals(i, list.indexOf(i, 0));
284     assertEquals(i, list.indexOf(i, i));
285     assertEquals(-1, list.indexOf(i, i + 1));
286     }
287    
288     list.add(1);
289     assertEquals(1, list.indexOf(1, 0));
290     assertEquals(1, list.indexOf(1, 1));
291     assertEquals(size, list.indexOf(1, 2));
292     assertEquals(size, list.indexOf(1, size));
293 dl 1.1 }
294    
295     /**
296 jsr166 1.18 * isEmpty returns true when empty, else false
297 dl 1.1 */
298 dl 1.4 public void testIsEmpty() {
299 jsr166 1.11 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
300     CopyOnWriteArrayList full = populatedArray(SIZE);
301     assertTrue(empty.isEmpty());
302     assertFalse(full.isEmpty());
303 dl 1.1 }
304    
305     /**
306 jsr166 1.27 * iterator() returns an iterator containing the elements of the
307     * list in insertion order
308 dl 1.1 */
309 dl 1.4 public void testIterator() {
310 jsr166 1.27 Collection empty = new CopyOnWriteArrayList();
311     assertFalse(empty.iterator().hasNext());
312     try {
313     empty.iterator().next();
314     shouldThrow();
315     } catch (NoSuchElementException success) {}
316    
317     Integer[] elements = new Integer[SIZE];
318     for (int i = 0; i < SIZE; i++)
319     elements[i] = i;
320 jsr166 1.38 shuffle(elements);
321 jsr166 1.27 Collection<Integer> full = populatedArray(elements);
322    
323     Iterator it = full.iterator();
324     for (int j = 0; j < SIZE; j++) {
325     assertTrue(it.hasNext());
326     assertEquals(elements[j], it.next());
327     }
328 jsr166 1.32 assertIteratorExhausted(it);
329     }
330    
331     /**
332     * iterator of empty collection has no elements
333     */
334     public void testEmptyIterator() {
335     Collection c = new CopyOnWriteArrayList();
336     assertIteratorExhausted(c.iterator());
337 dl 1.1 }
338    
339 dl 1.4 /**
340 dl 1.5 * iterator.remove throws UnsupportedOperationException
341 dl 1.4 */
342 jsr166 1.17 public void testIteratorRemove() {
343 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
344 dl 1.1 Iterator it = full.iterator();
345     it.next();
346     try {
347     it.remove();
348 dl 1.4 shouldThrow();
349 jsr166 1.12 } catch (UnsupportedOperationException success) {}
350 dl 1.1 }
351    
352 dl 1.4 /**
353 dl 1.5 * toString contains toString of elements
354 dl 1.4 */
355     public void testToString() {
356 jsr166 1.27 assertEquals("[]", new CopyOnWriteArrayList().toString());
357 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
358 dl 1.1 String s = full.toString();
359 jsr166 1.27 for (int i = 0; i < 3; ++i)
360 jsr166 1.20 assertTrue(s.contains(String.valueOf(i)));
361 jsr166 1.27 assertEquals(new ArrayList(full).toString(),
362     full.toString());
363 jsr166 1.8 }
364 dl 1.1
365     /**
366 jsr166 1.47 * lastIndexOf(Object) returns the index of the last occurrence of
367     * the specified element in this list, or -1 if this list does not
368     * contain the element
369 dl 1.1 */
370 dl 1.4 public void testLastIndexOf1() {
371 jsr166 1.47 CopyOnWriteArrayList list = populatedArray(3);
372     assertEquals(-1, list.lastIndexOf(-42));
373     int size = list.size();
374     for (int i = 0; i < size; i++) {
375     assertEquals(i, list.lastIndexOf(i));
376     assertEquals(i, list.subList(0, size).lastIndexOf(i));
377     assertEquals(i, list.subList(0, i + 1).lastIndexOf(i));
378     assertEquals(-1, list.subList(0, i).lastIndexOf(i));
379     assertEquals(0, list.subList(i, size).lastIndexOf(i));
380     assertEquals(-1, list.subList(i + 1, size).lastIndexOf(i));
381     }
382    
383     list.add(1);
384     assertEquals(size, list.lastIndexOf(1));
385     assertEquals(size, list.subList(0, size + 1).lastIndexOf(1));
386     assertEquals(1, list.subList(0, size).lastIndexOf(1));
387     assertEquals(0, list.subList(1, 2).lastIndexOf(1));
388     assertEquals(-1, list.subList(0, 1).indexOf(1));
389 dl 1.1 }
390    
391     /**
392 jsr166 1.47 * lastIndexOf(E, int) returns the index of the last occurrence of the
393     * specified element in this list, searching backwards from index, or
394     * returns -1 if the element is not found
395 dl 1.1 */
396 jsr166 1.21 public void testLastIndexOf2() {
397 jsr166 1.47 CopyOnWriteArrayList list = populatedArray(3);
398    
399     // we might expect IOOBE, but spec says otherwise
400     assertEquals(-1, list.lastIndexOf(0, -1));
401    
402     int size = list.size();
403     assertThrows(
404     IndexOutOfBoundsException.class,
405     () -> list.lastIndexOf(0, size),
406     () -> list.lastIndexOf(0, Integer.MAX_VALUE));
407    
408     for (int i = 0; i < size; i++) {
409     assertEquals(i, list.lastIndexOf(i, i));
410     assertEquals(list.indexOf(i), list.lastIndexOf(i, i));
411     if (i > 0)
412     assertEquals(-1, list.lastIndexOf(i, i - 1));
413     }
414     list.add(one);
415     list.add(three);
416     assertEquals(1, list.lastIndexOf(one, 1));
417     assertEquals(1, list.lastIndexOf(one, 2));
418     assertEquals(3, list.lastIndexOf(one, 3));
419     assertEquals(3, list.lastIndexOf(one, 4));
420     assertEquals(-1, list.lastIndexOf(three, 3));
421 dl 1.1 }
422    
423     /**
424 jsr166 1.18 * listIterator traverses all elements
425 dl 1.1 */
426 dl 1.4 public void testListIterator1() {
427 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
428     ListIterator i = full.listIterator();
429     int j;
430     for (j = 0; i.hasNext(); j++)
431 jsr166 1.14 assertEquals(j, i.next());
432 jsr166 1.11 assertEquals(SIZE, j);
433 dl 1.1 }
434    
435     /**
436 jsr166 1.18 * listIterator only returns those elements after the given index
437 dl 1.1 */
438 dl 1.4 public void testListIterator2() {
439 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
440     ListIterator i = full.listIterator(1);
441     int j;
442     for (j = 0; i.hasNext(); j++)
443 jsr166 1.36 assertEquals(j + 1, i.next());
444 jsr166 1.11 assertEquals(2, j);
445 dl 1.1 }
446    
447     /**
448 jsr166 1.28 * remove(int) removes and returns the object at the given index
449 dl 1.1 */
450 jsr166 1.28 public void testRemove_int() {
451     int SIZE = 3;
452     for (int i = 0; i < SIZE; i++) {
453     CopyOnWriteArrayList full = populatedArray(SIZE);
454     assertEquals(i, full.remove(i));
455     assertEquals(SIZE - 1, full.size());
456     assertFalse(full.contains(new Integer(i)));
457     }
458     }
459    
460     /**
461     * remove(Object) removes the object if found and returns true
462     */
463     public void testRemove_Object() {
464     int SIZE = 3;
465     for (int i = 0; i < SIZE; i++) {
466     CopyOnWriteArrayList full = populatedArray(SIZE);
467     assertFalse(full.remove(new Integer(-42)));
468     assertTrue(full.remove(new Integer(i)));
469     assertEquals(SIZE - 1, full.size());
470     assertFalse(full.contains(new Integer(i)));
471     }
472     CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
473     assertTrue(x.remove(new Integer(6)));
474     assertEquals(x, Arrays.asList(4, 5));
475     assertTrue(x.remove(new Integer(4)));
476     assertEquals(x, Arrays.asList(5));
477     assertTrue(x.remove(new Integer(5)));
478     assertEquals(x, Arrays.asList());
479     assertFalse(x.remove(new Integer(5)));
480 dl 1.1 }
481    
482     /**
483 jsr166 1.18 * removeAll removes all elements from the given collection
484 dl 1.1 */
485 dl 1.4 public void testRemoveAll() {
486 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
487 jsr166 1.33 assertTrue(full.removeAll(Arrays.asList(one, two)));
488     assertEquals(1, full.size());
489     assertFalse(full.removeAll(Arrays.asList(one, two)));
490 jsr166 1.11 assertEquals(1, full.size());
491 dl 1.1 }
492    
493     /**
494 jsr166 1.18 * set changes the element at the given index
495 dl 1.1 */
496 dl 1.4 public void testSet() {
497 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
498 jsr166 1.15 assertEquals(2, full.set(2, four));
499 jsr166 1.14 assertEquals(4, full.get(2));
500 dl 1.1 }
501    
502     /**
503 jsr166 1.18 * size returns the number of elements
504 dl 1.1 */
505 dl 1.4 public void testSize() {
506 jsr166 1.11 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
507     CopyOnWriteArrayList full = populatedArray(SIZE);
508     assertEquals(SIZE, full.size());
509     assertEquals(0, empty.size());
510 dl 1.1 }
511    
512     /**
513 jsr166 1.26 * toArray() returns an Object array containing all elements from
514     * the list in insertion order
515 dl 1.1 */
516 dl 1.4 public void testToArray() {
517 jsr166 1.26 Object[] a = new CopyOnWriteArrayList().toArray();
518     assertTrue(Arrays.equals(new Object[0], a));
519     assertSame(Object[].class, a.getClass());
520    
521     Integer[] elements = new Integer[SIZE];
522     for (int i = 0; i < SIZE; i++)
523     elements[i] = i;
524 jsr166 1.38 shuffle(elements);
525 jsr166 1.26 Collection<Integer> full = populatedArray(elements);
526    
527     assertTrue(Arrays.equals(elements, full.toArray()));
528     assertSame(Object[].class, full.toArray().getClass());
529 dl 1.1 }
530    
531     /**
532 jsr166 1.26 * toArray(Integer array) returns an Integer array containing all
533     * elements from the list in insertion order
534 dl 1.1 */
535 dl 1.4 public void testToArray2() {
536 jsr166 1.26 Collection empty = new CopyOnWriteArrayList();
537     Integer[] a;
538    
539     a = new Integer[0];
540     assertSame(a, empty.toArray(a));
541    
542 jsr166 1.35 a = new Integer[SIZE / 2];
543 jsr166 1.26 Arrays.fill(a, 42);
544     assertSame(a, empty.toArray(a));
545     assertNull(a[0]);
546     for (int i = 1; i < a.length; i++)
547     assertEquals(42, (int) a[i]);
548    
549     Integer[] elements = new Integer[SIZE];
550     for (int i = 0; i < SIZE; i++)
551     elements[i] = i;
552 jsr166 1.38 shuffle(elements);
553 jsr166 1.26 Collection<Integer> full = populatedArray(elements);
554    
555     Arrays.fill(a, 42);
556     assertTrue(Arrays.equals(elements, full.toArray(a)));
557     for (int i = 0; i < a.length; i++)
558     assertEquals(42, (int) a[i]);
559     assertSame(Integer[].class, full.toArray(a).getClass());
560    
561     a = new Integer[SIZE];
562     Arrays.fill(a, 42);
563     assertSame(a, full.toArray(a));
564     assertTrue(Arrays.equals(elements, a));
565    
566 jsr166 1.35 a = new Integer[2 * SIZE];
567 jsr166 1.26 Arrays.fill(a, 42);
568     assertSame(a, full.toArray(a));
569     assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
570     assertNull(a[SIZE]);
571     for (int i = SIZE + 1; i < a.length; i++)
572     assertEquals(42, (int) a[i]);
573 dl 1.1 }
574    
575 dl 1.4 /**
576 dl 1.5 * sublists contains elements at indexes offset from their base
577 dl 1.4 */
578 dl 1.1 public void testSubList() {
579 jsr166 1.11 CopyOnWriteArrayList a = populatedArray(10);
580 dl 1.1 assertTrue(a.subList(1,1).isEmpty());
581 jsr166 1.11 for (int j = 0; j < 9; ++j) {
582     for (int i = j ; i < 10; ++i) {
583     List b = a.subList(j,i);
584     for (int k = j; k < i; ++k) {
585     assertEquals(new Integer(k), b.get(k-j));
586     }
587     }
588     }
589 dl 1.1
590 jsr166 1.11 List s = a.subList(2, 5);
591 jsr166 1.24 assertEquals(3, s.size());
592 dl 1.3 s.set(2, m1);
593 dl 1.1 assertEquals(a.get(4), m1);
594 jsr166 1.11 s.clear();
595 jsr166 1.24 assertEquals(7, a.size());
596 jsr166 1.47
597     assertThrows(
598     IndexOutOfBoundsException.class,
599     () -> s.get(0),
600     () -> s.set(0, 42));
601 dl 1.1 }
602    
603     // Exception tests
604    
605     /**
606 jsr166 1.18 * toArray throws an ArrayStoreException when the given array
607     * can not store the objects inside the list
608 dl 1.1 */
609 dl 1.4 public void testToArray_ArrayStoreException() {
610 jsr166 1.30 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
611     c.add("zfasdfsdf");
612     c.add("asdadasd");
613 dl 1.4 try {
614 dl 1.1 c.toArray(new Long[5]);
615 jsr166 1.11 shouldThrow();
616 jsr166 1.13 } catch (ArrayStoreException success) {}
617 dl 1.1 }
618    
619     /**
620 jsr166 1.18 * get throws an IndexOutOfBoundsException on a negative index
621 dl 1.1 */
622 dl 1.4 public void testGet1_IndexOutOfBoundsException() {
623 jsr166 1.30 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 dl 1.1 }
632 jsr166 1.8
633 dl 1.1 /**
634 jsr166 1.18 * get throws an IndexOutOfBoundsException on a too high index
635 dl 1.1 */
636 dl 1.4 public void testGet2_IndexOutOfBoundsException() {
637 jsr166 1.30 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 dl 1.1 }
646    
647     /**
648 jsr166 1.18 * set throws an IndexOutOfBoundsException on a negative index
649 dl 1.1 */
650 dl 1.4 public void testSet1_IndexOutOfBoundsException() {
651 jsr166 1.30 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 dl 1.1 }
660 jsr166 1.8
661 dl 1.1 /**
662 jsr166 1.18 * set throws an IndexOutOfBoundsException on a too high index
663 dl 1.1 */
664 dl 1.4 public void testSet2() {
665 jsr166 1.30 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 dl 1.1 }
674    
675     /**
676 jsr166 1.18 * add throws an IndexOutOfBoundsException on a negative index
677 dl 1.1 */
678 dl 1.4 public void testAdd1_IndexOutOfBoundsException() {
679 jsr166 1.30 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 dl 1.1 }
688 jsr166 1.8
689 dl 1.1 /**
690 jsr166 1.18 * add throws an IndexOutOfBoundsException on a too high index
691 dl 1.1 */
692 dl 1.4 public void testAdd2_IndexOutOfBoundsException() {
693 jsr166 1.30 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 dl 1.1 }
702    
703     /**
704 jsr166 1.18 * remove throws an IndexOutOfBoundsException on a negative index
705 dl 1.1 */
706 dl 1.4 public void testRemove1_IndexOutOfBounds() {
707 jsr166 1.30 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 dl 1.1 }
716    
717     /**
718 jsr166 1.18 * remove throws an IndexOutOfBoundsException on a too high index
719 dl 1.1 */
720 dl 1.4 public void testRemove2_IndexOutOfBounds() {
721 jsr166 1.30 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 dl 1.1 }
730 jsr166 1.8
731 dl 1.1 /**
732 jsr166 1.18 * addAll throws an IndexOutOfBoundsException on a negative index
733 dl 1.1 */
734 dl 1.4 public void testAddAll1_IndexOutOfBoundsException() {
735 jsr166 1.30 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 dl 1.1 }
744 jsr166 1.8
745 dl 1.1 /**
746 jsr166 1.18 * addAll throws an IndexOutOfBoundsException on a too high index
747 dl 1.1 */
748 dl 1.4 public void testAddAll2_IndexOutOfBoundsException() {
749 jsr166 1.30 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 dl 1.1 }
758    
759     /**
760 jsr166 1.18 * listIterator throws an IndexOutOfBoundsException on a negative index
761 dl 1.1 */
762 dl 1.4 public void testListIterator1_IndexOutOfBoundsException() {
763 jsr166 1.30 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 dl 1.1 }
772    
773     /**
774 jsr166 1.18 * listIterator throws an IndexOutOfBoundsException on a too high index
775 dl 1.1 */
776 dl 1.4 public void testListIterator2_IndexOutOfBoundsException() {
777 jsr166 1.30 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     }
785 dl 1.1 }
786    
787     /**
788 jsr166 1.18 * subList throws an IndexOutOfBoundsException on a negative index
789 dl 1.1 */
790 dl 1.4 public void testSubList1_IndexOutOfBoundsException() {
791 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
792     List[] lists = { c, c.subList(1, c.size() - 1) };
793     for (List list : lists) {
794     try {
795     list.subList(-1, list.size());
796     shouldThrow();
797     } catch (IndexOutOfBoundsException success) {}
798     }
799 dl 1.1 }
800    
801     /**
802 jsr166 1.18 * subList throws an IndexOutOfBoundsException on a too high index
803 dl 1.1 */
804 dl 1.4 public void testSubList2_IndexOutOfBoundsException() {
805 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
806     List[] lists = { c, c.subList(1, c.size() - 1) };
807     for (List list : lists) {
808     try {
809     list.subList(0, list.size() + 1);
810     shouldThrow();
811     } catch (IndexOutOfBoundsException success) {}
812     }
813 dl 1.1 }
814    
815     /**
816 jsr166 1.18 * subList throws IndexOutOfBoundsException when the second index
817     * is lower then the first
818 dl 1.1 */
819 dl 1.4 public void testSubList3_IndexOutOfBoundsException() {
820 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
821     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     }
828 dl 1.2 }
829    
830 dl 1.4 /**
831 jsr166 1.45 * a deserialized/reserialized list equals original
832 dl 1.4 */
833 jsr166 1.12 public void testSerialization() throws Exception {
834 jsr166 1.22 List x = populatedArray(SIZE);
835     List y = serialClone(x);
836 dl 1.2
837 jsr166 1.29 assertNotSame(x, y);
838 jsr166 1.22 assertEquals(x.size(), y.size());
839     assertEquals(x.toString(), y.toString());
840     assertTrue(Arrays.equals(x.toArray(), y.toArray()));
841     assertEquals(x, y);
842     assertEquals(y, x);
843     while (!x.isEmpty()) {
844     assertFalse(y.isEmpty());
845     assertEquals(x.remove(0), y.remove(0));
846     }
847     assertTrue(y.isEmpty());
848 dl 1.1 }
849 jsr166 1.8
850 dl 1.1 }