1 |
dl |
1.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/licenses/publicdomain |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
|
|
import java.util.*; |
9 |
|
|
import java.util.concurrent.*; |
10 |
|
|
import java.io.*; |
11 |
|
|
|
12 |
|
|
public class ConcurrentSkipListSetTest extends JSR166TestCase { |
13 |
|
|
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(ConcurrentSkipListSetTest.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
static class MyReverseComparator implements Comparator { |
21 |
|
|
public int compare(Object x, Object y) { |
22 |
|
|
int i = ((Integer)x).intValue(); |
23 |
|
|
int j = ((Integer)y).intValue(); |
24 |
|
|
if (i < j) return 1; |
25 |
|
|
if (i > j) return -1; |
26 |
|
|
return 0; |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* Create a set of given size containing consecutive |
32 |
|
|
* Integers 0 ... n. |
33 |
|
|
*/ |
34 |
|
|
private ConcurrentSkipListSet populatedSet(int n) { |
35 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
36 |
|
|
assertTrue(q.isEmpty()); |
37 |
|
|
for(int i = n-1; i >= 0; i-=2) |
38 |
|
|
assertTrue(q.add(new Integer(i))); |
39 |
|
|
for(int i = (n & 1); i < n; i+=2) |
40 |
|
|
assertTrue(q.add(new Integer(i))); |
41 |
|
|
assertFalse(q.isEmpty()); |
42 |
|
|
assertEquals(n, q.size()); |
43 |
|
|
return q; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
/** |
47 |
|
|
* Create set of first 5 ints |
48 |
|
|
*/ |
49 |
|
|
private ConcurrentSkipListSet set5() { |
50 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
51 |
|
|
assertTrue(q.isEmpty()); |
52 |
|
|
q.add(one); |
53 |
|
|
q.add(two); |
54 |
|
|
q.add(three); |
55 |
|
|
q.add(four); |
56 |
|
|
q.add(five); |
57 |
|
|
assertEquals(5, q.size()); |
58 |
|
|
return q; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* A new set has unbounded capacity |
63 |
|
|
*/ |
64 |
|
|
public void testConstructor1() { |
65 |
|
|
assertEquals(0, new ConcurrentSkipListSet().size()); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
/** |
69 |
|
|
* Initializing from null Collection throws NPE |
70 |
|
|
*/ |
71 |
|
|
public void testConstructor3() { |
72 |
|
|
try { |
73 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); |
74 |
|
|
shouldThrow(); |
75 |
|
|
} |
76 |
|
|
catch (NullPointerException success) {} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/** |
80 |
|
|
* Initializing from Collection of null elements throws NPE |
81 |
|
|
*/ |
82 |
|
|
public void testConstructor4() { |
83 |
|
|
try { |
84 |
|
|
Integer[] ints = new Integer[SIZE]; |
85 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
86 |
|
|
shouldThrow(); |
87 |
|
|
} |
88 |
|
|
catch (NullPointerException success) {} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
/** |
92 |
|
|
* Initializing from Collection with some null elements throws NPE |
93 |
|
|
*/ |
94 |
|
|
public void testConstructor5() { |
95 |
|
|
try { |
96 |
|
|
Integer[] ints = new Integer[SIZE]; |
97 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
98 |
|
|
ints[i] = new Integer(i); |
99 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
100 |
|
|
shouldThrow(); |
101 |
|
|
} |
102 |
|
|
catch (NullPointerException success) {} |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
/** |
106 |
|
|
* Set contains all elements of collection used to initialize |
107 |
|
|
*/ |
108 |
|
|
public void testConstructor6() { |
109 |
|
|
try { |
110 |
|
|
Integer[] ints = new Integer[SIZE]; |
111 |
|
|
for (int i = 0; i < SIZE; ++i) |
112 |
|
|
ints[i] = new Integer(i); |
113 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
114 |
|
|
for (int i = 0; i < SIZE; ++i) |
115 |
|
|
assertEquals(ints[i], q.pollFirst()); |
116 |
|
|
} |
117 |
|
|
finally {} |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* The comparator used in constructor is used |
122 |
|
|
*/ |
123 |
|
|
public void testConstructor7() { |
124 |
|
|
try { |
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 |
|
|
finally {} |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/** |
139 |
|
|
* isEmpty is true before add, false after |
140 |
|
|
*/ |
141 |
|
|
public void testEmpty() { |
142 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
143 |
|
|
assertTrue(q.isEmpty()); |
144 |
|
|
q.add(new Integer(1)); |
145 |
|
|
assertFalse(q.isEmpty()); |
146 |
|
|
q.add(new Integer(2)); |
147 |
|
|
q.pollFirst(); |
148 |
|
|
q.pollFirst(); |
149 |
|
|
assertTrue(q.isEmpty()); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
/** |
153 |
|
|
* size changes when elements added and removed |
154 |
|
|
*/ |
155 |
|
|
public void testSize() { |
156 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
157 |
|
|
for (int i = 0; i < SIZE; ++i) { |
158 |
|
|
assertEquals(SIZE-i, q.size()); |
159 |
|
|
q.pollFirst(); |
160 |
|
|
} |
161 |
|
|
for (int i = 0; i < SIZE; ++i) { |
162 |
|
|
assertEquals(i, q.size()); |
163 |
|
|
q.add(new Integer(i)); |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
|
|
* add(null) throws NPE |
169 |
|
|
*/ |
170 |
|
|
public void testAddNull() { |
171 |
|
|
try { |
172 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
173 |
|
|
q.add(null); |
174 |
|
|
shouldThrow(); |
175 |
|
|
} catch (NullPointerException success) { } |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
|
|
* Add of comparable element succeeds |
180 |
|
|
*/ |
181 |
|
|
public void testAdd() { |
182 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
183 |
|
|
assertTrue(q.add(zero)); |
184 |
|
|
assertTrue(q.add(one)); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
/** |
188 |
|
|
* Add of duplicate element fails |
189 |
|
|
*/ |
190 |
|
|
public void testAddDup() { |
191 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
192 |
|
|
assertTrue(q.add(zero)); |
193 |
|
|
assertFalse(q.add(zero)); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
/** |
197 |
|
|
* Add of non-Comparable throws CCE |
198 |
|
|
*/ |
199 |
|
|
public void testAddNonComparable() { |
200 |
|
|
try { |
201 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
202 |
|
|
q.add(new Object()); |
203 |
|
|
q.add(new Object()); |
204 |
|
|
q.add(new Object()); |
205 |
|
|
shouldThrow(); |
206 |
|
|
} |
207 |
|
|
catch(ClassCastException success) {} |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
/** |
211 |
|
|
* addAll(null) throws NPE |
212 |
|
|
*/ |
213 |
|
|
public void testAddAll1() { |
214 |
|
|
try { |
215 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
216 |
|
|
q.addAll(null); |
217 |
|
|
shouldThrow(); |
218 |
|
|
} |
219 |
|
|
catch (NullPointerException success) {} |
220 |
|
|
} |
221 |
|
|
/** |
222 |
|
|
* addAll of a collection with null elements throws NPE |
223 |
|
|
*/ |
224 |
|
|
public void testAddAll2() { |
225 |
|
|
try { |
226 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
227 |
|
|
Integer[] ints = new Integer[SIZE]; |
228 |
|
|
q.addAll(Arrays.asList(ints)); |
229 |
|
|
shouldThrow(); |
230 |
|
|
} |
231 |
|
|
catch (NullPointerException success) {} |
232 |
|
|
} |
233 |
|
|
/** |
234 |
|
|
* addAll of a collection with any null elements throws NPE after |
235 |
|
|
* possibly adding some elements |
236 |
|
|
*/ |
237 |
|
|
public void testAddAll3() { |
238 |
|
|
try { |
239 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
240 |
|
|
Integer[] ints = new Integer[SIZE]; |
241 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
242 |
|
|
ints[i] = new Integer(i); |
243 |
|
|
q.addAll(Arrays.asList(ints)); |
244 |
|
|
shouldThrow(); |
245 |
|
|
} |
246 |
|
|
catch (NullPointerException success) {} |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
/** |
250 |
|
|
* Set contains all elements of successful addAll |
251 |
|
|
*/ |
252 |
|
|
public void testAddAll5() { |
253 |
|
|
try { |
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); |
258 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
259 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
260 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
261 |
|
|
for (int i = 0; i < SIZE; ++i) |
262 |
|
|
assertEquals(new Integer(i), q.pollFirst()); |
263 |
|
|
} |
264 |
|
|
finally {} |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
/** |
268 |
|
|
* pollFirst succeeds unless empty |
269 |
|
|
*/ |
270 |
|
|
public void testPollFirst() { |
271 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
272 |
|
|
for (int i = 0; i < SIZE; ++i) { |
273 |
|
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
274 |
|
|
} |
275 |
|
|
assertNull(q.pollFirst()); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/** |
279 |
|
|
* pollLast succeeds unless empty |
280 |
|
|
*/ |
281 |
|
|
public void testPollLast() { |
282 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
283 |
|
|
for (int i = SIZE-1; i >= 0; --i) { |
284 |
|
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
285 |
|
|
} |
286 |
|
|
assertNull(q.pollFirst()); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
|
290 |
|
|
/** |
291 |
|
|
* remove(x) removes x and returns true if present |
292 |
|
|
*/ |
293 |
|
|
public void testRemoveElement() { |
294 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
295 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
296 |
|
|
assertTrue(q.remove(new Integer(i))); |
297 |
|
|
} |
298 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
299 |
|
|
assertTrue(q.remove(new Integer(i))); |
300 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
301 |
|
|
} |
302 |
|
|
assertTrue(q.isEmpty()); |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
/** |
306 |
|
|
* contains(x) reports true when elements added but not yet removed |
307 |
|
|
*/ |
308 |
|
|
public void testContains() { |
309 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
310 |
|
|
for (int i = 0; i < SIZE; ++i) { |
311 |
|
|
assertTrue(q.contains(new Integer(i))); |
312 |
|
|
q.pollFirst(); |
313 |
|
|
assertFalse(q.contains(new Integer(i))); |
314 |
|
|
} |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
/** |
318 |
|
|
* clear removes all elements |
319 |
|
|
*/ |
320 |
|
|
public void testClear() { |
321 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
322 |
|
|
q.clear(); |
323 |
|
|
assertTrue(q.isEmpty()); |
324 |
|
|
assertEquals(0, q.size()); |
325 |
|
|
q.add(new Integer(1)); |
326 |
|
|
assertFalse(q.isEmpty()); |
327 |
|
|
q.clear(); |
328 |
|
|
assertTrue(q.isEmpty()); |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
/** |
332 |
|
|
* containsAll(c) is true when c contains a subset of elements |
333 |
|
|
*/ |
334 |
|
|
public void testContainsAll() { |
335 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
336 |
|
|
ConcurrentSkipListSet p = new ConcurrentSkipListSet(); |
337 |
|
|
for (int i = 0; i < SIZE; ++i) { |
338 |
|
|
assertTrue(q.containsAll(p)); |
339 |
|
|
assertFalse(p.containsAll(q)); |
340 |
|
|
p.add(new Integer(i)); |
341 |
|
|
} |
342 |
|
|
assertTrue(p.containsAll(q)); |
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
/** |
346 |
|
|
* retainAll(c) retains only those elements of c and reports true if changed |
347 |
|
|
*/ |
348 |
|
|
public void testRetainAll() { |
349 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
350 |
|
|
ConcurrentSkipListSet p = populatedSet(SIZE); |
351 |
|
|
for (int i = 0; i < SIZE; ++i) { |
352 |
|
|
boolean changed = q.retainAll(p); |
353 |
|
|
if (i == 0) |
354 |
|
|
assertFalse(changed); |
355 |
|
|
else |
356 |
|
|
assertTrue(changed); |
357 |
|
|
|
358 |
|
|
assertTrue(q.containsAll(p)); |
359 |
|
|
assertEquals(SIZE-i, q.size()); |
360 |
|
|
p.pollFirst(); |
361 |
|
|
} |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
/** |
365 |
|
|
* removeAll(c) removes only those elements of c and reports true if changed |
366 |
|
|
*/ |
367 |
|
|
public void testRemoveAll() { |
368 |
|
|
for (int i = 1; i < SIZE; ++i) { |
369 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
370 |
|
|
ConcurrentSkipListSet p = populatedSet(i); |
371 |
|
|
assertTrue(q.removeAll(p)); |
372 |
|
|
assertEquals(SIZE-i, q.size()); |
373 |
|
|
for (int j = 0; j < i; ++j) { |
374 |
|
|
Integer I = (Integer)(p.pollFirst()); |
375 |
|
|
assertFalse(q.contains(I)); |
376 |
|
|
} |
377 |
|
|
} |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
/** |
383 |
|
|
* lower returns preceding element |
384 |
|
|
*/ |
385 |
|
|
public void testLower() { |
386 |
|
|
ConcurrentSkipListSet q = set5(); |
387 |
|
|
Object e1 = q.lower(three); |
388 |
|
|
assertEquals(two, e1); |
389 |
|
|
|
390 |
|
|
Object e2 = q.lower(six); |
391 |
|
|
assertEquals(five, e2); |
392 |
|
|
|
393 |
|
|
Object e3 = q.lower(one); |
394 |
|
|
assertNull(e3); |
395 |
|
|
|
396 |
|
|
Object e4 = q.lower(zero); |
397 |
|
|
assertNull(e4); |
398 |
|
|
|
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
/** |
402 |
|
|
* higher returns next element |
403 |
|
|
*/ |
404 |
|
|
public void testHigher() { |
405 |
|
|
ConcurrentSkipListSet q = set5(); |
406 |
|
|
Object e1 = q.higher(three); |
407 |
|
|
assertEquals(four, e1); |
408 |
|
|
|
409 |
|
|
Object e2 = q.higher(zero); |
410 |
|
|
assertEquals(one, e2); |
411 |
|
|
|
412 |
|
|
Object e3 = q.higher(five); |
413 |
|
|
assertNull(e3); |
414 |
|
|
|
415 |
|
|
Object e4 = q.higher(six); |
416 |
|
|
assertNull(e4); |
417 |
|
|
|
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
/** |
421 |
|
|
* floor returns preceding element |
422 |
|
|
*/ |
423 |
|
|
public void testFloor() { |
424 |
|
|
ConcurrentSkipListSet q = set5(); |
425 |
|
|
Object e1 = q.floor(three); |
426 |
|
|
assertEquals(three, e1); |
427 |
|
|
|
428 |
|
|
Object e2 = q.floor(six); |
429 |
|
|
assertEquals(five, e2); |
430 |
|
|
|
431 |
|
|
Object e3 = q.floor(one); |
432 |
|
|
assertEquals(one, e3); |
433 |
|
|
|
434 |
|
|
Object e4 = q.floor(zero); |
435 |
|
|
assertNull(e4); |
436 |
|
|
|
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
/** |
440 |
|
|
* ceiling returns next element |
441 |
|
|
*/ |
442 |
|
|
public void testCeiling() { |
443 |
|
|
ConcurrentSkipListSet q = set5(); |
444 |
|
|
Object e1 = q.ceiling(three); |
445 |
|
|
assertEquals(three, e1); |
446 |
|
|
|
447 |
|
|
Object e2 = q.ceiling(zero); |
448 |
|
|
assertEquals(one, e2); |
449 |
|
|
|
450 |
|
|
Object e3 = q.ceiling(five); |
451 |
|
|
assertEquals(five, e3); |
452 |
|
|
|
453 |
|
|
Object e4 = q.ceiling(six); |
454 |
|
|
assertNull(e4); |
455 |
|
|
|
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
/** |
459 |
|
|
* toArray contains all elements |
460 |
|
|
*/ |
461 |
|
|
public void testToArray() { |
462 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
463 |
|
|
Object[] o = q.toArray(); |
464 |
|
|
Arrays.sort(o); |
465 |
|
|
for(int i = 0; i < o.length; i++) |
466 |
|
|
assertEquals(o[i], q.pollFirst()); |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
/** |
470 |
|
|
* toArray(a) contains all elements |
471 |
|
|
*/ |
472 |
|
|
public void testToArray2() { |
473 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
474 |
|
|
Integer[] ints = new Integer[SIZE]; |
475 |
|
|
ints = (Integer[])q.toArray(ints); |
476 |
|
|
Arrays.sort(ints); |
477 |
|
|
for(int i = 0; i < ints.length; i++) |
478 |
|
|
assertEquals(ints[i], q.pollFirst()); |
479 |
|
|
} |
480 |
|
|
|
481 |
|
|
/** |
482 |
|
|
* iterator iterates through all elements |
483 |
|
|
*/ |
484 |
|
|
public void testIterator() { |
485 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
486 |
|
|
int i = 0; |
487 |
|
|
Iterator it = q.iterator(); |
488 |
|
|
while(it.hasNext()) { |
489 |
|
|
assertTrue(q.contains(it.next())); |
490 |
|
|
++i; |
491 |
|
|
} |
492 |
|
|
assertEquals(i, SIZE); |
493 |
|
|
} |
494 |
|
|
|
495 |
|
|
/** |
496 |
|
|
* iterator of empty set has no elements |
497 |
|
|
*/ |
498 |
|
|
public void testEmptyIterator() { |
499 |
|
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
500 |
|
|
int i = 0; |
501 |
|
|
Iterator it = q.iterator(); |
502 |
|
|
while(it.hasNext()) { |
503 |
|
|
assertTrue(q.contains(it.next())); |
504 |
|
|
++i; |
505 |
|
|
} |
506 |
|
|
assertEquals(i, 0); |
507 |
|
|
} |
508 |
|
|
|
509 |
|
|
/** |
510 |
|
|
* iterator.remove removes current element |
511 |
|
|
*/ |
512 |
|
|
public void testIteratorRemove () { |
513 |
|
|
final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
514 |
|
|
q.add(new Integer(2)); |
515 |
|
|
q.add(new Integer(1)); |
516 |
|
|
q.add(new Integer(3)); |
517 |
|
|
|
518 |
|
|
Iterator it = q.iterator(); |
519 |
|
|
it.next(); |
520 |
|
|
it.remove(); |
521 |
|
|
|
522 |
|
|
it = q.iterator(); |
523 |
|
|
assertEquals(it.next(), new Integer(2)); |
524 |
|
|
assertEquals(it.next(), new Integer(3)); |
525 |
|
|
assertFalse(it.hasNext()); |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
|
529 |
|
|
/** |
530 |
|
|
* toString contains toStrings of elements |
531 |
|
|
*/ |
532 |
|
|
public void testToString() { |
533 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
534 |
|
|
String s = q.toString(); |
535 |
|
|
for (int i = 0; i < SIZE; ++i) { |
536 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
537 |
|
|
} |
538 |
|
|
} |
539 |
|
|
|
540 |
|
|
/** |
541 |
|
|
* A deserialized serialized set has same elements |
542 |
|
|
*/ |
543 |
|
|
public void testSerialization() { |
544 |
|
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
545 |
|
|
try { |
546 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
547 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
548 |
|
|
out.writeObject(q); |
549 |
|
|
out.close(); |
550 |
|
|
|
551 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
552 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
553 |
|
|
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
554 |
|
|
assertEquals(q.size(), r.size()); |
555 |
|
|
while (!q.isEmpty()) |
556 |
|
|
assertEquals(q.pollFirst(), r.pollFirst()); |
557 |
|
|
} catch(Exception e){ |
558 |
|
|
e.printStackTrace(); |
559 |
|
|
unexpectedException(); |
560 |
|
|
} |
561 |
|
|
} |
562 |
|
|
|
563 |
|
|
/** |
564 |
|
|
* subSet returns set with keys in requested range |
565 |
|
|
*/ |
566 |
|
|
public void testSubSetContents() { |
567 |
|
|
ConcurrentSkipListSet set = set5(); |
568 |
|
|
SortedSet sm = set.subSet(two, four); |
569 |
|
|
assertEquals(two, sm.first()); |
570 |
|
|
assertEquals(three, sm.last()); |
571 |
|
|
assertEquals(2, sm.size()); |
572 |
|
|
assertFalse(sm.contains(one)); |
573 |
|
|
assertTrue(sm.contains(two)); |
574 |
|
|
assertTrue(sm.contains(three)); |
575 |
|
|
assertFalse(sm.contains(four)); |
576 |
|
|
assertFalse(sm.contains(five)); |
577 |
|
|
Iterator i = sm.iterator(); |
578 |
|
|
Object k; |
579 |
|
|
k = (Integer)(i.next()); |
580 |
|
|
assertEquals(two, k); |
581 |
|
|
k = (Integer)(i.next()); |
582 |
|
|
assertEquals(three, k); |
583 |
|
|
assertFalse(i.hasNext()); |
584 |
|
|
Iterator j = sm.iterator(); |
585 |
|
|
j.next(); |
586 |
|
|
j.remove(); |
587 |
|
|
assertFalse(set.contains(two)); |
588 |
|
|
assertEquals(4, set.size()); |
589 |
|
|
assertEquals(1, sm.size()); |
590 |
|
|
assertEquals(three, sm.first()); |
591 |
|
|
assertEquals(three, sm.last()); |
592 |
|
|
assertTrue(sm.remove(three)); |
593 |
|
|
assertTrue(sm.isEmpty()); |
594 |
|
|
assertEquals(3, set.size()); |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
public void testSubSetContents2() { |
598 |
|
|
ConcurrentSkipListSet set = set5(); |
599 |
|
|
SortedSet sm = set.subSet(two, three); |
600 |
|
|
assertEquals(1, sm.size()); |
601 |
|
|
assertEquals(two, sm.first()); |
602 |
|
|
assertEquals(two, sm.last()); |
603 |
|
|
assertFalse(sm.contains(one)); |
604 |
|
|
assertTrue(sm.contains(two)); |
605 |
|
|
assertFalse(sm.contains(three)); |
606 |
|
|
assertFalse(sm.contains(four)); |
607 |
|
|
assertFalse(sm.contains(five)); |
608 |
|
|
Iterator i = sm.iterator(); |
609 |
|
|
Object k; |
610 |
|
|
k = (Integer)(i.next()); |
611 |
|
|
assertEquals(two, k); |
612 |
|
|
assertFalse(i.hasNext()); |
613 |
|
|
Iterator j = sm.iterator(); |
614 |
|
|
j.next(); |
615 |
|
|
j.remove(); |
616 |
|
|
assertFalse(set.contains(two)); |
617 |
|
|
assertEquals(4, set.size()); |
618 |
|
|
assertEquals(0, sm.size()); |
619 |
|
|
assertTrue(sm.isEmpty()); |
620 |
|
|
assertFalse(sm.remove(three)); |
621 |
|
|
assertEquals(4, set.size()); |
622 |
|
|
} |
623 |
|
|
|
624 |
|
|
/** |
625 |
|
|
* headSet returns set with keys in requested range |
626 |
|
|
*/ |
627 |
|
|
public void testHeadSetContents() { |
628 |
|
|
ConcurrentSkipListSet set = set5(); |
629 |
|
|
SortedSet sm = set.headSet(four); |
630 |
|
|
assertTrue(sm.contains(one)); |
631 |
|
|
assertTrue(sm.contains(two)); |
632 |
|
|
assertTrue(sm.contains(three)); |
633 |
|
|
assertFalse(sm.contains(four)); |
634 |
|
|
assertFalse(sm.contains(five)); |
635 |
|
|
Iterator i = sm.iterator(); |
636 |
|
|
Object k; |
637 |
|
|
k = (Integer)(i.next()); |
638 |
|
|
assertEquals(one, k); |
639 |
|
|
k = (Integer)(i.next()); |
640 |
|
|
assertEquals(two, k); |
641 |
|
|
k = (Integer)(i.next()); |
642 |
|
|
assertEquals(three, k); |
643 |
|
|
assertFalse(i.hasNext()); |
644 |
|
|
sm.clear(); |
645 |
|
|
assertTrue(sm.isEmpty()); |
646 |
|
|
assertEquals(2, set.size()); |
647 |
|
|
assertEquals(four, set.first()); |
648 |
|
|
} |
649 |
|
|
|
650 |
|
|
/** |
651 |
|
|
* tailSet returns set with keys in requested range |
652 |
|
|
*/ |
653 |
|
|
public void testTailSetContents() { |
654 |
|
|
ConcurrentSkipListSet set = set5(); |
655 |
|
|
SortedSet sm = set.tailSet(two); |
656 |
|
|
assertFalse(sm.contains(one)); |
657 |
|
|
assertTrue(sm.contains(two)); |
658 |
|
|
assertTrue(sm.contains(three)); |
659 |
|
|
assertTrue(sm.contains(four)); |
660 |
|
|
assertTrue(sm.contains(five)); |
661 |
|
|
Iterator i = sm.iterator(); |
662 |
|
|
Object k; |
663 |
|
|
k = (Integer)(i.next()); |
664 |
|
|
assertEquals(two, k); |
665 |
|
|
k = (Integer)(i.next()); |
666 |
|
|
assertEquals(three, k); |
667 |
|
|
k = (Integer)(i.next()); |
668 |
|
|
assertEquals(four, k); |
669 |
|
|
k = (Integer)(i.next()); |
670 |
|
|
assertEquals(five, k); |
671 |
|
|
assertFalse(i.hasNext()); |
672 |
|
|
|
673 |
|
|
SortedSet ssm = sm.tailSet(four); |
674 |
|
|
assertEquals(four, ssm.first()); |
675 |
|
|
assertEquals(five, ssm.last()); |
676 |
|
|
assertTrue(ssm.remove(four)); |
677 |
|
|
assertEquals(1, ssm.size()); |
678 |
|
|
assertEquals(3, sm.size()); |
679 |
|
|
assertEquals(4, set.size()); |
680 |
|
|
} |
681 |
|
|
|
682 |
dl |
1.2 |
Random rnd = new Random(666); |
683 |
|
|
BitSet bs; |
684 |
|
|
|
685 |
|
|
/** |
686 |
|
|
* Subsets of subsets subdivide correctly |
687 |
|
|
*/ |
688 |
|
|
public void testRecursiveSubSets() { |
689 |
|
|
int setSize = 1000; |
690 |
|
|
Class cl = ConcurrentSkipListSet.class; |
691 |
|
|
|
692 |
|
|
NavigableSet<Integer> set = newSet(cl); |
693 |
|
|
bs = new BitSet(setSize); |
694 |
|
|
|
695 |
|
|
populate(set, setSize); |
696 |
|
|
check(set, 0, setSize - 1, true); |
697 |
|
|
check(set.descendingSet(), 0, setSize - 1, false); |
698 |
|
|
|
699 |
|
|
mutateSet(set, 0, setSize - 1); |
700 |
|
|
check(set, 0, setSize - 1, true); |
701 |
|
|
check(set.descendingSet(), 0, setSize - 1, false); |
702 |
|
|
|
703 |
|
|
bashSubSet(set.navigableSubSet(0, true, setSize, false), |
704 |
|
|
0, setSize - 1, true); |
705 |
|
|
} |
706 |
|
|
|
707 |
|
|
static NavigableSet<Integer> newSet(Class cl) { |
708 |
|
|
NavigableSet<Integer> result = null; |
709 |
|
|
try { |
710 |
|
|
result = (NavigableSet<Integer>) cl.newInstance(); |
711 |
|
|
} catch(Exception e) { |
712 |
|
|
fail(); |
713 |
|
|
} |
714 |
|
|
assertEquals(result.size(), 0); |
715 |
|
|
assertFalse(result.iterator().hasNext()); |
716 |
|
|
return result; |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
void populate(NavigableSet<Integer> set, int limit) { |
720 |
|
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
721 |
|
|
int element = rnd.nextInt(limit); |
722 |
|
|
put(set, element); |
723 |
|
|
} |
724 |
|
|
} |
725 |
|
|
|
726 |
|
|
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
727 |
|
|
int size = set.size(); |
728 |
|
|
int rangeSize = max - min + 1; |
729 |
|
|
|
730 |
|
|
// Remove a bunch of entries directly |
731 |
|
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
732 |
|
|
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
733 |
|
|
} |
734 |
|
|
|
735 |
|
|
// Remove a bunch of entries with iterator |
736 |
|
|
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
737 |
|
|
if (rnd.nextBoolean()) { |
738 |
|
|
bs.clear(it.next()); |
739 |
|
|
it.remove(); |
740 |
|
|
} |
741 |
|
|
} |
742 |
|
|
|
743 |
|
|
// Add entries till we're back to original size |
744 |
|
|
while (set.size() < size) { |
745 |
|
|
int element = min + rnd.nextInt(rangeSize); |
746 |
|
|
assertTrue(element >= min && element<= max); |
747 |
|
|
put(set, element); |
748 |
|
|
} |
749 |
|
|
} |
750 |
|
|
|
751 |
|
|
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
752 |
|
|
int size = set.size(); |
753 |
|
|
int rangeSize = max - min + 1; |
754 |
|
|
|
755 |
|
|
// Remove a bunch of entries directly |
756 |
|
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
757 |
|
|
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
758 |
|
|
} |
759 |
|
|
|
760 |
|
|
// Remove a bunch of entries with iterator |
761 |
|
|
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
762 |
|
|
if (rnd.nextBoolean()) { |
763 |
|
|
bs.clear(it.next()); |
764 |
|
|
it.remove(); |
765 |
|
|
} |
766 |
|
|
} |
767 |
|
|
|
768 |
|
|
// Add entries till we're back to original size |
769 |
|
|
while (set.size() < size) { |
770 |
|
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
771 |
|
|
if (element >= min && element<= max) { |
772 |
|
|
put(set, element); |
773 |
|
|
} else { |
774 |
|
|
try { |
775 |
|
|
set.add(element); |
776 |
|
|
fail(); |
777 |
|
|
} catch(IllegalArgumentException e) { |
778 |
|
|
// expected |
779 |
|
|
} |
780 |
|
|
} |
781 |
|
|
} |
782 |
|
|
} |
783 |
|
|
|
784 |
|
|
void put(NavigableSet<Integer> set, int element) { |
785 |
|
|
if (set.add(element)) |
786 |
|
|
bs.set(element); |
787 |
|
|
} |
788 |
|
|
|
789 |
|
|
void remove(NavigableSet<Integer> set, int element) { |
790 |
|
|
if (set.remove(element)) |
791 |
|
|
bs.clear(element); |
792 |
|
|
} |
793 |
|
|
|
794 |
|
|
void bashSubSet(NavigableSet<Integer> set, |
795 |
|
|
int min, int max, boolean ascending) { |
796 |
|
|
check(set, min, max, ascending); |
797 |
|
|
check(set.descendingSet(), min, max, !ascending); |
798 |
|
|
|
799 |
|
|
mutateSubSet(set, min, max); |
800 |
|
|
check(set, min, max, ascending); |
801 |
|
|
check(set.descendingSet(), min, max, !ascending); |
802 |
|
|
|
803 |
|
|
// Recurse |
804 |
|
|
if (max - min < 2) |
805 |
|
|
return; |
806 |
|
|
int midPoint = (min + max) / 2; |
807 |
|
|
|
808 |
|
|
// headSet - pick direction and endpoint inclusion randomly |
809 |
|
|
boolean incl = rnd.nextBoolean(); |
810 |
|
|
NavigableSet<Integer> hm = set.navigableHeadSet(midPoint, incl); |
811 |
|
|
if (ascending) { |
812 |
|
|
if (rnd.nextBoolean()) |
813 |
|
|
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
814 |
|
|
else |
815 |
|
|
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
816 |
|
|
false); |
817 |
|
|
} else { |
818 |
|
|
if (rnd.nextBoolean()) |
819 |
|
|
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
820 |
|
|
else |
821 |
|
|
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
822 |
|
|
true); |
823 |
|
|
} |
824 |
|
|
|
825 |
|
|
// tailSet - pick direction and endpoint inclusion randomly |
826 |
|
|
incl = rnd.nextBoolean(); |
827 |
|
|
NavigableSet<Integer> tm = set.navigableTailSet(midPoint,incl); |
828 |
|
|
if (ascending) { |
829 |
|
|
if (rnd.nextBoolean()) |
830 |
|
|
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
831 |
|
|
else |
832 |
|
|
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
833 |
|
|
false); |
834 |
|
|
} else { |
835 |
|
|
if (rnd.nextBoolean()) { |
836 |
|
|
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
837 |
|
|
} else { |
838 |
|
|
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
839 |
|
|
true); |
840 |
|
|
} |
841 |
|
|
} |
842 |
|
|
|
843 |
|
|
// subSet - pick direction and endpoint inclusion randomly |
844 |
|
|
int rangeSize = max - min + 1; |
845 |
|
|
int[] endpoints = new int[2]; |
846 |
|
|
endpoints[0] = min + rnd.nextInt(rangeSize); |
847 |
|
|
endpoints[1] = min + rnd.nextInt(rangeSize); |
848 |
|
|
Arrays.sort(endpoints); |
849 |
|
|
boolean lowIncl = rnd.nextBoolean(); |
850 |
|
|
boolean highIncl = rnd.nextBoolean(); |
851 |
|
|
if (ascending) { |
852 |
|
|
NavigableSet<Integer> sm = set.navigableSubSet( |
853 |
|
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
854 |
|
|
if (rnd.nextBoolean()) |
855 |
|
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
856 |
|
|
endpoints[1] - (highIncl ? 0 : 1), true); |
857 |
|
|
else |
858 |
|
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
859 |
|
|
endpoints[1] - (highIncl ? 0 : 1), false); |
860 |
|
|
} else { |
861 |
|
|
NavigableSet<Integer> sm = set.navigableSubSet( |
862 |
|
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
863 |
|
|
if (rnd.nextBoolean()) |
864 |
|
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
865 |
|
|
endpoints[1] - (highIncl ? 0 : 1), false); |
866 |
|
|
else |
867 |
|
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
868 |
|
|
endpoints[1] - (highIncl ? 0 : 1), true); |
869 |
|
|
} |
870 |
|
|
} |
871 |
|
|
|
872 |
|
|
/** |
873 |
|
|
* min and max are both inclusive. If max < min, interval is empty. |
874 |
|
|
*/ |
875 |
|
|
void check(NavigableSet<Integer> set, |
876 |
|
|
final int min, final int max, final boolean ascending) { |
877 |
|
|
class ReferenceSet { |
878 |
|
|
int lower(int element) { |
879 |
|
|
return ascending ? |
880 |
|
|
lowerAscending(element) : higherAscending(element); |
881 |
|
|
} |
882 |
|
|
int floor(int element) { |
883 |
|
|
return ascending ? |
884 |
|
|
floorAscending(element) : ceilingAscending(element); |
885 |
|
|
} |
886 |
|
|
int ceiling(int element) { |
887 |
|
|
return ascending ? |
888 |
|
|
ceilingAscending(element) : floorAscending(element); |
889 |
|
|
} |
890 |
|
|
int higher(int element) { |
891 |
|
|
return ascending ? |
892 |
|
|
higherAscending(element) : lowerAscending(element); |
893 |
|
|
} |
894 |
|
|
int first() { |
895 |
|
|
return ascending ? firstAscending() : lastAscending(); |
896 |
|
|
} |
897 |
|
|
int last() { |
898 |
|
|
return ascending ? lastAscending() : firstAscending(); |
899 |
|
|
} |
900 |
|
|
int lowerAscending(int element) { |
901 |
|
|
return floorAscending(element - 1); |
902 |
|
|
} |
903 |
|
|
int floorAscending(int element) { |
904 |
|
|
if (element < min) |
905 |
|
|
return -1; |
906 |
|
|
else if (element > max) |
907 |
|
|
element = max; |
908 |
|
|
|
909 |
|
|
// BitSet should support this! Test would run much faster |
910 |
|
|
while (element >= min) { |
911 |
|
|
if (bs.get(element)) |
912 |
|
|
return(element); |
913 |
|
|
element--; |
914 |
|
|
} |
915 |
|
|
return -1; |
916 |
|
|
} |
917 |
|
|
int ceilingAscending(int element) { |
918 |
|
|
if (element < min) |
919 |
|
|
element = min; |
920 |
|
|
else if (element > max) |
921 |
|
|
return -1; |
922 |
|
|
int result = bs.nextSetBit(element); |
923 |
|
|
return result > max ? -1 : result; |
924 |
|
|
} |
925 |
|
|
int higherAscending(int element) { |
926 |
|
|
return ceilingAscending(element + 1); |
927 |
|
|
} |
928 |
|
|
private int firstAscending() { |
929 |
|
|
int result = ceilingAscending(min); |
930 |
|
|
return result > max ? -1 : result; |
931 |
|
|
} |
932 |
|
|
private int lastAscending() { |
933 |
|
|
int result = floorAscending(max); |
934 |
|
|
return result < min ? -1 : result; |
935 |
|
|
} |
936 |
|
|
} |
937 |
|
|
ReferenceSet rs = new ReferenceSet(); |
938 |
|
|
|
939 |
|
|
// Test contents using containsElement |
940 |
|
|
int size = 0; |
941 |
|
|
for (int i = min; i <= max; i++) { |
942 |
|
|
boolean bsContainsI = bs.get(i); |
943 |
|
|
assertEquals(bsContainsI, set.contains(i)); |
944 |
|
|
if (bsContainsI) |
945 |
|
|
size++; |
946 |
|
|
} |
947 |
|
|
assertEquals(set.size(), size); |
948 |
|
|
|
949 |
|
|
// Test contents using contains elementSet iterator |
950 |
|
|
int size2 = 0; |
951 |
|
|
int previousElement = -1; |
952 |
|
|
for (int element : set) { |
953 |
|
|
assertTrue(bs.get(element)); |
954 |
|
|
size2++; |
955 |
|
|
assertTrue(previousElement < 0 || (ascending ? |
956 |
|
|
element - previousElement > 0 : element - previousElement < 0)); |
957 |
|
|
previousElement = element; |
958 |
|
|
} |
959 |
|
|
assertEquals(size2, size); |
960 |
|
|
|
961 |
|
|
// Test navigation ops |
962 |
|
|
for (int element = min - 1; element <= max + 1; element++) { |
963 |
|
|
assertEq(set.lower(element), rs.lower(element)); |
964 |
|
|
assertEq(set.floor(element), rs.floor(element)); |
965 |
|
|
assertEq(set.higher(element), rs.higher(element)); |
966 |
|
|
assertEq(set.ceiling(element), rs.ceiling(element)); |
967 |
|
|
} |
968 |
|
|
|
969 |
|
|
// Test extrema |
970 |
|
|
if (set.size() != 0) { |
971 |
|
|
assertEq(set.first(), rs.first()); |
972 |
|
|
assertEq(set.last(), rs.last()); |
973 |
|
|
} else { |
974 |
|
|
assertEq(rs.first(), -1); |
975 |
|
|
assertEq(rs.last(), -1); |
976 |
|
|
try { |
977 |
|
|
set.first(); |
978 |
|
|
fail(); |
979 |
|
|
} catch(NoSuchElementException e) { |
980 |
|
|
// expected |
981 |
|
|
} |
982 |
|
|
try { |
983 |
|
|
set.last(); |
984 |
|
|
fail(); |
985 |
|
|
} catch(NoSuchElementException e) { |
986 |
|
|
// expected |
987 |
|
|
} |
988 |
|
|
} |
989 |
|
|
} |
990 |
|
|
|
991 |
|
|
static void assertEq(Integer i, int j) { |
992 |
|
|
if (i == null) |
993 |
|
|
assertEquals(j, -1); |
994 |
|
|
else |
995 |
|
|
assertEquals((int) i, j); |
996 |
|
|
} |
997 |
|
|
|
998 |
|
|
static boolean eq(Integer i, int j) { |
999 |
|
|
return i == null ? j == -1 : i == j; |
1000 |
|
|
} |
1001 |
|
|
|
1002 |
dl |
1.1 |
} |