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.Collections; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.LinkedList; |
15 |
> |
import java.util.List; |
16 |
> |
import java.util.ListIterator; |
17 |
> |
import java.util.NoSuchElementException; |
18 |
> |
import java.util.concurrent.CopyOnWriteArrayList; |
19 |
> |
|
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
24 |
|
|
30 |
|
return new TestSuite(CopyOnWriteArrayListTest.class); |
31 |
|
} |
32 |
|
|
33 |
< |
static CopyOnWriteArrayList populatedArray(int n) { |
34 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
33 |
> |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
34 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
35 |
|
assertTrue(a.isEmpty()); |
36 |
< |
for (int i = 0; i < n; ++i) |
37 |
< |
a.add(new Integer(i)); |
36 |
> |
for (int i = 0; i < n; i++) |
37 |
> |
a.add(i); |
38 |
|
assertFalse(a.isEmpty()); |
39 |
|
assertEquals(n, a.size()); |
40 |
|
return a; |
41 |
|
} |
42 |
|
|
43 |
+ |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
44 |
+ |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
45 |
+ |
assertTrue(a.isEmpty()); |
46 |
+ |
for (int i = 0; i < elements.length; i++) |
47 |
+ |
a.add(elements[i]); |
48 |
+ |
assertFalse(a.isEmpty()); |
49 |
+ |
assertEquals(elements.length, a.size()); |
50 |
+ |
return a; |
51 |
+ |
} |
52 |
|
|
53 |
|
/** |
54 |
|
* a new list is empty |
82 |
|
assertEquals(ints[i], a.get(i)); |
83 |
|
} |
84 |
|
|
67 |
– |
|
85 |
|
/** |
86 |
< |
* addAll adds each element from the given collection |
86 |
> |
* addAll adds each element from the given collection, including duplicates |
87 |
|
*/ |
88 |
|
public void testAddAll() { |
89 |
|
CopyOnWriteArrayList full = populatedArray(3); |
90 |
< |
Vector v = new Vector(); |
74 |
< |
v.add(three); |
75 |
< |
v.add(four); |
76 |
< |
v.add(five); |
77 |
< |
full.addAll(v); |
90 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
91 |
|
assertEquals(6, full.size()); |
92 |
+ |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
93 |
+ |
assertEquals(9, full.size()); |
94 |
|
} |
95 |
|
|
96 |
|
/** |
99 |
|
*/ |
100 |
|
public void testAddAllAbsent() { |
101 |
|
CopyOnWriteArrayList full = populatedArray(3); |
102 |
< |
Vector v = new Vector(); |
103 |
< |
v.add(three); |
104 |
< |
v.add(four); |
105 |
< |
v.add(one); // will not add this element |
91 |
< |
full.addAllAbsent(v); |
102 |
> |
// "one" is duplicate and will not be added |
103 |
> |
assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); |
104 |
> |
assertEquals(5, full.size()); |
105 |
> |
assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); |
106 |
|
assertEquals(5, full.size()); |
107 |
|
} |
108 |
|
|
133 |
|
assertEquals(0, full.size()); |
134 |
|
} |
135 |
|
|
122 |
– |
|
136 |
|
/** |
137 |
|
* Cloned list is equal |
138 |
|
*/ |
187 |
|
assertEquals(a.hashCode(), b.hashCode()); |
188 |
|
} |
189 |
|
|
177 |
– |
|
190 |
|
/** |
191 |
|
* containsAll returns true for collection with subset of elements |
192 |
|
*/ |
193 |
|
public void testContainsAll() { |
194 |
|
CopyOnWriteArrayList full = populatedArray(3); |
195 |
< |
Vector v = new Vector(); |
196 |
< |
v.add(one); |
197 |
< |
v.add(two); |
198 |
< |
assertTrue(full.containsAll(v)); |
199 |
< |
v.add(six); |
188 |
< |
assertFalse(full.containsAll(v)); |
195 |
> |
assertTrue(full.containsAll(Arrays.asList())); |
196 |
> |
assertTrue(full.containsAll(Arrays.asList(one))); |
197 |
> |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
198 |
> |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
199 |
> |
assertFalse(full.containsAll(Arrays.asList(six))); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* iterator() returns an iterator containing the elements of the list |
240 |
> |
* iterator() returns an iterator containing the elements of the |
241 |
> |
* list in insertion order |
242 |
|
*/ |
243 |
|
public void testIterator() { |
244 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
245 |
< |
Iterator i = full.iterator(); |
246 |
< |
int j; |
247 |
< |
for (j = 0; i.hasNext(); j++) |
248 |
< |
assertEquals(j, i.next()); |
249 |
< |
assertEquals(SIZE, j); |
244 |
> |
Collection empty = new CopyOnWriteArrayList(); |
245 |
> |
assertFalse(empty.iterator().hasNext()); |
246 |
> |
try { |
247 |
> |
empty.iterator().next(); |
248 |
> |
shouldThrow(); |
249 |
> |
} catch (NoSuchElementException success) {} |
250 |
> |
|
251 |
> |
Integer[] elements = new Integer[SIZE]; |
252 |
> |
for (int i = 0; i < SIZE; i++) |
253 |
> |
elements[i] = i; |
254 |
> |
Collections.shuffle(Arrays.asList(elements)); |
255 |
> |
Collection<Integer> full = populatedArray(elements); |
256 |
> |
|
257 |
> |
Iterator it = full.iterator(); |
258 |
> |
for (int j = 0; j < SIZE; j++) { |
259 |
> |
assertTrue(it.hasNext()); |
260 |
> |
assertEquals(elements[j], it.next()); |
261 |
> |
} |
262 |
> |
assertIteratorExhausted(it); |
263 |
> |
} |
264 |
> |
|
265 |
> |
/** |
266 |
> |
* iterator of empty collection has no elements |
267 |
> |
*/ |
268 |
> |
public void testEmptyIterator() { |
269 |
> |
Collection c = new CopyOnWriteArrayList(); |
270 |
> |
assertIteratorExhausted(c.iterator()); |
271 |
|
} |
272 |
|
|
273 |
|
/** |
287 |
|
* toString contains toString of elements |
288 |
|
*/ |
289 |
|
public void testToString() { |
290 |
+ |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
291 |
|
CopyOnWriteArrayList full = populatedArray(3); |
292 |
|
String s = full.toString(); |
293 |
< |
for (int i = 0; i < 3; ++i) { |
293 |
> |
for (int i = 0; i < 3; ++i) |
294 |
|
assertTrue(s.contains(String.valueOf(i))); |
295 |
< |
} |
295 |
> |
assertEquals(new ArrayList(full).toString(), |
296 |
> |
full.toString()); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
310 |
|
/** |
311 |
|
* lastIndexOf returns the index from the given starting point |
312 |
|
*/ |
313 |
< |
public void testlastIndexOf2() { |
313 |
> |
public void testLastIndexOf2() { |
314 |
|
CopyOnWriteArrayList full = populatedArray(3); |
315 |
|
full.add(one); |
316 |
|
full.add(three); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* remove removes and returns the object at the given index |
346 |
> |
* remove(int) removes and returns the object at the given index |
347 |
|
*/ |
348 |
< |
public void testRemove() { |
349 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
350 |
< |
assertEquals(2, full.remove(2)); |
351 |
< |
assertEquals(2, full.size()); |
348 |
> |
public void testRemove_int() { |
349 |
> |
int SIZE = 3; |
350 |
> |
for (int i = 0; i < SIZE; i++) { |
351 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
352 |
> |
assertEquals(i, full.remove(i)); |
353 |
> |
assertEquals(SIZE - 1, full.size()); |
354 |
> |
assertFalse(full.contains(new Integer(i))); |
355 |
> |
} |
356 |
> |
} |
357 |
> |
|
358 |
> |
/** |
359 |
> |
* remove(Object) removes the object if found and returns true |
360 |
> |
*/ |
361 |
> |
public void testRemove_Object() { |
362 |
> |
int SIZE = 3; |
363 |
> |
for (int i = 0; i < SIZE; i++) { |
364 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
365 |
> |
assertFalse(full.remove(new Integer(-42))); |
366 |
> |
assertTrue(full.remove(new Integer(i))); |
367 |
> |
assertEquals(SIZE - 1, full.size()); |
368 |
> |
assertFalse(full.contains(new Integer(i))); |
369 |
> |
} |
370 |
> |
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
371 |
> |
assertTrue(x.remove(new Integer(6))); |
372 |
> |
assertEquals(x, Arrays.asList(4, 5)); |
373 |
> |
assertTrue(x.remove(new Integer(4))); |
374 |
> |
assertEquals(x, Arrays.asList(5)); |
375 |
> |
assertTrue(x.remove(new Integer(5))); |
376 |
> |
assertEquals(x, Arrays.asList()); |
377 |
> |
assertFalse(x.remove(new Integer(5))); |
378 |
|
} |
379 |
|
|
380 |
|
/** |
382 |
|
*/ |
383 |
|
public void testRemoveAll() { |
384 |
|
CopyOnWriteArrayList full = populatedArray(3); |
385 |
< |
Vector v = new Vector(); |
386 |
< |
v.add(one); |
387 |
< |
v.add(two); |
327 |
< |
full.removeAll(v); |
385 |
> |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
386 |
> |
assertEquals(1, full.size()); |
387 |
> |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
388 |
|
assertEquals(1, full.size()); |
389 |
|
} |
390 |
|
|
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* toArray returns an Object array containing all elements from the list |
411 |
> |
* toArray() returns an Object array containing all elements from |
412 |
> |
* the list in insertion order |
413 |
|
*/ |
414 |
|
public void testToArray() { |
415 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
416 |
< |
Object[] o = full.toArray(); |
417 |
< |
assertEquals(3, o.length); |
418 |
< |
assertEquals(0, o[0]); |
419 |
< |
assertEquals(1, o[1]); |
420 |
< |
assertEquals(2, o[2]); |
415 |
> |
Object[] a = new CopyOnWriteArrayList().toArray(); |
416 |
> |
assertTrue(Arrays.equals(new Object[0], a)); |
417 |
> |
assertSame(Object[].class, a.getClass()); |
418 |
> |
|
419 |
> |
Integer[] elements = new Integer[SIZE]; |
420 |
> |
for (int i = 0; i < SIZE; i++) |
421 |
> |
elements[i] = i; |
422 |
> |
Collections.shuffle(Arrays.asList(elements)); |
423 |
> |
Collection<Integer> full = populatedArray(elements); |
424 |
> |
|
425 |
> |
assertTrue(Arrays.equals(elements, full.toArray())); |
426 |
> |
assertSame(Object[].class, full.toArray().getClass()); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
< |
* toArray returns an Integer array containing all elements from |
431 |
< |
* the list |
430 |
> |
* toArray(Integer array) returns an Integer array containing all |
431 |
> |
* elements from the list in insertion order |
432 |
|
*/ |
433 |
|
public void testToArray2() { |
434 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
435 |
< |
Integer[] i = new Integer[3]; |
369 |
< |
i = (Integer[])full.toArray(i); |
370 |
< |
assertEquals(3, i.length); |
371 |
< |
assertEquals(0, i[0].intValue()); |
372 |
< |
assertEquals(1, i[1].intValue()); |
373 |
< |
assertEquals(2, i[2].intValue()); |
374 |
< |
} |
434 |
> |
Collection empty = new CopyOnWriteArrayList(); |
435 |
> |
Integer[] a; |
436 |
|
|
437 |
+ |
a = new Integer[0]; |
438 |
+ |
assertSame(a, empty.toArray(a)); |
439 |
+ |
|
440 |
+ |
a = new Integer[SIZE/2]; |
441 |
+ |
Arrays.fill(a, 42); |
442 |
+ |
assertSame(a, empty.toArray(a)); |
443 |
+ |
assertNull(a[0]); |
444 |
+ |
for (int i = 1; i < a.length; i++) |
445 |
+ |
assertEquals(42, (int) a[i]); |
446 |
+ |
|
447 |
+ |
Integer[] elements = new Integer[SIZE]; |
448 |
+ |
for (int i = 0; i < SIZE; i++) |
449 |
+ |
elements[i] = i; |
450 |
+ |
Collections.shuffle(Arrays.asList(elements)); |
451 |
+ |
Collection<Integer> full = populatedArray(elements); |
452 |
+ |
|
453 |
+ |
Arrays.fill(a, 42); |
454 |
+ |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
455 |
+ |
for (int i = 0; i < a.length; i++) |
456 |
+ |
assertEquals(42, (int) a[i]); |
457 |
+ |
assertSame(Integer[].class, full.toArray(a).getClass()); |
458 |
+ |
|
459 |
+ |
a = new Integer[SIZE]; |
460 |
+ |
Arrays.fill(a, 42); |
461 |
+ |
assertSame(a, full.toArray(a)); |
462 |
+ |
assertTrue(Arrays.equals(elements, a)); |
463 |
+ |
|
464 |
+ |
a = new Integer[2*SIZE]; |
465 |
+ |
Arrays.fill(a, 42); |
466 |
+ |
assertSame(a, full.toArray(a)); |
467 |
+ |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
468 |
+ |
assertNull(a[SIZE]); |
469 |
+ |
for (int i = SIZE + 1; i < a.length; i++) |
470 |
+ |
assertEquals(42, (int) a[i]); |
471 |
+ |
} |
472 |
|
|
473 |
|
/** |
474 |
|
* sublists contains elements at indexes offset from their base |
486 |
|
} |
487 |
|
|
488 |
|
List s = a.subList(2, 5); |
489 |
< |
assertEquals(s.size(), 3); |
489 |
> |
assertEquals(3, s.size()); |
490 |
|
s.set(2, m1); |
491 |
|
assertEquals(a.get(4), m1); |
492 |
|
s.clear(); |
493 |
< |
assertEquals(a.size(), 7); |
493 |
> |
assertEquals(7, a.size()); |
494 |
|
} |
495 |
|
|
496 |
|
// Exception tests |
500 |
|
* can not store the objects inside the list |
501 |
|
*/ |
502 |
|
public void testToArray_ArrayStoreException() { |
503 |
+ |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
504 |
+ |
c.add("zfasdfsdf"); |
505 |
+ |
c.add("asdadasd"); |
506 |
|
try { |
408 |
– |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
409 |
– |
c.add("zfasdfsdf"); |
410 |
– |
c.add("asdadasd"); |
507 |
|
c.toArray(new Long[5]); |
508 |
|
shouldThrow(); |
509 |
|
} catch (ArrayStoreException success) {} |
513 |
|
* get throws an IndexOutOfBoundsException on a negative index |
514 |
|
*/ |
515 |
|
public void testGet1_IndexOutOfBoundsException() { |
516 |
< |
try { |
517 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
518 |
< |
c.get(-1); |
519 |
< |
shouldThrow(); |
520 |
< |
} catch (IndexOutOfBoundsException success) {} |
516 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
517 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
518 |
> |
for (List list : lists) { |
519 |
> |
try { |
520 |
> |
list.get(-1); |
521 |
> |
shouldThrow(); |
522 |
> |
} catch (IndexOutOfBoundsException success) {} |
523 |
> |
} |
524 |
|
} |
525 |
|
|
526 |
|
/** |
527 |
|
* get throws an IndexOutOfBoundsException on a too high index |
528 |
|
*/ |
529 |
|
public void testGet2_IndexOutOfBoundsException() { |
530 |
< |
try { |
531 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
532 |
< |
c.add("asdasd"); |
533 |
< |
c.add("asdad"); |
534 |
< |
c.get(100); |
535 |
< |
shouldThrow(); |
536 |
< |
} catch (IndexOutOfBoundsException success) {} |
530 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
531 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
532 |
> |
for (List list : lists) { |
533 |
> |
try { |
534 |
> |
list.get(list.size()); |
535 |
> |
shouldThrow(); |
536 |
> |
} catch (IndexOutOfBoundsException success) {} |
537 |
> |
} |
538 |
|
} |
539 |
|
|
540 |
|
/** |
541 |
|
* set throws an IndexOutOfBoundsException on a negative index |
542 |
|
*/ |
543 |
|
public void testSet1_IndexOutOfBoundsException() { |
544 |
< |
try { |
545 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
546 |
< |
c.set(-1,"qwerty"); |
547 |
< |
shouldThrow(); |
548 |
< |
} catch (IndexOutOfBoundsException success) {} |
544 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
545 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
546 |
> |
for (List list : lists) { |
547 |
> |
try { |
548 |
> |
list.set(-1, "qwerty"); |
549 |
> |
shouldThrow(); |
550 |
> |
} catch (IndexOutOfBoundsException success) {} |
551 |
> |
} |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
|
* set throws an IndexOutOfBoundsException on a too high index |
556 |
|
*/ |
557 |
|
public void testSet2() { |
558 |
< |
try { |
559 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
560 |
< |
c.add("asdasd"); |
561 |
< |
c.add("asdad"); |
562 |
< |
c.set(100, "qwerty"); |
563 |
< |
shouldThrow(); |
564 |
< |
} catch (IndexOutOfBoundsException success) {} |
558 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
559 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
560 |
> |
for (List list : lists) { |
561 |
> |
try { |
562 |
> |
list.set(list.size(), "qwerty"); |
563 |
> |
shouldThrow(); |
564 |
> |
} catch (IndexOutOfBoundsException success) {} |
565 |
> |
} |
566 |
|
} |
567 |
|
|
568 |
|
/** |
569 |
|
* add throws an IndexOutOfBoundsException on a negative index |
570 |
|
*/ |
571 |
|
public void testAdd1_IndexOutOfBoundsException() { |
572 |
< |
try { |
573 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
574 |
< |
c.add(-1,"qwerty"); |
575 |
< |
shouldThrow(); |
576 |
< |
} catch (IndexOutOfBoundsException success) {} |
572 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
573 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
574 |
> |
for (List list : lists) { |
575 |
> |
try { |
576 |
> |
list.add(-1, "qwerty"); |
577 |
> |
shouldThrow(); |
578 |
> |
} catch (IndexOutOfBoundsException success) {} |
579 |
> |
} |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
|
* add throws an IndexOutOfBoundsException on a too high index |
584 |
|
*/ |
585 |
|
public void testAdd2_IndexOutOfBoundsException() { |
586 |
< |
try { |
587 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
588 |
< |
c.add("asdasd"); |
589 |
< |
c.add("asdasdasd"); |
590 |
< |
c.add(100, "qwerty"); |
591 |
< |
shouldThrow(); |
592 |
< |
} catch (IndexOutOfBoundsException success) {} |
586 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
587 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
588 |
> |
for (List list : lists) { |
589 |
> |
try { |
590 |
> |
list.add(list.size() + 1, "qwerty"); |
591 |
> |
shouldThrow(); |
592 |
> |
} catch (IndexOutOfBoundsException success) {} |
593 |
> |
} |
594 |
|
} |
595 |
|
|
596 |
|
/** |
597 |
|
* remove throws an IndexOutOfBoundsException on a negative index |
598 |
|
*/ |
599 |
|
public void testRemove1_IndexOutOfBounds() { |
600 |
< |
try { |
601 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
602 |
< |
c.remove(-1); |
603 |
< |
shouldThrow(); |
604 |
< |
} catch (IndexOutOfBoundsException success) {} |
600 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
601 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
602 |
> |
for (List list : lists) { |
603 |
> |
try { |
604 |
> |
list.remove(-1); |
605 |
> |
shouldThrow(); |
606 |
> |
} catch (IndexOutOfBoundsException success) {} |
607 |
> |
} |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
|
* remove throws an IndexOutOfBoundsException on a too high index |
612 |
|
*/ |
613 |
|
public void testRemove2_IndexOutOfBounds() { |
614 |
< |
try { |
615 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
616 |
< |
c.add("asdasd"); |
617 |
< |
c.add("adasdasd"); |
618 |
< |
c.remove(100); |
619 |
< |
shouldThrow(); |
620 |
< |
} catch (IndexOutOfBoundsException success) {} |
614 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
615 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
616 |
> |
for (List list : lists) { |
617 |
> |
try { |
618 |
> |
list.remove(list.size()); |
619 |
> |
shouldThrow(); |
620 |
> |
} catch (IndexOutOfBoundsException success) {} |
621 |
> |
} |
622 |
|
} |
623 |
|
|
624 |
|
/** |
625 |
|
* addAll throws an IndexOutOfBoundsException on a negative index |
626 |
|
*/ |
627 |
|
public void testAddAll1_IndexOutOfBoundsException() { |
628 |
< |
try { |
629 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
630 |
< |
c.addAll(-1,new LinkedList()); |
631 |
< |
shouldThrow(); |
632 |
< |
} catch (IndexOutOfBoundsException success) {} |
628 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
629 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
630 |
> |
for (List list : lists) { |
631 |
> |
try { |
632 |
> |
list.addAll(-1, new LinkedList()); |
633 |
> |
shouldThrow(); |
634 |
> |
} catch (IndexOutOfBoundsException success) {} |
635 |
> |
} |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
|
* addAll throws an IndexOutOfBoundsException on a too high index |
640 |
|
*/ |
641 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
642 |
< |
try { |
643 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
644 |
< |
c.add("asdasd"); |
645 |
< |
c.add("asdasdasd"); |
646 |
< |
c.addAll(100, new LinkedList()); |
647 |
< |
shouldThrow(); |
648 |
< |
} catch (IndexOutOfBoundsException success) {} |
642 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
643 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
644 |
> |
for (List list : lists) { |
645 |
> |
try { |
646 |
> |
list.addAll(list.size() + 1, new LinkedList()); |
647 |
> |
shouldThrow(); |
648 |
> |
} catch (IndexOutOfBoundsException success) {} |
649 |
> |
} |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
|
* listIterator throws an IndexOutOfBoundsException on a negative index |
654 |
|
*/ |
655 |
|
public void testListIterator1_IndexOutOfBoundsException() { |
656 |
< |
try { |
657 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
658 |
< |
c.listIterator(-1); |
659 |
< |
shouldThrow(); |
660 |
< |
} catch (IndexOutOfBoundsException success) {} |
656 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
657 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
658 |
> |
for (List list : lists) { |
659 |
> |
try { |
660 |
> |
list.listIterator(-1); |
661 |
> |
shouldThrow(); |
662 |
> |
} catch (IndexOutOfBoundsException success) {} |
663 |
> |
} |
664 |
|
} |
665 |
|
|
666 |
|
/** |
667 |
|
* listIterator throws an IndexOutOfBoundsException on a too high index |
668 |
|
*/ |
669 |
|
public void testListIterator2_IndexOutOfBoundsException() { |
670 |
< |
try { |
671 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
672 |
< |
c.add("adasd"); |
673 |
< |
c.add("asdasdas"); |
674 |
< |
c.listIterator(100); |
675 |
< |
shouldThrow(); |
676 |
< |
} catch (IndexOutOfBoundsException success) {} |
670 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
671 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
672 |
> |
for (List list : lists) { |
673 |
> |
try { |
674 |
> |
list.listIterator(list.size() + 1); |
675 |
> |
shouldThrow(); |
676 |
> |
} catch (IndexOutOfBoundsException success) {} |
677 |
> |
} |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
|
* subList throws an IndexOutOfBoundsException on a negative index |
682 |
|
*/ |
683 |
|
public void testSubList1_IndexOutOfBoundsException() { |
684 |
< |
try { |
685 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
686 |
< |
c.subList(-1,100); |
687 |
< |
shouldThrow(); |
688 |
< |
} catch (IndexOutOfBoundsException success) {} |
684 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
685 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
686 |
> |
for (List list : lists) { |
687 |
> |
try { |
688 |
> |
list.subList(-1, list.size()); |
689 |
> |
shouldThrow(); |
690 |
> |
} catch (IndexOutOfBoundsException success) {} |
691 |
> |
} |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
|
* subList throws an IndexOutOfBoundsException on a too high index |
696 |
|
*/ |
697 |
|
public void testSubList2_IndexOutOfBoundsException() { |
698 |
< |
try { |
699 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
700 |
< |
c.add("asdasd"); |
701 |
< |
c.subList(1,100); |
702 |
< |
shouldThrow(); |
703 |
< |
} catch (IndexOutOfBoundsException success) {} |
698 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
699 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
700 |
> |
for (List list : lists) { |
701 |
> |
try { |
702 |
> |
list.subList(0, list.size() + 1); |
703 |
> |
shouldThrow(); |
704 |
> |
} catch (IndexOutOfBoundsException success) {} |
705 |
> |
} |
706 |
|
} |
707 |
|
|
708 |
|
/** |
710 |
|
* is lower then the first |
711 |
|
*/ |
712 |
|
public void testSubList3_IndexOutOfBoundsException() { |
713 |
< |
try { |
714 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
715 |
< |
c.subList(3,1); |
716 |
< |
shouldThrow(); |
717 |
< |
} catch (IndexOutOfBoundsException success) {} |
713 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
714 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
715 |
> |
for (List list : lists) { |
716 |
> |
try { |
717 |
> |
list.subList(list.size() - 1, 1); |
718 |
> |
shouldThrow(); |
719 |
> |
} catch (IndexOutOfBoundsException success) {} |
720 |
> |
} |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
|
* a deserialized serialized list is equal |
725 |
|
*/ |
726 |
|
public void testSerialization() throws Exception { |
727 |
< |
CopyOnWriteArrayList q = populatedArray(SIZE); |
727 |
> |
List x = populatedArray(SIZE); |
728 |
> |
List y = serialClone(x); |
729 |
|
|
730 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
731 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
732 |
< |
out.writeObject(q); |
733 |
< |
out.close(); |
734 |
< |
|
735 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
736 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
737 |
< |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
738 |
< |
assertEquals(q.size(), r.size()); |
739 |
< |
assertTrue(q.equals(r)); |
740 |
< |
assertTrue(r.equals(q)); |
730 |
> |
assertNotSame(x, y); |
731 |
> |
assertEquals(x.size(), y.size()); |
732 |
> |
assertEquals(x.toString(), y.toString()); |
733 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
734 |
> |
assertEquals(x, y); |
735 |
> |
assertEquals(y, x); |
736 |
> |
while (!x.isEmpty()) { |
737 |
> |
assertFalse(y.isEmpty()); |
738 |
> |
assertEquals(x.remove(0), y.remove(0)); |
739 |
> |
} |
740 |
> |
assertTrue(y.isEmpty()); |
741 |
|
} |
742 |
|
|
743 |
|
} |