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 |
< |
* Creates 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 |
< |
* Creates 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 { |
168 |
– |
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 { |
197 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
198 |
– |
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 { |
210 |
– |
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 { |
221 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
222 |
– |
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 { |
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); |
239 |
|
q.addAll(Arrays.asList(ints)); |
240 |
|
shouldThrow(); |
241 |
|
} catch (NullPointerException success) {} |
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 |
|
} |
472 |
|
*/ |
473 |
|
public void testIterator() { |
474 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
474 |
– |
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())); |
478 |
– |
++i; |
479 |
– |
} |
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(); |
490 |
< |
while (it.hasNext()) { |
491 |
< |
assertTrue(q.contains(it.next())); |
492 |
< |
++i; |
493 |
< |
} |
494 |
< |
assertEquals(0, i); |
487 |
> |
NavigableSet s = new ConcurrentSkipListSet(); |
488 |
> |
assertIteratorExhausted(s.iterator()); |
489 |
> |
assertIteratorExhausted(s.descendingSet().iterator()); |
490 |
|
} |
491 |
|
|
492 |
|
/** |
526 |
|
NavigableSet x = populatedSet(SIZE); |
527 |
|
NavigableSet y = serialClone(x); |
528 |
|
|
529 |
< |
assertTrue(x != y); |
529 |
> |
assertNotSame(x, y); |
530 |
|
assertEquals(x.size(), y.size()); |
531 |
|
assertEquals(x, y); |
532 |
|
assertEquals(y, x); |
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(0, result.size()); |
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); |
728 |
> |
assertTrue(element >= min && element <= max); |
729 |
|
put(set, element, bs); |
730 |
|
} |
731 |
|
} |
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) { |
754 |
> |
if (element >= min && element <= max) { |
755 |
|
put(set, element, bs); |
756 |
|
} else { |
757 |
|
try { |