5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.BitSet; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Comparator; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NavigableSet; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.SortedSet; |
18 |
> |
import java.util.concurrent.ConcurrentSkipListSet; |
19 |
|
|
20 |
|
public class ConcurrentSkipListSetTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
492 |
|
assertTrue(q.contains(it.next())); |
493 |
|
++i; |
494 |
|
} |
495 |
< |
assertEquals(i, 0); |
495 |
> |
assertEquals(0, i); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
529 |
|
* A deserialized serialized set has same elements |
530 |
|
*/ |
531 |
|
public void testSerialization() throws Exception { |
532 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
533 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
534 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
535 |
< |
out.writeObject(q); |
536 |
< |
out.close(); |
537 |
< |
|
538 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
539 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
540 |
< |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
541 |
< |
assertEquals(q.size(), r.size()); |
542 |
< |
while (!q.isEmpty()) |
543 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
532 |
> |
NavigableSet x = populatedSet(SIZE); |
533 |
> |
NavigableSet y = serialClone(x); |
534 |
> |
|
535 |
> |
assertTrue(x != y); |
536 |
> |
assertEquals(x.size(), y.size()); |
537 |
> |
assertEquals(x, y); |
538 |
> |
assertEquals(y, x); |
539 |
> |
while (!x.isEmpty()) { |
540 |
> |
assertFalse(y.isEmpty()); |
541 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
542 |
> |
} |
543 |
> |
assertTrue(y.isEmpty()); |
544 |
|
} |
545 |
|
|
546 |
|
/** |
663 |
|
} |
664 |
|
|
665 |
|
Random rnd = new Random(666); |
658 |
– |
BitSet bs; |
666 |
|
|
667 |
|
/** |
668 |
|
* Subsets of subsets subdivide correctly |
672 |
|
Class cl = ConcurrentSkipListSet.class; |
673 |
|
|
674 |
|
NavigableSet<Integer> set = newSet(cl); |
675 |
< |
bs = new BitSet(setSize); |
675 |
> |
BitSet bs = new BitSet(setSize); |
676 |
|
|
677 |
< |
populate(set, setSize); |
678 |
< |
check(set, 0, setSize - 1, true); |
679 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
680 |
< |
|
681 |
< |
mutateSet(set, 0, setSize - 1); |
682 |
< |
check(set, 0, setSize - 1, true); |
683 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
677 |
> |
populate(set, setSize, bs); |
678 |
> |
check(set, 0, setSize - 1, true, bs); |
679 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
680 |
> |
|
681 |
> |
mutateSet(set, 0, setSize - 1, bs); |
682 |
> |
check(set, 0, setSize - 1, true, bs); |
683 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
684 |
|
|
685 |
|
bashSubSet(set.subSet(0, true, setSize, false), |
686 |
< |
0, setSize - 1, true); |
686 |
> |
0, setSize - 1, true, bs); |
687 |
|
} |
688 |
|
|
689 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
690 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
691 |
< |
assertEquals(result.size(), 0); |
691 |
> |
assertEquals(0, result.size()); |
692 |
|
assertFalse(result.iterator().hasNext()); |
693 |
|
return result; |
694 |
|
} |
695 |
|
|
696 |
< |
void populate(NavigableSet<Integer> set, int limit) { |
696 |
> |
void populate(NavigableSet<Integer> set, int limit, BitSet bs) { |
697 |
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
698 |
|
int element = rnd.nextInt(limit); |
699 |
< |
put(set, element); |
699 |
> |
put(set, element, bs); |
700 |
|
} |
701 |
|
} |
702 |
|
|
703 |
< |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
703 |
> |
void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) { |
704 |
|
int size = set.size(); |
705 |
|
int rangeSize = max - min + 1; |
706 |
|
|
707 |
|
// Remove a bunch of entries directly |
708 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
709 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
709 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
710 |
|
} |
711 |
|
|
712 |
|
// Remove a bunch of entries with iterator |
721 |
|
while (set.size() < size) { |
722 |
|
int element = min + rnd.nextInt(rangeSize); |
723 |
|
assertTrue(element >= min && element<= max); |
724 |
< |
put(set, element); |
724 |
> |
put(set, element, bs); |
725 |
|
} |
726 |
|
} |
727 |
|
|
728 |
< |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
728 |
> |
void mutateSubSet(NavigableSet<Integer> set, int min, int max, |
729 |
> |
BitSet bs) { |
730 |
|
int size = set.size(); |
731 |
|
int rangeSize = max - min + 1; |
732 |
|
|
733 |
|
// Remove a bunch of entries directly |
734 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
735 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
735 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
736 |
|
} |
737 |
|
|
738 |
|
// Remove a bunch of entries with iterator |
747 |
|
while (set.size() < size) { |
748 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
749 |
|
if (element >= min && element<= max) { |
750 |
< |
put(set, element); |
750 |
> |
put(set, element, bs); |
751 |
|
} else { |
752 |
|
try { |
753 |
|
set.add(element); |
757 |
|
} |
758 |
|
} |
759 |
|
|
760 |
< |
void put(NavigableSet<Integer> set, int element) { |
760 |
> |
void put(NavigableSet<Integer> set, int element, BitSet bs) { |
761 |
|
if (set.add(element)) |
762 |
|
bs.set(element); |
763 |
|
} |
764 |
|
|
765 |
< |
void remove(NavigableSet<Integer> set, int element) { |
765 |
> |
void remove(NavigableSet<Integer> set, int element, BitSet bs) { |
766 |
|
if (set.remove(element)) |
767 |
|
bs.clear(element); |
768 |
|
} |
769 |
|
|
770 |
|
void bashSubSet(NavigableSet<Integer> set, |
771 |
< |
int min, int max, boolean ascending) { |
772 |
< |
check(set, min, max, ascending); |
773 |
< |
check(set.descendingSet(), min, max, !ascending); |
774 |
< |
|
775 |
< |
mutateSubSet(set, min, max); |
776 |
< |
check(set, min, max, ascending); |
777 |
< |
check(set.descendingSet(), min, max, !ascending); |
771 |
> |
int min, int max, boolean ascending, |
772 |
> |
BitSet bs) { |
773 |
> |
check(set, min, max, ascending, bs); |
774 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
775 |
> |
|
776 |
> |
mutateSubSet(set, min, max, bs); |
777 |
> |
check(set, min, max, ascending, bs); |
778 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
779 |
|
|
780 |
|
// Recurse |
781 |
|
if (max - min < 2) |
787 |
|
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
788 |
|
if (ascending) { |
789 |
|
if (rnd.nextBoolean()) |
790 |
< |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
790 |
> |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); |
791 |
|
else |
792 |
|
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
793 |
< |
false); |
793 |
> |
false, bs); |
794 |
|
} else { |
795 |
|
if (rnd.nextBoolean()) |
796 |
< |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
796 |
> |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs); |
797 |
|
else |
798 |
|
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
799 |
< |
true); |
799 |
> |
true, bs); |
800 |
|
} |
801 |
|
|
802 |
|
// tailSet - pick direction and endpoint inclusion randomly |
804 |
|
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
805 |
|
if (ascending) { |
806 |
|
if (rnd.nextBoolean()) |
807 |
< |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
807 |
> |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); |
808 |
|
else |
809 |
|
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
810 |
< |
false); |
810 |
> |
false, bs); |
811 |
|
} else { |
812 |
|
if (rnd.nextBoolean()) { |
813 |
< |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
813 |
> |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs); |
814 |
|
} else { |
815 |
|
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
816 |
< |
true); |
816 |
> |
true, bs); |
817 |
|
} |
818 |
|
} |
819 |
|
|
830 |
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
831 |
|
if (rnd.nextBoolean()) |
832 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
833 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
833 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
834 |
|
else |
835 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
836 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
836 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
837 |
|
} else { |
838 |
|
NavigableSet<Integer> sm = set.subSet( |
839 |
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
840 |
|
if (rnd.nextBoolean()) |
841 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
842 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
842 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
843 |
|
else |
844 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
845 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
845 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
846 |
|
} |
847 |
|
} |
848 |
|
|
850 |
|
* min and max are both inclusive. If max < min, interval is empty. |
851 |
|
*/ |
852 |
|
void check(NavigableSet<Integer> set, |
853 |
< |
final int min, final int max, final boolean ascending) { |
853 |
> |
final int min, final int max, final boolean ascending, |
854 |
> |
final BitSet bs) { |
855 |
|
class ReferenceSet { |
856 |
|
int lower(int element) { |
857 |
|
return ascending ? |