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 |
junit.textui.TestRunner.run(suite()); |
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 |
ConcurrentSkipListSet q = 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 |
ConcurrentSkipListSet q = 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 |
ConcurrentSkipListSet q = 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 |
try { |
170 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
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 |
try { |
199 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
200 |
q.add(new Object()); |
201 |
q.add(new Object()); |
202 |
q.add(new Object()); |
203 |
shouldThrow(); |
204 |
} catch (ClassCastException success) {} |
205 |
} |
206 |
|
207 |
/** |
208 |
* addAll(null) throws NPE |
209 |
*/ |
210 |
public void testAddAll1() { |
211 |
try { |
212 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
213 |
q.addAll(null); |
214 |
shouldThrow(); |
215 |
} catch (NullPointerException success) {} |
216 |
} |
217 |
|
218 |
/** |
219 |
* addAll of a collection with null elements throws NPE |
220 |
*/ |
221 |
public void testAddAll2() { |
222 |
try { |
223 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
224 |
Integer[] ints = new Integer[SIZE]; |
225 |
q.addAll(Arrays.asList(ints)); |
226 |
shouldThrow(); |
227 |
} catch (NullPointerException success) {} |
228 |
} |
229 |
|
230 |
/** |
231 |
* addAll of a collection with any null elements throws NPE after |
232 |
* possibly adding some elements |
233 |
*/ |
234 |
public void testAddAll3() { |
235 |
try { |
236 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
237 |
Integer[] ints = new Integer[SIZE]; |
238 |
for (int i = 0; i < SIZE-1; ++i) |
239 |
ints[i] = new Integer(i); |
240 |
q.addAll(Arrays.asList(ints)); |
241 |
shouldThrow(); |
242 |
} catch (NullPointerException success) {} |
243 |
} |
244 |
|
245 |
/** |
246 |
* Set contains all elements of successful addAll |
247 |
*/ |
248 |
public void testAddAll5() { |
249 |
Integer[] empty = new Integer[0]; |
250 |
Integer[] ints = new Integer[SIZE]; |
251 |
for (int i = 0; i < SIZE; ++i) |
252 |
ints[i] = new Integer(SIZE-1-i); |
253 |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
254 |
assertFalse(q.addAll(Arrays.asList(empty))); |
255 |
assertTrue(q.addAll(Arrays.asList(ints))); |
256 |
for (int i = 0; i < SIZE; ++i) |
257 |
assertEquals(i, q.pollFirst()); |
258 |
} |
259 |
|
260 |
/** |
261 |
* pollFirst succeeds unless empty |
262 |
*/ |
263 |
public void testPollFirst() { |
264 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
265 |
for (int i = 0; i < SIZE; ++i) { |
266 |
assertEquals(i, q.pollFirst()); |
267 |
} |
268 |
assertNull(q.pollFirst()); |
269 |
} |
270 |
|
271 |
/** |
272 |
* pollLast succeeds unless empty |
273 |
*/ |
274 |
public void testPollLast() { |
275 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
276 |
for (int i = SIZE-1; i >= 0; --i) { |
277 |
assertEquals(i, q.pollLast()); |
278 |
} |
279 |
assertNull(q.pollFirst()); |
280 |
} |
281 |
|
282 |
/** |
283 |
* remove(x) removes x and returns true if present |
284 |
*/ |
285 |
public void testRemoveElement() { |
286 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
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) { |
294 |
assertTrue(q.contains(i)); |
295 |
assertTrue(q.remove(i)); |
296 |
assertFalse(q.contains(i)); |
297 |
assertFalse(q.remove(i+1)); |
298 |
assertFalse(q.contains(i+1)); |
299 |
} |
300 |
assertTrue(q.isEmpty()); |
301 |
} |
302 |
|
303 |
/** |
304 |
* contains(x) reports true when elements added but not yet removed |
305 |
*/ |
306 |
public void testContains() { |
307 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
308 |
for (int i = 0; i < SIZE; ++i) { |
309 |
assertTrue(q.contains(new Integer(i))); |
310 |
q.pollFirst(); |
311 |
assertFalse(q.contains(new Integer(i))); |
312 |
} |
313 |
} |
314 |
|
315 |
/** |
316 |
* clear removes all elements |
317 |
*/ |
318 |
public void testClear() { |
319 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
320 |
q.clear(); |
321 |
assertTrue(q.isEmpty()); |
322 |
assertEquals(0, q.size()); |
323 |
q.add(new Integer(1)); |
324 |
assertFalse(q.isEmpty()); |
325 |
q.clear(); |
326 |
assertTrue(q.isEmpty()); |
327 |
} |
328 |
|
329 |
/** |
330 |
* containsAll(c) is true when c contains a subset of elements |
331 |
*/ |
332 |
public void testContainsAll() { |
333 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
334 |
ConcurrentSkipListSet p = new ConcurrentSkipListSet(); |
335 |
for (int i = 0; i < SIZE; ++i) { |
336 |
assertTrue(q.containsAll(p)); |
337 |
assertFalse(p.containsAll(q)); |
338 |
p.add(new Integer(i)); |
339 |
} |
340 |
assertTrue(p.containsAll(q)); |
341 |
} |
342 |
|
343 |
/** |
344 |
* retainAll(c) retains only those elements of c and reports true if changed |
345 |
*/ |
346 |
public void testRetainAll() { |
347 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
348 |
ConcurrentSkipListSet p = populatedSet(SIZE); |
349 |
for (int i = 0; i < SIZE; ++i) { |
350 |
boolean changed = q.retainAll(p); |
351 |
if (i == 0) |
352 |
assertFalse(changed); |
353 |
else |
354 |
assertTrue(changed); |
355 |
|
356 |
assertTrue(q.containsAll(p)); |
357 |
assertEquals(SIZE-i, q.size()); |
358 |
p.pollFirst(); |
359 |
} |
360 |
} |
361 |
|
362 |
/** |
363 |
* removeAll(c) removes only those elements of c and reports true if changed |
364 |
*/ |
365 |
public void testRemoveAll() { |
366 |
for (int i = 1; i < SIZE; ++i) { |
367 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
368 |
ConcurrentSkipListSet p = populatedSet(i); |
369 |
assertTrue(q.removeAll(p)); |
370 |
assertEquals(SIZE-i, q.size()); |
371 |
for (int j = 0; j < i; ++j) { |
372 |
Integer x = (Integer)(p.pollFirst()); |
373 |
assertFalse(q.contains(x)); |
374 |
} |
375 |
} |
376 |
} |
377 |
|
378 |
/** |
379 |
* lower returns preceding element |
380 |
*/ |
381 |
public void testLower() { |
382 |
ConcurrentSkipListSet q = set5(); |
383 |
Object e1 = q.lower(three); |
384 |
assertEquals(two, e1); |
385 |
|
386 |
Object e2 = q.lower(six); |
387 |
assertEquals(five, e2); |
388 |
|
389 |
Object e3 = q.lower(one); |
390 |
assertNull(e3); |
391 |
|
392 |
Object e4 = q.lower(zero); |
393 |
assertNull(e4); |
394 |
} |
395 |
|
396 |
/** |
397 |
* higher returns next element |
398 |
*/ |
399 |
public void testHigher() { |
400 |
ConcurrentSkipListSet q = set5(); |
401 |
Object e1 = q.higher(three); |
402 |
assertEquals(four, e1); |
403 |
|
404 |
Object e2 = q.higher(zero); |
405 |
assertEquals(one, e2); |
406 |
|
407 |
Object e3 = q.higher(five); |
408 |
assertNull(e3); |
409 |
|
410 |
Object e4 = q.higher(six); |
411 |
assertNull(e4); |
412 |
} |
413 |
|
414 |
/** |
415 |
* floor returns preceding element |
416 |
*/ |
417 |
public void testFloor() { |
418 |
ConcurrentSkipListSet q = set5(); |
419 |
Object e1 = q.floor(three); |
420 |
assertEquals(three, e1); |
421 |
|
422 |
Object e2 = q.floor(six); |
423 |
assertEquals(five, e2); |
424 |
|
425 |
Object e3 = q.floor(one); |
426 |
assertEquals(one, e3); |
427 |
|
428 |
Object e4 = q.floor(zero); |
429 |
assertNull(e4); |
430 |
} |
431 |
|
432 |
/** |
433 |
* ceiling returns next element |
434 |
*/ |
435 |
public void testCeiling() { |
436 |
ConcurrentSkipListSet q = set5(); |
437 |
Object e1 = q.ceiling(three); |
438 |
assertEquals(three, e1); |
439 |
|
440 |
Object e2 = q.ceiling(zero); |
441 |
assertEquals(one, e2); |
442 |
|
443 |
Object e3 = q.ceiling(five); |
444 |
assertEquals(five, e3); |
445 |
|
446 |
Object e4 = q.ceiling(six); |
447 |
assertNull(e4); |
448 |
} |
449 |
|
450 |
/** |
451 |
* toArray contains all elements in sorted order |
452 |
*/ |
453 |
public void testToArray() { |
454 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
455 |
Object[] o = q.toArray(); |
456 |
for (int i = 0; i < o.length; i++) |
457 |
assertSame(o[i], q.pollFirst()); |
458 |
} |
459 |
|
460 |
/** |
461 |
* toArray(a) contains all elements in sorted order |
462 |
*/ |
463 |
public void testToArray2() { |
464 |
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
465 |
Integer[] ints = new Integer[SIZE]; |
466 |
assertSame(ints, q.toArray(ints)); |
467 |
for (int i = 0; i < ints.length; i++) |
468 |
assertSame(ints[i], q.pollFirst()); |
469 |
} |
470 |
|
471 |
/** |
472 |
* iterator iterates through all elements |
473 |
*/ |
474 |
public void testIterator() { |
475 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
476 |
Iterator it = q.iterator(); |
477 |
int i; |
478 |
for (i = 0; it.hasNext(); i++) |
479 |
assertTrue(q.contains(it.next())); |
480 |
assertEquals(i, SIZE); |
481 |
assertIteratorExhausted(it); |
482 |
} |
483 |
|
484 |
/** |
485 |
* iterator of empty set has no elements |
486 |
*/ |
487 |
public void testEmptyIterator() { |
488 |
NavigableSet s = new ConcurrentSkipListSet(); |
489 |
assertIteratorExhausted(s.iterator()); |
490 |
assertIteratorExhausted(s.descendingSet().iterator()); |
491 |
} |
492 |
|
493 |
/** |
494 |
* iterator.remove removes current element |
495 |
*/ |
496 |
public void testIteratorRemove() { |
497 |
final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
498 |
q.add(new Integer(2)); |
499 |
q.add(new Integer(1)); |
500 |
q.add(new Integer(3)); |
501 |
|
502 |
Iterator it = q.iterator(); |
503 |
it.next(); |
504 |
it.remove(); |
505 |
|
506 |
it = q.iterator(); |
507 |
assertEquals(it.next(), new Integer(2)); |
508 |
assertEquals(it.next(), new Integer(3)); |
509 |
assertFalse(it.hasNext()); |
510 |
} |
511 |
|
512 |
/** |
513 |
* toString contains toStrings of elements |
514 |
*/ |
515 |
public void testToString() { |
516 |
ConcurrentSkipListSet q = populatedSet(SIZE); |
517 |
String s = q.toString(); |
518 |
for (int i = 0; i < SIZE; ++i) { |
519 |
assertTrue(s.contains(String.valueOf(i))); |
520 |
} |
521 |
} |
522 |
|
523 |
/** |
524 |
* A deserialized serialized set has same elements |
525 |
*/ |
526 |
public void testSerialization() throws Exception { |
527 |
NavigableSet x = populatedSet(SIZE); |
528 |
NavigableSet y = serialClone(x); |
529 |
|
530 |
assertNotSame(x, y); |
531 |
assertEquals(x.size(), y.size()); |
532 |
assertEquals(x, y); |
533 |
assertEquals(y, x); |
534 |
while (!x.isEmpty()) { |
535 |
assertFalse(y.isEmpty()); |
536 |
assertEquals(x.pollFirst(), y.pollFirst()); |
537 |
} |
538 |
assertTrue(y.isEmpty()); |
539 |
} |
540 |
|
541 |
/** |
542 |
* subSet returns set with keys in requested range |
543 |
*/ |
544 |
public void testSubSetContents() { |
545 |
ConcurrentSkipListSet set = set5(); |
546 |
SortedSet sm = set.subSet(two, four); |
547 |
assertEquals(two, sm.first()); |
548 |
assertEquals(three, sm.last()); |
549 |
assertEquals(2, sm.size()); |
550 |
assertFalse(sm.contains(one)); |
551 |
assertTrue(sm.contains(two)); |
552 |
assertTrue(sm.contains(three)); |
553 |
assertFalse(sm.contains(four)); |
554 |
assertFalse(sm.contains(five)); |
555 |
Iterator i = sm.iterator(); |
556 |
Object k; |
557 |
k = (Integer)(i.next()); |
558 |
assertEquals(two, k); |
559 |
k = (Integer)(i.next()); |
560 |
assertEquals(three, k); |
561 |
assertFalse(i.hasNext()); |
562 |
Iterator j = sm.iterator(); |
563 |
j.next(); |
564 |
j.remove(); |
565 |
assertFalse(set.contains(two)); |
566 |
assertEquals(4, set.size()); |
567 |
assertEquals(1, sm.size()); |
568 |
assertEquals(three, sm.first()); |
569 |
assertEquals(three, sm.last()); |
570 |
assertTrue(sm.remove(three)); |
571 |
assertTrue(sm.isEmpty()); |
572 |
assertEquals(3, set.size()); |
573 |
} |
574 |
|
575 |
public void testSubSetContents2() { |
576 |
ConcurrentSkipListSet set = set5(); |
577 |
SortedSet sm = set.subSet(two, three); |
578 |
assertEquals(1, sm.size()); |
579 |
assertEquals(two, sm.first()); |
580 |
assertEquals(two, sm.last()); |
581 |
assertFalse(sm.contains(one)); |
582 |
assertTrue(sm.contains(two)); |
583 |
assertFalse(sm.contains(three)); |
584 |
assertFalse(sm.contains(four)); |
585 |
assertFalse(sm.contains(five)); |
586 |
Iterator i = sm.iterator(); |
587 |
Object k; |
588 |
k = (Integer)(i.next()); |
589 |
assertEquals(two, k); |
590 |
assertFalse(i.hasNext()); |
591 |
Iterator j = sm.iterator(); |
592 |
j.next(); |
593 |
j.remove(); |
594 |
assertFalse(set.contains(two)); |
595 |
assertEquals(4, set.size()); |
596 |
assertEquals(0, sm.size()); |
597 |
assertTrue(sm.isEmpty()); |
598 |
assertFalse(sm.remove(three)); |
599 |
assertEquals(4, set.size()); |
600 |
} |
601 |
|
602 |
/** |
603 |
* headSet returns set with keys in requested range |
604 |
*/ |
605 |
public void testHeadSetContents() { |
606 |
ConcurrentSkipListSet set = set5(); |
607 |
SortedSet sm = set.headSet(four); |
608 |
assertTrue(sm.contains(one)); |
609 |
assertTrue(sm.contains(two)); |
610 |
assertTrue(sm.contains(three)); |
611 |
assertFalse(sm.contains(four)); |
612 |
assertFalse(sm.contains(five)); |
613 |
Iterator i = sm.iterator(); |
614 |
Object k; |
615 |
k = (Integer)(i.next()); |
616 |
assertEquals(one, k); |
617 |
k = (Integer)(i.next()); |
618 |
assertEquals(two, k); |
619 |
k = (Integer)(i.next()); |
620 |
assertEquals(three, k); |
621 |
assertFalse(i.hasNext()); |
622 |
sm.clear(); |
623 |
assertTrue(sm.isEmpty()); |
624 |
assertEquals(2, set.size()); |
625 |
assertEquals(four, set.first()); |
626 |
} |
627 |
|
628 |
/** |
629 |
* tailSet returns set with keys in requested range |
630 |
*/ |
631 |
public void testTailSetContents() { |
632 |
ConcurrentSkipListSet set = set5(); |
633 |
SortedSet sm = set.tailSet(two); |
634 |
assertFalse(sm.contains(one)); |
635 |
assertTrue(sm.contains(two)); |
636 |
assertTrue(sm.contains(three)); |
637 |
assertTrue(sm.contains(four)); |
638 |
assertTrue(sm.contains(five)); |
639 |
Iterator i = sm.iterator(); |
640 |
Object k; |
641 |
k = (Integer)(i.next()); |
642 |
assertEquals(two, k); |
643 |
k = (Integer)(i.next()); |
644 |
assertEquals(three, k); |
645 |
k = (Integer)(i.next()); |
646 |
assertEquals(four, k); |
647 |
k = (Integer)(i.next()); |
648 |
assertEquals(five, k); |
649 |
assertFalse(i.hasNext()); |
650 |
|
651 |
SortedSet ssm = sm.tailSet(four); |
652 |
assertEquals(four, ssm.first()); |
653 |
assertEquals(five, ssm.last()); |
654 |
assertTrue(ssm.remove(four)); |
655 |
assertEquals(1, ssm.size()); |
656 |
assertEquals(3, sm.size()); |
657 |
assertEquals(4, set.size()); |
658 |
} |
659 |
|
660 |
Random rnd = new Random(666); |
661 |
|
662 |
/** |
663 |
* Subsets of subsets subdivide correctly |
664 |
*/ |
665 |
public void testRecursiveSubSets() throws Exception { |
666 |
int setSize = expensiveTests ? 1000 : 100; |
667 |
Class cl = ConcurrentSkipListSet.class; |
668 |
|
669 |
NavigableSet<Integer> set = newSet(cl); |
670 |
BitSet bs = new BitSet(setSize); |
671 |
|
672 |
populate(set, setSize, bs); |
673 |
check(set, 0, setSize - 1, true, bs); |
674 |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
675 |
|
676 |
mutateSet(set, 0, setSize - 1, bs); |
677 |
check(set, 0, setSize - 1, true, bs); |
678 |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
679 |
|
680 |
bashSubSet(set.subSet(0, true, setSize, false), |
681 |
0, setSize - 1, true, bs); |
682 |
} |
683 |
|
684 |
/** |
685 |
* addAll is idempotent |
686 |
*/ |
687 |
public void testAddAll_idempotent() throws Exception { |
688 |
Set x = populatedSet(SIZE); |
689 |
Set y = new ConcurrentSkipListSet(x); |
690 |
y.addAll(x); |
691 |
assertEquals(x, y); |
692 |
assertEquals(y, x); |
693 |
} |
694 |
|
695 |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
696 |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
697 |
assertEquals(0, result.size()); |
698 |
assertFalse(result.iterator().hasNext()); |
699 |
return result; |
700 |
} |
701 |
|
702 |
void populate(NavigableSet<Integer> set, int limit, BitSet bs) { |
703 |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
704 |
int element = rnd.nextInt(limit); |
705 |
put(set, element, bs); |
706 |
} |
707 |
} |
708 |
|
709 |
void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) { |
710 |
int size = set.size(); |
711 |
int rangeSize = max - min + 1; |
712 |
|
713 |
// Remove a bunch of entries directly |
714 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
715 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
716 |
} |
717 |
|
718 |
// Remove a bunch of entries with iterator |
719 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
720 |
if (rnd.nextBoolean()) { |
721 |
bs.clear(it.next()); |
722 |
it.remove(); |
723 |
} |
724 |
} |
725 |
|
726 |
// Add entries till we're back to original size |
727 |
while (set.size() < size) { |
728 |
int element = min + rnd.nextInt(rangeSize); |
729 |
assertTrue(element >= min && element <= max); |
730 |
put(set, element, bs); |
731 |
} |
732 |
} |
733 |
|
734 |
void mutateSubSet(NavigableSet<Integer> set, int min, int max, |
735 |
BitSet bs) { |
736 |
int size = set.size(); |
737 |
int rangeSize = max - min + 1; |
738 |
|
739 |
// Remove a bunch of entries directly |
740 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
741 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
742 |
} |
743 |
|
744 |
// Remove a bunch of entries with iterator |
745 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
746 |
if (rnd.nextBoolean()) { |
747 |
bs.clear(it.next()); |
748 |
it.remove(); |
749 |
} |
750 |
} |
751 |
|
752 |
// Add entries till we're back to original size |
753 |
while (set.size() < size) { |
754 |
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
755 |
if (element >= min && element <= max) { |
756 |
put(set, element, bs); |
757 |
} else { |
758 |
try { |
759 |
set.add(element); |
760 |
shouldThrow(); |
761 |
} catch (IllegalArgumentException success) {} |
762 |
} |
763 |
} |
764 |
} |
765 |
|
766 |
void put(NavigableSet<Integer> set, int element, BitSet bs) { |
767 |
if (set.add(element)) |
768 |
bs.set(element); |
769 |
} |
770 |
|
771 |
void remove(NavigableSet<Integer> set, int element, BitSet bs) { |
772 |
if (set.remove(element)) |
773 |
bs.clear(element); |
774 |
} |
775 |
|
776 |
void bashSubSet(NavigableSet<Integer> set, |
777 |
int min, int max, boolean ascending, |
778 |
BitSet bs) { |
779 |
check(set, min, max, ascending, bs); |
780 |
check(set.descendingSet(), min, max, !ascending, bs); |
781 |
|
782 |
mutateSubSet(set, min, max, bs); |
783 |
check(set, min, max, ascending, bs); |
784 |
check(set.descendingSet(), min, max, !ascending, bs); |
785 |
|
786 |
// Recurse |
787 |
if (max - min < 2) |
788 |
return; |
789 |
int midPoint = (min + max) / 2; |
790 |
|
791 |
// headSet - pick direction and endpoint inclusion randomly |
792 |
boolean incl = rnd.nextBoolean(); |
793 |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
794 |
if (ascending) { |
795 |
if (rnd.nextBoolean()) |
796 |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); |
797 |
else |
798 |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
799 |
false, bs); |
800 |
} else { |
801 |
if (rnd.nextBoolean()) |
802 |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs); |
803 |
else |
804 |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
805 |
true, bs); |
806 |
} |
807 |
|
808 |
// tailSet - pick direction and endpoint inclusion randomly |
809 |
incl = rnd.nextBoolean(); |
810 |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
811 |
if (ascending) { |
812 |
if (rnd.nextBoolean()) |
813 |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); |
814 |
else |
815 |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
816 |
false, bs); |
817 |
} else { |
818 |
if (rnd.nextBoolean()) { |
819 |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs); |
820 |
} else { |
821 |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
822 |
true, bs); |
823 |
} |
824 |
} |
825 |
|
826 |
// subSet - pick direction and endpoint inclusion randomly |
827 |
int rangeSize = max - min + 1; |
828 |
int[] endpoints = new int[2]; |
829 |
endpoints[0] = min + rnd.nextInt(rangeSize); |
830 |
endpoints[1] = min + rnd.nextInt(rangeSize); |
831 |
Arrays.sort(endpoints); |
832 |
boolean lowIncl = rnd.nextBoolean(); |
833 |
boolean highIncl = rnd.nextBoolean(); |
834 |
if (ascending) { |
835 |
NavigableSet<Integer> sm = set.subSet( |
836 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
837 |
if (rnd.nextBoolean()) |
838 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
839 |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
840 |
else |
841 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
842 |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
843 |
} else { |
844 |
NavigableSet<Integer> sm = set.subSet( |
845 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
846 |
if (rnd.nextBoolean()) |
847 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
848 |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
849 |
else |
850 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
851 |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
852 |
} |
853 |
} |
854 |
|
855 |
/** |
856 |
* min and max are both inclusive. If max < min, interval is empty. |
857 |
*/ |
858 |
void check(NavigableSet<Integer> set, |
859 |
final int min, final int max, final boolean ascending, |
860 |
final BitSet bs) { |
861 |
class ReferenceSet { |
862 |
int lower(int element) { |
863 |
return ascending ? |
864 |
lowerAscending(element) : higherAscending(element); |
865 |
} |
866 |
int floor(int element) { |
867 |
return ascending ? |
868 |
floorAscending(element) : ceilingAscending(element); |
869 |
} |
870 |
int ceiling(int element) { |
871 |
return ascending ? |
872 |
ceilingAscending(element) : floorAscending(element); |
873 |
} |
874 |
int higher(int element) { |
875 |
return ascending ? |
876 |
higherAscending(element) : lowerAscending(element); |
877 |
} |
878 |
int first() { |
879 |
return ascending ? firstAscending() : lastAscending(); |
880 |
} |
881 |
int last() { |
882 |
return ascending ? lastAscending() : firstAscending(); |
883 |
} |
884 |
int lowerAscending(int element) { |
885 |
return floorAscending(element - 1); |
886 |
} |
887 |
int floorAscending(int element) { |
888 |
if (element < min) |
889 |
return -1; |
890 |
else if (element > max) |
891 |
element = max; |
892 |
|
893 |
// BitSet should support this! Test would run much faster |
894 |
while (element >= min) { |
895 |
if (bs.get(element)) |
896 |
return element; |
897 |
element--; |
898 |
} |
899 |
return -1; |
900 |
} |
901 |
int ceilingAscending(int element) { |
902 |
if (element < min) |
903 |
element = min; |
904 |
else if (element > max) |
905 |
return -1; |
906 |
int result = bs.nextSetBit(element); |
907 |
return result > max ? -1 : result; |
908 |
} |
909 |
int higherAscending(int element) { |
910 |
return ceilingAscending(element + 1); |
911 |
} |
912 |
private int firstAscending() { |
913 |
int result = ceilingAscending(min); |
914 |
return result > max ? -1 : result; |
915 |
} |
916 |
private int lastAscending() { |
917 |
int result = floorAscending(max); |
918 |
return result < min ? -1 : result; |
919 |
} |
920 |
} |
921 |
ReferenceSet rs = new ReferenceSet(); |
922 |
|
923 |
// Test contents using containsElement |
924 |
int size = 0; |
925 |
for (int i = min; i <= max; i++) { |
926 |
boolean bsContainsI = bs.get(i); |
927 |
assertEquals(bsContainsI, set.contains(i)); |
928 |
if (bsContainsI) |
929 |
size++; |
930 |
} |
931 |
assertEquals(size, set.size()); |
932 |
|
933 |
// Test contents using contains elementSet iterator |
934 |
int size2 = 0; |
935 |
int previousElement = -1; |
936 |
for (int element : set) { |
937 |
assertTrue(bs.get(element)); |
938 |
size2++; |
939 |
assertTrue(previousElement < 0 || (ascending ? |
940 |
element - previousElement > 0 : element - previousElement < 0)); |
941 |
previousElement = element; |
942 |
} |
943 |
assertEquals(size2, size); |
944 |
|
945 |
// Test navigation ops |
946 |
for (int element = min - 1; element <= max + 1; element++) { |
947 |
assertEq(set.lower(element), rs.lower(element)); |
948 |
assertEq(set.floor(element), rs.floor(element)); |
949 |
assertEq(set.higher(element), rs.higher(element)); |
950 |
assertEq(set.ceiling(element), rs.ceiling(element)); |
951 |
} |
952 |
|
953 |
// Test extrema |
954 |
if (set.size() != 0) { |
955 |
assertEq(set.first(), rs.first()); |
956 |
assertEq(set.last(), rs.last()); |
957 |
} else { |
958 |
assertEq(rs.first(), -1); |
959 |
assertEq(rs.last(), -1); |
960 |
try { |
961 |
set.first(); |
962 |
shouldThrow(); |
963 |
} catch (NoSuchElementException success) {} |
964 |
try { |
965 |
set.last(); |
966 |
shouldThrow(); |
967 |
} catch (NoSuchElementException success) {} |
968 |
} |
969 |
} |
970 |
|
971 |
static void assertEq(Integer i, int j) { |
972 |
if (i == null) |
973 |
assertEquals(j, -1); |
974 |
else |
975 |
assertEquals((int) i, j); |
976 |
} |
977 |
|
978 |
static boolean eq(Integer i, int j) { |
979 |
return i == null ? j == -1 : i == j; |
980 |
} |
981 |
|
982 |
} |