ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.41
Committed: Tue Nov 15 22:52:15 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +2 -2 lines
Log Message:
start running sublist tests

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