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 |
> |
junit.textui.TestRunner.run(suite()); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
< |
return new TestSuite(ConcurrentSkipListSetTest.class); |
27 |
> |
return new TestSuite(ConcurrentSkipListSetTest.class); |
28 |
|
} |
29 |
|
|
30 |
< |
static class MyReverseComparator implements Comparator { |
30 |
> |
static class MyReverseComparator implements Comparator { |
31 |
|
public int compare(Object x, Object y) { |
32 |
< |
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; |
32 |
> |
return ((Comparable)y).compareTo(x); |
33 |
|
} |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
< |
* Create a set of given size containing consecutive |
37 |
> |
* Returns a new set of given size containing consecutive |
38 |
|
* Integers 0 ... n. |
39 |
|
*/ |
40 |
< |
private ConcurrentSkipListSet populatedSet(int n) { |
41 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
40 |
> |
private ConcurrentSkipListSet<Integer> populatedSet(int n) { |
41 |
> |
ConcurrentSkipListSet<Integer> q = |
42 |
> |
new ConcurrentSkipListSet<Integer>(); |
43 |
|
assertTrue(q.isEmpty()); |
44 |
< |
for(int i = n-1; i >= 0; i-=2) |
45 |
< |
assertTrue(q.add(new Integer(i))); |
46 |
< |
for(int i = (n & 1); i < n; i+=2) |
47 |
< |
assertTrue(q.add(new Integer(i))); |
44 |
> |
for (int i = n-1; i >= 0; i -= 2) |
45 |
> |
assertTrue(q.add(new Integer(i))); |
46 |
> |
for (int i = (n & 1); i < n; i += 2) |
47 |
> |
assertTrue(q.add(new Integer(i))); |
48 |
|
assertFalse(q.isEmpty()); |
49 |
< |
assertEquals(n, q.size()); |
49 |
> |
assertEquals(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(); |
61 |
|
q.add(three); |
62 |
|
q.add(four); |
63 |
|
q.add(five); |
64 |
< |
assertEquals(5, q.size()); |
64 |
> |
assertEquals(5, q.size()); |
65 |
|
return q; |
66 |
|
} |
67 |
< |
|
67 |
> |
|
68 |
|
/** |
69 |
|
* A new set has unbounded capacity |
70 |
|
*/ |
79 |
|
try { |
80 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); |
81 |
|
shouldThrow(); |
82 |
< |
} |
76 |
< |
catch (NullPointerException success) {} |
82 |
> |
} catch (NullPointerException success) {} |
83 |
|
} |
84 |
|
|
85 |
|
/** |
90 |
|
Integer[] ints = new Integer[SIZE]; |
91 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
92 |
|
shouldThrow(); |
93 |
< |
} |
88 |
< |
catch (NullPointerException success) {} |
93 |
> |
} catch (NullPointerException success) {} |
94 |
|
} |
95 |
|
|
96 |
|
/** |
103 |
|
ints[i] = new Integer(i); |
104 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
105 |
|
shouldThrow(); |
106 |
< |
} |
102 |
< |
catch (NullPointerException success) {} |
106 |
> |
} catch (NullPointerException success) {} |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
|
* Set contains all elements of collection used to initialize |
111 |
|
*/ |
112 |
|
public void testConstructor6() { |
113 |
< |
try { |
114 |
< |
Integer[] ints = new Integer[SIZE]; |
115 |
< |
for (int i = 0; i < SIZE; ++i) |
116 |
< |
ints[i] = new Integer(i); |
117 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
118 |
< |
for (int i = 0; i < SIZE; ++i) |
115 |
< |
assertEquals(ints[i], q.pollFirst()); |
116 |
< |
} |
117 |
< |
finally {} |
113 |
> |
Integer[] ints = new Integer[SIZE]; |
114 |
> |
for (int i = 0; i < SIZE; ++i) |
115 |
> |
ints[i] = new Integer(i); |
116 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
117 |
> |
for (int i = 0; i < SIZE; ++i) |
118 |
> |
assertEquals(ints[i], q.pollFirst()); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
|
* The comparator used in constructor is used |
123 |
|
*/ |
124 |
|
public void testConstructor7() { |
125 |
< |
try { |
126 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
127 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
128 |
< |
assertEquals(cmp, q.comparator()); |
129 |
< |
Integer[] ints = new Integer[SIZE]; |
130 |
< |
for (int i = 0; i < SIZE; ++i) |
131 |
< |
ints[i] = new Integer(i); |
132 |
< |
q.addAll(Arrays.asList(ints)); |
133 |
< |
for (int i = SIZE-1; i >= 0; --i) |
133 |
< |
assertEquals(ints[i], q.pollFirst()); |
134 |
< |
} |
135 |
< |
finally {} |
125 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
126 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
127 |
> |
assertEquals(cmp, q.comparator()); |
128 |
> |
Integer[] ints = new Integer[SIZE]; |
129 |
> |
for (int i = 0; i < SIZE; ++i) |
130 |
> |
ints[i] = new Integer(i); |
131 |
> |
q.addAll(Arrays.asList(ints)); |
132 |
> |
for (int i = SIZE-1; i >= 0; --i) |
133 |
> |
assertEquals(ints[i], q.pollFirst()); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
166 |
|
* add(null) throws NPE |
167 |
|
*/ |
168 |
|
public void testAddNull() { |
169 |
< |
try { |
169 |
> |
try { |
170 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
171 |
|
q.add(null); |
172 |
|
shouldThrow(); |
173 |
< |
} catch (NullPointerException success) { } |
173 |
> |
} catch (NullPointerException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
201 |
|
q.add(new Object()); |
202 |
|
q.add(new Object()); |
203 |
|
shouldThrow(); |
204 |
< |
} |
207 |
< |
catch(ClassCastException success) {} |
204 |
> |
} catch (ClassCastException success) {} |
205 |
|
} |
206 |
|
|
207 |
|
/** |
212 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
213 |
|
q.addAll(null); |
214 |
|
shouldThrow(); |
215 |
< |
} |
219 |
< |
catch (NullPointerException success) {} |
215 |
> |
} catch (NullPointerException success) {} |
216 |
|
} |
217 |
+ |
|
218 |
|
/** |
219 |
|
* addAll of a collection with null elements throws NPE |
220 |
|
*/ |
224 |
|
Integer[] ints = new Integer[SIZE]; |
225 |
|
q.addAll(Arrays.asList(ints)); |
226 |
|
shouldThrow(); |
227 |
< |
} |
231 |
< |
catch (NullPointerException success) {} |
227 |
> |
} catch (NullPointerException success) {} |
228 |
|
} |
229 |
+ |
|
230 |
|
/** |
231 |
|
* addAll of a collection with any null elements throws NPE after |
232 |
|
* possibly adding some elements |
239 |
|
ints[i] = new Integer(i); |
240 |
|
q.addAll(Arrays.asList(ints)); |
241 |
|
shouldThrow(); |
242 |
< |
} |
246 |
< |
catch (NullPointerException success) {} |
242 |
> |
} catch (NullPointerException success) {} |
243 |
|
} |
244 |
|
|
245 |
|
/** |
246 |
|
* Set contains all elements of successful addAll |
247 |
|
*/ |
248 |
|
public void testAddAll5() { |
249 |
< |
try { |
250 |
< |
Integer[] empty = new Integer[0]; |
251 |
< |
Integer[] ints = new Integer[SIZE]; |
252 |
< |
for (int i = 0; i < SIZE; ++i) |
253 |
< |
ints[i] = new Integer(SIZE-1-i); |
254 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
255 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
256 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
257 |
< |
for (int i = 0; i < SIZE; ++i) |
262 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
263 |
< |
} |
264 |
< |
finally {} |
249 |
> |
Integer[] empty = new Integer[0]; |
250 |
> |
Integer[] ints = new Integer[SIZE]; |
251 |
> |
for (int i = 0; i < SIZE; ++i) |
252 |
> |
ints[i] = new Integer(SIZE-1-i); |
253 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
254 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
255 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
256 |
> |
for (int i = 0; i < SIZE; ++i) |
257 |
> |
assertEquals(i, q.pollFirst()); |
258 |
|
} |
259 |
|
|
260 |
|
/** |
263 |
|
public void testPollFirst() { |
264 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
265 |
|
for (int i = 0; i < SIZE; ++i) { |
266 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
266 |
> |
assertEquals(i, q.pollFirst()); |
267 |
|
} |
268 |
< |
assertNull(q.pollFirst()); |
268 |
> |
assertNull(q.pollFirst()); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
274 |
|
public void testPollLast() { |
275 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
276 |
|
for (int i = SIZE-1; i >= 0; --i) { |
277 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
277 |
> |
assertEquals(i, q.pollLast()); |
278 |
|
} |
279 |
< |
assertNull(q.pollFirst()); |
279 |
> |
assertNull(q.pollFirst()); |
280 |
|
} |
281 |
|
|
289 |
– |
|
282 |
|
/** |
283 |
|
* remove(x) removes x and returns true if present |
284 |
|
*/ |
285 |
|
public void testRemoveElement() { |
286 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
287 |
< |
for (int i = 1; i < SIZE; i+=2) { |
288 |
< |
assertTrue(q.remove(new Integer(i))); |
289 |
< |
} |
290 |
< |
for (int i = 0; i < SIZE; i+=2) { |
291 |
< |
assertTrue(q.remove(new Integer(i))); |
292 |
< |
assertFalse(q.remove(new Integer(i+1))); |
287 |
> |
for (int i = 1; i < SIZE; i += 2) { |
288 |
> |
assertTrue(q.contains(i)); |
289 |
> |
assertTrue(q.remove(i)); |
290 |
> |
assertFalse(q.contains(i)); |
291 |
> |
assertTrue(q.contains(i-1)); |
292 |
> |
} |
293 |
> |
for (int i = 0; i < SIZE; i += 2) { |
294 |
> |
assertTrue(q.contains(i)); |
295 |
> |
assertTrue(q.remove(i)); |
296 |
> |
assertFalse(q.contains(i)); |
297 |
> |
assertFalse(q.remove(i+1)); |
298 |
> |
assertFalse(q.contains(i+1)); |
299 |
|
} |
300 |
|
assertTrue(q.isEmpty()); |
301 |
|
} |
302 |
< |
|
302 |
> |
|
303 |
|
/** |
304 |
|
* contains(x) reports true when elements added but not yet removed |
305 |
|
*/ |
375 |
|
} |
376 |
|
} |
377 |
|
|
380 |
– |
|
381 |
– |
|
378 |
|
/** |
379 |
|
* lower returns preceding element |
380 |
|
*/ |
391 |
|
|
392 |
|
Object e4 = q.lower(zero); |
393 |
|
assertNull(e4); |
398 |
– |
|
394 |
|
} |
395 |
|
|
396 |
|
/** |
409 |
|
|
410 |
|
Object e4 = q.higher(six); |
411 |
|
assertNull(e4); |
417 |
– |
|
412 |
|
} |
413 |
|
|
414 |
|
/** |
427 |
|
|
428 |
|
Object e4 = q.floor(zero); |
429 |
|
assertNull(e4); |
436 |
– |
|
430 |
|
} |
431 |
|
|
432 |
|
/** |
445 |
|
|
446 |
|
Object e4 = q.ceiling(six); |
447 |
|
assertNull(e4); |
455 |
– |
|
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
< |
* toArray contains all elements |
451 |
> |
* toArray contains all elements in sorted order |
452 |
|
*/ |
453 |
|
public void testToArray() { |
454 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
455 |
< |
Object[] o = q.toArray(); |
456 |
< |
Arrays.sort(o); |
457 |
< |
for(int i = 0; i < o.length; i++) |
466 |
< |
assertEquals(o[i], q.pollFirst()); |
455 |
> |
Object[] o = q.toArray(); |
456 |
> |
for (int i = 0; i < o.length; i++) |
457 |
> |
assertSame(o[i], q.pollFirst()); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* toArray(a) contains all elements |
461 |
> |
* toArray(a) contains all elements in sorted order |
462 |
|
*/ |
463 |
|
public void testToArray2() { |
464 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
465 |
< |
Integer[] ints = new Integer[SIZE]; |
466 |
< |
ints = (Integer[])q.toArray(ints); |
467 |
< |
Arrays.sort(ints); |
468 |
< |
for(int i = 0; i < ints.length; i++) |
478 |
< |
assertEquals(ints[i], q.pollFirst()); |
464 |
> |
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
465 |
> |
Integer[] ints = new Integer[SIZE]; |
466 |
> |
assertSame(ints, q.toArray(ints)); |
467 |
> |
for (int i = 0; i < ints.length; i++) |
468 |
> |
assertSame(ints[i], q.pollFirst()); |
469 |
|
} |
470 |
< |
|
470 |
> |
|
471 |
|
/** |
472 |
|
* iterator iterates through all elements |
473 |
|
*/ |
474 |
|
public void testIterator() { |
475 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
476 |
|
int i = 0; |
477 |
< |
Iterator it = q.iterator(); |
478 |
< |
while(it.hasNext()) { |
477 |
> |
Iterator it = q.iterator(); |
478 |
> |
while (it.hasNext()) { |
479 |
|
assertTrue(q.contains(it.next())); |
480 |
|
++i; |
481 |
|
} |
488 |
|
public void testEmptyIterator() { |
489 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
490 |
|
int i = 0; |
491 |
< |
Iterator it = q.iterator(); |
492 |
< |
while(it.hasNext()) { |
491 |
> |
Iterator it = q.iterator(); |
492 |
> |
while (it.hasNext()) { |
493 |
|
assertTrue(q.contains(it.next())); |
494 |
|
++i; |
495 |
|
} |
496 |
< |
assertEquals(i, 0); |
496 |
> |
assertEquals(0, i); |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
|
* iterator.remove removes current element |
501 |
|
*/ |
502 |
< |
public void testIteratorRemove () { |
502 |
> |
public void testIteratorRemove() { |
503 |
|
final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
504 |
|
q.add(new Integer(2)); |
505 |
|
q.add(new Integer(1)); |
515 |
|
assertFalse(it.hasNext()); |
516 |
|
} |
517 |
|
|
528 |
– |
|
518 |
|
/** |
519 |
|
* toString contains toStrings of elements |
520 |
|
*/ |
522 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
523 |
|
String s = q.toString(); |
524 |
|
for (int i = 0; i < SIZE; ++i) { |
525 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
525 |
> |
assertTrue(s.contains(String.valueOf(i))); |
526 |
|
} |
527 |
< |
} |
527 |
> |
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* A deserialized serialized set has same elements |
530 |
> |
* A deserialized serialized set has same elements |
531 |
|
*/ |
532 |
< |
public void testSerialization() { |
533 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
534 |
< |
try { |
535 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
536 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
537 |
< |
out.writeObject(q); |
538 |
< |
out.close(); |
539 |
< |
|
540 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
541 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
542 |
< |
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(); |
532 |
> |
public void testSerialization() throws Exception { |
533 |
> |
NavigableSet x = populatedSet(SIZE); |
534 |
> |
NavigableSet y = serialClone(x); |
535 |
> |
|
536 |
> |
assertNotSame(x, y); |
537 |
> |
assertEquals(x.size(), y.size()); |
538 |
> |
assertEquals(x, y); |
539 |
> |
assertEquals(y, x); |
540 |
> |
while (!x.isEmpty()) { |
541 |
> |
assertFalse(y.isEmpty()); |
542 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
543 |
|
} |
544 |
+ |
assertTrue(y.isEmpty()); |
545 |
|
} |
546 |
|
|
547 |
|
/** |
664 |
|
} |
665 |
|
|
666 |
|
Random rnd = new Random(666); |
683 |
– |
BitSet bs; |
667 |
|
|
668 |
|
/** |
669 |
|
* Subsets of subsets subdivide correctly |
670 |
|
*/ |
671 |
< |
public void testRecursiveSubSets() { |
672 |
< |
int setSize = 1000; |
673 |
< |
Class cl = ConcurrentSkipListSet.class; |
671 |
> |
public void testRecursiveSubSets() throws Exception { |
672 |
> |
int setSize = expensiveTests ? 1000 : 100; |
673 |
> |
Class cl = ConcurrentSkipListSet.class; |
674 |
|
|
675 |
|
NavigableSet<Integer> set = newSet(cl); |
676 |
< |
bs = new BitSet(setSize); |
676 |
> |
BitSet bs = new BitSet(setSize); |
677 |
|
|
678 |
< |
populate(set, setSize); |
679 |
< |
check(set, 0, setSize - 1, true); |
680 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
681 |
< |
|
682 |
< |
mutateSet(set, 0, setSize - 1); |
683 |
< |
check(set, 0, setSize - 1, true); |
684 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
678 |
> |
populate(set, setSize, bs); |
679 |
> |
check(set, 0, setSize - 1, true, bs); |
680 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
681 |
> |
|
682 |
> |
mutateSet(set, 0, setSize - 1, bs); |
683 |
> |
check(set, 0, setSize - 1, true, bs); |
684 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
685 |
|
|
686 |
|
bashSubSet(set.subSet(0, true, setSize, false), |
687 |
< |
0, setSize - 1, true); |
687 |
> |
0, setSize - 1, true, bs); |
688 |
|
} |
689 |
|
|
690 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
691 |
< |
NavigableSet<Integer> result = null; |
692 |
< |
try { |
693 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
694 |
< |
} catch(Exception e) { |
695 |
< |
fail(); |
696 |
< |
} |
697 |
< |
assertEquals(result.size(), 0); |
690 |
> |
/** |
691 |
> |
* addAll is idempotent |
692 |
> |
*/ |
693 |
> |
public void testAddAll_idempotent() throws Exception { |
694 |
> |
Set x = populatedSet(SIZE); |
695 |
> |
Set y = new ConcurrentSkipListSet(x); |
696 |
> |
y.addAll(x); |
697 |
> |
assertEquals(x, y); |
698 |
> |
assertEquals(y, x); |
699 |
> |
} |
700 |
> |
|
701 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
702 |
> |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
703 |
> |
assertEquals(0, result.size()); |
704 |
|
assertFalse(result.iterator().hasNext()); |
705 |
|
return result; |
706 |
|
} |
707 |
|
|
708 |
< |
void populate(NavigableSet<Integer> set, int limit) { |
708 |
> |
void populate(NavigableSet<Integer> set, int limit, BitSet bs) { |
709 |
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
710 |
|
int element = rnd.nextInt(limit); |
711 |
< |
put(set, element); |
711 |
> |
put(set, element, bs); |
712 |
|
} |
713 |
|
} |
714 |
|
|
715 |
< |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
715 |
> |
void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) { |
716 |
|
int size = set.size(); |
717 |
|
int rangeSize = max - min + 1; |
718 |
|
|
719 |
|
// Remove a bunch of entries directly |
720 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
721 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
721 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
722 |
|
} |
723 |
|
|
724 |
|
// Remove a bunch of entries with iterator |
725 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
725 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
726 |
|
if (rnd.nextBoolean()) { |
727 |
|
bs.clear(it.next()); |
728 |
|
it.remove(); |
733 |
|
while (set.size() < size) { |
734 |
|
int element = min + rnd.nextInt(rangeSize); |
735 |
|
assertTrue(element >= min && element<= max); |
736 |
< |
put(set, element); |
736 |
> |
put(set, element, bs); |
737 |
|
} |
738 |
|
} |
739 |
|
|
740 |
< |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
740 |
> |
void mutateSubSet(NavigableSet<Integer> set, int min, int max, |
741 |
> |
BitSet bs) { |
742 |
|
int size = set.size(); |
743 |
|
int rangeSize = max - min + 1; |
744 |
|
|
745 |
|
// Remove a bunch of entries directly |
746 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
747 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
747 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
748 |
|
} |
749 |
|
|
750 |
|
// Remove a bunch of entries with iterator |
751 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
751 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
752 |
|
if (rnd.nextBoolean()) { |
753 |
|
bs.clear(it.next()); |
754 |
|
it.remove(); |
759 |
|
while (set.size() < size) { |
760 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
761 |
|
if (element >= min && element<= max) { |
762 |
< |
put(set, element); |
762 |
> |
put(set, element, bs); |
763 |
|
} else { |
764 |
|
try { |
765 |
|
set.add(element); |
766 |
< |
fail(); |
767 |
< |
} catch(IllegalArgumentException e) { |
778 |
< |
// expected |
779 |
< |
} |
766 |
> |
shouldThrow(); |
767 |
> |
} catch (IllegalArgumentException success) {} |
768 |
|
} |
769 |
|
} |
770 |
|
} |
771 |
|
|
772 |
< |
void put(NavigableSet<Integer> set, int element) { |
772 |
> |
void put(NavigableSet<Integer> set, int element, BitSet bs) { |
773 |
|
if (set.add(element)) |
774 |
|
bs.set(element); |
775 |
|
} |
776 |
|
|
777 |
< |
void remove(NavigableSet<Integer> set, int element) { |
777 |
> |
void remove(NavigableSet<Integer> set, int element, BitSet bs) { |
778 |
|
if (set.remove(element)) |
779 |
|
bs.clear(element); |
780 |
|
} |
781 |
|
|
782 |
|
void bashSubSet(NavigableSet<Integer> set, |
783 |
< |
int min, int max, boolean ascending) { |
784 |
< |
check(set, min, max, ascending); |
785 |
< |
check(set.descendingSet(), min, max, !ascending); |
786 |
< |
|
787 |
< |
mutateSubSet(set, min, max); |
788 |
< |
check(set, min, max, ascending); |
789 |
< |
check(set.descendingSet(), min, max, !ascending); |
783 |
> |
int min, int max, boolean ascending, |
784 |
> |
BitSet bs) { |
785 |
> |
check(set, min, max, ascending, bs); |
786 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
787 |
> |
|
788 |
> |
mutateSubSet(set, min, max, bs); |
789 |
> |
check(set, min, max, ascending, bs); |
790 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
791 |
|
|
792 |
|
// Recurse |
793 |
|
if (max - min < 2) |
799 |
|
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
800 |
|
if (ascending) { |
801 |
|
if (rnd.nextBoolean()) |
802 |
< |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
802 |
> |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); |
803 |
|
else |
804 |
|
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
805 |
< |
false); |
805 |
> |
false, bs); |
806 |
|
} else { |
807 |
|
if (rnd.nextBoolean()) |
808 |
< |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
808 |
> |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs); |
809 |
|
else |
810 |
|
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
811 |
< |
true); |
811 |
> |
true, bs); |
812 |
|
} |
813 |
|
|
814 |
|
// tailSet - pick direction and endpoint inclusion randomly |
816 |
|
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
817 |
|
if (ascending) { |
818 |
|
if (rnd.nextBoolean()) |
819 |
< |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
819 |
> |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); |
820 |
|
else |
821 |
|
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
822 |
< |
false); |
822 |
> |
false, bs); |
823 |
|
} else { |
824 |
|
if (rnd.nextBoolean()) { |
825 |
< |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
825 |
> |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs); |
826 |
|
} else { |
827 |
|
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
828 |
< |
true); |
828 |
> |
true, bs); |
829 |
|
} |
830 |
|
} |
831 |
|
|
842 |
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
843 |
|
if (rnd.nextBoolean()) |
844 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
845 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
845 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
846 |
|
else |
847 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
848 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
848 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
849 |
|
} else { |
850 |
|
NavigableSet<Integer> sm = set.subSet( |
851 |
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
852 |
|
if (rnd.nextBoolean()) |
853 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
854 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
854 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
855 |
|
else |
856 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
857 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
857 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
858 |
|
} |
859 |
|
} |
860 |
|
|
862 |
|
* min and max are both inclusive. If max < min, interval is empty. |
863 |
|
*/ |
864 |
|
void check(NavigableSet<Integer> set, |
865 |
< |
final int min, final int max, final boolean ascending) { |
866 |
< |
class ReferenceSet { |
865 |
> |
final int min, final int max, final boolean ascending, |
866 |
> |
final BitSet bs) { |
867 |
> |
class ReferenceSet { |
868 |
|
int lower(int element) { |
869 |
|
return ascending ? |
870 |
|
lowerAscending(element) : higherAscending(element); |
899 |
|
// BitSet should support this! Test would run much faster |
900 |
|
while (element >= min) { |
901 |
|
if (bs.get(element)) |
902 |
< |
return(element); |
902 |
> |
return element; |
903 |
|
element--; |
904 |
|
} |
905 |
|
return -1; |
934 |
|
if (bsContainsI) |
935 |
|
size++; |
936 |
|
} |
937 |
< |
assertEquals(set.size(), size); |
937 |
> |
assertEquals(size, set.size()); |
938 |
|
|
939 |
|
// Test contents using contains elementSet iterator |
940 |
|
int size2 = 0; |
965 |
|
assertEq(rs.last(), -1); |
966 |
|
try { |
967 |
|
set.first(); |
968 |
< |
fail(); |
969 |
< |
} catch(NoSuchElementException e) { |
980 |
< |
// expected |
981 |
< |
} |
968 |
> |
shouldThrow(); |
969 |
> |
} catch (NoSuchElementException success) {} |
970 |
|
try { |
971 |
|
set.last(); |
972 |
< |
fail(); |
973 |
< |
} catch(NoSuchElementException e) { |
986 |
< |
// expected |
987 |
< |
} |
972 |
> |
shouldThrow(); |
973 |
> |
} catch (NoSuchElementException success) {} |
974 |
|
} |
975 |
|
} |
976 |
|
|