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