4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
7 |
|
import java.util.Arrays; |
8 |
|
import java.util.BitSet; |
9 |
|
import java.util.Collection; |
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) { |
24 |
|
junit.textui.TestRunner.run(suite()); |
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(); |
284 |
|
*/ |
285 |
|
public void testRemoveElement() { |
286 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
287 |
< |
for (int i = 1; i < SIZE; i+=2) { |
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) { |
293 |
> |
for (int i = 0; i < SIZE; i += 2) { |
294 |
|
assertTrue(q.contains(i)); |
295 |
|
assertTrue(q.remove(i)); |
296 |
|
assertFalse(q.contains(i)); |
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 |
|
} |
463 |
|
public void testToArray2() { |
464 |
|
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
465 |
|
Integer[] ints = new Integer[SIZE]; |
466 |
< |
Integer[] array = q.toArray(ints); |
465 |
< |
assertSame(ints, array); |
466 |
> |
assertSame(ints, q.toArray(ints)); |
467 |
|
for (int i = 0; i < ints.length; i++) |
468 |
|
assertSame(ints[i], q.pollFirst()); |
469 |
|
} |
533 |
|
NavigableSet x = populatedSet(SIZE); |
534 |
|
NavigableSet y = serialClone(x); |
535 |
|
|
536 |
< |
assertTrue(x != y); |
536 |
> |
assertNotSame(x, y); |
537 |
|
assertEquals(x.size(), y.size()); |
538 |
|
assertEquals(x, y); |
539 |
|
assertEquals(y, x); |
687 |
|
0, setSize - 1, true, bs); |
688 |
|
} |
689 |
|
|
690 |
+ |
/** |
691 |
+ |
* addAll is idempotent |
692 |
+ |
*/ |
693 |
+ |
public void testAddAll_idempotent() throws Exception { |
694 |
+ |
Set x = populatedSet(SIZE); |
695 |
+ |
Set y = new ConcurrentSkipListSet(x); |
696 |
+ |
y.addAll(x); |
697 |
+ |
assertEquals(x, y); |
698 |
+ |
assertEquals(y, x); |
699 |
+ |
} |
700 |
+ |
|
701 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
702 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
703 |
|
assertEquals(0, result.size()); |
732 |
|
// Add entries till we're back to original size |
733 |
|
while (set.size() < size) { |
734 |
|
int element = min + rnd.nextInt(rangeSize); |
735 |
< |
assertTrue(element >= min && element<= max); |
735 |
> |
assertTrue(element >= min && element <= max); |
736 |
|
put(set, element, bs); |
737 |
|
} |
738 |
|
} |
758 |
|
// Add entries till we're back to original size |
759 |
|
while (set.size() < size) { |
760 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
761 |
< |
if (element >= min && element<= max) { |
761 |
> |
if (element >= min && element <= max) { |
762 |
|
put(set, element, bs); |
763 |
|
} else { |
764 |
|
try { |
934 |
|
if (bsContainsI) |
935 |
|
size++; |
936 |
|
} |
937 |
< |
assertEquals(set.size(), size); |
937 |
> |
assertEquals(size, set.size()); |
938 |
|
|
939 |
|
// Test contents using contains elementSet iterator |
940 |
|
int size2 = 0; |