6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
9 |
> |
import java.util.ArrayList; |
10 |
|
import java.util.Arrays; |
11 |
|
import java.util.Collection; |
12 |
|
import java.util.Collections; |
14 |
|
import java.util.LinkedList; |
15 |
|
import java.util.List; |
16 |
|
import java.util.ListIterator; |
17 |
+ |
import java.util.NoSuchElementException; |
18 |
|
import java.util.Vector; |
19 |
|
import java.util.concurrent.CopyOnWriteArrayList; |
20 |
|
|
21 |
+ |
import junit.framework.Test; |
22 |
+ |
import junit.framework.TestSuite; |
23 |
+ |
|
24 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
25 |
|
|
26 |
|
public static void main(String[] args) { |
242 |
|
} |
243 |
|
|
244 |
|
/** |
245 |
< |
* iterator() returns an iterator containing the elements of the list |
245 |
> |
* iterator() returns an iterator containing the elements of the |
246 |
> |
* list in insertion order |
247 |
|
*/ |
248 |
|
public void testIterator() { |
249 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
250 |
< |
Iterator i = full.iterator(); |
251 |
< |
int j; |
252 |
< |
for (j = 0; i.hasNext(); j++) |
253 |
< |
assertEquals(j, i.next()); |
254 |
< |
assertEquals(SIZE, j); |
249 |
> |
Collection empty = new CopyOnWriteArrayList(); |
250 |
> |
assertFalse(empty.iterator().hasNext()); |
251 |
> |
try { |
252 |
> |
empty.iterator().next(); |
253 |
> |
shouldThrow(); |
254 |
> |
} catch (NoSuchElementException success) {} |
255 |
> |
|
256 |
> |
Integer[] elements = new Integer[SIZE]; |
257 |
> |
for (int i = 0; i < SIZE; i++) |
258 |
> |
elements[i] = i; |
259 |
> |
Collections.shuffle(Arrays.asList(elements)); |
260 |
> |
Collection<Integer> full = populatedArray(elements); |
261 |
> |
|
262 |
> |
Iterator it = full.iterator(); |
263 |
> |
for (int j = 0; j < SIZE; j++) { |
264 |
> |
assertTrue(it.hasNext()); |
265 |
> |
assertEquals(elements[j], it.next()); |
266 |
> |
} |
267 |
> |
assertFalse(it.hasNext()); |
268 |
> |
try { |
269 |
> |
it.next(); |
270 |
> |
shouldThrow(); |
271 |
> |
} catch (NoSuchElementException success) {} |
272 |
|
} |
273 |
|
|
274 |
|
/** |
288 |
|
* toString contains toString of elements |
289 |
|
*/ |
290 |
|
public void testToString() { |
291 |
+ |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
292 |
|
CopyOnWriteArrayList full = populatedArray(3); |
293 |
|
String s = full.toString(); |
294 |
< |
for (int i = 0; i < 3; ++i) { |
294 |
> |
for (int i = 0; i < 3; ++i) |
295 |
|
assertTrue(s.contains(String.valueOf(i))); |
296 |
< |
} |
296 |
> |
assertEquals(new ArrayList(full).toString(), |
297 |
> |
full.toString()); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* remove removes and returns the object at the given index |
347 |
> |
* remove(int) removes and returns the object at the given index |
348 |
|
*/ |
349 |
< |
public void testRemove() { |
350 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
351 |
< |
assertEquals(2, full.remove(2)); |
352 |
< |
assertEquals(2, full.size()); |
349 |
> |
public void testRemove_int() { |
350 |
> |
int SIZE = 3; |
351 |
> |
for (int i = 0; i < SIZE; i++) { |
352 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
353 |
> |
assertEquals(i, full.remove(i)); |
354 |
> |
assertEquals(SIZE - 1, full.size()); |
355 |
> |
assertFalse(full.contains(new Integer(i))); |
356 |
> |
} |
357 |
> |
} |
358 |
> |
|
359 |
> |
/** |
360 |
> |
* remove(Object) removes the object if found and returns true |
361 |
> |
*/ |
362 |
> |
public void testRemove_Object() { |
363 |
> |
int SIZE = 3; |
364 |
> |
for (int i = 0; i < SIZE; i++) { |
365 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
366 |
> |
assertFalse(full.remove(new Integer(-42))); |
367 |
> |
assertTrue(full.remove(new Integer(i))); |
368 |
> |
assertEquals(SIZE - 1, full.size()); |
369 |
> |
assertFalse(full.contains(new Integer(i))); |
370 |
> |
} |
371 |
> |
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
372 |
> |
assertTrue(x.remove(new Integer(6))); |
373 |
> |
assertEquals(x, Arrays.asList(4, 5)); |
374 |
> |
assertTrue(x.remove(new Integer(4))); |
375 |
> |
assertEquals(x, Arrays.asList(5)); |
376 |
> |
assertTrue(x.remove(new Integer(5))); |
377 |
> |
assertEquals(x, Arrays.asList()); |
378 |
> |
assertFalse(x.remove(new Integer(5))); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
502 |
|
* can not store the objects inside the list |
503 |
|
*/ |
504 |
|
public void testToArray_ArrayStoreException() { |
505 |
+ |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
506 |
+ |
c.add("zfasdfsdf"); |
507 |
+ |
c.add("asdadasd"); |
508 |
|
try { |
456 |
– |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
457 |
– |
c.add("zfasdfsdf"); |
458 |
– |
c.add("asdadasd"); |
509 |
|
c.toArray(new Long[5]); |
510 |
|
shouldThrow(); |
511 |
|
} catch (ArrayStoreException success) {} |
515 |
|
* get throws an IndexOutOfBoundsException on a negative index |
516 |
|
*/ |
517 |
|
public void testGet1_IndexOutOfBoundsException() { |
518 |
< |
try { |
519 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
520 |
< |
c.get(-1); |
521 |
< |
shouldThrow(); |
522 |
< |
} catch (IndexOutOfBoundsException success) {} |
518 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
519 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
520 |
> |
for (List list : lists) { |
521 |
> |
try { |
522 |
> |
list.get(-1); |
523 |
> |
shouldThrow(); |
524 |
> |
} catch (IndexOutOfBoundsException success) {} |
525 |
> |
} |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
|
* get throws an IndexOutOfBoundsException on a too high index |
530 |
|
*/ |
531 |
|
public void testGet2_IndexOutOfBoundsException() { |
532 |
< |
try { |
533 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
534 |
< |
c.add("asdasd"); |
535 |
< |
c.add("asdad"); |
536 |
< |
c.get(100); |
537 |
< |
shouldThrow(); |
538 |
< |
} catch (IndexOutOfBoundsException success) {} |
532 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
533 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
534 |
> |
for (List list : lists) { |
535 |
> |
try { |
536 |
> |
list.get(list.size()); |
537 |
> |
shouldThrow(); |
538 |
> |
} catch (IndexOutOfBoundsException success) {} |
539 |
> |
} |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
|
* set throws an IndexOutOfBoundsException on a negative index |
544 |
|
*/ |
545 |
|
public void testSet1_IndexOutOfBoundsException() { |
546 |
< |
try { |
547 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
548 |
< |
c.set(-1,"qwerty"); |
549 |
< |
shouldThrow(); |
550 |
< |
} catch (IndexOutOfBoundsException success) {} |
546 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
547 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
548 |
> |
for (List list : lists) { |
549 |
> |
try { |
550 |
> |
list.set(-1, "qwerty"); |
551 |
> |
shouldThrow(); |
552 |
> |
} catch (IndexOutOfBoundsException success) {} |
553 |
> |
} |
554 |
|
} |
555 |
|
|
556 |
|
/** |
557 |
|
* set throws an IndexOutOfBoundsException on a too high index |
558 |
|
*/ |
559 |
|
public void testSet2() { |
560 |
< |
try { |
561 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
562 |
< |
c.add("asdasd"); |
563 |
< |
c.add("asdad"); |
564 |
< |
c.set(100, "qwerty"); |
565 |
< |
shouldThrow(); |
566 |
< |
} catch (IndexOutOfBoundsException success) {} |
560 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
561 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
562 |
> |
for (List list : lists) { |
563 |
> |
try { |
564 |
> |
list.set(list.size(), "qwerty"); |
565 |
> |
shouldThrow(); |
566 |
> |
} catch (IndexOutOfBoundsException success) {} |
567 |
> |
} |
568 |
|
} |
569 |
|
|
570 |
|
/** |
571 |
|
* add throws an IndexOutOfBoundsException on a negative index |
572 |
|
*/ |
573 |
|
public void testAdd1_IndexOutOfBoundsException() { |
574 |
< |
try { |
575 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
576 |
< |
c.add(-1,"qwerty"); |
577 |
< |
shouldThrow(); |
578 |
< |
} catch (IndexOutOfBoundsException success) {} |
574 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
575 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
576 |
> |
for (List list : lists) { |
577 |
> |
try { |
578 |
> |
list.add(-1, "qwerty"); |
579 |
> |
shouldThrow(); |
580 |
> |
} catch (IndexOutOfBoundsException success) {} |
581 |
> |
} |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
|
* add throws an IndexOutOfBoundsException on a too high index |
586 |
|
*/ |
587 |
|
public void testAdd2_IndexOutOfBoundsException() { |
588 |
< |
try { |
589 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
590 |
< |
c.add("asdasd"); |
591 |
< |
c.add("asdasdasd"); |
592 |
< |
c.add(100, "qwerty"); |
593 |
< |
shouldThrow(); |
594 |
< |
} catch (IndexOutOfBoundsException success) {} |
588 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
589 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
590 |
> |
for (List list : lists) { |
591 |
> |
try { |
592 |
> |
list.add(list.size() + 1, "qwerty"); |
593 |
> |
shouldThrow(); |
594 |
> |
} catch (IndexOutOfBoundsException success) {} |
595 |
> |
} |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
|
* remove throws an IndexOutOfBoundsException on a negative index |
600 |
|
*/ |
601 |
|
public void testRemove1_IndexOutOfBounds() { |
602 |
< |
try { |
603 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
604 |
< |
c.remove(-1); |
605 |
< |
shouldThrow(); |
606 |
< |
} catch (IndexOutOfBoundsException success) {} |
602 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
603 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
604 |
> |
for (List list : lists) { |
605 |
> |
try { |
606 |
> |
list.remove(-1); |
607 |
> |
shouldThrow(); |
608 |
> |
} catch (IndexOutOfBoundsException success) {} |
609 |
> |
} |
610 |
|
} |
611 |
|
|
612 |
|
/** |
613 |
|
* remove throws an IndexOutOfBoundsException on a too high index |
614 |
|
*/ |
615 |
|
public void testRemove2_IndexOutOfBounds() { |
616 |
< |
try { |
617 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
618 |
< |
c.add("asdasd"); |
619 |
< |
c.add("adasdasd"); |
620 |
< |
c.remove(100); |
621 |
< |
shouldThrow(); |
622 |
< |
} catch (IndexOutOfBoundsException success) {} |
616 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
617 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
618 |
> |
for (List list : lists) { |
619 |
> |
try { |
620 |
> |
list.remove(list.size()); |
621 |
> |
shouldThrow(); |
622 |
> |
} catch (IndexOutOfBoundsException success) {} |
623 |
> |
} |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
|
* addAll throws an IndexOutOfBoundsException on a negative index |
628 |
|
*/ |
629 |
|
public void testAddAll1_IndexOutOfBoundsException() { |
630 |
< |
try { |
631 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
632 |
< |
c.addAll(-1,new LinkedList()); |
633 |
< |
shouldThrow(); |
634 |
< |
} catch (IndexOutOfBoundsException success) {} |
630 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
631 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
632 |
> |
for (List list : lists) { |
633 |
> |
try { |
634 |
> |
list.addAll(-1, new LinkedList()); |
635 |
> |
shouldThrow(); |
636 |
> |
} catch (IndexOutOfBoundsException success) {} |
637 |
> |
} |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
|
* addAll throws an IndexOutOfBoundsException on a too high index |
642 |
|
*/ |
643 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
644 |
< |
try { |
645 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
646 |
< |
c.add("asdasd"); |
647 |
< |
c.add("asdasdasd"); |
648 |
< |
c.addAll(100, new LinkedList()); |
649 |
< |
shouldThrow(); |
650 |
< |
} catch (IndexOutOfBoundsException success) {} |
644 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
645 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
646 |
> |
for (List list : lists) { |
647 |
> |
try { |
648 |
> |
list.addAll(list.size() + 1, new LinkedList()); |
649 |
> |
shouldThrow(); |
650 |
> |
} catch (IndexOutOfBoundsException success) {} |
651 |
> |
} |
652 |
|
} |
653 |
|
|
654 |
|
/** |
655 |
|
* listIterator throws an IndexOutOfBoundsException on a negative index |
656 |
|
*/ |
657 |
|
public void testListIterator1_IndexOutOfBoundsException() { |
658 |
< |
try { |
659 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
660 |
< |
c.listIterator(-1); |
661 |
< |
shouldThrow(); |
662 |
< |
} catch (IndexOutOfBoundsException success) {} |
658 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
659 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
660 |
> |
for (List list : lists) { |
661 |
> |
try { |
662 |
> |
list.listIterator(-1); |
663 |
> |
shouldThrow(); |
664 |
> |
} catch (IndexOutOfBoundsException success) {} |
665 |
> |
} |
666 |
|
} |
667 |
|
|
668 |
|
/** |
669 |
|
* listIterator throws an IndexOutOfBoundsException on a too high index |
670 |
|
*/ |
671 |
|
public void testListIterator2_IndexOutOfBoundsException() { |
672 |
< |
try { |
673 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
674 |
< |
c.add("adasd"); |
675 |
< |
c.add("asdasdas"); |
676 |
< |
c.listIterator(100); |
677 |
< |
shouldThrow(); |
678 |
< |
} catch (IndexOutOfBoundsException success) {} |
672 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
673 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
674 |
> |
for (List list : lists) { |
675 |
> |
try { |
676 |
> |
list.listIterator(list.size() + 1); |
677 |
> |
shouldThrow(); |
678 |
> |
} catch (IndexOutOfBoundsException success) {} |
679 |
> |
} |
680 |
|
} |
681 |
|
|
682 |
|
/** |
683 |
|
* subList throws an IndexOutOfBoundsException on a negative index |
684 |
|
*/ |
685 |
|
public void testSubList1_IndexOutOfBoundsException() { |
686 |
< |
try { |
687 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
688 |
< |
c.subList(-1,100); |
689 |
< |
shouldThrow(); |
690 |
< |
} catch (IndexOutOfBoundsException success) {} |
686 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
687 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
688 |
> |
for (List list : lists) { |
689 |
> |
try { |
690 |
> |
list.subList(-1, list.size()); |
691 |
> |
shouldThrow(); |
692 |
> |
} catch (IndexOutOfBoundsException success) {} |
693 |
> |
} |
694 |
|
} |
695 |
|
|
696 |
|
/** |
697 |
|
* subList throws an IndexOutOfBoundsException on a too high index |
698 |
|
*/ |
699 |
|
public void testSubList2_IndexOutOfBoundsException() { |
700 |
< |
try { |
701 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
702 |
< |
c.add("asdasd"); |
703 |
< |
c.subList(1,100); |
704 |
< |
shouldThrow(); |
705 |
< |
} catch (IndexOutOfBoundsException success) {} |
700 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
701 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
702 |
> |
for (List list : lists) { |
703 |
> |
try { |
704 |
> |
list.subList(0, list.size() + 1); |
705 |
> |
shouldThrow(); |
706 |
> |
} catch (IndexOutOfBoundsException success) {} |
707 |
> |
} |
708 |
|
} |
709 |
|
|
710 |
|
/** |
712 |
|
* is lower then the first |
713 |
|
*/ |
714 |
|
public void testSubList3_IndexOutOfBoundsException() { |
715 |
< |
try { |
716 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
717 |
< |
c.subList(3,1); |
718 |
< |
shouldThrow(); |
719 |
< |
} catch (IndexOutOfBoundsException success) {} |
715 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
716 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
717 |
> |
for (List list : lists) { |
718 |
> |
try { |
719 |
> |
list.subList(list.size() - 1, 1); |
720 |
> |
shouldThrow(); |
721 |
> |
} catch (IndexOutOfBoundsException success) {} |
722 |
> |
} |
723 |
|
} |
724 |
|
|
725 |
|
/** |
729 |
|
List x = populatedArray(SIZE); |
730 |
|
List y = serialClone(x); |
731 |
|
|
732 |
< |
assertTrue(x != y); |
732 |
> |
assertNotSame(x, y); |
733 |
|
assertEquals(x.size(), y.size()); |
734 |
|
assertEquals(x.toString(), y.toString()); |
735 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |