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