ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.32
Committed: Sat Jan 17 22:55:06 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +9 -5 lines
Log Message:
add more tests of exhausted iterators

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