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