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; |
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) { |
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) {} |
267 |
> |
assertIteratorExhausted(it); |
268 |
> |
} |
269 |
> |
|
270 |
> |
/** |
271 |
> |
* iterator of empty collection has no elements |
272 |
> |
*/ |
273 |
> |
public void testEmptyIterator() { |
274 |
> |
Collection c = new CopyOnWriteArrayList(); |
275 |
> |
assertIteratorExhausted(c.iterator()); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* remove removes and returns the object at the given index |
351 |
> |
* remove(int) removes and returns the object at the given index |
352 |
|
*/ |
353 |
< |
public void testRemove() { |
354 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
355 |
< |
assertEquals(2, full.remove(2)); |
356 |
< |
assertEquals(2, full.size()); |
353 |
> |
public void testRemove_int() { |
354 |
> |
int SIZE = 3; |
355 |
> |
for (int i = 0; i < SIZE; i++) { |
356 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
357 |
> |
assertEquals(i, full.remove(i)); |
358 |
> |
assertEquals(SIZE - 1, full.size()); |
359 |
> |
assertFalse(full.contains(new Integer(i))); |
360 |
> |
} |
361 |
> |
} |
362 |
> |
|
363 |
> |
/** |
364 |
> |
* remove(Object) removes the object if found and returns true |
365 |
> |
*/ |
366 |
> |
public void testRemove_Object() { |
367 |
> |
int SIZE = 3; |
368 |
> |
for (int i = 0; i < SIZE; i++) { |
369 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
370 |
> |
assertFalse(full.remove(new Integer(-42))); |
371 |
> |
assertTrue(full.remove(new Integer(i))); |
372 |
> |
assertEquals(SIZE - 1, full.size()); |
373 |
> |
assertFalse(full.contains(new Integer(i))); |
374 |
> |
} |
375 |
> |
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
376 |
> |
assertTrue(x.remove(new Integer(6))); |
377 |
> |
assertEquals(x, Arrays.asList(4, 5)); |
378 |
> |
assertTrue(x.remove(new Integer(4))); |
379 |
> |
assertEquals(x, Arrays.asList(5)); |
380 |
> |
assertTrue(x.remove(new Integer(5))); |
381 |
> |
assertEquals(x, Arrays.asList()); |
382 |
> |
assertFalse(x.remove(new Integer(5))); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
506 |
|
* can not store the objects inside the list |
507 |
|
*/ |
508 |
|
public void testToArray_ArrayStoreException() { |
509 |
+ |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
510 |
+ |
c.add("zfasdfsdf"); |
511 |
+ |
c.add("asdadasd"); |
512 |
|
try { |
478 |
– |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
479 |
– |
c.add("zfasdfsdf"); |
480 |
– |
c.add("asdadasd"); |
513 |
|
c.toArray(new Long[5]); |
514 |
|
shouldThrow(); |
515 |
|
} catch (ArrayStoreException success) {} |
519 |
|
* get throws an IndexOutOfBoundsException on a negative index |
520 |
|
*/ |
521 |
|
public void testGet1_IndexOutOfBoundsException() { |
522 |
< |
try { |
523 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
524 |
< |
c.get(-1); |
525 |
< |
shouldThrow(); |
526 |
< |
} catch (IndexOutOfBoundsException success) {} |
522 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
523 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
524 |
> |
for (List list : lists) { |
525 |
> |
try { |
526 |
> |
list.get(-1); |
527 |
> |
shouldThrow(); |
528 |
> |
} catch (IndexOutOfBoundsException success) {} |
529 |
> |
} |
530 |
|
} |
531 |
|
|
532 |
|
/** |
533 |
|
* get throws an IndexOutOfBoundsException on a too high index |
534 |
|
*/ |
535 |
|
public void testGet2_IndexOutOfBoundsException() { |
536 |
< |
try { |
537 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
538 |
< |
c.add("asdasd"); |
539 |
< |
c.add("asdad"); |
540 |
< |
c.get(100); |
541 |
< |
shouldThrow(); |
542 |
< |
} catch (IndexOutOfBoundsException success) {} |
536 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
537 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
538 |
> |
for (List list : lists) { |
539 |
> |
try { |
540 |
> |
list.get(list.size()); |
541 |
> |
shouldThrow(); |
542 |
> |
} catch (IndexOutOfBoundsException success) {} |
543 |
> |
} |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
|
* set throws an IndexOutOfBoundsException on a negative index |
548 |
|
*/ |
549 |
|
public void testSet1_IndexOutOfBoundsException() { |
550 |
< |
try { |
551 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
552 |
< |
c.set(-1,"qwerty"); |
553 |
< |
shouldThrow(); |
554 |
< |
} catch (IndexOutOfBoundsException success) {} |
550 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
551 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
552 |
> |
for (List list : lists) { |
553 |
> |
try { |
554 |
> |
list.set(-1, "qwerty"); |
555 |
> |
shouldThrow(); |
556 |
> |
} catch (IndexOutOfBoundsException success) {} |
557 |
> |
} |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
|
* set throws an IndexOutOfBoundsException on a too high index |
562 |
|
*/ |
563 |
|
public void testSet2() { |
564 |
< |
try { |
565 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
566 |
< |
c.add("asdasd"); |
567 |
< |
c.add("asdad"); |
568 |
< |
c.set(100, "qwerty"); |
569 |
< |
shouldThrow(); |
570 |
< |
} catch (IndexOutOfBoundsException success) {} |
564 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
565 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
566 |
> |
for (List list : lists) { |
567 |
> |
try { |
568 |
> |
list.set(list.size(), "qwerty"); |
569 |
> |
shouldThrow(); |
570 |
> |
} catch (IndexOutOfBoundsException success) {} |
571 |
> |
} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
|
* add throws an IndexOutOfBoundsException on a negative index |
576 |
|
*/ |
577 |
|
public void testAdd1_IndexOutOfBoundsException() { |
578 |
< |
try { |
579 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
580 |
< |
c.add(-1,"qwerty"); |
581 |
< |
shouldThrow(); |
582 |
< |
} catch (IndexOutOfBoundsException success) {} |
578 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
579 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
580 |
> |
for (List list : lists) { |
581 |
> |
try { |
582 |
> |
list.add(-1, "qwerty"); |
583 |
> |
shouldThrow(); |
584 |
> |
} catch (IndexOutOfBoundsException success) {} |
585 |
> |
} |
586 |
|
} |
587 |
|
|
588 |
|
/** |
589 |
|
* add throws an IndexOutOfBoundsException on a too high index |
590 |
|
*/ |
591 |
|
public void testAdd2_IndexOutOfBoundsException() { |
592 |
< |
try { |
593 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
594 |
< |
c.add("asdasd"); |
595 |
< |
c.add("asdasdasd"); |
596 |
< |
c.add(100, "qwerty"); |
597 |
< |
shouldThrow(); |
598 |
< |
} catch (IndexOutOfBoundsException success) {} |
592 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
593 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
594 |
> |
for (List list : lists) { |
595 |
> |
try { |
596 |
> |
list.add(list.size() + 1, "qwerty"); |
597 |
> |
shouldThrow(); |
598 |
> |
} catch (IndexOutOfBoundsException success) {} |
599 |
> |
} |
600 |
|
} |
601 |
|
|
602 |
|
/** |
603 |
|
* remove throws an IndexOutOfBoundsException on a negative index |
604 |
|
*/ |
605 |
|
public void testRemove1_IndexOutOfBounds() { |
606 |
< |
try { |
607 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
608 |
< |
c.remove(-1); |
609 |
< |
shouldThrow(); |
610 |
< |
} catch (IndexOutOfBoundsException success) {} |
606 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
607 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
608 |
> |
for (List list : lists) { |
609 |
> |
try { |
610 |
> |
list.remove(-1); |
611 |
> |
shouldThrow(); |
612 |
> |
} catch (IndexOutOfBoundsException success) {} |
613 |
> |
} |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
|
* remove throws an IndexOutOfBoundsException on a too high index |
618 |
|
*/ |
619 |
|
public void testRemove2_IndexOutOfBounds() { |
620 |
< |
try { |
621 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
622 |
< |
c.add("asdasd"); |
623 |
< |
c.add("adasdasd"); |
624 |
< |
c.remove(100); |
625 |
< |
shouldThrow(); |
626 |
< |
} catch (IndexOutOfBoundsException success) {} |
620 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
621 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
622 |
> |
for (List list : lists) { |
623 |
> |
try { |
624 |
> |
list.remove(list.size()); |
625 |
> |
shouldThrow(); |
626 |
> |
} catch (IndexOutOfBoundsException success) {} |
627 |
> |
} |
628 |
|
} |
629 |
|
|
630 |
|
/** |
631 |
|
* addAll throws an IndexOutOfBoundsException on a negative index |
632 |
|
*/ |
633 |
|
public void testAddAll1_IndexOutOfBoundsException() { |
634 |
< |
try { |
635 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
636 |
< |
c.addAll(-1,new LinkedList()); |
637 |
< |
shouldThrow(); |
638 |
< |
} catch (IndexOutOfBoundsException success) {} |
634 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
635 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
636 |
> |
for (List list : lists) { |
637 |
> |
try { |
638 |
> |
list.addAll(-1, new LinkedList()); |
639 |
> |
shouldThrow(); |
640 |
> |
} catch (IndexOutOfBoundsException success) {} |
641 |
> |
} |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
|
* addAll throws an IndexOutOfBoundsException on a too high index |
646 |
|
*/ |
647 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
648 |
< |
try { |
649 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
650 |
< |
c.add("asdasd"); |
651 |
< |
c.add("asdasdasd"); |
652 |
< |
c.addAll(100, new LinkedList()); |
653 |
< |
shouldThrow(); |
654 |
< |
} catch (IndexOutOfBoundsException success) {} |
648 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
649 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
650 |
> |
for (List list : lists) { |
651 |
> |
try { |
652 |
> |
list.addAll(list.size() + 1, new LinkedList()); |
653 |
> |
shouldThrow(); |
654 |
> |
} catch (IndexOutOfBoundsException success) {} |
655 |
> |
} |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
|
* listIterator throws an IndexOutOfBoundsException on a negative index |
660 |
|
*/ |
661 |
|
public void testListIterator1_IndexOutOfBoundsException() { |
662 |
< |
try { |
663 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
664 |
< |
c.listIterator(-1); |
665 |
< |
shouldThrow(); |
666 |
< |
} catch (IndexOutOfBoundsException success) {} |
662 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
663 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
664 |
> |
for (List list : lists) { |
665 |
> |
try { |
666 |
> |
list.listIterator(-1); |
667 |
> |
shouldThrow(); |
668 |
> |
} catch (IndexOutOfBoundsException success) {} |
669 |
> |
} |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
|
* listIterator throws an IndexOutOfBoundsException on a too high index |
674 |
|
*/ |
675 |
|
public void testListIterator2_IndexOutOfBoundsException() { |
676 |
< |
try { |
677 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
678 |
< |
c.add("adasd"); |
679 |
< |
c.add("asdasdas"); |
680 |
< |
c.listIterator(100); |
681 |
< |
shouldThrow(); |
682 |
< |
} catch (IndexOutOfBoundsException success) {} |
676 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
677 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
678 |
> |
for (List list : lists) { |
679 |
> |
try { |
680 |
> |
list.listIterator(list.size() + 1); |
681 |
> |
shouldThrow(); |
682 |
> |
} catch (IndexOutOfBoundsException success) {} |
683 |
> |
} |
684 |
|
} |
685 |
|
|
686 |
|
/** |
687 |
|
* subList throws an IndexOutOfBoundsException on a negative index |
688 |
|
*/ |
689 |
|
public void testSubList1_IndexOutOfBoundsException() { |
690 |
< |
try { |
691 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
692 |
< |
c.subList(-1,100); |
693 |
< |
shouldThrow(); |
694 |
< |
} catch (IndexOutOfBoundsException success) {} |
690 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
691 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
692 |
> |
for (List list : lists) { |
693 |
> |
try { |
694 |
> |
list.subList(-1, list.size()); |
695 |
> |
shouldThrow(); |
696 |
> |
} catch (IndexOutOfBoundsException success) {} |
697 |
> |
} |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
|
* subList throws an IndexOutOfBoundsException on a too high index |
702 |
|
*/ |
703 |
|
public void testSubList2_IndexOutOfBoundsException() { |
704 |
< |
try { |
705 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
706 |
< |
c.add("asdasd"); |
707 |
< |
c.subList(1,100); |
708 |
< |
shouldThrow(); |
709 |
< |
} catch (IndexOutOfBoundsException success) {} |
704 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
705 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
706 |
> |
for (List list : lists) { |
707 |
> |
try { |
708 |
> |
list.subList(0, list.size() + 1); |
709 |
> |
shouldThrow(); |
710 |
> |
} catch (IndexOutOfBoundsException success) {} |
711 |
> |
} |
712 |
|
} |
713 |
|
|
714 |
|
/** |
716 |
|
* is lower then the first |
717 |
|
*/ |
718 |
|
public void testSubList3_IndexOutOfBoundsException() { |
719 |
< |
try { |
720 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
721 |
< |
c.subList(3,1); |
722 |
< |
shouldThrow(); |
723 |
< |
} catch (IndexOutOfBoundsException success) {} |
719 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
720 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
721 |
> |
for (List list : lists) { |
722 |
> |
try { |
723 |
> |
list.subList(list.size() - 1, 1); |
724 |
> |
shouldThrow(); |
725 |
> |
} catch (IndexOutOfBoundsException success) {} |
726 |
> |
} |
727 |
|
} |
728 |
|
|
729 |
|
/** |
733 |
|
List x = populatedArray(SIZE); |
734 |
|
List y = serialClone(x); |
735 |
|
|
736 |
< |
assertTrue(x != y); |
736 |
> |
assertNotSame(x, y); |
737 |
|
assertEquals(x.size(), y.size()); |
738 |
|
assertEquals(x.toString(), y.toString()); |
739 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |