ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.39
Committed: Wed Aug 24 22:22:39 2016 UTC (7 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +0 -1 lines
Log Message:
fix imports

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     import junit.framework.TestSuite;
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.11 return new TestSuite(CopyOnWriteArrayListTest.class);
30 dl 1.1 }
31    
32 jsr166 1.25 static CopyOnWriteArrayList<Integer> populatedArray(int n) {
33 jsr166 1.26 CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
34 dl 1.1 assertTrue(a.isEmpty());
35 jsr166 1.26 for (int i = 0; i < n; i++)
36 jsr166 1.25 a.add(i);
37 dl 1.1 assertFalse(a.isEmpty());
38     assertEquals(n, a.size());
39     return a;
40     }
41    
42 jsr166 1.26 static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) {
43     CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
44     assertTrue(a.isEmpty());
45     for (int i = 0; i < elements.length; i++)
46     a.add(elements[i]);
47     assertFalse(a.isEmpty());
48     assertEquals(elements.length, a.size());
49     return a;
50     }
51    
52 dl 1.4 /**
53 dl 1.5 * a new list is empty
54 dl 1.4 */
55 dl 1.3 public void testConstructor() {
56 jsr166 1.11 CopyOnWriteArrayList a = new CopyOnWriteArrayList();
57 dl 1.3 assertTrue(a.isEmpty());
58     }
59    
60 dl 1.4 /**
61 dl 1.5 * new list contains all elements of initializing array
62 dl 1.4 */
63 dl 1.3 public void testConstructor2() {
64     Integer[] ints = new Integer[SIZE];
65 jsr166 1.35 for (int i = 0; i < SIZE - 1; ++i)
66 dl 1.3 ints[i] = new Integer(i);
67 jsr166 1.11 CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
68 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
69 dl 1.3 assertEquals(ints[i], a.get(i));
70     }
71    
72 dl 1.4 /**
73 dl 1.5 * new list contains all elements of initializing collection
74 dl 1.4 */
75 dl 1.3 public void testConstructor3() {
76     Integer[] ints = new Integer[SIZE];
77 jsr166 1.35 for (int i = 0; i < SIZE - 1; ++i)
78 dl 1.3 ints[i] = new Integer(i);
79 jsr166 1.11 CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
80 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
81 dl 1.3 assertEquals(ints[i], a.get(i));
82     }
83 jsr166 1.8
84 dl 1.1 /**
85 jsr166 1.33 * addAll adds each element from the given collection, including duplicates
86 dl 1.1 */
87 dl 1.4 public void testAddAll() {
88 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
89 jsr166 1.33 assertTrue(full.addAll(Arrays.asList(three, four, five)));
90 jsr166 1.11 assertEquals(6, full.size());
91 jsr166 1.33 assertTrue(full.addAll(Arrays.asList(three, four, five)));
92     assertEquals(9, full.size());
93 dl 1.1 }
94    
95     /**
96 jsr166 1.18 * addAllAbsent adds each element from the given collection that did not
97     * already exist in the List
98 dl 1.1 */
99 dl 1.4 public void testAddAllAbsent() {
100 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
101 jsr166 1.33 // "one" is duplicate and will not be added
102     assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one)));
103     assertEquals(5, full.size());
104     assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one)));
105 jsr166 1.11 assertEquals(5, full.size());
106 dl 1.1 }
107    
108     /**
109 jsr166 1.18 * addIfAbsent will not add the element if it already exists in the list
110 dl 1.1 */
111 dl 1.4 public void testAddIfAbsent() {
112 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
113     full.addIfAbsent(one);
114     assertEquals(SIZE, full.size());
115 dl 1.1 }
116    
117     /**
118 jsr166 1.18 * addIfAbsent adds the element when it does not exist in the list
119 dl 1.1 */
120 dl 1.4 public void testAddIfAbsent2() {
121 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
122 dl 1.3 full.addIfAbsent(three);
123     assertTrue(full.contains(three));
124 dl 1.1 }
125    
126     /**
127 jsr166 1.18 * clear removes all elements from the list
128 dl 1.1 */
129 dl 1.4 public void testClear() {
130 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
131     full.clear();
132     assertEquals(0, full.size());
133 dl 1.1 }
134    
135 dl 1.4 /**
136 jsr166 1.18 * Cloned list is equal
137 dl 1.4 */
138     public void testClone() {
139 jsr166 1.11 CopyOnWriteArrayList l1 = populatedArray(SIZE);
140     CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
141 dl 1.4 assertEquals(l1, l2);
142 jsr166 1.11 l1.clear();
143 dl 1.4 assertFalse(l1.equals(l2));
144     }
145    
146 dl 1.1 /**
147 jsr166 1.18 * contains is true for added elements
148 dl 1.1 */
149 dl 1.4 public void testContains() {
150 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
151     assertTrue(full.contains(one));
152     assertFalse(full.contains(five));
153 dl 1.1 }
154    
155 dl 1.4 /**
156 dl 1.7 * adding at an index places it in the indicated index
157 dl 1.4 */
158 dl 1.1 public void testAddIndex() {
159 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
160 dl 1.3 full.add(0, m1);
161 dl 1.1 assertEquals(4, full.size());
162 dl 1.3 assertEquals(m1, full.get(0));
163     assertEquals(zero, full.get(1));
164 dl 1.1
165 dl 1.3 full.add(2, m2);
166 dl 1.1 assertEquals(5, full.size());
167 dl 1.3 assertEquals(m2, full.get(2));
168     assertEquals(two, full.get(4));
169 dl 1.1 }
170    
171 dl 1.4 /**
172 dl 1.5 * lists with same elements are equal and have same hashCode
173 dl 1.4 */
174 dl 1.1 public void testEquals() {
175 jsr166 1.11 CopyOnWriteArrayList a = populatedArray(3);
176     CopyOnWriteArrayList b = populatedArray(3);
177 dl 1.1 assertTrue(a.equals(b));
178     assertTrue(b.equals(a));
179 jsr166 1.37 assertTrue(a.containsAll(b));
180     assertTrue(b.containsAll(a));
181 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
182 dl 1.3 a.add(m1);
183 dl 1.1 assertFalse(a.equals(b));
184     assertFalse(b.equals(a));
185 jsr166 1.37 assertTrue(a.containsAll(b));
186     assertFalse(b.containsAll(a));
187 dl 1.3 b.add(m1);
188 dl 1.1 assertTrue(a.equals(b));
189     assertTrue(b.equals(a));
190 jsr166 1.37 assertTrue(a.containsAll(b));
191     assertTrue(b.containsAll(a));
192 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
193 jsr166 1.37
194     assertFalse(a.equals(null));
195 dl 1.1 }
196    
197     /**
198 jsr166 1.37 * containsAll returns true for collections with subset of elements
199 dl 1.1 */
200 dl 1.4 public void testContainsAll() {
201 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
202 jsr166 1.33 assertTrue(full.containsAll(Arrays.asList()));
203     assertTrue(full.containsAll(Arrays.asList(one)));
204     assertTrue(full.containsAll(Arrays.asList(one, two)));
205     assertFalse(full.containsAll(Arrays.asList(one, two, six)));
206     assertFalse(full.containsAll(Arrays.asList(six)));
207 jsr166 1.37
208     try {
209     full.containsAll(null);
210     shouldThrow();
211     } catch (NullPointerException success) {}
212 dl 1.1 }
213    
214     /**
215 jsr166 1.18 * get returns the value at the given index
216 dl 1.1 */
217 dl 1.4 public void testGet() {
218 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
219 jsr166 1.14 assertEquals(0, full.get(0));
220 dl 1.1 }
221    
222     /**
223 jsr166 1.18 * indexOf gives the index for the given object
224 dl 1.1 */
225 dl 1.4 public void testIndexOf() {
226 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
227     assertEquals(1, full.indexOf(one));
228     assertEquals(-1, full.indexOf("puppies"));
229 dl 1.1 }
230    
231     /**
232 jsr166 1.18 * indexOf gives the index based on the given index
233     * at which to start searching
234 dl 1.1 */
235 dl 1.4 public void testIndexOf2() {
236 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
237     assertEquals(1, full.indexOf(one, 0));
238     assertEquals(-1, full.indexOf(one, 2));
239 dl 1.1 }
240    
241     /**
242 jsr166 1.18 * isEmpty returns true when empty, else false
243 dl 1.1 */
244 dl 1.4 public void testIsEmpty() {
245 jsr166 1.11 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
246     CopyOnWriteArrayList full = populatedArray(SIZE);
247     assertTrue(empty.isEmpty());
248     assertFalse(full.isEmpty());
249 dl 1.1 }
250    
251     /**
252 jsr166 1.27 * iterator() returns an iterator containing the elements of the
253     * list in insertion order
254 dl 1.1 */
255 dl 1.4 public void testIterator() {
256 jsr166 1.27 Collection empty = new CopyOnWriteArrayList();
257     assertFalse(empty.iterator().hasNext());
258     try {
259     empty.iterator().next();
260     shouldThrow();
261     } catch (NoSuchElementException success) {}
262    
263     Integer[] elements = new Integer[SIZE];
264     for (int i = 0; i < SIZE; i++)
265     elements[i] = i;
266 jsr166 1.38 shuffle(elements);
267 jsr166 1.27 Collection<Integer> full = populatedArray(elements);
268    
269     Iterator it = full.iterator();
270     for (int j = 0; j < SIZE; j++) {
271     assertTrue(it.hasNext());
272     assertEquals(elements[j], it.next());
273     }
274 jsr166 1.32 assertIteratorExhausted(it);
275     }
276    
277     /**
278     * iterator of empty collection has no elements
279     */
280     public void testEmptyIterator() {
281     Collection c = new CopyOnWriteArrayList();
282     assertIteratorExhausted(c.iterator());
283 dl 1.1 }
284    
285 dl 1.4 /**
286 dl 1.5 * iterator.remove throws UnsupportedOperationException
287 dl 1.4 */
288 jsr166 1.17 public void testIteratorRemove() {
289 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
290 dl 1.1 Iterator it = full.iterator();
291     it.next();
292     try {
293     it.remove();
294 dl 1.4 shouldThrow();
295 jsr166 1.12 } catch (UnsupportedOperationException success) {}
296 dl 1.1 }
297    
298 dl 1.4 /**
299 dl 1.5 * toString contains toString of elements
300 dl 1.4 */
301     public void testToString() {
302 jsr166 1.27 assertEquals("[]", new CopyOnWriteArrayList().toString());
303 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
304 dl 1.1 String s = full.toString();
305 jsr166 1.27 for (int i = 0; i < 3; ++i)
306 jsr166 1.20 assertTrue(s.contains(String.valueOf(i)));
307 jsr166 1.27 assertEquals(new ArrayList(full).toString(),
308     full.toString());
309 jsr166 1.8 }
310 dl 1.1
311     /**
312 jsr166 1.18 * lastIndexOf returns the index for the given object
313 dl 1.1 */
314 dl 1.4 public void testLastIndexOf1() {
315 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
316     full.add(one);
317     full.add(three);
318     assertEquals(3, full.lastIndexOf(one));
319     assertEquals(-1, full.lastIndexOf(six));
320 dl 1.1 }
321    
322     /**
323 jsr166 1.18 * lastIndexOf returns the index from the given starting point
324 dl 1.1 */
325 jsr166 1.21 public void testLastIndexOf2() {
326 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
327     full.add(one);
328     full.add(three);
329     assertEquals(3, full.lastIndexOf(one, 4));
330     assertEquals(-1, full.lastIndexOf(three, 3));
331 dl 1.1 }
332    
333     /**
334 jsr166 1.18 * listIterator traverses all elements
335 dl 1.1 */
336 dl 1.4 public void testListIterator1() {
337 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(SIZE);
338     ListIterator i = full.listIterator();
339     int j;
340     for (j = 0; i.hasNext(); j++)
341 jsr166 1.14 assertEquals(j, i.next());
342 jsr166 1.11 assertEquals(SIZE, j);
343 dl 1.1 }
344    
345     /**
346 jsr166 1.18 * listIterator only returns those elements after the given index
347 dl 1.1 */
348 dl 1.4 public void testListIterator2() {
349 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
350     ListIterator i = full.listIterator(1);
351     int j;
352     for (j = 0; i.hasNext(); j++)
353 jsr166 1.36 assertEquals(j + 1, i.next());
354 jsr166 1.11 assertEquals(2, j);
355 dl 1.1 }
356    
357     /**
358 jsr166 1.28 * remove(int) removes and returns the object at the given index
359 dl 1.1 */
360 jsr166 1.28 public void testRemove_int() {
361     int SIZE = 3;
362     for (int i = 0; i < SIZE; i++) {
363     CopyOnWriteArrayList full = populatedArray(SIZE);
364     assertEquals(i, full.remove(i));
365     assertEquals(SIZE - 1, full.size());
366     assertFalse(full.contains(new Integer(i)));
367     }
368     }
369    
370     /**
371     * remove(Object) removes the object if found and returns true
372     */
373     public void testRemove_Object() {
374     int SIZE = 3;
375     for (int i = 0; i < SIZE; i++) {
376     CopyOnWriteArrayList full = populatedArray(SIZE);
377     assertFalse(full.remove(new Integer(-42)));
378     assertTrue(full.remove(new Integer(i)));
379     assertEquals(SIZE - 1, full.size());
380     assertFalse(full.contains(new Integer(i)));
381     }
382     CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
383     assertTrue(x.remove(new Integer(6)));
384     assertEquals(x, Arrays.asList(4, 5));
385     assertTrue(x.remove(new Integer(4)));
386     assertEquals(x, Arrays.asList(5));
387     assertTrue(x.remove(new Integer(5)));
388     assertEquals(x, Arrays.asList());
389     assertFalse(x.remove(new Integer(5)));
390 dl 1.1 }
391    
392     /**
393 jsr166 1.18 * removeAll removes all elements from the given collection
394 dl 1.1 */
395 dl 1.4 public void testRemoveAll() {
396 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
397 jsr166 1.33 assertTrue(full.removeAll(Arrays.asList(one, two)));
398     assertEquals(1, full.size());
399     assertFalse(full.removeAll(Arrays.asList(one, two)));
400 jsr166 1.11 assertEquals(1, full.size());
401 dl 1.1 }
402    
403     /**
404 jsr166 1.18 * set changes the element at the given index
405 dl 1.1 */
406 dl 1.4 public void testSet() {
407 jsr166 1.11 CopyOnWriteArrayList full = populatedArray(3);
408 jsr166 1.15 assertEquals(2, full.set(2, four));
409 jsr166 1.14 assertEquals(4, full.get(2));
410 dl 1.1 }
411    
412     /**
413 jsr166 1.18 * size returns the number of elements
414 dl 1.1 */
415 dl 1.4 public void testSize() {
416 jsr166 1.11 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
417     CopyOnWriteArrayList full = populatedArray(SIZE);
418     assertEquals(SIZE, full.size());
419     assertEquals(0, empty.size());
420 dl 1.1 }
421    
422     /**
423 jsr166 1.26 * toArray() returns an Object array containing all elements from
424     * the list in insertion order
425 dl 1.1 */
426 dl 1.4 public void testToArray() {
427 jsr166 1.26 Object[] a = new CopyOnWriteArrayList().toArray();
428     assertTrue(Arrays.equals(new Object[0], a));
429     assertSame(Object[].class, a.getClass());
430    
431     Integer[] elements = new Integer[SIZE];
432     for (int i = 0; i < SIZE; i++)
433     elements[i] = i;
434 jsr166 1.38 shuffle(elements);
435 jsr166 1.26 Collection<Integer> full = populatedArray(elements);
436    
437     assertTrue(Arrays.equals(elements, full.toArray()));
438     assertSame(Object[].class, full.toArray().getClass());
439 dl 1.1 }
440    
441     /**
442 jsr166 1.26 * toArray(Integer array) returns an Integer array containing all
443     * elements from the list in insertion order
444 dl 1.1 */
445 dl 1.4 public void testToArray2() {
446 jsr166 1.26 Collection empty = new CopyOnWriteArrayList();
447     Integer[] a;
448    
449     a = new Integer[0];
450     assertSame(a, empty.toArray(a));
451    
452 jsr166 1.35 a = new Integer[SIZE / 2];
453 jsr166 1.26 Arrays.fill(a, 42);
454     assertSame(a, empty.toArray(a));
455     assertNull(a[0]);
456     for (int i = 1; i < a.length; i++)
457     assertEquals(42, (int) a[i]);
458    
459     Integer[] elements = new Integer[SIZE];
460     for (int i = 0; i < SIZE; i++)
461     elements[i] = i;
462 jsr166 1.38 shuffle(elements);
463 jsr166 1.26 Collection<Integer> full = populatedArray(elements);
464    
465     Arrays.fill(a, 42);
466     assertTrue(Arrays.equals(elements, full.toArray(a)));
467     for (int i = 0; i < a.length; i++)
468     assertEquals(42, (int) a[i]);
469     assertSame(Integer[].class, full.toArray(a).getClass());
470    
471     a = new Integer[SIZE];
472     Arrays.fill(a, 42);
473     assertSame(a, full.toArray(a));
474     assertTrue(Arrays.equals(elements, a));
475    
476 jsr166 1.35 a = new Integer[2 * SIZE];
477 jsr166 1.26 Arrays.fill(a, 42);
478     assertSame(a, full.toArray(a));
479     assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
480     assertNull(a[SIZE]);
481     for (int i = SIZE + 1; i < a.length; i++)
482     assertEquals(42, (int) a[i]);
483 dl 1.1 }
484    
485 dl 1.4 /**
486 dl 1.5 * sublists contains elements at indexes offset from their base
487 dl 1.4 */
488 dl 1.1 public void testSubList() {
489 jsr166 1.11 CopyOnWriteArrayList a = populatedArray(10);
490 dl 1.1 assertTrue(a.subList(1,1).isEmpty());
491 jsr166 1.11 for (int j = 0; j < 9; ++j) {
492     for (int i = j ; i < 10; ++i) {
493     List b = a.subList(j,i);
494     for (int k = j; k < i; ++k) {
495     assertEquals(new Integer(k), b.get(k-j));
496     }
497     }
498     }
499 dl 1.1
500 jsr166 1.11 List s = a.subList(2, 5);
501 jsr166 1.24 assertEquals(3, s.size());
502 dl 1.3 s.set(2, m1);
503 dl 1.1 assertEquals(a.get(4), m1);
504 jsr166 1.11 s.clear();
505 jsr166 1.24 assertEquals(7, a.size());
506 dl 1.1 }
507    
508     // Exception tests
509    
510     /**
511 jsr166 1.18 * toArray throws an ArrayStoreException when the given array
512     * can not store the objects inside the list
513 dl 1.1 */
514 dl 1.4 public void testToArray_ArrayStoreException() {
515 jsr166 1.30 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
516     c.add("zfasdfsdf");
517     c.add("asdadasd");
518 dl 1.4 try {
519 dl 1.1 c.toArray(new Long[5]);
520 jsr166 1.11 shouldThrow();
521 jsr166 1.13 } catch (ArrayStoreException success) {}
522 dl 1.1 }
523    
524     /**
525 jsr166 1.18 * get throws an IndexOutOfBoundsException on a negative index
526 dl 1.1 */
527 dl 1.4 public void testGet1_IndexOutOfBoundsException() {
528 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
529     List[] lists = { c, c.subList(1, c.size() - 1) };
530     for (List list : lists) {
531     try {
532     list.get(-1);
533     shouldThrow();
534     } catch (IndexOutOfBoundsException success) {}
535     }
536 dl 1.1 }
537 jsr166 1.8
538 dl 1.1 /**
539 jsr166 1.18 * get throws an IndexOutOfBoundsException on a too high index
540 dl 1.1 */
541 dl 1.4 public void testGet2_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(list.size());
547     shouldThrow();
548     } catch (IndexOutOfBoundsException success) {}
549     }
550 dl 1.1 }
551    
552     /**
553 jsr166 1.18 * set throws an IndexOutOfBoundsException on a negative index
554 dl 1.1 */
555 dl 1.4 public void testSet1_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.set(-1, "qwerty");
561     shouldThrow();
562     } catch (IndexOutOfBoundsException success) {}
563     }
564 dl 1.1 }
565 jsr166 1.8
566 dl 1.1 /**
567 jsr166 1.18 * set throws an IndexOutOfBoundsException on a too high index
568 dl 1.1 */
569 dl 1.4 public void testSet2() {
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(list.size(), "qwerty");
575     shouldThrow();
576     } catch (IndexOutOfBoundsException success) {}
577     }
578 dl 1.1 }
579    
580     /**
581 jsr166 1.18 * add throws an IndexOutOfBoundsException on a negative index
582 dl 1.1 */
583 dl 1.4 public void testAdd1_IndexOutOfBoundsException() {
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.add(-1, "qwerty");
589     shouldThrow();
590     } catch (IndexOutOfBoundsException success) {}
591     }
592 dl 1.1 }
593 jsr166 1.8
594 dl 1.1 /**
595 jsr166 1.18 * add throws an IndexOutOfBoundsException on a too high index
596 dl 1.1 */
597 dl 1.4 public void testAdd2_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(list.size() + 1, "qwerty");
603     shouldThrow();
604     } catch (IndexOutOfBoundsException success) {}
605     }
606 dl 1.1 }
607    
608     /**
609 jsr166 1.18 * remove throws an IndexOutOfBoundsException on a negative index
610 dl 1.1 */
611 dl 1.4 public void testRemove1_IndexOutOfBounds() {
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.remove(-1);
617     shouldThrow();
618     } catch (IndexOutOfBoundsException success) {}
619     }
620 dl 1.1 }
621    
622     /**
623 jsr166 1.18 * remove throws an IndexOutOfBoundsException on a too high index
624 dl 1.1 */
625 dl 1.4 public void testRemove2_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(list.size());
631     shouldThrow();
632     } catch (IndexOutOfBoundsException success) {}
633     }
634 dl 1.1 }
635 jsr166 1.8
636 dl 1.1 /**
637 jsr166 1.18 * addAll throws an IndexOutOfBoundsException on a negative index
638 dl 1.1 */
639 dl 1.4 public void testAddAll1_IndexOutOfBoundsException() {
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.addAll(-1, new LinkedList());
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 too high index
652 dl 1.1 */
653 dl 1.4 public void testAddAll2_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(list.size() + 1, new LinkedList());
659     shouldThrow();
660     } catch (IndexOutOfBoundsException success) {}
661     }
662 dl 1.1 }
663    
664     /**
665 jsr166 1.18 * listIterator throws an IndexOutOfBoundsException on a negative index
666 dl 1.1 */
667 dl 1.4 public void testListIterator1_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.listIterator(-1);
673     shouldThrow();
674     } catch (IndexOutOfBoundsException success) {}
675     }
676 dl 1.1 }
677    
678     /**
679 jsr166 1.18 * listIterator throws an IndexOutOfBoundsException on a too high index
680 dl 1.1 */
681 dl 1.4 public void testListIterator2_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(list.size() + 1);
687     shouldThrow();
688     } catch (IndexOutOfBoundsException success) {}
689     }
690 dl 1.1 }
691    
692     /**
693 jsr166 1.18 * subList throws an IndexOutOfBoundsException on a negative index
694 dl 1.1 */
695 dl 1.4 public void testSubList1_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.subList(-1, list.size());
701     shouldThrow();
702     } catch (IndexOutOfBoundsException success) {}
703     }
704 dl 1.1 }
705    
706     /**
707 jsr166 1.18 * subList throws an IndexOutOfBoundsException on a too high index
708 dl 1.1 */
709 dl 1.4 public void testSubList2_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(0, list.size() + 1);
715     shouldThrow();
716     } catch (IndexOutOfBoundsException success) {}
717     }
718 dl 1.1 }
719    
720     /**
721 jsr166 1.18 * subList throws IndexOutOfBoundsException when the second index
722     * is lower then the first
723 dl 1.1 */
724 dl 1.4 public void testSubList3_IndexOutOfBoundsException() {
725 jsr166 1.30 CopyOnWriteArrayList c = populatedArray(5);
726     List[] lists = { c, c.subList(1, c.size() - 1) };
727     for (List list : lists) {
728     try {
729     list.subList(list.size() - 1, 1);
730     shouldThrow();
731     } catch (IndexOutOfBoundsException success) {}
732     }
733 dl 1.2 }
734    
735 dl 1.4 /**
736 jsr166 1.16 * a deserialized serialized list is equal
737 dl 1.4 */
738 jsr166 1.12 public void testSerialization() throws Exception {
739 jsr166 1.22 List x = populatedArray(SIZE);
740     List y = serialClone(x);
741 dl 1.2
742 jsr166 1.29 assertNotSame(x, y);
743 jsr166 1.22 assertEquals(x.size(), y.size());
744     assertEquals(x.toString(), y.toString());
745     assertTrue(Arrays.equals(x.toArray(), y.toArray()));
746     assertEquals(x, y);
747     assertEquals(y, x);
748     while (!x.isEmpty()) {
749     assertFalse(y.isEmpty());
750     assertEquals(x.remove(0), y.remove(0));
751     }
752     assertTrue(y.isEmpty());
753 dl 1.1 }
754 jsr166 1.8
755 dl 1.1 }