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