1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
7 |
> |
import java.util.Arrays; |
8 |
> |
import java.util.BitSet; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.Comparator; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.NavigableSet; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
import java.util.Random; |
15 |
> |
import java.util.Set; |
16 |
> |
import java.util.SortedSet; |
17 |
> |
import java.util.concurrent.ConcurrentSkipListSet; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class ConcurrentSkipListSetTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
< |
* Create a set of given size containing consecutive |
37 |
> |
* Returns a new set of given size containing consecutive |
38 |
|
* Integers 0 ... n. |
39 |
|
*/ |
40 |
< |
private ConcurrentSkipListSet populatedSet(int n) { |
41 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
40 |
> |
private ConcurrentSkipListSet<Integer> populatedSet(int n) { |
41 |
> |
ConcurrentSkipListSet<Integer> q = |
42 |
> |
new ConcurrentSkipListSet<Integer>(); |
43 |
|
assertTrue(q.isEmpty()); |
44 |
< |
for (int i = n-1; i >= 0; i-=2) |
44 |
> |
for (int i = n-1; i >= 0; i -= 2) |
45 |
|
assertTrue(q.add(new Integer(i))); |
46 |
< |
for (int i = (n & 1); i < n; i+=2) |
46 |
> |
for (int i = (n & 1); i < n; i += 2) |
47 |
|
assertTrue(q.add(new Integer(i))); |
48 |
|
assertFalse(q.isEmpty()); |
49 |
|
assertEquals(n, q.size()); |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* Create set of first 5 ints |
54 |
> |
* Returns a new set of first 5 ints. |
55 |
|
*/ |
56 |
|
private ConcurrentSkipListSet set5() { |
57 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
77 |
|
*/ |
78 |
|
public void testConstructor3() { |
79 |
|
try { |
80 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); |
80 |
> |
new ConcurrentSkipListSet((Collection)null); |
81 |
|
shouldThrow(); |
82 |
|
} catch (NullPointerException success) {} |
83 |
|
} |
88 |
|
public void testConstructor4() { |
89 |
|
try { |
90 |
|
Integer[] ints = new Integer[SIZE]; |
91 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
91 |
> |
new ConcurrentSkipListSet(Arrays.asList(ints)); |
92 |
|
shouldThrow(); |
93 |
|
} catch (NullPointerException success) {} |
94 |
|
} |
101 |
|
Integer[] ints = new Integer[SIZE]; |
102 |
|
for (int i = 0; i < SIZE-1; ++i) |
103 |
|
ints[i] = new Integer(i); |
104 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
104 |
> |
new ConcurrentSkipListSet(Arrays.asList(ints)); |
105 |
|
shouldThrow(); |
106 |
|
} catch (NullPointerException success) {} |
107 |
|
} |
279 |
|
assertNull(q.pollFirst()); |
280 |
|
} |
281 |
|
|
271 |
– |
|
282 |
|
/** |
283 |
|
* remove(x) removes x and returns true if present |
284 |
|
*/ |
285 |
|
public void testRemoveElement() { |
286 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
287 |
< |
for (int i = 1; i < SIZE; i+=2) { |
288 |
< |
assertTrue(q.remove(new Integer(i))); |
289 |
< |
} |
290 |
< |
for (int i = 0; i < SIZE; i+=2) { |
291 |
< |
assertTrue(q.remove(new Integer(i))); |
292 |
< |
assertFalse(q.remove(new Integer(i+1))); |
287 |
> |
for (int i = 1; i < SIZE; i += 2) { |
288 |
> |
assertTrue(q.contains(i)); |
289 |
> |
assertTrue(q.remove(i)); |
290 |
> |
assertFalse(q.contains(i)); |
291 |
> |
assertTrue(q.contains(i-1)); |
292 |
> |
} |
293 |
> |
for (int i = 0; i < SIZE; i += 2) { |
294 |
> |
assertTrue(q.contains(i)); |
295 |
> |
assertTrue(q.remove(i)); |
296 |
> |
assertFalse(q.contains(i)); |
297 |
> |
assertFalse(q.remove(i+1)); |
298 |
> |
assertFalse(q.contains(i+1)); |
299 |
|
} |
300 |
|
assertTrue(q.isEmpty()); |
301 |
|
} |
369 |
|
assertTrue(q.removeAll(p)); |
370 |
|
assertEquals(SIZE-i, q.size()); |
371 |
|
for (int j = 0; j < i; ++j) { |
372 |
< |
Integer I = (Integer)(p.pollFirst()); |
373 |
< |
assertFalse(q.contains(I)); |
372 |
> |
Integer x = (Integer)(p.pollFirst()); |
373 |
> |
assertFalse(q.contains(x)); |
374 |
|
} |
375 |
|
} |
376 |
|
} |
377 |
|
|
362 |
– |
|
363 |
– |
|
378 |
|
/** |
379 |
|
* lower returns preceding element |
380 |
|
*/ |
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
< |
* toArray contains all elements |
451 |
> |
* toArray contains all elements in sorted order |
452 |
|
*/ |
453 |
|
public void testToArray() { |
454 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
455 |
|
Object[] o = q.toArray(); |
442 |
– |
Arrays.sort(o); |
456 |
|
for (int i = 0; i < o.length; i++) |
457 |
< |
assertEquals(o[i], q.pollFirst()); |
457 |
> |
assertSame(o[i], q.pollFirst()); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* toArray(a) contains all elements |
461 |
> |
* toArray(a) contains all elements in sorted order |
462 |
|
*/ |
463 |
|
public void testToArray2() { |
464 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
464 |
> |
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
465 |
|
Integer[] ints = new Integer[SIZE]; |
466 |
< |
ints = (Integer[])q.toArray(ints); |
454 |
< |
Arrays.sort(ints); |
466 |
> |
assertSame(ints, q.toArray(ints)); |
467 |
|
for (int i = 0; i < ints.length; i++) |
468 |
< |
assertEquals(ints[i], q.pollFirst()); |
468 |
> |
assertSame(ints[i], q.pollFirst()); |
469 |
|
} |
470 |
|
|
471 |
|
/** |
473 |
|
*/ |
474 |
|
public void testIterator() { |
475 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
464 |
– |
int i = 0; |
476 |
|
Iterator it = q.iterator(); |
477 |
< |
while (it.hasNext()) { |
477 |
> |
int i; |
478 |
> |
for (i = 0; it.hasNext(); i++) |
479 |
|
assertTrue(q.contains(it.next())); |
468 |
– |
++i; |
469 |
– |
} |
480 |
|
assertEquals(i, SIZE); |
481 |
+ |
assertIteratorExhausted(it); |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
|
* iterator of empty set has no elements |
486 |
|
*/ |
487 |
|
public void testEmptyIterator() { |
488 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
489 |
< |
int i = 0; |
490 |
< |
Iterator it = q.iterator(); |
480 |
< |
while (it.hasNext()) { |
481 |
< |
assertTrue(q.contains(it.next())); |
482 |
< |
++i; |
483 |
< |
} |
484 |
< |
assertEquals(i, 0); |
488 |
> |
NavigableSet s = new ConcurrentSkipListSet(); |
489 |
> |
assertIteratorExhausted(s.iterator()); |
490 |
> |
assertIteratorExhausted(s.descendingSet().iterator()); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
509 |
|
assertFalse(it.hasNext()); |
510 |
|
} |
511 |
|
|
506 |
– |
|
512 |
|
/** |
513 |
|
* toString contains toStrings of elements |
514 |
|
*/ |
516 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
517 |
|
String s = q.toString(); |
518 |
|
for (int i = 0; i < SIZE; ++i) { |
519 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
519 |
> |
assertTrue(s.contains(String.valueOf(i))); |
520 |
|
} |
521 |
|
} |
522 |
|
|
524 |
|
* A deserialized serialized set has same elements |
525 |
|
*/ |
526 |
|
public void testSerialization() throws Exception { |
527 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
528 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
529 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
530 |
< |
out.writeObject(q); |
531 |
< |
out.close(); |
532 |
< |
|
533 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
534 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
535 |
< |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
536 |
< |
assertEquals(q.size(), r.size()); |
537 |
< |
while (!q.isEmpty()) |
538 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
527 |
> |
NavigableSet x = populatedSet(SIZE); |
528 |
> |
NavigableSet y = serialClone(x); |
529 |
> |
|
530 |
> |
assertNotSame(x, y); |
531 |
> |
assertEquals(x.size(), y.size()); |
532 |
> |
assertEquals(x, y); |
533 |
> |
assertEquals(y, x); |
534 |
> |
while (!x.isEmpty()) { |
535 |
> |
assertFalse(y.isEmpty()); |
536 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
537 |
> |
} |
538 |
> |
assertTrue(y.isEmpty()); |
539 |
|
} |
540 |
|
|
541 |
|
/** |
658 |
|
} |
659 |
|
|
660 |
|
Random rnd = new Random(666); |
656 |
– |
BitSet bs; |
661 |
|
|
662 |
|
/** |
663 |
|
* Subsets of subsets subdivide correctly |
664 |
|
*/ |
665 |
|
public void testRecursiveSubSets() throws Exception { |
666 |
< |
int setSize = 1000; |
666 |
> |
int setSize = expensiveTests ? 1000 : 100; |
667 |
|
Class cl = ConcurrentSkipListSet.class; |
668 |
|
|
669 |
|
NavigableSet<Integer> set = newSet(cl); |
670 |
< |
bs = new BitSet(setSize); |
670 |
> |
BitSet bs = new BitSet(setSize); |
671 |
|
|
672 |
< |
populate(set, setSize); |
673 |
< |
check(set, 0, setSize - 1, true); |
674 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
675 |
< |
|
676 |
< |
mutateSet(set, 0, setSize - 1); |
677 |
< |
check(set, 0, setSize - 1, true); |
678 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
672 |
> |
populate(set, setSize, bs); |
673 |
> |
check(set, 0, setSize - 1, true, bs); |
674 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
675 |
> |
|
676 |
> |
mutateSet(set, 0, setSize - 1, bs); |
677 |
> |
check(set, 0, setSize - 1, true, bs); |
678 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
679 |
|
|
680 |
|
bashSubSet(set.subSet(0, true, setSize, false), |
681 |
< |
0, setSize - 1, true); |
681 |
> |
0, setSize - 1, true, bs); |
682 |
> |
} |
683 |
> |
|
684 |
> |
/** |
685 |
> |
* addAll is idempotent |
686 |
> |
*/ |
687 |
> |
public void testAddAll_idempotent() throws Exception { |
688 |
> |
Set x = populatedSet(SIZE); |
689 |
> |
Set y = new ConcurrentSkipListSet(x); |
690 |
> |
y.addAll(x); |
691 |
> |
assertEquals(x, y); |
692 |
> |
assertEquals(y, x); |
693 |
|
} |
694 |
|
|
695 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
696 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
697 |
< |
assertEquals(result.size(), 0); |
697 |
> |
assertEquals(0, result.size()); |
698 |
|
assertFalse(result.iterator().hasNext()); |
699 |
|
return result; |
700 |
|
} |
701 |
|
|
702 |
< |
void populate(NavigableSet<Integer> set, int limit) { |
702 |
> |
void populate(NavigableSet<Integer> set, int limit, BitSet bs) { |
703 |
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
704 |
|
int element = rnd.nextInt(limit); |
705 |
< |
put(set, element); |
705 |
> |
put(set, element, bs); |
706 |
|
} |
707 |
|
} |
708 |
|
|
709 |
< |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
709 |
> |
void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) { |
710 |
|
int size = set.size(); |
711 |
|
int rangeSize = max - min + 1; |
712 |
|
|
713 |
|
// Remove a bunch of entries directly |
714 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
715 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
715 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
716 |
|
} |
717 |
|
|
718 |
|
// Remove a bunch of entries with iterator |
726 |
|
// Add entries till we're back to original size |
727 |
|
while (set.size() < size) { |
728 |
|
int element = min + rnd.nextInt(rangeSize); |
729 |
< |
assertTrue(element >= min && element<= max); |
730 |
< |
put(set, element); |
729 |
> |
assertTrue(element >= min && element <= max); |
730 |
> |
put(set, element, bs); |
731 |
|
} |
732 |
|
} |
733 |
|
|
734 |
< |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
734 |
> |
void mutateSubSet(NavigableSet<Integer> set, int min, int max, |
735 |
> |
BitSet bs) { |
736 |
|
int size = set.size(); |
737 |
|
int rangeSize = max - min + 1; |
738 |
|
|
739 |
|
// Remove a bunch of entries directly |
740 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
741 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
741 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
742 |
|
} |
743 |
|
|
744 |
|
// Remove a bunch of entries with iterator |
752 |
|
// Add entries till we're back to original size |
753 |
|
while (set.size() < size) { |
754 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
755 |
< |
if (element >= min && element<= max) { |
756 |
< |
put(set, element); |
755 |
> |
if (element >= min && element <= max) { |
756 |
> |
put(set, element, bs); |
757 |
|
} else { |
758 |
|
try { |
759 |
|
set.add(element); |
763 |
|
} |
764 |
|
} |
765 |
|
|
766 |
< |
void put(NavigableSet<Integer> set, int element) { |
766 |
> |
void put(NavigableSet<Integer> set, int element, BitSet bs) { |
767 |
|
if (set.add(element)) |
768 |
|
bs.set(element); |
769 |
|
} |
770 |
|
|
771 |
< |
void remove(NavigableSet<Integer> set, int element) { |
771 |
> |
void remove(NavigableSet<Integer> set, int element, BitSet bs) { |
772 |
|
if (set.remove(element)) |
773 |
|
bs.clear(element); |
774 |
|
} |
775 |
|
|
776 |
|
void bashSubSet(NavigableSet<Integer> set, |
777 |
< |
int min, int max, boolean ascending) { |
778 |
< |
check(set, min, max, ascending); |
779 |
< |
check(set.descendingSet(), min, max, !ascending); |
780 |
< |
|
781 |
< |
mutateSubSet(set, min, max); |
782 |
< |
check(set, min, max, ascending); |
783 |
< |
check(set.descendingSet(), min, max, !ascending); |
777 |
> |
int min, int max, boolean ascending, |
778 |
> |
BitSet bs) { |
779 |
> |
check(set, min, max, ascending, bs); |
780 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
781 |
> |
|
782 |
> |
mutateSubSet(set, min, max, bs); |
783 |
> |
check(set, min, max, ascending, bs); |
784 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
785 |
|
|
786 |
|
// Recurse |
787 |
|
if (max - min < 2) |
793 |
|
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
794 |
|
if (ascending) { |
795 |
|
if (rnd.nextBoolean()) |
796 |
< |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
796 |
> |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); |
797 |
|
else |
798 |
|
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
799 |
< |
false); |
799 |
> |
false, bs); |
800 |
|
} else { |
801 |
|
if (rnd.nextBoolean()) |
802 |
< |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
802 |
> |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs); |
803 |
|
else |
804 |
|
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
805 |
< |
true); |
805 |
> |
true, bs); |
806 |
|
} |
807 |
|
|
808 |
|
// tailSet - pick direction and endpoint inclusion randomly |
810 |
|
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
811 |
|
if (ascending) { |
812 |
|
if (rnd.nextBoolean()) |
813 |
< |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
813 |
> |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); |
814 |
|
else |
815 |
|
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
816 |
< |
false); |
816 |
> |
false, bs); |
817 |
|
} else { |
818 |
|
if (rnd.nextBoolean()) { |
819 |
< |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
819 |
> |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs); |
820 |
|
} else { |
821 |
|
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
822 |
< |
true); |
822 |
> |
true, bs); |
823 |
|
} |
824 |
|
} |
825 |
|
|
836 |
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
837 |
|
if (rnd.nextBoolean()) |
838 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
839 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
839 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
840 |
|
else |
841 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
842 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
842 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
843 |
|
} else { |
844 |
|
NavigableSet<Integer> sm = set.subSet( |
845 |
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
846 |
|
if (rnd.nextBoolean()) |
847 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
848 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
848 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
849 |
|
else |
850 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
851 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
851 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
852 |
|
} |
853 |
|
} |
854 |
|
|
856 |
|
* min and max are both inclusive. If max < min, interval is empty. |
857 |
|
*/ |
858 |
|
void check(NavigableSet<Integer> set, |
859 |
< |
final int min, final int max, final boolean ascending) { |
860 |
< |
class ReferenceSet { |
859 |
> |
final int min, final int max, final boolean ascending, |
860 |
> |
final BitSet bs) { |
861 |
> |
class ReferenceSet { |
862 |
|
int lower(int element) { |
863 |
|
return ascending ? |
864 |
|
lowerAscending(element) : higherAscending(element); |
893 |
|
// BitSet should support this! Test would run much faster |
894 |
|
while (element >= min) { |
895 |
|
if (bs.get(element)) |
896 |
< |
return(element); |
896 |
> |
return element; |
897 |
|
element--; |
898 |
|
} |
899 |
|
return -1; |
928 |
|
if (bsContainsI) |
929 |
|
size++; |
930 |
|
} |
931 |
< |
assertEquals(set.size(), size); |
931 |
> |
assertEquals(size, set.size()); |
932 |
|
|
933 |
|
// Test contents using contains elementSet iterator |
934 |
|
int size2 = 0; |