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