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