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/licenses/publicdomain |
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.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
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()); |
21 |
> |
junit.textui.TestRunner.run(suite()); |
22 |
|
} |
23 |
|
|
24 |
|
public static Test suite() { |
70 |
|
|
71 |
|
|
72 |
|
/** |
73 |
< |
* addAll adds each element from the given collection |
73 |
> |
* addAll adds each element from the given collection |
74 |
|
*/ |
75 |
|
public void testAddAll() { |
76 |
|
CopyOnWriteArrayList full = populatedArray(3); |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* addAllAbsent adds each element from the given collection that did not |
87 |
< |
* already exist in the List |
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); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
< |
* addIfAbsent will not add the element if it already exists in the list |
100 |
> |
* addIfAbsent will not add the element if it already exists in the list |
101 |
|
*/ |
102 |
|
public void testAddIfAbsent() { |
103 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* addIfAbsent adds the element when it does not exist in the list |
109 |
> |
* addIfAbsent adds the element when it does not exist in the list |
110 |
|
*/ |
111 |
|
public void testAddIfAbsent2() { |
112 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* clear removes all elements from the list |
118 |
> |
* clear removes all elements from the list |
119 |
|
*/ |
120 |
|
public void testClear() { |
121 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
125 |
|
|
126 |
|
|
127 |
|
/** |
128 |
< |
* Cloned list is equal |
128 |
> |
* Cloned list is equal |
129 |
|
*/ |
130 |
|
public void testClone() { |
131 |
|
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
< |
* contains is true for added elements |
139 |
> |
* contains is true for added elements |
140 |
|
*/ |
141 |
|
public void testContains() { |
142 |
|
CopyOnWriteArrayList full = populatedArray(3); |
180 |
|
|
181 |
|
|
182 |
|
/** |
183 |
< |
* containsAll returns true for collection with subset of elements |
183 |
> |
* containsAll returns true for collection with subset of elements |
184 |
|
*/ |
185 |
|
public void testContainsAll() { |
186 |
|
CopyOnWriteArrayList full = populatedArray(3); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* get returns the value at the given index |
196 |
> |
* get returns the value at the given index |
197 |
|
*/ |
198 |
|
public void testGet() { |
199 |
|
CopyOnWriteArrayList full = populatedArray(3); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
< |
* indexOf gives the index for the given object |
204 |
> |
* indexOf gives the index for the given object |
205 |
|
*/ |
206 |
|
public void testIndexOf() { |
207 |
|
CopyOnWriteArrayList full = populatedArray(3); |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
< |
* indexOf gives the index based on the given index |
214 |
< |
* at which to start searching |
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); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* isEmpty returns true when empty, else false |
223 |
> |
* isEmpty returns true when empty, else false |
224 |
|
*/ |
225 |
|
public void testIsEmpty() { |
226 |
|
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
< |
* iterator() returns an iterator containing the elements of the list |
233 |
> |
* iterator() returns an iterator containing the elements of the list |
234 |
|
*/ |
235 |
|
public void testIterator() { |
236 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
244 |
|
/** |
245 |
|
* iterator.remove throws UnsupportedOperationException |
246 |
|
*/ |
247 |
< |
public void testIteratorRemove () { |
247 |
> |
public void testIteratorRemove() { |
248 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
249 |
|
Iterator it = full.iterator(); |
250 |
|
it.next(); |
261 |
|
CopyOnWriteArrayList full = populatedArray(3); |
262 |
|
String s = full.toString(); |
263 |
|
for (int i = 0; i < 3; ++i) { |
264 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
264 |
> |
assertTrue(s.contains(String.valueOf(i))); |
265 |
|
} |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
< |
* lastIndexOf returns the index for the given object |
269 |
> |
* lastIndexOf returns the index for the given object |
270 |
|
*/ |
271 |
|
public void testLastIndexOf1() { |
272 |
|
CopyOnWriteArrayList full = populatedArray(3); |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
< |
* lastIndexOf returns the index from the given starting point |
280 |
> |
* lastIndexOf returns the index from the given starting point |
281 |
|
*/ |
282 |
< |
public void testlastIndexOf2() { |
282 |
> |
public void testLastIndexOf2() { |
283 |
|
CopyOnWriteArrayList full = populatedArray(3); |
284 |
|
full.add(one); |
285 |
|
full.add(three); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
< |
* listIterator traverses all elements |
291 |
> |
* listIterator traverses all elements |
292 |
|
*/ |
293 |
|
public void testListIterator1() { |
294 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
< |
* listIterator only returns those elements after the given index |
303 |
> |
* listIterator only returns those elements after the given index |
304 |
|
*/ |
305 |
|
public void testListIterator2() { |
306 |
|
CopyOnWriteArrayList full = populatedArray(3); |
312 |
|
} |
313 |
|
|
314 |
|
/** |
315 |
< |
* remove removes and returns the object at the given index |
315 |
> |
* remove removes and returns the object at the given index |
316 |
|
*/ |
317 |
|
public void testRemove() { |
318 |
|
CopyOnWriteArrayList full = populatedArray(3); |
319 |
< |
assertEquals(two, full.remove(2)); |
319 |
> |
assertEquals(2, full.remove(2)); |
320 |
|
assertEquals(2, full.size()); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
< |
* removeAll removes all elements from the given collection |
324 |
> |
* removeAll removes all elements from the given collection |
325 |
|
*/ |
326 |
|
public void testRemoveAll() { |
327 |
|
CopyOnWriteArrayList full = populatedArray(3); |
333 |
|
} |
334 |
|
|
335 |
|
/** |
336 |
< |
* set changes the element at the given index |
336 |
> |
* set changes the element at the given index |
337 |
|
*/ |
338 |
|
public void testSet() { |
339 |
|
CopyOnWriteArrayList full = populatedArray(3); |
340 |
< |
assertEquals(two, full.set(2, four)); |
340 |
> |
assertEquals(2, full.set(2, four)); |
341 |
|
assertEquals(4, full.get(2)); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
345 |
< |
* size returns the number of elements |
345 |
> |
* size returns the number of elements |
346 |
|
*/ |
347 |
|
public void testSize() { |
348 |
|
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
< |
* toArray returns an Object array containing all elements from the list |
355 |
> |
* toArray returns an Object array containing all elements from the list |
356 |
|
*/ |
357 |
|
public void testToArray() { |
358 |
|
CopyOnWriteArrayList full = populatedArray(3); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
< |
* toArray returns an Integer array containing all elements from |
368 |
< |
* the list |
367 |
> |
* toArray returns an Integer array containing all elements from |
368 |
> |
* the list |
369 |
|
*/ |
370 |
|
public void testToArray2() { |
371 |
|
CopyOnWriteArrayList full = populatedArray(3); |
404 |
|
// Exception tests |
405 |
|
|
406 |
|
/** |
407 |
< |
* toArray throws an ArrayStoreException when the given array |
408 |
< |
* can not store the objects inside the list |
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 { |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* get throws an IndexOutOfBoundsException on a negative index |
421 |
> |
* get throws an IndexOutOfBoundsException on a negative index |
422 |
|
*/ |
423 |
|
public void testGet1_IndexOutOfBoundsException() { |
424 |
|
try { |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* get throws an IndexOutOfBoundsException on a too high index |
432 |
> |
* get throws an IndexOutOfBoundsException on a too high index |
433 |
|
*/ |
434 |
|
public void testGet2_IndexOutOfBoundsException() { |
435 |
|
try { |
442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
< |
* set throws an IndexOutOfBoundsException on a negative index |
445 |
> |
* set throws an IndexOutOfBoundsException on a negative index |
446 |
|
*/ |
447 |
|
public void testSet1_IndexOutOfBoundsException() { |
448 |
|
try { |
453 |
|
} |
454 |
|
|
455 |
|
/** |
456 |
< |
* set throws an IndexOutOfBoundsException on a too high index |
456 |
> |
* set throws an IndexOutOfBoundsException on a too high index |
457 |
|
*/ |
458 |
|
public void testSet2() { |
459 |
|
try { |
466 |
|
} |
467 |
|
|
468 |
|
/** |
469 |
< |
* add throws an IndexOutOfBoundsException on a negative index |
469 |
> |
* add throws an IndexOutOfBoundsException on a negative index |
470 |
|
*/ |
471 |
|
public void testAdd1_IndexOutOfBoundsException() { |
472 |
|
try { |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* add throws an IndexOutOfBoundsException on a too high index |
480 |
> |
* add throws an IndexOutOfBoundsException on a too high index |
481 |
|
*/ |
482 |
|
public void testAdd2_IndexOutOfBoundsException() { |
483 |
|
try { |
490 |
|
} |
491 |
|
|
492 |
|
/** |
493 |
< |
* remove throws an IndexOutOfBoundsException on a negative index |
493 |
> |
* remove throws an IndexOutOfBoundsException on a negative index |
494 |
|
*/ |
495 |
|
public void testRemove1_IndexOutOfBounds() { |
496 |
|
try { |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
< |
* remove throws an IndexOutOfBoundsException on a too high index |
504 |
> |
* remove throws an IndexOutOfBoundsException on a too high index |
505 |
|
*/ |
506 |
|
public void testRemove2_IndexOutOfBounds() { |
507 |
|
try { |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
< |
* addAll throws an IndexOutOfBoundsException on a negative index |
517 |
> |
* addAll throws an IndexOutOfBoundsException on a negative index |
518 |
|
*/ |
519 |
|
public void testAddAll1_IndexOutOfBoundsException() { |
520 |
|
try { |
525 |
|
} |
526 |
|
|
527 |
|
/** |
528 |
< |
* addAll throws an IndexOutOfBoundsException on a too high index |
528 |
> |
* addAll throws an IndexOutOfBoundsException on a too high index |
529 |
|
*/ |
530 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
531 |
|
try { |
538 |
|
} |
539 |
|
|
540 |
|
/** |
541 |
< |
* listIterator throws an IndexOutOfBoundsException on a negative index |
541 |
> |
* listIterator throws an IndexOutOfBoundsException on a negative index |
542 |
|
*/ |
543 |
|
public void testListIterator1_IndexOutOfBoundsException() { |
544 |
|
try { |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
< |
* listIterator throws an IndexOutOfBoundsException on a too high index |
552 |
> |
* listIterator throws an IndexOutOfBoundsException on a too high index |
553 |
|
*/ |
554 |
|
public void testListIterator2_IndexOutOfBoundsException() { |
555 |
|
try { |
562 |
|
} |
563 |
|
|
564 |
|
/** |
565 |
< |
* subList throws an IndexOutOfBoundsException on a negative index |
565 |
> |
* subList throws an IndexOutOfBoundsException on a negative index |
566 |
|
*/ |
567 |
|
public void testSubList1_IndexOutOfBoundsException() { |
568 |
|
try { |
573 |
|
} |
574 |
|
|
575 |
|
/** |
576 |
< |
* subList throws an IndexOutOfBoundsException on a too high index |
576 |
> |
* subList throws an IndexOutOfBoundsException on a too high index |
577 |
|
*/ |
578 |
|
public void testSubList2_IndexOutOfBoundsException() { |
579 |
|
try { |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
< |
* subList throws IndexOutOfBoundsException when the second index |
589 |
< |
* is lower then the first |
588 |
> |
* subList throws IndexOutOfBoundsException when the second index |
589 |
> |
* is lower then the first |
590 |
|
*/ |
591 |
|
public void testSubList3_IndexOutOfBoundsException() { |
592 |
|
try { |
597 |
|
} |
598 |
|
|
599 |
|
/** |
600 |
< |
* a deserialized serialiszed list is equal |
600 |
> |
* a deserialized serialized list is equal |
601 |
|
*/ |
602 |
|
public void testSerialization() throws Exception { |
603 |
< |
CopyOnWriteArrayList q = populatedArray(SIZE); |
603 |
> |
List x = populatedArray(SIZE); |
604 |
> |
List y = serialClone(x); |
605 |
|
|
606 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
607 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
608 |
< |
out.writeObject(q); |
609 |
< |
out.close(); |
610 |
< |
|
611 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
612 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
613 |
< |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
614 |
< |
assertEquals(q.size(), r.size()); |
615 |
< |
assertTrue(q.equals(r)); |
616 |
< |
assertTrue(r.equals(q)); |
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 |
|
} |