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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
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) { |
24 |
main(suite(), args); |
25 |
} |
26 |
public static Test suite() { |
27 |
return new TestSuite(ConcurrentSkipListSetTest.class); |
28 |
} |
29 |
|
30 |
static class MyReverseComparator implements Comparator { |
31 |
public int compare(Object x, Object y) { |
32 |
return ((Comparable)y).compareTo(x); |
33 |
} |
34 |
} |
35 |
|
36 |
/** |
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) |
45 |
assertTrue(q.add(new Integer(i))); |
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()); |
50 |
return q; |
51 |
} |
52 |
|
53 |
/** |
54 |
* Returns a new set of first 5 ints. |
55 |
*/ |
56 |
private ConcurrentSkipListSet set5() { |
57 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
58 |
assertTrue(q.isEmpty()); |
59 |
q.add(one); |
60 |
q.add(two); |
61 |
q.add(three); |
62 |
q.add(four); |
63 |
q.add(five); |
64 |
assertEquals(5, q.size()); |
65 |
return q; |
66 |
} |
67 |
|
68 |
/** |
69 |
* A new set has unbounded capacity |
70 |
*/ |
71 |
public void testConstructor1() { |
72 |
assertEquals(0, new ConcurrentSkipListSet().size()); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Initializing from null Collection throws NPE |
77 |
*/ |
78 |
public void testConstructor3() { |
79 |
try { |
80 |
new ConcurrentSkipListSet((Collection)null); |
81 |
shouldThrow(); |
82 |
} catch (NullPointerException success) {} |
83 |
} |
84 |
|
85 |
/** |
86 |
* Initializing from Collection of null elements throws NPE |
87 |
*/ |
88 |
public void testConstructor4() { |
89 |
try { |
90 |
Integer[] ints = new Integer[SIZE]; |
91 |
new ConcurrentSkipListSet(Arrays.asList(ints)); |
92 |
shouldThrow(); |
93 |
} catch (NullPointerException success) {} |
94 |
} |
95 |
|
96 |
/** |
97 |
* Initializing from Collection with some null elements throws NPE |
98 |
*/ |
99 |
public void testConstructor5() { |
100 |
try { |
101 |
Integer[] ints = new Integer[SIZE]; |
102 |
for (int i = 0; i < SIZE-1; ++i) |
103 |
ints[i] = new Integer(i); |
104 |
new ConcurrentSkipListSet(Arrays.asList(ints)); |
105 |
shouldThrow(); |
106 |
} catch (NullPointerException success) {} |
107 |
} |
108 |
|
109 |
/** |
110 |
* Set contains all elements of collection used to initialize |
111 |
*/ |
112 |
public void testConstructor6() { |
113 |
Integer[] ints = new Integer[SIZE]; |
114 |
for (int i = 0; i < SIZE; ++i) |
115 |
ints[i] = new Integer(i); |
116 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
117 |
for (int i = 0; i < SIZE; ++i) |
118 |
assertEquals(ints[i], q.pollFirst()); |
119 |
} |
120 |
|
121 |
/** |
122 |
* The comparator used in constructor is used |
123 |
*/ |
124 |
public void testConstructor7() { |
125 |
MyReverseComparator cmp = new MyReverseComparator(); |
126 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
127 |
assertEquals(cmp, q.comparator()); |
128 |
Integer[] ints = new Integer[SIZE]; |
129 |
for (int i = 0; i < SIZE; ++i) |
130 |
ints[i] = new Integer(i); |
131 |
q.addAll(Arrays.asList(ints)); |
132 |
for (int i = SIZE-1; i >= 0; --i) |
133 |
assertEquals(ints[i], q.pollFirst()); |
134 |
} |
135 |
|
136 |
/** |
137 |
* isEmpty is true before add, false after |
138 |
*/ |
139 |
public void testEmpty() { |
140 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
141 |
assertTrue(q.isEmpty()); |
142 |
q.add(new Integer(1)); |
143 |
assertFalse(q.isEmpty()); |
144 |
q.add(new Integer(2)); |
145 |
q.pollFirst(); |
146 |
q.pollFirst(); |
147 |
assertTrue(q.isEmpty()); |
148 |
} |
149 |
|
150 |
/** |
151 |
* size changes when elements added and removed |
152 |
*/ |
153 |
public void testSize() { |
154 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
155 |
for (int i = 0; i < SIZE; ++i) { |
156 |
assertEquals(SIZE-i, q.size()); |
157 |
q.pollFirst(); |
158 |
} |
159 |
for (int i = 0; i < SIZE; ++i) { |
160 |
assertEquals(i, q.size()); |
161 |
q.add(new Integer(i)); |
162 |
} |
163 |
} |
164 |
|
165 |
/** |
166 |
* add(null) throws NPE |
167 |
*/ |
168 |
public void testAddNull() { |
169 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
170 |
try { |
171 |
q.add(null); |
172 |
shouldThrow(); |
173 |
} catch (NullPointerException success) {} |
174 |
} |
175 |
|
176 |
/** |
177 |
* Add of comparable element succeeds |
178 |
*/ |
179 |
public void testAdd() { |
180 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
181 |
assertTrue(q.add(zero)); |
182 |
assertTrue(q.add(one)); |
183 |
} |
184 |
|
185 |
/** |
186 |
* Add of duplicate element fails |
187 |
*/ |
188 |
public void testAddDup() { |
189 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
190 |
assertTrue(q.add(zero)); |
191 |
assertFalse(q.add(zero)); |
192 |
} |
193 |
|
194 |
/** |
195 |
* Add of non-Comparable throws CCE |
196 |
*/ |
197 |
public void testAddNonComparable() { |
198 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
199 |
try { |
200 |
q.add(new Object()); |
201 |
q.add(new Object()); |
202 |
shouldThrow(); |
203 |
} catch (ClassCastException success) {} |
204 |
} |
205 |
|
206 |
/** |
207 |
* addAll(null) throws NPE |
208 |
*/ |
209 |
public void testAddAll1() { |
210 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
211 |
try { |
212 |
q.addAll(null); |
213 |
shouldThrow(); |
214 |
} catch (NullPointerException success) {} |
215 |
} |
216 |
|
217 |
/** |
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 { |
224 |
q.addAll(Arrays.asList(ints)); |
225 |
shouldThrow(); |
226 |
} catch (NullPointerException success) {} |
227 |
} |
228 |
|
229 |
/** |
230 |
* addAll of a collection with any null elements throws NPE after |
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 { |
239 |
q.addAll(Arrays.asList(ints)); |
240 |
shouldThrow(); |
241 |
} catch (NullPointerException success) {} |
242 |
} |
243 |
|
244 |
/** |
245 |
* Set contains all elements of successful addAll |
246 |
*/ |
247 |
public void testAddAll5() { |
248 |
Integer[] empty = new Integer[0]; |
249 |
Integer[] ints = new Integer[SIZE]; |
250 |
for (int i = 0; i < SIZE; ++i) |
251 |
ints[i] = new Integer(SIZE-1-i); |
252 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
253 |
assertFalse(q.addAll(Arrays.asList(empty))); |
254 |
assertTrue(q.addAll(Arrays.asList(ints))); |
255 |
for (int i = 0; i < SIZE; ++i) |
256 |
assertEquals(i, q.pollFirst()); |
257 |
} |
258 |
|
259 |
/** |
260 |
* pollFirst succeeds unless empty |
261 |
*/ |
262 |
public void testPollFirst() { |
263 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
264 |
for (int i = 0; i < SIZE; ++i) { |
265 |
assertEquals(i, q.pollFirst()); |
266 |
} |
267 |
assertNull(q.pollFirst()); |
268 |
} |
269 |
|
270 |
/** |
271 |
* pollLast succeeds unless empty |
272 |
*/ |
273 |
public void testPollLast() { |
274 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
275 |
for (int i = SIZE-1; i >= 0; --i) { |
276 |
assertEquals(i, q.pollLast()); |
277 |
} |
278 |
assertNull(q.pollFirst()); |
279 |
} |
280 |
|
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) { |
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) { |
293 |
assertTrue(q.contains(i)); |
294 |
assertTrue(q.remove(i)); |
295 |
assertFalse(q.contains(i)); |
296 |
assertFalse(q.remove(i+1)); |
297 |
assertFalse(q.contains(i+1)); |
298 |
} |
299 |
assertTrue(q.isEmpty()); |
300 |
} |
301 |
|
302 |
/** |
303 |
* contains(x) reports true when elements added but not yet removed |
304 |
*/ |
305 |
public void testContains() { |
306 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
307 |
for (int i = 0; i < SIZE; ++i) { |
308 |
assertTrue(q.contains(new Integer(i))); |
309 |
q.pollFirst(); |
310 |
assertFalse(q.contains(new Integer(i))); |
311 |
} |
312 |
} |
313 |
|
314 |
/** |
315 |
* clear removes all elements |
316 |
*/ |
317 |
public void testClear() { |
318 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
319 |
q.clear(); |
320 |
assertTrue(q.isEmpty()); |
321 |
assertEquals(0, q.size()); |
322 |
q.add(new Integer(1)); |
323 |
assertFalse(q.isEmpty()); |
324 |
q.clear(); |
325 |
assertTrue(q.isEmpty()); |
326 |
} |
327 |
|
328 |
/** |
329 |
* containsAll(c) is true when c contains a subset of elements |
330 |
*/ |
331 |
public void testContainsAll() { |
332 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
333 |
ConcurrentSkipListSet p = new ConcurrentSkipListSet(); |
334 |
for (int i = 0; i < SIZE; ++i) { |
335 |
assertTrue(q.containsAll(p)); |
336 |
assertFalse(p.containsAll(q)); |
337 |
p.add(new Integer(i)); |
338 |
} |
339 |
assertTrue(p.containsAll(q)); |
340 |
} |
341 |
|
342 |
/** |
343 |
* retainAll(c) retains only those elements of c and reports true if changed |
344 |
*/ |
345 |
public void testRetainAll() { |
346 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
347 |
ConcurrentSkipListSet p = populatedSet(SIZE); |
348 |
for (int i = 0; i < SIZE; ++i) { |
349 |
boolean changed = q.retainAll(p); |
350 |
if (i == 0) |
351 |
assertFalse(changed); |
352 |
else |
353 |
assertTrue(changed); |
354 |
|
355 |
assertTrue(q.containsAll(p)); |
356 |
assertEquals(SIZE-i, q.size()); |
357 |
p.pollFirst(); |
358 |
} |
359 |
} |
360 |
|
361 |
/** |
362 |
* removeAll(c) removes only those elements of c and reports true if changed |
363 |
*/ |
364 |
public void testRemoveAll() { |
365 |
for (int i = 1; i < SIZE; ++i) { |
366 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
367 |
ConcurrentSkipListSet p = populatedSet(i); |
368 |
assertTrue(q.removeAll(p)); |
369 |
assertEquals(SIZE-i, q.size()); |
370 |
for (int j = 0; j < i; ++j) { |
371 |
Integer x = (Integer)(p.pollFirst()); |
372 |
assertFalse(q.contains(x)); |
373 |
} |
374 |
} |
375 |
} |
376 |
|
377 |
/** |
378 |
* lower returns preceding element |
379 |
*/ |
380 |
public void testLower() { |
381 |
ConcurrentSkipListSet q = set5(); |
382 |
Object e1 = q.lower(three); |
383 |
assertEquals(two, e1); |
384 |
|
385 |
Object e2 = q.lower(six); |
386 |
assertEquals(five, e2); |
387 |
|
388 |
Object e3 = q.lower(one); |
389 |
assertNull(e3); |
390 |
|
391 |
Object e4 = q.lower(zero); |
392 |
assertNull(e4); |
393 |
} |
394 |
|
395 |
/** |
396 |
* higher returns next element |
397 |
*/ |
398 |
public void testHigher() { |
399 |
ConcurrentSkipListSet q = set5(); |
400 |
Object e1 = q.higher(three); |
401 |
assertEquals(four, e1); |
402 |
|
403 |
Object e2 = q.higher(zero); |
404 |
assertEquals(one, e2); |
405 |
|
406 |
Object e3 = q.higher(five); |
407 |
assertNull(e3); |
408 |
|
409 |
Object e4 = q.higher(six); |
410 |
assertNull(e4); |
411 |
} |
412 |
|
413 |
/** |
414 |
* floor returns preceding element |
415 |
*/ |
416 |
public void testFloor() { |
417 |
ConcurrentSkipListSet q = set5(); |
418 |
Object e1 = q.floor(three); |
419 |
assertEquals(three, e1); |
420 |
|
421 |
Object e2 = q.floor(six); |
422 |
assertEquals(five, e2); |
423 |
|
424 |
Object e3 = q.floor(one); |
425 |
assertEquals(one, e3); |
426 |
|
427 |
Object e4 = q.floor(zero); |
428 |
assertNull(e4); |
429 |
} |
430 |
|
431 |
/** |
432 |
* ceiling returns next element |
433 |
*/ |
434 |
public void testCeiling() { |
435 |
ConcurrentSkipListSet q = set5(); |
436 |
Object e1 = q.ceiling(three); |
437 |
assertEquals(three, e1); |
438 |
|
439 |
Object e2 = q.ceiling(zero); |
440 |
assertEquals(one, e2); |
441 |
|
442 |
Object e3 = q.ceiling(five); |
443 |
assertEquals(five, e3); |
444 |
|
445 |
Object e4 = q.ceiling(six); |
446 |
assertNull(e4); |
447 |
} |
448 |
|
449 |
/** |
450 |
* toArray contains all elements in sorted order |
451 |
*/ |
452 |
public void testToArray() { |
453 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
454 |
Object[] o = q.toArray(); |
455 |
for (int i = 0; i < o.length; i++) |
456 |
assertSame(o[i], q.pollFirst()); |
457 |
} |
458 |
|
459 |
/** |
460 |
* toArray(a) contains all elements in sorted order |
461 |
*/ |
462 |
public void testToArray2() { |
463 |
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
464 |
Integer[] ints = new Integer[SIZE]; |
465 |
assertSame(ints, q.toArray(ints)); |
466 |
for (int i = 0; i < ints.length; i++) |
467 |
assertSame(ints[i], q.pollFirst()); |
468 |
} |
469 |
|
470 |
/** |
471 |
* iterator iterates through all elements |
472 |
*/ |
473 |
public void testIterator() { |
474 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
475 |
Iterator it = q.iterator(); |
476 |
int i; |
477 |
for (i = 0; it.hasNext(); i++) |
478 |
assertTrue(q.contains(it.next())); |
479 |
assertEquals(i, SIZE); |
480 |
assertIteratorExhausted(it); |
481 |
} |
482 |
|
483 |
/** |
484 |
* iterator of empty set has no elements |
485 |
*/ |
486 |
public void testEmptyIterator() { |
487 |
NavigableSet s = new ConcurrentSkipListSet(); |
488 |
assertIteratorExhausted(s.iterator()); |
489 |
assertIteratorExhausted(s.descendingSet().iterator()); |
490 |
} |
491 |
|
492 |
/** |
493 |
* iterator.remove removes current element |
494 |
*/ |
495 |
public void testIteratorRemove() { |
496 |
final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
497 |
q.add(new Integer(2)); |
498 |
q.add(new Integer(1)); |
499 |
q.add(new Integer(3)); |
500 |
|
501 |
Iterator it = q.iterator(); |
502 |
it.next(); |
503 |
it.remove(); |
504 |
|
505 |
it = q.iterator(); |
506 |
assertEquals(it.next(), new Integer(2)); |
507 |
assertEquals(it.next(), new Integer(3)); |
508 |
assertFalse(it.hasNext()); |
509 |
} |
510 |
|
511 |
/** |
512 |
* toString contains toStrings of elements |
513 |
*/ |
514 |
public void testToString() { |
515 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
516 |
String s = q.toString(); |
517 |
for (int i = 0; i < SIZE; ++i) { |
518 |
assertTrue(s.contains(String.valueOf(i))); |
519 |
} |
520 |
} |
521 |
|
522 |
/** |
523 |
* A deserialized serialized set has same elements |
524 |
*/ |
525 |
public void testSerialization() throws Exception { |
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 |
/** |
541 |
* subSet returns set with keys in requested range |
542 |
*/ |
543 |
public void testSubSetContents() { |
544 |
ConcurrentSkipListSet set = set5(); |
545 |
SortedSet sm = set.subSet(two, four); |
546 |
assertEquals(two, sm.first()); |
547 |
assertEquals(three, sm.last()); |
548 |
assertEquals(2, sm.size()); |
549 |
assertFalse(sm.contains(one)); |
550 |
assertTrue(sm.contains(two)); |
551 |
assertTrue(sm.contains(three)); |
552 |
assertFalse(sm.contains(four)); |
553 |
assertFalse(sm.contains(five)); |
554 |
Iterator i = sm.iterator(); |
555 |
Object k; |
556 |
k = (Integer)(i.next()); |
557 |
assertEquals(two, k); |
558 |
k = (Integer)(i.next()); |
559 |
assertEquals(three, k); |
560 |
assertFalse(i.hasNext()); |
561 |
Iterator j = sm.iterator(); |
562 |
j.next(); |
563 |
j.remove(); |
564 |
assertFalse(set.contains(two)); |
565 |
assertEquals(4, set.size()); |
566 |
assertEquals(1, sm.size()); |
567 |
assertEquals(three, sm.first()); |
568 |
assertEquals(three, sm.last()); |
569 |
assertTrue(sm.remove(three)); |
570 |
assertTrue(sm.isEmpty()); |
571 |
assertEquals(3, set.size()); |
572 |
} |
573 |
|
574 |
public void testSubSetContents2() { |
575 |
ConcurrentSkipListSet set = set5(); |
576 |
SortedSet sm = set.subSet(two, three); |
577 |
assertEquals(1, sm.size()); |
578 |
assertEquals(two, sm.first()); |
579 |
assertEquals(two, sm.last()); |
580 |
assertFalse(sm.contains(one)); |
581 |
assertTrue(sm.contains(two)); |
582 |
assertFalse(sm.contains(three)); |
583 |
assertFalse(sm.contains(four)); |
584 |
assertFalse(sm.contains(five)); |
585 |
Iterator i = sm.iterator(); |
586 |
Object k; |
587 |
k = (Integer)(i.next()); |
588 |
assertEquals(two, k); |
589 |
assertFalse(i.hasNext()); |
590 |
Iterator j = sm.iterator(); |
591 |
j.next(); |
592 |
j.remove(); |
593 |
assertFalse(set.contains(two)); |
594 |
assertEquals(4, set.size()); |
595 |
assertEquals(0, sm.size()); |
596 |
assertTrue(sm.isEmpty()); |
597 |
assertFalse(sm.remove(three)); |
598 |
assertEquals(4, set.size()); |
599 |
} |
600 |
|
601 |
/** |
602 |
* headSet returns set with keys in requested range |
603 |
*/ |
604 |
public void testHeadSetContents() { |
605 |
ConcurrentSkipListSet set = set5(); |
606 |
SortedSet sm = set.headSet(four); |
607 |
assertTrue(sm.contains(one)); |
608 |
assertTrue(sm.contains(two)); |
609 |
assertTrue(sm.contains(three)); |
610 |
assertFalse(sm.contains(four)); |
611 |
assertFalse(sm.contains(five)); |
612 |
Iterator i = sm.iterator(); |
613 |
Object k; |
614 |
k = (Integer)(i.next()); |
615 |
assertEquals(one, k); |
616 |
k = (Integer)(i.next()); |
617 |
assertEquals(two, k); |
618 |
k = (Integer)(i.next()); |
619 |
assertEquals(three, k); |
620 |
assertFalse(i.hasNext()); |
621 |
sm.clear(); |
622 |
assertTrue(sm.isEmpty()); |
623 |
assertEquals(2, set.size()); |
624 |
assertEquals(four, set.first()); |
625 |
} |
626 |
|
627 |
/** |
628 |
* tailSet returns set with keys in requested range |
629 |
*/ |
630 |
public void testTailSetContents() { |
631 |
ConcurrentSkipListSet set = set5(); |
632 |
SortedSet sm = set.tailSet(two); |
633 |
assertFalse(sm.contains(one)); |
634 |
assertTrue(sm.contains(two)); |
635 |
assertTrue(sm.contains(three)); |
636 |
assertTrue(sm.contains(four)); |
637 |
assertTrue(sm.contains(five)); |
638 |
Iterator i = sm.iterator(); |
639 |
Object k; |
640 |
k = (Integer)(i.next()); |
641 |
assertEquals(two, k); |
642 |
k = (Integer)(i.next()); |
643 |
assertEquals(three, k); |
644 |
k = (Integer)(i.next()); |
645 |
assertEquals(four, k); |
646 |
k = (Integer)(i.next()); |
647 |
assertEquals(five, k); |
648 |
assertFalse(i.hasNext()); |
649 |
|
650 |
SortedSet ssm = sm.tailSet(four); |
651 |
assertEquals(four, ssm.first()); |
652 |
assertEquals(five, ssm.last()); |
653 |
assertTrue(ssm.remove(four)); |
654 |
assertEquals(1, ssm.size()); |
655 |
assertEquals(3, sm.size()); |
656 |
assertEquals(4, set.size()); |
657 |
} |
658 |
|
659 |
Random rnd = new Random(666); |
660 |
|
661 |
/** |
662 |
* Subsets of subsets subdivide correctly |
663 |
*/ |
664 |
public void testRecursiveSubSets() throws Exception { |
665 |
int setSize = expensiveTests ? 1000 : 100; |
666 |
Class cl = ConcurrentSkipListSet.class; |
667 |
|
668 |
NavigableSet<Integer> set = newSet(cl); |
669 |
BitSet bs = new BitSet(setSize); |
670 |
|
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, 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()); |
697 |
assertFalse(result.iterator().hasNext()); |
698 |
return result; |
699 |
} |
700 |
|
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, bs); |
705 |
} |
706 |
} |
707 |
|
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), bs); |
715 |
} |
716 |
|
717 |
// Remove a bunch of entries with iterator |
718 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
719 |
if (rnd.nextBoolean()) { |
720 |
bs.clear(it.next()); |
721 |
it.remove(); |
722 |
} |
723 |
} |
724 |
|
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, bs); |
730 |
} |
731 |
} |
732 |
|
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), bs); |
741 |
} |
742 |
|
743 |
// Remove a bunch of entries with iterator |
744 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
745 |
if (rnd.nextBoolean()) { |
746 |
bs.clear(it.next()); |
747 |
it.remove(); |
748 |
} |
749 |
} |
750 |
|
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, bs); |
756 |
} else { |
757 |
try { |
758 |
set.add(element); |
759 |
shouldThrow(); |
760 |
} catch (IllegalArgumentException success) {} |
761 |
} |
762 |
} |
763 |
} |
764 |
|
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, 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 |
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) |
787 |
return; |
788 |
int midPoint = (min + max) / 2; |
789 |
|
790 |
// headSet - pick direction and endpoint inclusion randomly |
791 |
boolean incl = rnd.nextBoolean(); |
792 |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
793 |
if (ascending) { |
794 |
if (rnd.nextBoolean()) |
795 |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); |
796 |
else |
797 |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
798 |
false, bs); |
799 |
} else { |
800 |
if (rnd.nextBoolean()) |
801 |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs); |
802 |
else |
803 |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
804 |
true, bs); |
805 |
} |
806 |
|
807 |
// tailSet - pick direction and endpoint inclusion randomly |
808 |
incl = rnd.nextBoolean(); |
809 |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
810 |
if (ascending) { |
811 |
if (rnd.nextBoolean()) |
812 |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); |
813 |
else |
814 |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
815 |
false, bs); |
816 |
} else { |
817 |
if (rnd.nextBoolean()) { |
818 |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs); |
819 |
} else { |
820 |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
821 |
true, bs); |
822 |
} |
823 |
} |
824 |
|
825 |
// subSet - pick direction and endpoint inclusion randomly |
826 |
int rangeSize = max - min + 1; |
827 |
int[] endpoints = new int[2]; |
828 |
endpoints[0] = min + rnd.nextInt(rangeSize); |
829 |
endpoints[1] = min + rnd.nextInt(rangeSize); |
830 |
Arrays.sort(endpoints); |
831 |
boolean lowIncl = rnd.nextBoolean(); |
832 |
boolean highIncl = rnd.nextBoolean(); |
833 |
if (ascending) { |
834 |
NavigableSet<Integer> sm = set.subSet( |
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, bs); |
839 |
else |
840 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
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, bs); |
848 |
else |
849 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
850 |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
851 |
} |
852 |
} |
853 |
|
854 |
/** |
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 |
final BitSet bs) { |
860 |
class ReferenceSet { |
861 |
int lower(int element) { |
862 |
return ascending ? |
863 |
lowerAscending(element) : higherAscending(element); |
864 |
} |
865 |
int floor(int element) { |
866 |
return ascending ? |
867 |
floorAscending(element) : ceilingAscending(element); |
868 |
} |
869 |
int ceiling(int element) { |
870 |
return ascending ? |
871 |
ceilingAscending(element) : floorAscending(element); |
872 |
} |
873 |
int higher(int element) { |
874 |
return ascending ? |
875 |
higherAscending(element) : lowerAscending(element); |
876 |
} |
877 |
int first() { |
878 |
return ascending ? firstAscending() : lastAscending(); |
879 |
} |
880 |
int last() { |
881 |
return ascending ? lastAscending() : firstAscending(); |
882 |
} |
883 |
int lowerAscending(int element) { |
884 |
return floorAscending(element - 1); |
885 |
} |
886 |
int floorAscending(int element) { |
887 |
if (element < min) |
888 |
return -1; |
889 |
else if (element > max) |
890 |
element = max; |
891 |
|
892 |
// BitSet should support this! Test would run much faster |
893 |
while (element >= min) { |
894 |
if (bs.get(element)) |
895 |
return element; |
896 |
element--; |
897 |
} |
898 |
return -1; |
899 |
} |
900 |
int ceilingAscending(int element) { |
901 |
if (element < min) |
902 |
element = min; |
903 |
else if (element > max) |
904 |
return -1; |
905 |
int result = bs.nextSetBit(element); |
906 |
return result > max ? -1 : result; |
907 |
} |
908 |
int higherAscending(int element) { |
909 |
return ceilingAscending(element + 1); |
910 |
} |
911 |
private int firstAscending() { |
912 |
int result = ceilingAscending(min); |
913 |
return result > max ? -1 : result; |
914 |
} |
915 |
private int lastAscending() { |
916 |
int result = floorAscending(max); |
917 |
return result < min ? -1 : result; |
918 |
} |
919 |
} |
920 |
ReferenceSet rs = new ReferenceSet(); |
921 |
|
922 |
// Test contents using containsElement |
923 |
int size = 0; |
924 |
for (int i = min; i <= max; i++) { |
925 |
boolean bsContainsI = bs.get(i); |
926 |
assertEquals(bsContainsI, set.contains(i)); |
927 |
if (bsContainsI) |
928 |
size++; |
929 |
} |
930 |
assertEquals(size, set.size()); |
931 |
|
932 |
// Test contents using contains elementSet iterator |
933 |
int size2 = 0; |
934 |
int previousElement = -1; |
935 |
for (int element : set) { |
936 |
assertTrue(bs.get(element)); |
937 |
size2++; |
938 |
assertTrue(previousElement < 0 || (ascending ? |
939 |
element - previousElement > 0 : element - previousElement < 0)); |
940 |
previousElement = element; |
941 |
} |
942 |
assertEquals(size2, size); |
943 |
|
944 |
// Test navigation ops |
945 |
for (int element = min - 1; element <= max + 1; element++) { |
946 |
assertEq(set.lower(element), rs.lower(element)); |
947 |
assertEq(set.floor(element), rs.floor(element)); |
948 |
assertEq(set.higher(element), rs.higher(element)); |
949 |
assertEq(set.ceiling(element), rs.ceiling(element)); |
950 |
} |
951 |
|
952 |
// Test extrema |
953 |
if (set.size() != 0) { |
954 |
assertEq(set.first(), rs.first()); |
955 |
assertEq(set.last(), rs.last()); |
956 |
} else { |
957 |
assertEq(rs.first(), -1); |
958 |
assertEq(rs.last(), -1); |
959 |
try { |
960 |
set.first(); |
961 |
shouldThrow(); |
962 |
} catch (NoSuchElementException success) {} |
963 |
try { |
964 |
set.last(); |
965 |
shouldThrow(); |
966 |
} catch (NoSuchElementException success) {} |
967 |
} |
968 |
} |
969 |
|
970 |
static void assertEq(Integer i, int j) { |
971 |
if (i == null) |
972 |
assertEquals(j, -1); |
973 |
else |
974 |
assertEquals((int) i, j); |
975 |
} |
976 |
|
977 |
static boolean eq(Integer i, int j) { |
978 |
return i == null ? j == -1 : i == j; |
979 |
} |
980 |
|
981 |
} |