ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.45
Committed: Fri Aug 4 03:30:21 2017 UTC (6 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.44: +1 -1 lines
Log Message:
improve javadoc wording for testSerialization methods

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     for (int i = 0; i < elements.length; i++)
60     a.add(elements[i]);
61     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.18 * indexOf gives the index for the given object
238 dl 1.1 */
239 dl 1.4 public void testIndexOf() {
240 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
241     assertEquals(1, full.indexOf(one));
242     assertEquals(-1, full.indexOf("puppies"));
243 dl 1.1 }
244    
245     /**
246 jsr166 1.18 * indexOf gives the index based on the given index
247     * at which to start searching
248 dl 1.1 */
249 dl 1.4 public void testIndexOf2() {
250 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
251     assertEquals(1, full.indexOf(one, 0));
252     assertEquals(-1, full.indexOf(one, 2));
253 dl 1.1 }
254    
255     /**
256 jsr166 1.18 * isEmpty returns true when empty, else false
257 dl 1.1 */
258 dl 1.4 public void testIsEmpty() {
259 jsr166 1.11 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
260     CopyOnWriteArrayList full = populatedArray(SIZE);
261     assertTrue(empty.isEmpty());
262     assertFalse(full.isEmpty());
263 dl 1.1 }
264    
265     /**
266 jsr166 1.27 * iterator() returns an iterator containing the elements of the
267     * list in insertion order
268 dl 1.1 */
269 dl 1.4 public void testIterator() {
270 jsr166 1.27 Collection empty = new CopyOnWriteArrayList();
271     assertFalse(empty.iterator().hasNext());
272     try {
273     empty.iterator().next();
274     shouldThrow();
275     } catch (NoSuchElementException success) {}
276    
277     Integer[] elements = new Integer[SIZE];
278     for (int i = 0; i < SIZE; i++)
279     elements[i] = i;
280 jsr166 1.38 shuffle(elements);
281 jsr166 1.27 Collection<Integer> full = populatedArray(elements);
282    
283     Iterator it = full.iterator();
284     for (int j = 0; j < SIZE; j++) {
285     assertTrue(it.hasNext());
286     assertEquals(elements[j], it.next());
287     }
288 jsr166 1.32 assertIteratorExhausted(it);
289     }
290    
291     /**
292     * iterator of empty collection has no elements
293     */
294     public void testEmptyIterator() {
295     Collection c = new CopyOnWriteArrayList();
296     assertIteratorExhausted(c.iterator());
297 dl 1.1 }
298    
299 dl 1.4 /**
300 dl 1.5 * iterator.remove throws UnsupportedOperationException
301 dl 1.4 */
302 jsr166 1.17 public void testIteratorRemove() {
303 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
304 dl 1.1 Iterator it = full.iterator();
305     it.next();
306     try {
307     it.remove();
308 dl 1.4 shouldThrow();
309 jsr166 1.12 } catch (UnsupportedOperationException success) {}
310 dl 1.1 }
311    
312 dl 1.4 /**
313 dl 1.5 * toString contains toString of elements
314 dl 1.4 */
315     public void testToString() {
316 jsr166 1.27 assertEquals("[]", new CopyOnWriteArrayList().toString());
317 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
318 dl 1.1 String s = full.toString();
319 jsr166 1.27 for (int i = 0; i < 3; ++i)
320 jsr166 1.20 assertTrue(s.contains(String.valueOf(i)));
321 jsr166 1.27 assertEquals(new ArrayList(full).toString(),
322     full.toString());
323 jsr166 1.8 }
324 dl 1.1
325     /**
326 jsr166 1.18 * lastIndexOf returns the index for the given object
327 dl 1.1 */
328 dl 1.4 public void testLastIndexOf1() {
329 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
330     full.add(one);
331     full.add(three);
332     assertEquals(3, full.lastIndexOf(one));
333     assertEquals(-1, full.lastIndexOf(six));
334 dl 1.1 }
335    
336     /**
337 jsr166 1.18 * lastIndexOf returns the index from the given starting point
338 dl 1.1 */
339 jsr166 1.21 public void testLastIndexOf2() {
340 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
341     full.add(one);
342     full.add(three);
343     assertEquals(3, full.lastIndexOf(one, 4));
344     assertEquals(-1, full.lastIndexOf(three, 3));
345 dl 1.1 }
346    
347     /**
348 jsr166 1.18 * listIterator traverses all elements
349 dl 1.1 */
350 dl 1.4 public void testListIterator1() {
351 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
352     ListIterator i = full.listIterator();
353     int j;
354     for (j = 0; i.hasNext(); j++)
355 jsr166 1.14 assertEquals(j, i.next());
356 jsr166 1.11 assertEquals(SIZE, j);
357 dl 1.1 }
358    
359     /**
360 jsr166 1.18 * listIterator only returns those elements after the given index
361 dl 1.1 */
362 dl 1.4 public void testListIterator2() {
363 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
364     ListIterator i = full.listIterator(1);
365     int j;
366     for (j = 0; i.hasNext(); j++)
367 jsr166 1.36 assertEquals(j + 1, i.next());
368 jsr166 1.11 assertEquals(2, j);
369 dl 1.1 }
370    
371     /**
372 jsr166 1.28 * remove(int) removes and returns the object at the given index
373 dl 1.1 */
374 jsr166 1.28 public void testRemove_int() {
375     int SIZE = 3;
376     for (int i = 0; i < SIZE; i++) {
377     CopyOnWriteArrayList full = populatedArray(SIZE);
378     assertEquals(i, full.remove(i));
379     assertEquals(SIZE - 1, full.size());
380     assertFalse(full.contains(new Integer(i)));
381     }
382     }
383    
384     /**
385     * remove(Object) removes the object if found and returns true
386     */
387     public void testRemove_Object() {
388     int SIZE = 3;
389     for (int i = 0; i < SIZE; i++) {
390     CopyOnWriteArrayList full = populatedArray(SIZE);
391     assertFalse(full.remove(new Integer(-42)));
392     assertTrue(full.remove(new Integer(i)));
393     assertEquals(SIZE - 1, full.size());
394     assertFalse(full.contains(new Integer(i)));
395     }
396     CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
397     assertTrue(x.remove(new Integer(6)));
398     assertEquals(x, Arrays.asList(4, 5));
399     assertTrue(x.remove(new Integer(4)));
400     assertEquals(x, Arrays.asList(5));
401     assertTrue(x.remove(new Integer(5)));
402     assertEquals(x, Arrays.asList());
403     assertFalse(x.remove(new Integer(5)));
404 dl 1.1 }
405    
406     /**
407 jsr166 1.18 * removeAll removes all elements from the given collection
408 dl 1.1 */
409 dl 1.4 public void testRemoveAll() {
410 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
411 jsr166 1.33 assertTrue(full.removeAll(Arrays.asList(one, two)));
412     assertEquals(1, full.size());
413     assertFalse(full.removeAll(Arrays.asList(one, two)));
414 jsr166 1.11 assertEquals(1, full.size());
415 dl 1.1 }
416    
417     /**
418 jsr166 1.18 * set changes the element at the given index
419 dl 1.1 */
420 dl 1.4 public void testSet() {
421 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
422 jsr166 1.15 assertEquals(2, full.set(2, four));
423 jsr166 1.14 assertEquals(4, full.get(2));
424 dl 1.1 }
425    
426     /**
427 jsr166 1.18 * size returns the number of elements
428 dl 1.1 */
429 dl 1.4 public void testSize() {
430 jsr166 1.11 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
431     CopyOnWriteArrayList full = populatedArray(SIZE);
432     assertEquals(SIZE, full.size());
433     assertEquals(0, empty.size());
434 dl 1.1 }
435    
436     /**
437 jsr166 1.26 * toArray() returns an Object array containing all elements from
438     * the list in insertion order
439 dl 1.1 */
440 dl 1.4 public void testToArray() {
441 jsr166 1.26 Object[] a = new CopyOnWriteArrayList().toArray();
442     assertTrue(Arrays.equals(new Object[0], a));
443     assertSame(Object[].class, a.getClass());
444    
445     Integer[] elements = new Integer[SIZE];
446     for (int i = 0; i < SIZE; i++)
447     elements[i] = i;
448 jsr166 1.38 shuffle(elements);
449 jsr166 1.26 Collection<Integer> full = populatedArray(elements);
450    
451     assertTrue(Arrays.equals(elements, full.toArray()));
452     assertSame(Object[].class, full.toArray().getClass());
453 dl 1.1 }
454    
455     /**
456 jsr166 1.26 * toArray(Integer array) returns an Integer array containing all
457     * elements from the list in insertion order
458 dl 1.1 */
459 dl 1.4 public void testToArray2() {
460 jsr166 1.26 Collection empty = new CopyOnWriteArrayList();
461     Integer[] a;
462    
463     a = new Integer[0];
464     assertSame(a, empty.toArray(a));
465    
466 jsr166 1.35 a = new Integer[SIZE / 2];
467 jsr166 1.26 Arrays.fill(a, 42);
468     assertSame(a, empty.toArray(a));
469     assertNull(a[0]);
470     for (int i = 1; i < a.length; i++)
471     assertEquals(42, (int) a[i]);
472    
473     Integer[] elements = new Integer[SIZE];
474     for (int i = 0; i < SIZE; i++)
475     elements[i] = i;
476 jsr166 1.38 shuffle(elements);
477 jsr166 1.26 Collection<Integer> full = populatedArray(elements);
478    
479     Arrays.fill(a, 42);
480     assertTrue(Arrays.equals(elements, full.toArray(a)));
481     for (int i = 0; i < a.length; i++)
482     assertEquals(42, (int) a[i]);
483     assertSame(Integer[].class, full.toArray(a).getClass());
484    
485     a = new Integer[SIZE];
486     Arrays.fill(a, 42);
487     assertSame(a, full.toArray(a));
488     assertTrue(Arrays.equals(elements, a));
489    
490 jsr166 1.35 a = new Integer[2 * SIZE];
491 jsr166 1.26 Arrays.fill(a, 42);
492     assertSame(a, full.toArray(a));
493     assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
494     assertNull(a[SIZE]);
495     for (int i = SIZE + 1; i < a.length; i++)
496     assertEquals(42, (int) a[i]);
497 dl 1.1 }
498    
499 dl 1.4 /**
500 dl 1.5 * sublists contains elements at indexes offset from their base
501 dl 1.4 */
502 dl 1.1 public void testSubList() {
503 jsr166 1.11 CopyOnWriteArrayList a = populatedArray(10);
504 dl 1.1 assertTrue(a.subList(1,1).isEmpty());
505 jsr166 1.11 for (int j = 0; j < 9; ++j) {
506     for (int i = j ; i < 10; ++i) {
507     List b = a.subList(j,i);
508     for (int k = j; k < i; ++k) {
509     assertEquals(new Integer(k), b.get(k-j));
510     }
511     }
512     }
513 dl 1.1
514 jsr166 1.11 List s = a.subList(2, 5);
515 jsr166 1.24 assertEquals(3, s.size());
516 dl 1.3 s.set(2, m1);
517 dl 1.1 assertEquals(a.get(4), m1);
518 jsr166 1.11 s.clear();
519 jsr166 1.24 assertEquals(7, a.size());
520 dl 1.1 }
521    
522     // Exception tests
523    
524     /**
525 jsr166 1.18 * toArray throws an ArrayStoreException when the given array
526     * can not store the objects inside the list
527 dl 1.1 */
528 dl 1.4 public void testToArray_ArrayStoreException() {
529 jsr166 1.30 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
530     c.add("zfasdfsdf");
531     c.add("asdadasd");
532 dl 1.4 try {
533 dl 1.1 c.toArray(new Long[5]);
534 jsr166 1.11 shouldThrow();
535 jsr166 1.13 } catch (ArrayStoreException success) {}
536 dl 1.1 }
537    
538     /**
539 jsr166 1.18 * get throws an IndexOutOfBoundsException on a negative index
540 dl 1.1 */
541 dl 1.4 public void testGet1_IndexOutOfBoundsException() {
542 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
543     List[] lists = { c, c.subList(1, c.size() - 1) };
544     for (List list : lists) {
545     try {
546     list.get(-1);
547     shouldThrow();
548     } catch (IndexOutOfBoundsException success) {}
549     }
550 dl 1.1 }
551 jsr166 1.8
552 dl 1.1 /**
553 jsr166 1.18 * get throws an IndexOutOfBoundsException on a too high index
554 dl 1.1 */
555 dl 1.4 public void testGet2_IndexOutOfBoundsException() {
556 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
557     List[] lists = { c, c.subList(1, c.size() - 1) };
558     for (List list : lists) {
559     try {
560     list.get(list.size());
561     shouldThrow();
562     } catch (IndexOutOfBoundsException success) {}
563     }
564 dl 1.1 }
565    
566     /**
567 jsr166 1.18 * set throws an IndexOutOfBoundsException on a negative index
568 dl 1.1 */
569 dl 1.4 public void testSet1_IndexOutOfBoundsException() {
570 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
571     List[] lists = { c, c.subList(1, c.size() - 1) };
572     for (List list : lists) {
573     try {
574     list.set(-1, "qwerty");
575     shouldThrow();
576     } catch (IndexOutOfBoundsException success) {}
577     }
578 dl 1.1 }
579 jsr166 1.8
580 dl 1.1 /**
581 jsr166 1.18 * set throws an IndexOutOfBoundsException on a too high index
582 dl 1.1 */
583 dl 1.4 public void testSet2() {
584 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
585     List[] lists = { c, c.subList(1, c.size() - 1) };
586     for (List list : lists) {
587     try {
588     list.set(list.size(), "qwerty");
589     shouldThrow();
590     } catch (IndexOutOfBoundsException success) {}
591     }
592 dl 1.1 }
593    
594     /**
595 jsr166 1.18 * add throws an IndexOutOfBoundsException on a negative index
596 dl 1.1 */
597 dl 1.4 public void testAdd1_IndexOutOfBoundsException() {
598 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
599     List[] lists = { c, c.subList(1, c.size() - 1) };
600     for (List list : lists) {
601     try {
602     list.add(-1, "qwerty");
603     shouldThrow();
604     } catch (IndexOutOfBoundsException success) {}
605     }
606 dl 1.1 }
607 jsr166 1.8
608 dl 1.1 /**
609 jsr166 1.18 * add throws an IndexOutOfBoundsException on a too high index
610 dl 1.1 */
611 dl 1.4 public void testAdd2_IndexOutOfBoundsException() {
612 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
613     List[] lists = { c, c.subList(1, c.size() - 1) };
614     for (List list : lists) {
615     try {
616     list.add(list.size() + 1, "qwerty");
617     shouldThrow();
618     } catch (IndexOutOfBoundsException success) {}
619     }
620 dl 1.1 }
621    
622     /**
623 jsr166 1.18 * remove throws an IndexOutOfBoundsException on a negative index
624 dl 1.1 */
625 dl 1.4 public void testRemove1_IndexOutOfBounds() {
626 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
627     List[] lists = { c, c.subList(1, c.size() - 1) };
628     for (List list : lists) {
629     try {
630     list.remove(-1);
631     shouldThrow();
632     } catch (IndexOutOfBoundsException success) {}
633     }
634 dl 1.1 }
635    
636     /**
637 jsr166 1.18 * remove throws an IndexOutOfBoundsException on a too high index
638 dl 1.1 */
639 dl 1.4 public void testRemove2_IndexOutOfBounds() {
640 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
641     List[] lists = { c, c.subList(1, c.size() - 1) };
642     for (List list : lists) {
643     try {
644     list.remove(list.size());
645     shouldThrow();
646     } catch (IndexOutOfBoundsException success) {}
647     }
648 dl 1.1 }
649 jsr166 1.8
650 dl 1.1 /**
651 jsr166 1.18 * addAll throws an IndexOutOfBoundsException on a negative index
652 dl 1.1 */
653 dl 1.4 public void testAddAll1_IndexOutOfBoundsException() {
654 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
655     List[] lists = { c, c.subList(1, c.size() - 1) };
656     for (List list : lists) {
657     try {
658     list.addAll(-1, new LinkedList());
659     shouldThrow();
660     } catch (IndexOutOfBoundsException success) {}
661     }
662 dl 1.1 }
663 jsr166 1.8
664 dl 1.1 /**
665 jsr166 1.18 * addAll throws an IndexOutOfBoundsException on a too high index
666 dl 1.1 */
667 dl 1.4 public void testAddAll2_IndexOutOfBoundsException() {
668 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
669     List[] lists = { c, c.subList(1, c.size() - 1) };
670     for (List list : lists) {
671     try {
672     list.addAll(list.size() + 1, new LinkedList());
673     shouldThrow();
674     } catch (IndexOutOfBoundsException success) {}
675     }
676 dl 1.1 }
677    
678     /**
679 jsr166 1.18 * listIterator throws an IndexOutOfBoundsException on a negative index
680 dl 1.1 */
681 dl 1.4 public void testListIterator1_IndexOutOfBoundsException() {
682 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
683     List[] lists = { c, c.subList(1, c.size() - 1) };
684     for (List list : lists) {
685     try {
686     list.listIterator(-1);
687     shouldThrow();
688     } catch (IndexOutOfBoundsException success) {}
689     }
690 dl 1.1 }
691    
692     /**
693 jsr166 1.18 * listIterator throws an IndexOutOfBoundsException on a too high index
694 dl 1.1 */
695 dl 1.4 public void testListIterator2_IndexOutOfBoundsException() {
696 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
697     List[] lists = { c, c.subList(1, c.size() - 1) };
698     for (List list : lists) {
699     try {
700     list.listIterator(list.size() + 1);
701     shouldThrow();
702     } catch (IndexOutOfBoundsException success) {}
703     }
704 dl 1.1 }
705    
706     /**
707 jsr166 1.18 * subList throws an IndexOutOfBoundsException on a negative index
708 dl 1.1 */
709 dl 1.4 public void testSubList1_IndexOutOfBoundsException() {
710 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
711     List[] lists = { c, c.subList(1, c.size() - 1) };
712     for (List list : lists) {
713     try {
714     list.subList(-1, list.size());
715     shouldThrow();
716     } catch (IndexOutOfBoundsException success) {}
717     }
718 dl 1.1 }
719    
720     /**
721 jsr166 1.18 * subList throws an IndexOutOfBoundsException on a too high index
722 dl 1.1 */
723 dl 1.4 public void testSubList2_IndexOutOfBoundsException() {
724 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
725     List[] lists = { c, c.subList(1, c.size() - 1) };
726     for (List list : lists) {
727     try {
728     list.subList(0, list.size() + 1);
729     shouldThrow();
730     } catch (IndexOutOfBoundsException success) {}
731     }
732 dl 1.1 }
733    
734     /**
735 jsr166 1.18 * subList throws IndexOutOfBoundsException when the second index
736     * is lower then the first
737 dl 1.1 */
738 dl 1.4 public void testSubList3_IndexOutOfBoundsException() {
739 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
740     List[] lists = { c, c.subList(1, c.size() - 1) };
741     for (List list : lists) {
742     try {
743     list.subList(list.size() - 1, 1);
744     shouldThrow();
745     } catch (IndexOutOfBoundsException success) {}
746     }
747 dl 1.2 }
748    
749 dl 1.4 /**
750 jsr166 1.45 * a deserialized/reserialized list equals original
751 dl 1.4 */
752 jsr166 1.12 public void testSerialization() throws Exception {
753 jsr166 1.22 List x = populatedArray(SIZE);
754     List y = serialClone(x);
755 dl 1.2
756 jsr166 1.29 assertNotSame(x, y);
757 jsr166 1.22 assertEquals(x.size(), y.size());
758     assertEquals(x.toString(), y.toString());
759     assertTrue(Arrays.equals(x.toArray(), y.toArray()));
760     assertEquals(x, y);
761     assertEquals(y, x);
762     while (!x.isEmpty()) {
763     assertFalse(y.isEmpty());
764     assertEquals(x.remove(0), y.remove(0));
765     }
766     assertTrue(y.isEmpty());
767 dl 1.1 }
768 jsr166 1.8
769 dl 1.1 }