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()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
|
return new TestSuite(ConcurrentSkipListSetTest.class); |
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 |
|
} |
87 |
|
*/ |
88 |
|
public void testConstructor4() { |
89 |
|
try { |
90 |
< |
Integer[] ints = new Integer[SIZE]; |
89 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
90 |
> |
new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE])); |
91 |
|
shouldThrow(); |
92 |
|
} catch (NullPointerException success) {} |
93 |
|
} |
96 |
|
* Initializing from Collection with some null elements throws NPE |
97 |
|
*/ |
98 |
|
public void testConstructor5() { |
99 |
+ |
Integer[] ints = new Integer[SIZE]; |
100 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
101 |
+ |
ints[i] = new Integer(i); |
102 |
|
try { |
103 |
< |
Integer[] ints = new Integer[SIZE]; |
100 |
< |
for (int i = 0; i < SIZE-1; ++i) |
101 |
< |
ints[i] = new Integer(i); |
102 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
103 |
> |
new ConcurrentSkipListSet(Arrays.asList(ints)); |
104 |
|
shouldThrow(); |
105 |
|
} catch (NullPointerException success) {} |
106 |
|
} |
128 |
|
for (int i = 0; i < SIZE; ++i) |
129 |
|
ints[i] = new Integer(i); |
130 |
|
q.addAll(Arrays.asList(ints)); |
131 |
< |
for (int i = SIZE-1; i >= 0; --i) |
131 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
132 |
|
assertEquals(ints[i], q.pollFirst()); |
133 |
|
} |
134 |
|
|
152 |
|
public void testSize() { |
153 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
154 |
|
for (int i = 0; i < SIZE; ++i) { |
155 |
< |
assertEquals(SIZE-i, q.size()); |
155 |
> |
assertEquals(SIZE - i, q.size()); |
156 |
|
q.pollFirst(); |
157 |
|
} |
158 |
|
for (int i = 0; i < SIZE; ++i) { |
165 |
|
* add(null) throws NPE |
166 |
|
*/ |
167 |
|
public void testAddNull() { |
168 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
169 |
|
try { |
168 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
170 |
|
q.add(null); |
171 |
|
shouldThrow(); |
172 |
|
} catch (NullPointerException success) {} |
194 |
|
* Add of non-Comparable throws CCE |
195 |
|
*/ |
196 |
|
public void testAddNonComparable() { |
197 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
198 |
|
try { |
197 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
198 |
– |
q.add(new Object()); |
199 |
|
q.add(new Object()); |
200 |
|
q.add(new Object()); |
201 |
|
shouldThrow(); |
202 |
< |
} catch (ClassCastException success) {} |
202 |
> |
} catch (ClassCastException success) { |
203 |
> |
assertTrue(q.size() < 2); |
204 |
> |
for (int i = 0, size = q.size(); i < size; i++) |
205 |
> |
assertTrue(q.pollFirst().getClass() == Object.class); |
206 |
> |
assertNull(q.pollFirst()); |
207 |
> |
assertTrue(q.isEmpty()); |
208 |
> |
assertEquals(0, q.size()); |
209 |
> |
} |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
|
* addAll(null) throws NPE |
214 |
|
*/ |
215 |
|
public void testAddAll1() { |
216 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
217 |
|
try { |
210 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
218 |
|
q.addAll(null); |
219 |
|
shouldThrow(); |
220 |
|
} catch (NullPointerException success) {} |
224 |
|
* addAll of a collection with null elements throws NPE |
225 |
|
*/ |
226 |
|
public void testAddAll2() { |
227 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
228 |
+ |
Integer[] ints = new Integer[SIZE]; |
229 |
|
try { |
221 |
– |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
222 |
– |
Integer[] ints = new Integer[SIZE]; |
230 |
|
q.addAll(Arrays.asList(ints)); |
231 |
|
shouldThrow(); |
232 |
|
} catch (NullPointerException success) {} |
237 |
|
* possibly adding some elements |
238 |
|
*/ |
239 |
|
public void testAddAll3() { |
240 |
+ |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
241 |
+ |
Integer[] ints = new Integer[SIZE]; |
242 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
243 |
+ |
ints[i] = new Integer(i); |
244 |
|
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); |
245 |
|
q.addAll(Arrays.asList(ints)); |
246 |
|
shouldThrow(); |
247 |
|
} catch (NullPointerException success) {} |
254 |
|
Integer[] empty = new Integer[0]; |
255 |
|
Integer[] ints = new Integer[SIZE]; |
256 |
|
for (int i = 0; i < SIZE; ++i) |
257 |
< |
ints[i] = new Integer(SIZE-1-i); |
257 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
258 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
259 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
260 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
278 |
|
*/ |
279 |
|
public void testPollLast() { |
280 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
281 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
281 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
282 |
|
assertEquals(i, q.pollLast()); |
283 |
|
} |
284 |
|
assertNull(q.pollFirst()); |
289 |
|
*/ |
290 |
|
public void testRemoveElement() { |
291 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
292 |
< |
for (int i = 1; i < SIZE; i+=2) { |
292 |
> |
for (int i = 1; i < SIZE; i += 2) { |
293 |
|
assertTrue(q.contains(i)); |
294 |
|
assertTrue(q.remove(i)); |
295 |
|
assertFalse(q.contains(i)); |
296 |
< |
assertTrue(q.contains(i-1)); |
296 |
> |
assertTrue(q.contains(i - 1)); |
297 |
|
} |
298 |
< |
for (int i = 0; i < SIZE; i+=2) { |
298 |
> |
for (int i = 0; i < SIZE; i += 2) { |
299 |
|
assertTrue(q.contains(i)); |
300 |
|
assertTrue(q.remove(i)); |
301 |
|
assertFalse(q.contains(i)); |
302 |
< |
assertFalse(q.remove(i+1)); |
303 |
< |
assertFalse(q.contains(i+1)); |
302 |
> |
assertFalse(q.remove(i + 1)); |
303 |
> |
assertFalse(q.contains(i + 1)); |
304 |
|
} |
305 |
|
assertTrue(q.isEmpty()); |
306 |
|
} |
359 |
|
assertTrue(changed); |
360 |
|
|
361 |
|
assertTrue(q.containsAll(p)); |
362 |
< |
assertEquals(SIZE-i, q.size()); |
362 |
> |
assertEquals(SIZE - i, q.size()); |
363 |
|
p.pollFirst(); |
364 |
|
} |
365 |
|
} |
372 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
373 |
|
ConcurrentSkipListSet p = populatedSet(i); |
374 |
|
assertTrue(q.removeAll(p)); |
375 |
< |
assertEquals(SIZE-i, q.size()); |
375 |
> |
assertEquals(SIZE - i, q.size()); |
376 |
|
for (int j = 0; j < i; ++j) { |
377 |
< |
Integer I = (Integer)(p.pollFirst()); |
378 |
< |
assertFalse(q.contains(I)); |
377 |
> |
Integer x = (Integer)(p.pollFirst()); |
378 |
> |
assertFalse(q.contains(x)); |
379 |
|
} |
380 |
|
} |
381 |
|
} |
478 |
|
*/ |
479 |
|
public void testIterator() { |
480 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
474 |
– |
int i = 0; |
481 |
|
Iterator it = q.iterator(); |
482 |
< |
while (it.hasNext()) { |
482 |
> |
int i; |
483 |
> |
for (i = 0; it.hasNext(); i++) |
484 |
|
assertTrue(q.contains(it.next())); |
478 |
– |
++i; |
479 |
– |
} |
485 |
|
assertEquals(i, SIZE); |
486 |
+ |
assertIteratorExhausted(it); |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
|
* iterator of empty set has no elements |
491 |
|
*/ |
492 |
|
public void testEmptyIterator() { |
493 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
494 |
< |
int i = 0; |
495 |
< |
Iterator it = q.iterator(); |
490 |
< |
while (it.hasNext()) { |
491 |
< |
assertTrue(q.contains(it.next())); |
492 |
< |
++i; |
493 |
< |
} |
494 |
< |
assertEquals(0, i); |
493 |
> |
NavigableSet s = new ConcurrentSkipListSet(); |
494 |
> |
assertIteratorExhausted(s.iterator()); |
495 |
> |
assertIteratorExhausted(s.descendingSet().iterator()); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
532 |
|
NavigableSet x = populatedSet(SIZE); |
533 |
|
NavigableSet y = serialClone(x); |
534 |
|
|
535 |
< |
assertTrue(x != y); |
535 |
> |
assertNotSame(x, y); |
536 |
|
assertEquals(x.size(), y.size()); |
537 |
|
assertEquals(x, y); |
538 |
|
assertEquals(y, x); |
686 |
|
0, setSize - 1, true, bs); |
687 |
|
} |
688 |
|
|
689 |
+ |
/** |
690 |
+ |
* addAll is idempotent |
691 |
+ |
*/ |
692 |
+ |
public void testAddAll_idempotent() throws Exception { |
693 |
+ |
Set x = populatedSet(SIZE); |
694 |
+ |
Set y = new ConcurrentSkipListSet(x); |
695 |
+ |
y.addAll(x); |
696 |
+ |
assertEquals(x, y); |
697 |
+ |
assertEquals(y, x); |
698 |
+ |
} |
699 |
+ |
|
700 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
701 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
702 |
|
assertEquals(0, result.size()); |
731 |
|
// Add entries till we're back to original size |
732 |
|
while (set.size() < size) { |
733 |
|
int element = min + rnd.nextInt(rangeSize); |
734 |
< |
assertTrue(element >= min && element<= max); |
734 |
> |
assertTrue(element >= min && element <= max); |
735 |
|
put(set, element, bs); |
736 |
|
} |
737 |
|
} |
757 |
|
// Add entries till we're back to original size |
758 |
|
while (set.size() < size) { |
759 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
760 |
< |
if (element >= min && element<= max) { |
760 |
> |
if (element >= min && element <= max) { |
761 |
|
put(set, element, bs); |
762 |
|
} else { |
763 |
|
try { |
981 |
|
} |
982 |
|
|
983 |
|
static boolean eq(Integer i, int j) { |
984 |
< |
return i == null ? j == -1 : i == j; |
984 |
> |
return (i == null) ? j == -1 : i == j; |
985 |
|
} |
986 |
|
|
987 |
|
} |