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.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.BitSet; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Comparator; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NavigableSet; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.SortedSet; |
18 |
> |
import java.util.concurrent.ConcurrentSkipListSet; |
19 |
|
|
20 |
|
public class ConcurrentSkipListSetTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run (suite()); |
22 |
> |
junit.textui.TestRunner.run(suite()); |
23 |
|
} |
24 |
|
public static Test suite() { |
25 |
|
return new TestSuite(ConcurrentSkipListSetTest.class); |
27 |
|
|
28 |
|
static class MyReverseComparator implements Comparator { |
29 |
|
public int compare(Object x, Object y) { |
30 |
< |
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; |
30 |
> |
return ((Comparable)y).compareTo(x); |
31 |
|
} |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* Create a set of given size containing consecutive |
35 |
> |
* Returns a new set of given size containing consecutive |
36 |
|
* Integers 0 ... n. |
37 |
|
*/ |
38 |
< |
private ConcurrentSkipListSet populatedSet(int n) { |
39 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
38 |
> |
private ConcurrentSkipListSet<Integer> populatedSet(int n) { |
39 |
> |
ConcurrentSkipListSet<Integer> q = |
40 |
> |
new ConcurrentSkipListSet<Integer>(); |
41 |
|
assertTrue(q.isEmpty()); |
42 |
|
for (int i = n-1; i >= 0; i-=2) |
43 |
|
assertTrue(q.add(new Integer(i))); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
52 |
< |
* Create set of first 5 ints |
52 |
> |
* Returns a new set of first 5 ints. |
53 |
|
*/ |
54 |
|
private ConcurrentSkipListSet set5() { |
55 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
77 |
|
try { |
78 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); |
79 |
|
shouldThrow(); |
80 |
< |
} |
76 |
< |
catch (NullPointerException success) {} |
80 |
> |
} catch (NullPointerException success) {} |
81 |
|
} |
82 |
|
|
83 |
|
/** |
88 |
|
Integer[] ints = new Integer[SIZE]; |
89 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
90 |
|
shouldThrow(); |
91 |
< |
} |
88 |
< |
catch (NullPointerException success) {} |
91 |
> |
} catch (NullPointerException success) {} |
92 |
|
} |
93 |
|
|
94 |
|
/** |
101 |
|
ints[i] = new Integer(i); |
102 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
103 |
|
shouldThrow(); |
104 |
< |
} |
102 |
< |
catch (NullPointerException success) {} |
104 |
> |
} catch (NullPointerException success) {} |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
|
* Set contains all elements of collection used to initialize |
109 |
|
*/ |
110 |
|
public void testConstructor6() { |
111 |
< |
try { |
112 |
< |
Integer[] ints = new Integer[SIZE]; |
113 |
< |
for (int i = 0; i < SIZE; ++i) |
114 |
< |
ints[i] = new Integer(i); |
115 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
116 |
< |
for (int i = 0; i < SIZE; ++i) |
115 |
< |
assertEquals(ints[i], q.pollFirst()); |
116 |
< |
} |
117 |
< |
finally {} |
111 |
> |
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 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
|
* The comparator used in constructor is used |
121 |
|
*/ |
122 |
|
public void testConstructor7() { |
123 |
< |
try { |
124 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
125 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
126 |
< |
assertEquals(cmp, q.comparator()); |
127 |
< |
Integer[] ints = new Integer[SIZE]; |
128 |
< |
for (int i = 0; i < SIZE; ++i) |
129 |
< |
ints[i] = new Integer(i); |
130 |
< |
q.addAll(Arrays.asList(ints)); |
131 |
< |
for (int i = SIZE-1; i >= 0; --i) |
133 |
< |
assertEquals(ints[i], q.pollFirst()); |
134 |
< |
} |
135 |
< |
finally {} |
123 |
> |
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 |
> |
for (int i = SIZE-1; i >= 0; --i) |
131 |
> |
assertEquals(ints[i], q.pollFirst()); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
168 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
169 |
|
q.add(null); |
170 |
|
shouldThrow(); |
171 |
< |
} catch (NullPointerException success) { } |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
199 |
|
q.add(new Object()); |
200 |
|
q.add(new Object()); |
201 |
|
shouldThrow(); |
202 |
< |
} |
207 |
< |
catch (ClassCastException success) {} |
202 |
> |
} catch (ClassCastException success) {} |
203 |
|
} |
204 |
|
|
205 |
|
/** |
210 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
211 |
|
q.addAll(null); |
212 |
|
shouldThrow(); |
213 |
< |
} |
219 |
< |
catch (NullPointerException success) {} |
213 |
> |
} catch (NullPointerException success) {} |
214 |
|
} |
215 |
+ |
|
216 |
|
/** |
217 |
|
* addAll of a collection with null elements throws NPE |
218 |
|
*/ |
222 |
|
Integer[] ints = new Integer[SIZE]; |
223 |
|
q.addAll(Arrays.asList(ints)); |
224 |
|
shouldThrow(); |
225 |
< |
} |
231 |
< |
catch (NullPointerException success) {} |
225 |
> |
} catch (NullPointerException success) {} |
226 |
|
} |
227 |
+ |
|
228 |
|
/** |
229 |
|
* addAll of a collection with any null elements throws NPE after |
230 |
|
* possibly adding some elements |
237 |
|
ints[i] = new Integer(i); |
238 |
|
q.addAll(Arrays.asList(ints)); |
239 |
|
shouldThrow(); |
240 |
< |
} |
246 |
< |
catch (NullPointerException success) {} |
240 |
> |
} catch (NullPointerException success) {} |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
|
* Set contains all elements of successful addAll |
245 |
|
*/ |
246 |
|
public void testAddAll5() { |
247 |
< |
try { |
248 |
< |
Integer[] empty = new Integer[0]; |
249 |
< |
Integer[] ints = new Integer[SIZE]; |
250 |
< |
for (int i = 0; i < SIZE; ++i) |
251 |
< |
ints[i] = new Integer(SIZE-1-i); |
252 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
253 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
254 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
255 |
< |
for (int i = 0; i < SIZE; ++i) |
262 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
263 |
< |
} |
264 |
< |
finally {} |
247 |
> |
Integer[] empty = new Integer[0]; |
248 |
> |
Integer[] ints = new Integer[SIZE]; |
249 |
> |
for (int i = 0; i < SIZE; ++i) |
250 |
> |
ints[i] = new Integer(SIZE-1-i); |
251 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
252 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
253 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
254 |
> |
for (int i = 0; i < SIZE; ++i) |
255 |
> |
assertEquals(i, q.pollFirst()); |
256 |
|
} |
257 |
|
|
258 |
|
/** |
261 |
|
public void testPollFirst() { |
262 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
263 |
|
for (int i = 0; i < SIZE; ++i) { |
264 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
264 |
> |
assertEquals(i, q.pollFirst()); |
265 |
|
} |
266 |
|
assertNull(q.pollFirst()); |
267 |
|
} |
272 |
|
public void testPollLast() { |
273 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
274 |
|
for (int i = SIZE-1; i >= 0; --i) { |
275 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
275 |
> |
assertEquals(i, q.pollLast()); |
276 |
|
} |
277 |
|
assertNull(q.pollFirst()); |
278 |
|
} |
279 |
|
|
289 |
– |
|
280 |
|
/** |
281 |
|
* remove(x) removes x and returns true if present |
282 |
|
*/ |
283 |
|
public void testRemoveElement() { |
284 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
285 |
|
for (int i = 1; i < SIZE; i+=2) { |
286 |
< |
assertTrue(q.remove(new Integer(i))); |
286 |
> |
assertTrue(q.contains(i)); |
287 |
> |
assertTrue(q.remove(i)); |
288 |
> |
assertFalse(q.contains(i)); |
289 |
> |
assertTrue(q.contains(i-1)); |
290 |
|
} |
291 |
|
for (int i = 0; i < SIZE; i+=2) { |
292 |
< |
assertTrue(q.remove(new Integer(i))); |
293 |
< |
assertFalse(q.remove(new Integer(i+1))); |
292 |
> |
assertTrue(q.contains(i)); |
293 |
> |
assertTrue(q.remove(i)); |
294 |
> |
assertFalse(q.contains(i)); |
295 |
> |
assertFalse(q.remove(i+1)); |
296 |
> |
assertFalse(q.contains(i+1)); |
297 |
|
} |
298 |
|
assertTrue(q.isEmpty()); |
299 |
|
} |
373 |
|
} |
374 |
|
} |
375 |
|
|
380 |
– |
|
381 |
– |
|
376 |
|
/** |
377 |
|
* lower returns preceding element |
378 |
|
*/ |
389 |
|
|
390 |
|
Object e4 = q.lower(zero); |
391 |
|
assertNull(e4); |
398 |
– |
|
392 |
|
} |
393 |
|
|
394 |
|
/** |
407 |
|
|
408 |
|
Object e4 = q.higher(six); |
409 |
|
assertNull(e4); |
417 |
– |
|
410 |
|
} |
411 |
|
|
412 |
|
/** |
425 |
|
|
426 |
|
Object e4 = q.floor(zero); |
427 |
|
assertNull(e4); |
436 |
– |
|
428 |
|
} |
429 |
|
|
430 |
|
/** |
443 |
|
|
444 |
|
Object e4 = q.ceiling(six); |
445 |
|
assertNull(e4); |
455 |
– |
|
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
< |
* toArray contains all elements |
449 |
> |
* toArray contains all elements in sorted order |
450 |
|
*/ |
451 |
|
public void testToArray() { |
452 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
453 |
|
Object[] o = q.toArray(); |
464 |
– |
Arrays.sort(o); |
454 |
|
for (int i = 0; i < o.length; i++) |
455 |
< |
assertEquals(o[i], q.pollFirst()); |
455 |
> |
assertSame(o[i], q.pollFirst()); |
456 |
|
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* toArray(a) contains all elements |
459 |
> |
* toArray(a) contains all elements in sorted order |
460 |
|
*/ |
461 |
|
public void testToArray2() { |
462 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
462 |
> |
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
463 |
|
Integer[] ints = new Integer[SIZE]; |
464 |
< |
ints = (Integer[])q.toArray(ints); |
476 |
< |
Arrays.sort(ints); |
464 |
> |
assertSame(ints, q.toArray(ints)); |
465 |
|
for (int i = 0; i < ints.length; i++) |
466 |
< |
assertEquals(ints[i], q.pollFirst()); |
466 |
> |
assertSame(ints[i], q.pollFirst()); |
467 |
|
} |
468 |
|
|
469 |
|
/** |
491 |
|
assertTrue(q.contains(it.next())); |
492 |
|
++i; |
493 |
|
} |
494 |
< |
assertEquals(i, 0); |
494 |
> |
assertEquals(0, i); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
498 |
|
* iterator.remove removes current element |
499 |
|
*/ |
500 |
< |
public void testIteratorRemove () { |
500 |
> |
public void testIteratorRemove() { |
501 |
|
final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
502 |
|
q.add(new Integer(2)); |
503 |
|
q.add(new Integer(1)); |
513 |
|
assertFalse(it.hasNext()); |
514 |
|
} |
515 |
|
|
528 |
– |
|
516 |
|
/** |
517 |
|
* toString contains toStrings of elements |
518 |
|
*/ |
520 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
521 |
|
String s = q.toString(); |
522 |
|
for (int i = 0; i < SIZE; ++i) { |
523 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
523 |
> |
assertTrue(s.contains(String.valueOf(i))); |
524 |
|
} |
525 |
|
} |
526 |
|
|
527 |
|
/** |
528 |
|
* A deserialized serialized set has same elements |
529 |
|
*/ |
530 |
< |
public void testSerialization() { |
531 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
532 |
< |
try { |
533 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
534 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
535 |
< |
out.writeObject(q); |
536 |
< |
out.close(); |
537 |
< |
|
538 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
539 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
540 |
< |
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(); |
530 |
> |
public void testSerialization() throws Exception { |
531 |
> |
NavigableSet x = populatedSet(SIZE); |
532 |
> |
NavigableSet y = serialClone(x); |
533 |
> |
|
534 |
> |
assertTrue(x != y); |
535 |
> |
assertEquals(x.size(), y.size()); |
536 |
> |
assertEquals(x, y); |
537 |
> |
assertEquals(y, x); |
538 |
> |
while (!x.isEmpty()) { |
539 |
> |
assertFalse(y.isEmpty()); |
540 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
541 |
|
} |
542 |
+ |
assertTrue(y.isEmpty()); |
543 |
|
} |
544 |
|
|
545 |
|
/** |
662 |
|
} |
663 |
|
|
664 |
|
Random rnd = new Random(666); |
683 |
– |
BitSet bs; |
665 |
|
|
666 |
|
/** |
667 |
|
* Subsets of subsets subdivide correctly |
668 |
|
*/ |
669 |
< |
public void testRecursiveSubSets() { |
670 |
< |
int setSize = 1000; |
669 |
> |
public void testRecursiveSubSets() throws Exception { |
670 |
> |
int setSize = expensiveTests ? 1000 : 100; |
671 |
|
Class cl = ConcurrentSkipListSet.class; |
672 |
|
|
673 |
|
NavigableSet<Integer> set = newSet(cl); |
674 |
< |
bs = new BitSet(setSize); |
674 |
> |
BitSet bs = new BitSet(setSize); |
675 |
|
|
676 |
< |
populate(set, setSize); |
677 |
< |
check(set, 0, setSize - 1, true); |
678 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
679 |
< |
|
680 |
< |
mutateSet(set, 0, setSize - 1); |
681 |
< |
check(set, 0, setSize - 1, true); |
682 |
< |
check(set.descendingSet(), 0, setSize - 1, false); |
676 |
> |
populate(set, setSize, bs); |
677 |
> |
check(set, 0, setSize - 1, true, bs); |
678 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
679 |
> |
|
680 |
> |
mutateSet(set, 0, setSize - 1, bs); |
681 |
> |
check(set, 0, setSize - 1, true, bs); |
682 |
> |
check(set.descendingSet(), 0, setSize - 1, false, bs); |
683 |
|
|
684 |
|
bashSubSet(set.subSet(0, true, setSize, false), |
685 |
< |
0, setSize - 1, true); |
685 |
> |
0, setSize - 1, true, bs); |
686 |
|
} |
687 |
|
|
688 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
689 |
< |
NavigableSet<Integer> result = null; |
690 |
< |
try { |
691 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
692 |
< |
} catch (Exception e) { |
693 |
< |
fail(); |
694 |
< |
} |
695 |
< |
assertEquals(result.size(), 0); |
688 |
> |
/** |
689 |
> |
* addAll is idempotent |
690 |
> |
*/ |
691 |
> |
public void testAddAll_idempotent() throws Exception { |
692 |
> |
Set x = populatedSet(SIZE); |
693 |
> |
Set y = new ConcurrentSkipListSet(x); |
694 |
> |
y.addAll(x); |
695 |
> |
assertEquals(x, y); |
696 |
> |
assertEquals(y, x); |
697 |
> |
} |
698 |
> |
|
699 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
700 |
> |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
701 |
> |
assertEquals(0, result.size()); |
702 |
|
assertFalse(result.iterator().hasNext()); |
703 |
|
return result; |
704 |
|
} |
705 |
|
|
706 |
< |
void populate(NavigableSet<Integer> set, int limit) { |
706 |
> |
void populate(NavigableSet<Integer> set, int limit, BitSet bs) { |
707 |
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
708 |
|
int element = rnd.nextInt(limit); |
709 |
< |
put(set, element); |
709 |
> |
put(set, element, bs); |
710 |
|
} |
711 |
|
} |
712 |
|
|
713 |
< |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
713 |
> |
void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) { |
714 |
|
int size = set.size(); |
715 |
|
int rangeSize = max - min + 1; |
716 |
|
|
717 |
|
// Remove a bunch of entries directly |
718 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
719 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
719 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
720 |
|
} |
721 |
|
|
722 |
|
// Remove a bunch of entries with iterator |
731 |
|
while (set.size() < size) { |
732 |
|
int element = min + rnd.nextInt(rangeSize); |
733 |
|
assertTrue(element >= min && element<= max); |
734 |
< |
put(set, element); |
734 |
> |
put(set, element, bs); |
735 |
|
} |
736 |
|
} |
737 |
|
|
738 |
< |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
738 |
> |
void mutateSubSet(NavigableSet<Integer> set, int min, int max, |
739 |
> |
BitSet bs) { |
740 |
|
int size = set.size(); |
741 |
|
int rangeSize = max - min + 1; |
742 |
|
|
743 |
|
// Remove a bunch of entries directly |
744 |
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
745 |
< |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
745 |
> |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs); |
746 |
|
} |
747 |
|
|
748 |
|
// Remove a bunch of entries with iterator |
757 |
|
while (set.size() < size) { |
758 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
759 |
|
if (element >= min && element<= max) { |
760 |
< |
put(set, element); |
760 |
> |
put(set, element, bs); |
761 |
|
} else { |
762 |
|
try { |
763 |
|
set.add(element); |
764 |
< |
fail(); |
765 |
< |
} catch (IllegalArgumentException e) { |
778 |
< |
// expected |
779 |
< |
} |
764 |
> |
shouldThrow(); |
765 |
> |
} catch (IllegalArgumentException success) {} |
766 |
|
} |
767 |
|
} |
768 |
|
} |
769 |
|
|
770 |
< |
void put(NavigableSet<Integer> set, int element) { |
770 |
> |
void put(NavigableSet<Integer> set, int element, BitSet bs) { |
771 |
|
if (set.add(element)) |
772 |
|
bs.set(element); |
773 |
|
} |
774 |
|
|
775 |
< |
void remove(NavigableSet<Integer> set, int element) { |
775 |
> |
void remove(NavigableSet<Integer> set, int element, BitSet bs) { |
776 |
|
if (set.remove(element)) |
777 |
|
bs.clear(element); |
778 |
|
} |
779 |
|
|
780 |
|
void bashSubSet(NavigableSet<Integer> set, |
781 |
< |
int min, int max, boolean ascending) { |
782 |
< |
check(set, min, max, ascending); |
783 |
< |
check(set.descendingSet(), min, max, !ascending); |
784 |
< |
|
785 |
< |
mutateSubSet(set, min, max); |
786 |
< |
check(set, min, max, ascending); |
787 |
< |
check(set.descendingSet(), min, max, !ascending); |
781 |
> |
int min, int max, boolean ascending, |
782 |
> |
BitSet bs) { |
783 |
> |
check(set, min, max, ascending, bs); |
784 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
785 |
> |
|
786 |
> |
mutateSubSet(set, min, max, bs); |
787 |
> |
check(set, min, max, ascending, bs); |
788 |
> |
check(set.descendingSet(), min, max, !ascending, bs); |
789 |
|
|
790 |
|
// Recurse |
791 |
|
if (max - min < 2) |
797 |
|
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
798 |
|
if (ascending) { |
799 |
|
if (rnd.nextBoolean()) |
800 |
< |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
800 |
> |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); |
801 |
|
else |
802 |
|
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
803 |
< |
false); |
803 |
> |
false, bs); |
804 |
|
} else { |
805 |
|
if (rnd.nextBoolean()) |
806 |
< |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
806 |
> |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs); |
807 |
|
else |
808 |
|
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
809 |
< |
true); |
809 |
> |
true, bs); |
810 |
|
} |
811 |
|
|
812 |
|
// tailSet - pick direction and endpoint inclusion randomly |
814 |
|
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
815 |
|
if (ascending) { |
816 |
|
if (rnd.nextBoolean()) |
817 |
< |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
817 |
> |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); |
818 |
|
else |
819 |
|
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
820 |
< |
false); |
820 |
> |
false, bs); |
821 |
|
} else { |
822 |
|
if (rnd.nextBoolean()) { |
823 |
< |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
823 |
> |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs); |
824 |
|
} else { |
825 |
|
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
826 |
< |
true); |
826 |
> |
true, bs); |
827 |
|
} |
828 |
|
} |
829 |
|
|
840 |
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
841 |
|
if (rnd.nextBoolean()) |
842 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
843 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
843 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
844 |
|
else |
845 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
846 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
846 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
847 |
|
} else { |
848 |
|
NavigableSet<Integer> sm = set.subSet( |
849 |
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
850 |
|
if (rnd.nextBoolean()) |
851 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
852 |
< |
endpoints[1] - (highIncl ? 0 : 1), false); |
852 |
> |
endpoints[1] - (highIncl ? 0 : 1), false, bs); |
853 |
|
else |
854 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
855 |
< |
endpoints[1] - (highIncl ? 0 : 1), true); |
855 |
> |
endpoints[1] - (highIncl ? 0 : 1), true, bs); |
856 |
|
} |
857 |
|
} |
858 |
|
|
860 |
|
* min and max are both inclusive. If max < min, interval is empty. |
861 |
|
*/ |
862 |
|
void check(NavigableSet<Integer> set, |
863 |
< |
final int min, final int max, final boolean ascending) { |
864 |
< |
class ReferenceSet { |
863 |
> |
final int min, final int max, final boolean ascending, |
864 |
> |
final BitSet bs) { |
865 |
> |
class ReferenceSet { |
866 |
|
int lower(int element) { |
867 |
|
return ascending ? |
868 |
|
lowerAscending(element) : higherAscending(element); |
897 |
|
// BitSet should support this! Test would run much faster |
898 |
|
while (element >= min) { |
899 |
|
if (bs.get(element)) |
900 |
< |
return(element); |
900 |
> |
return element; |
901 |
|
element--; |
902 |
|
} |
903 |
|
return -1; |
932 |
|
if (bsContainsI) |
933 |
|
size++; |
934 |
|
} |
935 |
< |
assertEquals(set.size(), size); |
935 |
> |
assertEquals(size, set.size()); |
936 |
|
|
937 |
|
// Test contents using contains elementSet iterator |
938 |
|
int size2 = 0; |
963 |
|
assertEq(rs.last(), -1); |
964 |
|
try { |
965 |
|
set.first(); |
966 |
< |
fail(); |
967 |
< |
} catch (NoSuchElementException e) { |
980 |
< |
// expected |
981 |
< |
} |
966 |
> |
shouldThrow(); |
967 |
> |
} catch (NoSuchElementException success) {} |
968 |
|
try { |
969 |
|
set.last(); |
970 |
< |
fail(); |
971 |
< |
} catch (NoSuchElementException e) { |
986 |
< |
// expected |
987 |
< |
} |
970 |
> |
shouldThrow(); |
971 |
> |
} catch (NoSuchElementException success) {} |
972 |
|
} |
973 |
|
} |
974 |
|
|