6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
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 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
|
27 |
|
public static Test suite() { |
28 |
< |
return new TestSuite(CopyOnWriteArrayListTest.class); |
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 populatedArray(int n) { |
47 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
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(new Integer(i)); |
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 |
76 |
|
*/ |
77 |
|
public void testConstructor2() { |
78 |
|
Integer[] ints = new Integer[SIZE]; |
79 |
< |
for (int i = 0; i < SIZE-1; ++i) |
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) |
88 |
|
*/ |
89 |
|
public void testConstructor3() { |
90 |
|
Integer[] ints = new Integer[SIZE]; |
91 |
< |
for (int i = 0; i < SIZE-1; ++i) |
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 |
|
|
67 |
– |
|
98 |
|
/** |
99 |
< |
* addAll adds each element from the given collection |
99 |
> |
* addAll adds each element from the given collection, including duplicates |
100 |
|
*/ |
101 |
|
public void testAddAll() { |
102 |
|
CopyOnWriteArrayList full = populatedArray(3); |
103 |
< |
Vector v = new Vector(); |
74 |
< |
v.add(three); |
75 |
< |
v.add(four); |
76 |
< |
v.add(five); |
77 |
< |
full.addAll(v); |
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 |
|
/** |
112 |
|
*/ |
113 |
|
public void testAddAllAbsent() { |
114 |
|
CopyOnWriteArrayList full = populatedArray(3); |
115 |
< |
Vector v = new Vector(); |
116 |
< |
v.add(three); |
117 |
< |
v.add(four); |
118 |
< |
v.add(one); // will not add this element |
91 |
< |
full.addAllAbsent(v); |
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 |
|
|
146 |
|
assertEquals(0, full.size()); |
147 |
|
} |
148 |
|
|
122 |
– |
|
149 |
|
/** |
150 |
|
* Cloned list is equal |
151 |
|
*/ |
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()); |
175 |
– |
} |
207 |
|
|
208 |
+ |
assertFalse(a.equals(null)); |
209 |
+ |
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* containsAll returns true for collection with subset of elements |
212 |
> |
* containsAll returns true for collections with subset of elements |
213 |
|
*/ |
214 |
|
public void testContainsAll() { |
215 |
|
CopyOnWriteArrayList full = populatedArray(3); |
216 |
< |
Vector v = new Vector(); |
217 |
< |
v.add(one); |
218 |
< |
v.add(two); |
219 |
< |
assertTrue(full.containsAll(v)); |
220 |
< |
v.add(six); |
221 |
< |
assertFalse(full.containsAll(v)); |
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 |
|
/** |
263 |
|
} |
264 |
|
|
265 |
|
/** |
266 |
< |
* iterator() returns an iterator containing the elements of the list |
266 |
> |
* iterator() returns an iterator containing the elements of the |
267 |
> |
* list in insertion order |
268 |
|
*/ |
269 |
|
public void testIterator() { |
270 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
271 |
< |
Iterator i = full.iterator(); |
272 |
< |
int j; |
273 |
< |
for (j = 0; i.hasNext(); j++) |
274 |
< |
assertEquals(j, i.next()); |
275 |
< |
assertEquals(SIZE, j); |
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 |
|
/** |
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) { |
319 |
> |
for (int i = 0; i < 3; ++i) |
320 |
|
assertTrue(s.contains(String.valueOf(i))); |
321 |
< |
} |
321 |
> |
assertEquals(new ArrayList(full).toString(), |
322 |
> |
full.toString()); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
364 |
|
ListIterator i = full.listIterator(1); |
365 |
|
int j; |
366 |
|
for (j = 0; i.hasNext(); j++) |
367 |
< |
assertEquals(j+1, i.next()); |
367 |
> |
assertEquals(j + 1, i.next()); |
368 |
|
assertEquals(2, j); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* remove removes and returns the object at the given index |
372 |
> |
* remove(int) removes and returns the object at the given index |
373 |
|
*/ |
374 |
< |
public void testRemove() { |
375 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
376 |
< |
assertEquals(2, full.remove(2)); |
377 |
< |
assertEquals(2, full.size()); |
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 |
|
/** |
408 |
|
*/ |
409 |
|
public void testRemoveAll() { |
410 |
|
CopyOnWriteArrayList full = populatedArray(3); |
411 |
< |
Vector v = new Vector(); |
412 |
< |
v.add(one); |
413 |
< |
v.add(two); |
327 |
< |
full.removeAll(v); |
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 |
|
|
434 |
|
} |
435 |
|
|
436 |
|
/** |
437 |
< |
* toArray returns an Object array containing all elements from the list |
437 |
> |
* toArray() returns an Object array containing all elements from |
438 |
> |
* the list in insertion order |
439 |
|
*/ |
440 |
|
public void testToArray() { |
441 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
442 |
< |
Object[] o = full.toArray(); |
443 |
< |
assertEquals(3, o.length); |
444 |
< |
assertEquals(0, o[0]); |
445 |
< |
assertEquals(1, o[1]); |
446 |
< |
assertEquals(2, o[2]); |
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 returns an Integer array containing all elements from |
457 |
< |
* the list |
456 |
> |
* toArray(Integer array) returns an Integer array containing all |
457 |
> |
* elements from the list in insertion order |
458 |
|
*/ |
459 |
|
public void testToArray2() { |
460 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
461 |
< |
Integer[] i = new Integer[3]; |
462 |
< |
i = (Integer[])full.toArray(i); |
463 |
< |
assertEquals(3, i.length); |
464 |
< |
assertEquals(0, i[0].intValue()); |
372 |
< |
assertEquals(1, i[1].intValue()); |
373 |
< |
assertEquals(2, i[2].intValue()); |
374 |
< |
} |
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 |
512 |
|
} |
513 |
|
|
514 |
|
List s = a.subList(2, 5); |
515 |
< |
assertEquals(s.size(), 3); |
515 |
> |
assertEquals(3, s.size()); |
516 |
|
s.set(2, m1); |
517 |
|
assertEquals(a.get(4), m1); |
518 |
|
s.clear(); |
519 |
< |
assertEquals(a.size(), 7); |
519 |
> |
assertEquals(7, a.size()); |
520 |
|
} |
521 |
|
|
522 |
|
// Exception tests |
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 { |
408 |
– |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
409 |
– |
c.add("zfasdfsdf"); |
410 |
– |
c.add("asdadasd"); |
533 |
|
c.toArray(new Long[5]); |
534 |
|
shouldThrow(); |
535 |
|
} catch (ArrayStoreException success) {} |
539 |
|
* get throws an IndexOutOfBoundsException on a negative index |
540 |
|
*/ |
541 |
|
public void testGet1_IndexOutOfBoundsException() { |
542 |
< |
try { |
543 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
544 |
< |
c.get(-1); |
545 |
< |
shouldThrow(); |
546 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
557 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
558 |
< |
c.add("asdasd"); |
559 |
< |
c.add("asdad"); |
560 |
< |
c.get(100); |
561 |
< |
shouldThrow(); |
562 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
571 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
572 |
< |
c.set(-1,"qwerty"); |
573 |
< |
shouldThrow(); |
574 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
585 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
586 |
< |
c.add("asdasd"); |
587 |
< |
c.add("asdad"); |
588 |
< |
c.set(100, "qwerty"); |
589 |
< |
shouldThrow(); |
590 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
599 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
600 |
< |
c.add(-1,"qwerty"); |
601 |
< |
shouldThrow(); |
602 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
613 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
614 |
< |
c.add("asdasd"); |
615 |
< |
c.add("asdasdasd"); |
616 |
< |
c.add(100, "qwerty"); |
617 |
< |
shouldThrow(); |
618 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
627 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
628 |
< |
c.remove(-1); |
629 |
< |
shouldThrow(); |
630 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
641 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
642 |
< |
c.add("asdasd"); |
643 |
< |
c.add("adasdasd"); |
644 |
< |
c.remove(100); |
645 |
< |
shouldThrow(); |
646 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
655 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
656 |
< |
c.addAll(-1,new LinkedList()); |
657 |
< |
shouldThrow(); |
658 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
669 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
670 |
< |
c.add("asdasd"); |
671 |
< |
c.add("asdasdasd"); |
672 |
< |
c.addAll(100, new LinkedList()); |
673 |
< |
shouldThrow(); |
674 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
683 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
684 |
< |
c.listIterator(-1); |
685 |
< |
shouldThrow(); |
686 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
697 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
698 |
< |
c.add("adasd"); |
699 |
< |
c.add("asdasdas"); |
700 |
< |
c.listIterator(100); |
701 |
< |
shouldThrow(); |
702 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
711 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
712 |
< |
c.subList(-1,100); |
713 |
< |
shouldThrow(); |
714 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
725 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
726 |
< |
c.add("asdasd"); |
727 |
< |
c.subList(1,100); |
728 |
< |
shouldThrow(); |
729 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
|
/** |
736 |
|
* is lower then the first |
737 |
|
*/ |
738 |
|
public void testSubList3_IndexOutOfBoundsException() { |
739 |
< |
try { |
740 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
741 |
< |
c.subList(3,1); |
742 |
< |
shouldThrow(); |
743 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
750 |
> |
* a deserialized/reserialized list equals original |
751 |
|
*/ |
752 |
|
public void testSerialization() throws Exception { |
753 |
< |
CopyOnWriteArrayList q = populatedArray(SIZE); |
753 |
> |
List x = populatedArray(SIZE); |
754 |
> |
List y = serialClone(x); |
755 |
|
|
756 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
757 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
758 |
< |
out.writeObject(q); |
759 |
< |
out.close(); |
760 |
< |
|
761 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
762 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
763 |
< |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
764 |
< |
assertEquals(q.size(), r.size()); |
765 |
< |
assertTrue(q.equals(r)); |
766 |
< |
assertTrue(r.equals(q)); |
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 |
|
} |