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<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 |
|
} |
166 |
|
* add(null) throws NPE |
167 |
|
*/ |
168 |
|
public void testAddNull() { |
169 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
170 |
|
try { |
160 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
171 |
|
q.add(null); |
172 |
|
shouldThrow(); |
173 |
|
} catch (NullPointerException success) {} |
195 |
|
* Add of non-Comparable throws CCE |
196 |
|
*/ |
197 |
|
public void testAddNonComparable() { |
198 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
199 |
|
try { |
189 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
190 |
– |
q.add(new Object()); |
200 |
|
q.add(new Object()); |
201 |
|
q.add(new Object()); |
202 |
|
shouldThrow(); |
207 |
|
* addAll(null) throws NPE |
208 |
|
*/ |
209 |
|
public void testAddAll1() { |
210 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
211 |
|
try { |
202 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
212 |
|
q.addAll(null); |
213 |
|
shouldThrow(); |
214 |
|
} catch (NullPointerException success) {} |
218 |
|
* addAll of a collection with null elements throws NPE |
219 |
|
*/ |
220 |
|
public void testAddAll2() { |
221 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
222 |
+ |
Integer[] ints = new Integer[SIZE]; |
223 |
|
try { |
213 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
214 |
– |
Integer[] ints = new Integer[SIZE]; |
224 |
|
q.addAll(Arrays.asList(ints)); |
225 |
|
shouldThrow(); |
226 |
|
} catch (NullPointerException success) {} |
231 |
|
* possibly adding some elements |
232 |
|
*/ |
233 |
|
public void testAddAll3() { |
234 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
235 |
+ |
Integer[] ints = new Integer[SIZE]; |
236 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
237 |
+ |
ints[i] = new Integer(i); |
238 |
|
try { |
226 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
227 |
– |
Integer[] ints = new Integer[SIZE]; |
228 |
– |
for (int i = 0; i < SIZE-1; ++i) |
229 |
– |
ints[i] = new Integer(i); |
239 |
|
q.addAll(Arrays.asList(ints)); |
240 |
|
shouldThrow(); |
241 |
|
} catch (NullPointerException success) {} |
278 |
|
assertNull(q.pollFirst()); |
279 |
|
} |
280 |
|
|
272 |
– |
|
281 |
|
/** |
282 |
|
* remove(x) removes x and returns true if present |
283 |
|
*/ |
284 |
|
public void testRemoveElement() { |
285 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
286 |
< |
for (int i = 1; i < SIZE; i+=2) { |
286 |
> |
for (int i = 1; i < SIZE; i += 2) { |
287 |
|
assertTrue(q.contains(i)); |
288 |
|
assertTrue(q.remove(i)); |
289 |
|
assertFalse(q.contains(i)); |
290 |
|
assertTrue(q.contains(i-1)); |
291 |
|
} |
292 |
< |
for (int i = 0; i < SIZE; i+=2) { |
292 |
> |
for (int i = 0; i < SIZE; i += 2) { |
293 |
|
assertTrue(q.contains(i)); |
294 |
|
assertTrue(q.remove(i)); |
295 |
|
assertFalse(q.contains(i)); |
368 |
|
assertTrue(q.removeAll(p)); |
369 |
|
assertEquals(SIZE-i, q.size()); |
370 |
|
for (int j = 0; j < i; ++j) { |
371 |
< |
Integer I = (Integer)(p.pollFirst()); |
372 |
< |
assertFalse(q.contains(I)); |
371 |
> |
Integer x = (Integer)(p.pollFirst()); |
372 |
> |
assertFalse(q.contains(x)); |
373 |
|
} |
374 |
|
} |
375 |
|
} |
376 |
|
|
369 |
– |
|
370 |
– |
|
377 |
|
/** |
378 |
|
* lower returns preceding element |
379 |
|
*/ |
462 |
|
public void testToArray2() { |
463 |
|
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
464 |
|
Integer[] ints = new Integer[SIZE]; |
465 |
< |
Integer[] array = q.toArray(ints); |
460 |
< |
assertSame(ints, array); |
465 |
> |
assertSame(ints, q.toArray(ints)); |
466 |
|
for (int i = 0; i < ints.length; i++) |
467 |
|
assertSame(ints[i], q.pollFirst()); |
468 |
|
} |
472 |
|
*/ |
473 |
|
public void testIterator() { |
474 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
470 |
– |
int i = 0; |
475 |
|
Iterator it = q.iterator(); |
476 |
< |
while (it.hasNext()) { |
476 |
> |
int i; |
477 |
> |
for (i = 0; it.hasNext(); i++) |
478 |
|
assertTrue(q.contains(it.next())); |
474 |
– |
++i; |
475 |
– |
} |
479 |
|
assertEquals(i, SIZE); |
480 |
+ |
assertIteratorExhausted(it); |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
|
* iterator of empty set has no elements |
485 |
|
*/ |
486 |
|
public void testEmptyIterator() { |
487 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
488 |
< |
int i = 0; |
489 |
< |
Iterator it = q.iterator(); |
486 |
< |
while (it.hasNext()) { |
487 |
< |
assertTrue(q.contains(it.next())); |
488 |
< |
++i; |
489 |
< |
} |
490 |
< |
assertEquals(i, 0); |
487 |
> |
NavigableSet s = new ConcurrentSkipListSet(); |
488 |
> |
assertIteratorExhausted(s.iterator()); |
489 |
> |
assertIteratorExhausted(s.descendingSet().iterator()); |
490 |
|
} |
491 |
|
|
492 |
|
/** |
508 |
|
assertFalse(it.hasNext()); |
509 |
|
} |
510 |
|
|
512 |
– |
|
511 |
|
/** |
512 |
|
* toString contains toStrings of elements |
513 |
|
*/ |
515 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
516 |
|
String s = q.toString(); |
517 |
|
for (int i = 0; i < SIZE; ++i) { |
518 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
518 |
> |
assertTrue(s.contains(String.valueOf(i))); |
519 |
|
} |
520 |
|
} |
521 |
|
|
523 |
|
* A deserialized serialized set has same elements |
524 |
|
*/ |
525 |
|
public void testSerialization() throws Exception { |
526 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
527 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
528 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
529 |
< |
out.writeObject(q); |
530 |
< |
out.close(); |
531 |
< |
|
532 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
533 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
534 |
< |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
535 |
< |
assertEquals(q.size(), r.size()); |
536 |
< |
while (!q.isEmpty()) |
537 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
526 |
> |
NavigableSet x = populatedSet(SIZE); |
527 |
> |
NavigableSet y = serialClone(x); |
528 |
> |
|
529 |
> |
assertNotSame(x, y); |
530 |
> |
assertEquals(x.size(), y.size()); |
531 |
> |
assertEquals(x, y); |
532 |
> |
assertEquals(y, x); |
533 |
> |
while (!x.isEmpty()) { |
534 |
> |
assertFalse(y.isEmpty()); |
535 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
536 |
> |
} |
537 |
> |
assertTrue(y.isEmpty()); |
538 |
|
} |
539 |
|
|
540 |
|
/** |
657 |
|
} |
658 |
|
|
659 |
|
Random rnd = new Random(666); |
662 |
– |
BitSet bs; |
660 |
|
|
661 |
|
/** |
662 |
|
* Subsets of subsets subdivide correctly |
666 |
|
Class cl = ConcurrentSkipListSet.class; |
667 |
|
|
668 |
|
NavigableSet<Integer> set = newSet(cl); |
669 |
< |
bs = new BitSet(setSize); |
669 |
> |
BitSet bs = new BitSet(setSize); |
670 |
|
|
671 |
< |
populate(set, setSize); |
672 |
< |
check(set, 0, setSize - 1, true); |
673 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
674 |
< |
|
675 |
< |
mutateSet(set, 0, setSize - 1); |
676 |
< |
check(set, 0, setSize - 1, true); |
677 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
671 |
> |
populate(set, setSize, bs); |
672 |
> |
check(set, 0, setSize - 1, true, bs); |
673 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
674 |
> |
|
675 |
> |
mutateSet(set, 0, setSize - 1, bs); |
676 |
> |
check(set, 0, setSize - 1, true, bs); |
677 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
678 |
|
|
679 |
|
bashSubSet(set.subSet(0, true, setSize, false), |
680 |
< |
0, setSize - 1, true); |
680 |
> |
0, setSize - 1, true, bs); |
681 |
> |
} |
682 |
> |
|
683 |
> |
/** |
684 |
> |
* addAll is idempotent |
685 |
> |
*/ |
686 |
> |
public void testAddAll_idempotent() throws Exception { |
687 |
> |
Set x = populatedSet(SIZE); |
688 |
> |
Set y = new ConcurrentSkipListSet(x); |
689 |
> |
y.addAll(x); |
690 |
> |
assertEquals(x, y); |
691 |
> |
assertEquals(y, x); |
692 |
|
} |
693 |
|
|
694 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
695 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
696 |
< |
assertEquals(result.size(), 0); |
696 |
> |
assertEquals(0, result.size()); |
697 |
|
assertFalse(result.iterator().hasNext()); |
698 |
|
return result; |
699 |
|
} |
700 |
|
|
701 |
< |
void populate(NavigableSet<Integer> set, int limit) { |
701 |
> |
void populate(NavigableSet<Integer> set, int limit, BitSet bs) { |
702 |
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
703 |
|
int element = rnd.nextInt(limit); |
704 |
< |
put(set, element); |
704 |
> |
put(set, element, bs); |
705 |
|
} |
706 |
|
} |
707 |
|
|
708 |
< |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
708 |
> |
void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) { |
709 |
|
int size = set.size(); |
710 |
|
int rangeSize = max - min + 1; |
711 |
|
|
712 |
|
// Remove a bunch of entries directly |
713 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
714 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
714 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
715 |
|
} |
716 |
|
|
717 |
|
// Remove a bunch of entries with iterator |
725 |
|
// Add entries till we're back to original size |
726 |
|
while (set.size() < size) { |
727 |
|
int element = min + rnd.nextInt(rangeSize); |
728 |
< |
assertTrue(element >= min && element<= max); |
729 |
< |
put(set, element); |
728 |
> |
assertTrue(element >= min && element <= max); |
729 |
> |
put(set, element, bs); |
730 |
|
} |
731 |
|
} |
732 |
|
|
733 |
< |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
733 |
> |
void mutateSubSet(NavigableSet<Integer> set, int min, int max, |
734 |
> |
BitSet bs) { |
735 |
|
int size = set.size(); |
736 |
|
int rangeSize = max - min + 1; |
737 |
|
|
738 |
|
// Remove a bunch of entries directly |
739 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
740 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
740 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
741 |
|
} |
742 |
|
|
743 |
|
// Remove a bunch of entries with iterator |
751 |
|
// Add entries till we're back to original size |
752 |
|
while (set.size() < size) { |
753 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
754 |
< |
if (element >= min && element<= max) { |
755 |
< |
put(set, element); |
754 |
> |
if (element >= min && element <= max) { |
755 |
> |
put(set, element, bs); |
756 |
|
} else { |
757 |
|
try { |
758 |
|
set.add(element); |
762 |
|
} |
763 |
|
} |
764 |
|
|
765 |
< |
void put(NavigableSet<Integer> set, int element) { |
765 |
> |
void put(NavigableSet<Integer> set, int element, BitSet bs) { |
766 |
|
if (set.add(element)) |
767 |
|
bs.set(element); |
768 |
|
} |
769 |
|
|
770 |
< |
void remove(NavigableSet<Integer> set, int element) { |
770 |
> |
void remove(NavigableSet<Integer> set, int element, BitSet bs) { |
771 |
|
if (set.remove(element)) |
772 |
|
bs.clear(element); |
773 |
|
} |
774 |
|
|
775 |
|
void bashSubSet(NavigableSet<Integer> set, |
776 |
< |
int min, int max, boolean ascending) { |
777 |
< |
check(set, min, max, ascending); |
778 |
< |
check(set.descendingSet(), min, max, !ascending); |
779 |
< |
|
780 |
< |
mutateSubSet(set, min, max); |
781 |
< |
check(set, min, max, ascending); |
782 |
< |
check(set.descendingSet(), min, max, !ascending); |
776 |
> |
int min, int max, boolean ascending, |
777 |
> |
BitSet bs) { |
778 |
> |
check(set, min, max, ascending, bs); |
779 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
780 |
> |
|
781 |
> |
mutateSubSet(set, min, max, bs); |
782 |
> |
check(set, min, max, ascending, bs); |
783 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
784 |
|
|
785 |
|
// Recurse |
786 |
|
if (max - min < 2) |
792 |
|
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
793 |
|
if (ascending) { |
794 |
|
if (rnd.nextBoolean()) |
795 |
< |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
795 |
> |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); |
796 |
|
else |
797 |
|
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
798 |
< |
false); |
798 |
> |
false, bs); |
799 |
|
} else { |
800 |
|
if (rnd.nextBoolean()) |
801 |
< |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
801 |
> |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs); |
802 |
|
else |
803 |
|
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
804 |
< |
true); |
804 |
> |
true, bs); |
805 |
|
} |
806 |
|
|
807 |
|
// tailSet - pick direction and endpoint inclusion randomly |
809 |
|
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
810 |
|
if (ascending) { |
811 |
|
if (rnd.nextBoolean()) |
812 |
< |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
812 |
> |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); |
813 |
|
else |
814 |
|
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
815 |
< |
false); |
815 |
> |
false, bs); |
816 |
|
} else { |
817 |
|
if (rnd.nextBoolean()) { |
818 |
< |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
818 |
> |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs); |
819 |
|
} else { |
820 |
|
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
821 |
< |
true); |
821 |
> |
true, bs); |
822 |
|
} |
823 |
|
} |
824 |
|
|
835 |
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
836 |
|
if (rnd.nextBoolean()) |
837 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
838 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
838 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
839 |
|
else |
840 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
841 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
841 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
842 |
|
} else { |
843 |
|
NavigableSet<Integer> sm = set.subSet( |
844 |
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
845 |
|
if (rnd.nextBoolean()) |
846 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
847 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
847 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
848 |
|
else |
849 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
850 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
850 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
851 |
|
} |
852 |
|
} |
853 |
|
|
855 |
|
* min and max are both inclusive. If max < min, interval is empty. |
856 |
|
*/ |
857 |
|
void check(NavigableSet<Integer> set, |
858 |
< |
final int min, final int max, final boolean ascending) { |
859 |
< |
class ReferenceSet { |
858 |
> |
final int min, final int max, final boolean ascending, |
859 |
> |
final BitSet bs) { |
860 |
> |
class ReferenceSet { |
861 |
|
int lower(int element) { |
862 |
|
return ascending ? |
863 |
|
lowerAscending(element) : higherAscending(element); |
927 |
|
if (bsContainsI) |
928 |
|
size++; |
929 |
|
} |
930 |
< |
assertEquals(set.size(), size); |
930 |
> |
assertEquals(size, set.size()); |
931 |
|
|
932 |
|
// Test contents using contains elementSet iterator |
933 |
|
int size2 = 0; |