1 |
/* |
2 |
* 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 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import java.util.ArrayList; |
10 |
import java.util.Arrays; |
11 |
import java.util.Collection; |
12 |
import java.util.Iterator; |
13 |
import java.util.LinkedList; |
14 |
import java.util.List; |
15 |
import java.util.ListIterator; |
16 |
import java.util.NoSuchElementException; |
17 |
import java.util.concurrent.CopyOnWriteArrayList; |
18 |
|
19 |
import junit.framework.Test; |
20 |
import junit.framework.TestSuite; |
21 |
|
22 |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
23 |
|
24 |
public static void main(String[] args) { |
25 |
main(suite(), args); |
26 |
} |
27 |
|
28 |
public static Test suite() { |
29 |
return new TestSuite(CopyOnWriteArrayListTest.class); |
30 |
} |
31 |
|
32 |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
33 |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
34 |
assertTrue(a.isEmpty()); |
35 |
for (int i = 0; i < n; i++) |
36 |
a.add(i); |
37 |
assertFalse(a.isEmpty()); |
38 |
assertEquals(n, a.size()); |
39 |
return a; |
40 |
} |
41 |
|
42 |
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 |
/** |
53 |
* a new list is empty |
54 |
*/ |
55 |
public void testConstructor() { |
56 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
57 |
assertTrue(a.isEmpty()); |
58 |
} |
59 |
|
60 |
/** |
61 |
* new list contains all elements of initializing array |
62 |
*/ |
63 |
public void testConstructor2() { |
64 |
Integer[] ints = new Integer[SIZE]; |
65 |
for (int i = 0; i < SIZE - 1; ++i) |
66 |
ints[i] = new Integer(i); |
67 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
68 |
for (int i = 0; i < SIZE; ++i) |
69 |
assertEquals(ints[i], a.get(i)); |
70 |
} |
71 |
|
72 |
/** |
73 |
* new list contains all elements of initializing collection |
74 |
*/ |
75 |
public void testConstructor3() { |
76 |
Integer[] ints = new Integer[SIZE]; |
77 |
for (int i = 0; i < SIZE - 1; ++i) |
78 |
ints[i] = new Integer(i); |
79 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
80 |
for (int i = 0; i < SIZE; ++i) |
81 |
assertEquals(ints[i], a.get(i)); |
82 |
} |
83 |
|
84 |
/** |
85 |
* addAll adds each element from the given collection, including duplicates |
86 |
*/ |
87 |
public void testAddAll() { |
88 |
CopyOnWriteArrayList full = populatedArray(3); |
89 |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
90 |
assertEquals(6, full.size()); |
91 |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
92 |
assertEquals(9, full.size()); |
93 |
} |
94 |
|
95 |
/** |
96 |
* addAllAbsent adds each element from the given collection that did not |
97 |
* already exist in the List |
98 |
*/ |
99 |
public void testAddAllAbsent() { |
100 |
CopyOnWriteArrayList full = populatedArray(3); |
101 |
// "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 |
assertEquals(5, full.size()); |
106 |
} |
107 |
|
108 |
/** |
109 |
* addIfAbsent will not add the element if it already exists in the list |
110 |
*/ |
111 |
public void testAddIfAbsent() { |
112 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
113 |
full.addIfAbsent(one); |
114 |
assertEquals(SIZE, full.size()); |
115 |
} |
116 |
|
117 |
/** |
118 |
* addIfAbsent adds the element when it does not exist in the list |
119 |
*/ |
120 |
public void testAddIfAbsent2() { |
121 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
122 |
full.addIfAbsent(three); |
123 |
assertTrue(full.contains(three)); |
124 |
} |
125 |
|
126 |
/** |
127 |
* clear removes all elements from the list |
128 |
*/ |
129 |
public void testClear() { |
130 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
131 |
full.clear(); |
132 |
assertEquals(0, full.size()); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Cloned list is equal |
137 |
*/ |
138 |
public void testClone() { |
139 |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
140 |
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
141 |
assertEquals(l1, l2); |
142 |
l1.clear(); |
143 |
assertFalse(l1.equals(l2)); |
144 |
} |
145 |
|
146 |
/** |
147 |
* contains is true for added elements |
148 |
*/ |
149 |
public void testContains() { |
150 |
CopyOnWriteArrayList full = populatedArray(3); |
151 |
assertTrue(full.contains(one)); |
152 |
assertFalse(full.contains(five)); |
153 |
} |
154 |
|
155 |
/** |
156 |
* adding at an index places it in the indicated index |
157 |
*/ |
158 |
public void testAddIndex() { |
159 |
CopyOnWriteArrayList full = populatedArray(3); |
160 |
full.add(0, m1); |
161 |
assertEquals(4, full.size()); |
162 |
assertEquals(m1, full.get(0)); |
163 |
assertEquals(zero, full.get(1)); |
164 |
|
165 |
full.add(2, m2); |
166 |
assertEquals(5, full.size()); |
167 |
assertEquals(m2, full.get(2)); |
168 |
assertEquals(two, full.get(4)); |
169 |
} |
170 |
|
171 |
/** |
172 |
* lists with same elements are equal and have same hashCode |
173 |
*/ |
174 |
public void testEquals() { |
175 |
CopyOnWriteArrayList a = populatedArray(3); |
176 |
CopyOnWriteArrayList b = populatedArray(3); |
177 |
assertTrue(a.equals(b)); |
178 |
assertTrue(b.equals(a)); |
179 |
assertTrue(a.containsAll(b)); |
180 |
assertTrue(b.containsAll(a)); |
181 |
assertEquals(a.hashCode(), b.hashCode()); |
182 |
a.add(m1); |
183 |
assertFalse(a.equals(b)); |
184 |
assertFalse(b.equals(a)); |
185 |
assertTrue(a.containsAll(b)); |
186 |
assertFalse(b.containsAll(a)); |
187 |
b.add(m1); |
188 |
assertTrue(a.equals(b)); |
189 |
assertTrue(b.equals(a)); |
190 |
assertTrue(a.containsAll(b)); |
191 |
assertTrue(b.containsAll(a)); |
192 |
assertEquals(a.hashCode(), b.hashCode()); |
193 |
|
194 |
assertFalse(a.equals(null)); |
195 |
} |
196 |
|
197 |
/** |
198 |
* containsAll returns true for collections with subset of elements |
199 |
*/ |
200 |
public void testContainsAll() { |
201 |
CopyOnWriteArrayList full = populatedArray(3); |
202 |
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 |
|
208 |
try { |
209 |
full.containsAll(null); |
210 |
shouldThrow(); |
211 |
} catch (NullPointerException success) {} |
212 |
} |
213 |
|
214 |
/** |
215 |
* get returns the value at the given index |
216 |
*/ |
217 |
public void testGet() { |
218 |
CopyOnWriteArrayList full = populatedArray(3); |
219 |
assertEquals(0, full.get(0)); |
220 |
} |
221 |
|
222 |
/** |
223 |
* indexOf gives the index for the given object |
224 |
*/ |
225 |
public void testIndexOf() { |
226 |
CopyOnWriteArrayList full = populatedArray(3); |
227 |
assertEquals(1, full.indexOf(one)); |
228 |
assertEquals(-1, full.indexOf("puppies")); |
229 |
} |
230 |
|
231 |
/** |
232 |
* indexOf gives the index based on the given index |
233 |
* at which to start searching |
234 |
*/ |
235 |
public void testIndexOf2() { |
236 |
CopyOnWriteArrayList full = populatedArray(3); |
237 |
assertEquals(1, full.indexOf(one, 0)); |
238 |
assertEquals(-1, full.indexOf(one, 2)); |
239 |
} |
240 |
|
241 |
/** |
242 |
* isEmpty returns true when empty, else false |
243 |
*/ |
244 |
public void testIsEmpty() { |
245 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
246 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
247 |
assertTrue(empty.isEmpty()); |
248 |
assertFalse(full.isEmpty()); |
249 |
} |
250 |
|
251 |
/** |
252 |
* iterator() returns an iterator containing the elements of the |
253 |
* list in insertion order |
254 |
*/ |
255 |
public void testIterator() { |
256 |
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 |
shuffle(elements); |
267 |
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 |
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 |
} |
284 |
|
285 |
/** |
286 |
* iterator.remove throws UnsupportedOperationException |
287 |
*/ |
288 |
public void testIteratorRemove() { |
289 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
290 |
Iterator it = full.iterator(); |
291 |
it.next(); |
292 |
try { |
293 |
it.remove(); |
294 |
shouldThrow(); |
295 |
} catch (UnsupportedOperationException success) {} |
296 |
} |
297 |
|
298 |
/** |
299 |
* toString contains toString of elements |
300 |
*/ |
301 |
public void testToString() { |
302 |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
303 |
CopyOnWriteArrayList full = populatedArray(3); |
304 |
String s = full.toString(); |
305 |
for (int i = 0; i < 3; ++i) |
306 |
assertTrue(s.contains(String.valueOf(i))); |
307 |
assertEquals(new ArrayList(full).toString(), |
308 |
full.toString()); |
309 |
} |
310 |
|
311 |
/** |
312 |
* lastIndexOf returns the index for the given object |
313 |
*/ |
314 |
public void testLastIndexOf1() { |
315 |
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 |
} |
321 |
|
322 |
/** |
323 |
* lastIndexOf returns the index from the given starting point |
324 |
*/ |
325 |
public void testLastIndexOf2() { |
326 |
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 |
} |
332 |
|
333 |
/** |
334 |
* listIterator traverses all elements |
335 |
*/ |
336 |
public void testListIterator1() { |
337 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
338 |
ListIterator i = full.listIterator(); |
339 |
int j; |
340 |
for (j = 0; i.hasNext(); j++) |
341 |
assertEquals(j, i.next()); |
342 |
assertEquals(SIZE, j); |
343 |
} |
344 |
|
345 |
/** |
346 |
* listIterator only returns those elements after the given index |
347 |
*/ |
348 |
public void testListIterator2() { |
349 |
CopyOnWriteArrayList full = populatedArray(3); |
350 |
ListIterator i = full.listIterator(1); |
351 |
int j; |
352 |
for (j = 0; i.hasNext(); j++) |
353 |
assertEquals(j + 1, i.next()); |
354 |
assertEquals(2, j); |
355 |
} |
356 |
|
357 |
/** |
358 |
* remove(int) removes and returns the object at the given index |
359 |
*/ |
360 |
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 |
} |
391 |
|
392 |
/** |
393 |
* removeAll removes all elements from the given collection |
394 |
*/ |
395 |
public void testRemoveAll() { |
396 |
CopyOnWriteArrayList full = populatedArray(3); |
397 |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
398 |
assertEquals(1, full.size()); |
399 |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
400 |
assertEquals(1, full.size()); |
401 |
} |
402 |
|
403 |
/** |
404 |
* set changes the element at the given index |
405 |
*/ |
406 |
public void testSet() { |
407 |
CopyOnWriteArrayList full = populatedArray(3); |
408 |
assertEquals(2, full.set(2, four)); |
409 |
assertEquals(4, full.get(2)); |
410 |
} |
411 |
|
412 |
/** |
413 |
* size returns the number of elements |
414 |
*/ |
415 |
public void testSize() { |
416 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
417 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
418 |
assertEquals(SIZE, full.size()); |
419 |
assertEquals(0, empty.size()); |
420 |
} |
421 |
|
422 |
/** |
423 |
* toArray() returns an Object array containing all elements from |
424 |
* the list in insertion order |
425 |
*/ |
426 |
public void testToArray() { |
427 |
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 |
shuffle(elements); |
435 |
Collection<Integer> full = populatedArray(elements); |
436 |
|
437 |
assertTrue(Arrays.equals(elements, full.toArray())); |
438 |
assertSame(Object[].class, full.toArray().getClass()); |
439 |
} |
440 |
|
441 |
/** |
442 |
* toArray(Integer array) returns an Integer array containing all |
443 |
* elements from the list in insertion order |
444 |
*/ |
445 |
public void testToArray2() { |
446 |
Collection empty = new CopyOnWriteArrayList(); |
447 |
Integer[] a; |
448 |
|
449 |
a = new Integer[0]; |
450 |
assertSame(a, empty.toArray(a)); |
451 |
|
452 |
a = new Integer[SIZE / 2]; |
453 |
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 |
shuffle(elements); |
463 |
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 |
a = new Integer[2 * SIZE]; |
477 |
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 |
} |
484 |
|
485 |
/** |
486 |
* sublists contains elements at indexes offset from their base |
487 |
*/ |
488 |
public void testSubList() { |
489 |
CopyOnWriteArrayList a = populatedArray(10); |
490 |
assertTrue(a.subList(1,1).isEmpty()); |
491 |
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 |
|
500 |
List s = a.subList(2, 5); |
501 |
assertEquals(3, s.size()); |
502 |
s.set(2, m1); |
503 |
assertEquals(a.get(4), m1); |
504 |
s.clear(); |
505 |
assertEquals(7, a.size()); |
506 |
} |
507 |
|
508 |
// Exception tests |
509 |
|
510 |
/** |
511 |
* toArray throws an ArrayStoreException when the given array |
512 |
* can not store the objects inside the list |
513 |
*/ |
514 |
public void testToArray_ArrayStoreException() { |
515 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
516 |
c.add("zfasdfsdf"); |
517 |
c.add("asdadasd"); |
518 |
try { |
519 |
c.toArray(new Long[5]); |
520 |
shouldThrow(); |
521 |
} catch (ArrayStoreException success) {} |
522 |
} |
523 |
|
524 |
/** |
525 |
* get throws an IndexOutOfBoundsException on a negative index |
526 |
*/ |
527 |
public void testGet1_IndexOutOfBoundsException() { |
528 |
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 |
} |
537 |
|
538 |
/** |
539 |
* get throws an IndexOutOfBoundsException on a too high index |
540 |
*/ |
541 |
public void testGet2_IndexOutOfBoundsException() { |
542 |
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 |
} |
551 |
|
552 |
/** |
553 |
* set throws an IndexOutOfBoundsException on a negative index |
554 |
*/ |
555 |
public void testSet1_IndexOutOfBoundsException() { |
556 |
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 |
} |
565 |
|
566 |
/** |
567 |
* set throws an IndexOutOfBoundsException on a too high index |
568 |
*/ |
569 |
public void testSet2() { |
570 |
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 |
} |
579 |
|
580 |
/** |
581 |
* add throws an IndexOutOfBoundsException on a negative index |
582 |
*/ |
583 |
public void testAdd1_IndexOutOfBoundsException() { |
584 |
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 |
} |
593 |
|
594 |
/** |
595 |
* add throws an IndexOutOfBoundsException on a too high index |
596 |
*/ |
597 |
public void testAdd2_IndexOutOfBoundsException() { |
598 |
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 |
} |
607 |
|
608 |
/** |
609 |
* remove throws an IndexOutOfBoundsException on a negative index |
610 |
*/ |
611 |
public void testRemove1_IndexOutOfBounds() { |
612 |
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 |
} |
621 |
|
622 |
/** |
623 |
* remove throws an IndexOutOfBoundsException on a too high index |
624 |
*/ |
625 |
public void testRemove2_IndexOutOfBounds() { |
626 |
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 |
} |
635 |
|
636 |
/** |
637 |
* addAll throws an IndexOutOfBoundsException on a negative index |
638 |
*/ |
639 |
public void testAddAll1_IndexOutOfBoundsException() { |
640 |
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 |
} |
649 |
|
650 |
/** |
651 |
* addAll throws an IndexOutOfBoundsException on a too high index |
652 |
*/ |
653 |
public void testAddAll2_IndexOutOfBoundsException() { |
654 |
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 |
} |
663 |
|
664 |
/** |
665 |
* listIterator throws an IndexOutOfBoundsException on a negative index |
666 |
*/ |
667 |
public void testListIterator1_IndexOutOfBoundsException() { |
668 |
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 |
} |
677 |
|
678 |
/** |
679 |
* listIterator throws an IndexOutOfBoundsException on a too high index |
680 |
*/ |
681 |
public void testListIterator2_IndexOutOfBoundsException() { |
682 |
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 |
} |
691 |
|
692 |
/** |
693 |
* subList throws an IndexOutOfBoundsException on a negative index |
694 |
*/ |
695 |
public void testSubList1_IndexOutOfBoundsException() { |
696 |
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 |
} |
705 |
|
706 |
/** |
707 |
* subList throws an IndexOutOfBoundsException on a too high index |
708 |
*/ |
709 |
public void testSubList2_IndexOutOfBoundsException() { |
710 |
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 |
} |
719 |
|
720 |
/** |
721 |
* subList throws IndexOutOfBoundsException when the second index |
722 |
* is lower then the first |
723 |
*/ |
724 |
public void testSubList3_IndexOutOfBoundsException() { |
725 |
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 |
} |
734 |
|
735 |
/** |
736 |
* a deserialized serialized list is equal |
737 |
*/ |
738 |
public void testSerialization() throws Exception { |
739 |
List x = populatedArray(SIZE); |
740 |
List y = serialClone(x); |
741 |
|
742 |
assertNotSame(x, y); |
743 |
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 |
} |
754 |
|
755 |
} |