11 |
|
|
12 |
|
public class ConcurrentSkipListSetTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(ConcurrentSkipListSetTest.class); |
17 |
> |
return new TestSuite(ConcurrentSkipListSetTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
static class MyReverseComparator implements Comparator { |
20 |
> |
static class MyReverseComparator implements Comparator { |
21 |
|
public int compare(Object x, Object y) { |
22 |
|
int i = ((Integer)x).intValue(); |
23 |
|
int j = ((Integer)y).intValue(); |
34 |
|
private ConcurrentSkipListSet populatedSet(int n) { |
35 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
36 |
|
assertTrue(q.isEmpty()); |
37 |
< |
for(int i = n-1; i >= 0; i-=2) |
38 |
< |
assertTrue(q.add(new Integer(i))); |
39 |
< |
for(int i = (n & 1); i < n; i+=2) |
40 |
< |
assertTrue(q.add(new Integer(i))); |
37 |
> |
for (int i = n-1; i >= 0; i-=2) |
38 |
> |
assertTrue(q.add(new Integer(i))); |
39 |
> |
for (int i = (n & 1); i < n; i+=2) |
40 |
> |
assertTrue(q.add(new Integer(i))); |
41 |
|
assertFalse(q.isEmpty()); |
42 |
< |
assertEquals(n, q.size()); |
42 |
> |
assertEquals(n, q.size()); |
43 |
|
return q; |
44 |
|
} |
45 |
|
|
54 |
|
q.add(three); |
55 |
|
q.add(four); |
56 |
|
q.add(five); |
57 |
< |
assertEquals(5, q.size()); |
57 |
> |
assertEquals(5, q.size()); |
58 |
|
return q; |
59 |
|
} |
60 |
< |
|
60 |
> |
|
61 |
|
/** |
62 |
|
* A new set has unbounded capacity |
63 |
|
*/ |
72 |
|
try { |
73 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); |
74 |
|
shouldThrow(); |
75 |
< |
} |
76 |
< |
catch (NullPointerException success) {} |
75 |
> |
} catch (NullPointerException success) {} |
76 |
|
} |
77 |
|
|
78 |
|
/** |
83 |
|
Integer[] ints = new Integer[SIZE]; |
84 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
85 |
|
shouldThrow(); |
86 |
< |
} |
88 |
< |
catch (NullPointerException success) {} |
86 |
> |
} catch (NullPointerException success) {} |
87 |
|
} |
88 |
|
|
89 |
|
/** |
96 |
|
ints[i] = new Integer(i); |
97 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
98 |
|
shouldThrow(); |
99 |
< |
} |
102 |
< |
catch (NullPointerException success) {} |
99 |
> |
} catch (NullPointerException success) {} |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Set contains all elements of collection used to initialize |
104 |
|
*/ |
105 |
|
public void testConstructor6() { |
106 |
< |
try { |
107 |
< |
Integer[] ints = new Integer[SIZE]; |
108 |
< |
for (int i = 0; i < SIZE; ++i) |
109 |
< |
ints[i] = new Integer(i); |
110 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
111 |
< |
for (int i = 0; i < SIZE; ++i) |
115 |
< |
assertEquals(ints[i], q.pollFirst()); |
116 |
< |
} |
117 |
< |
finally {} |
106 |
> |
Integer[] ints = new Integer[SIZE]; |
107 |
> |
for (int i = 0; i < SIZE; ++i) |
108 |
> |
ints[i] = new Integer(i); |
109 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
110 |
> |
for (int i = 0; i < SIZE; ++i) |
111 |
> |
assertEquals(ints[i], q.pollFirst()); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
|
* The comparator used in constructor is used |
116 |
|
*/ |
117 |
|
public void testConstructor7() { |
118 |
< |
try { |
119 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
120 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
121 |
< |
assertEquals(cmp, q.comparator()); |
122 |
< |
Integer[] ints = new Integer[SIZE]; |
123 |
< |
for (int i = 0; i < SIZE; ++i) |
124 |
< |
ints[i] = new Integer(i); |
125 |
< |
q.addAll(Arrays.asList(ints)); |
126 |
< |
for (int i = SIZE-1; i >= 0; --i) |
133 |
< |
assertEquals(ints[i], q.pollFirst()); |
134 |
< |
} |
135 |
< |
finally {} |
118 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
119 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
120 |
> |
assertEquals(cmp, q.comparator()); |
121 |
> |
Integer[] ints = new Integer[SIZE]; |
122 |
> |
for (int i = 0; i < SIZE; ++i) |
123 |
> |
ints[i] = new Integer(i); |
124 |
> |
q.addAll(Arrays.asList(ints)); |
125 |
> |
for (int i = SIZE-1; i >= 0; --i) |
126 |
> |
assertEquals(ints[i], q.pollFirst()); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
159 |
|
* add(null) throws NPE |
160 |
|
*/ |
161 |
|
public void testAddNull() { |
162 |
< |
try { |
162 |
> |
try { |
163 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
164 |
|
q.add(null); |
165 |
|
shouldThrow(); |
166 |
< |
} catch (NullPointerException success) { } |
166 |
> |
} catch (NullPointerException success) {} |
167 |
|
} |
168 |
|
|
169 |
|
/** |
194 |
|
q.add(new Object()); |
195 |
|
q.add(new Object()); |
196 |
|
shouldThrow(); |
197 |
< |
} |
207 |
< |
catch(ClassCastException success) {} |
197 |
> |
} catch (ClassCastException success) {} |
198 |
|
} |
199 |
|
|
200 |
|
/** |
205 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
206 |
|
q.addAll(null); |
207 |
|
shouldThrow(); |
208 |
< |
} |
219 |
< |
catch (NullPointerException success) {} |
208 |
> |
} catch (NullPointerException success) {} |
209 |
|
} |
210 |
|
/** |
211 |
|
* addAll of a collection with null elements throws NPE |
216 |
|
Integer[] ints = new Integer[SIZE]; |
217 |
|
q.addAll(Arrays.asList(ints)); |
218 |
|
shouldThrow(); |
219 |
< |
} |
231 |
< |
catch (NullPointerException success) {} |
219 |
> |
} catch (NullPointerException success) {} |
220 |
|
} |
221 |
|
/** |
222 |
|
* addAll of a collection with any null elements throws NPE after |
230 |
|
ints[i] = new Integer(i); |
231 |
|
q.addAll(Arrays.asList(ints)); |
232 |
|
shouldThrow(); |
233 |
< |
} |
246 |
< |
catch (NullPointerException success) {} |
233 |
> |
} catch (NullPointerException success) {} |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
|
* Set contains all elements of successful addAll |
238 |
|
*/ |
239 |
|
public void testAddAll5() { |
240 |
< |
try { |
241 |
< |
Integer[] empty = new Integer[0]; |
242 |
< |
Integer[] ints = new Integer[SIZE]; |
243 |
< |
for (int i = 0; i < SIZE; ++i) |
244 |
< |
ints[i] = new Integer(SIZE-1-i); |
245 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
246 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
247 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
248 |
< |
for (int i = 0; i < SIZE; ++i) |
262 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
263 |
< |
} |
264 |
< |
finally {} |
240 |
> |
Integer[] empty = new Integer[0]; |
241 |
> |
Integer[] ints = new Integer[SIZE]; |
242 |
> |
for (int i = 0; i < SIZE; ++i) |
243 |
> |
ints[i] = new Integer(SIZE-1-i); |
244 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
245 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
246 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
247 |
> |
for (int i = 0; i < SIZE; ++i) |
248 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
256 |
|
for (int i = 0; i < SIZE; ++i) { |
257 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
258 |
|
} |
259 |
< |
assertNull(q.pollFirst()); |
259 |
> |
assertNull(q.pollFirst()); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
267 |
|
for (int i = SIZE-1; i >= 0; --i) { |
268 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
269 |
|
} |
270 |
< |
assertNull(q.pollFirst()); |
270 |
> |
assertNull(q.pollFirst()); |
271 |
|
} |
272 |
|
|
273 |
|
|
285 |
|
} |
286 |
|
assertTrue(q.isEmpty()); |
287 |
|
} |
288 |
< |
|
288 |
> |
|
289 |
|
/** |
290 |
|
* contains(x) reports true when elements added but not yet removed |
291 |
|
*/ |
361 |
|
} |
362 |
|
} |
363 |
|
|
364 |
< |
|
364 |
> |
|
365 |
|
|
366 |
|
/** |
367 |
|
* lower returns preceding element |
444 |
|
*/ |
445 |
|
public void testToArray() { |
446 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
447 |
< |
Object[] o = q.toArray(); |
447 |
> |
Object[] o = q.toArray(); |
448 |
|
Arrays.sort(o); |
449 |
< |
for(int i = 0; i < o.length; i++) |
450 |
< |
assertEquals(o[i], q.pollFirst()); |
449 |
> |
for (int i = 0; i < o.length; i++) |
450 |
> |
assertEquals(o[i], q.pollFirst()); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
455 |
|
*/ |
456 |
|
public void testToArray2() { |
457 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
458 |
< |
Integer[] ints = new Integer[SIZE]; |
459 |
< |
ints = (Integer[])q.toArray(ints); |
458 |
> |
Integer[] ints = new Integer[SIZE]; |
459 |
> |
ints = (Integer[])q.toArray(ints); |
460 |
|
Arrays.sort(ints); |
461 |
< |
for(int i = 0; i < ints.length; i++) |
461 |
> |
for (int i = 0; i < ints.length; i++) |
462 |
|
assertEquals(ints[i], q.pollFirst()); |
463 |
|
} |
464 |
< |
|
464 |
> |
|
465 |
|
/** |
466 |
|
* iterator iterates through all elements |
467 |
|
*/ |
468 |
|
public void testIterator() { |
469 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
470 |
|
int i = 0; |
471 |
< |
Iterator it = q.iterator(); |
472 |
< |
while(it.hasNext()) { |
471 |
> |
Iterator it = q.iterator(); |
472 |
> |
while (it.hasNext()) { |
473 |
|
assertTrue(q.contains(it.next())); |
474 |
|
++i; |
475 |
|
} |
482 |
|
public void testEmptyIterator() { |
483 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
484 |
|
int i = 0; |
485 |
< |
Iterator it = q.iterator(); |
486 |
< |
while(it.hasNext()) { |
485 |
> |
Iterator it = q.iterator(); |
486 |
> |
while (it.hasNext()) { |
487 |
|
assertTrue(q.contains(it.next())); |
488 |
|
++i; |
489 |
|
} |
519 |
|
for (int i = 0; i < SIZE; ++i) { |
520 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
521 |
|
} |
522 |
< |
} |
522 |
> |
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* A deserialized serialized set has same elements |
525 |
> |
* A deserialized serialized set has same elements |
526 |
|
*/ |
527 |
< |
public void testSerialization() { |
527 |
> |
public void testSerialization() throws Exception { |
528 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
529 |
< |
try { |
530 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
531 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
532 |
< |
out.writeObject(q); |
533 |
< |
out.close(); |
534 |
< |
|
535 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
536 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
537 |
< |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
538 |
< |
assertEquals(q.size(), r.size()); |
539 |
< |
while (!q.isEmpty()) |
556 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
557 |
< |
} catch(Exception e){ |
558 |
< |
e.printStackTrace(); |
559 |
< |
unexpectedException(); |
560 |
< |
} |
529 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
530 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
531 |
> |
out.writeObject(q); |
532 |
> |
out.close(); |
533 |
> |
|
534 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
535 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
536 |
> |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
537 |
> |
assertEquals(q.size(), r.size()); |
538 |
> |
while (!q.isEmpty()) |
539 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
658 |
|
assertEquals(4, set.size()); |
659 |
|
} |
660 |
|
|
661 |
+ |
Random rnd = new Random(666); |
662 |
+ |
BitSet bs; |
663 |
+ |
|
664 |
+ |
/** |
665 |
+ |
* Subsets of subsets subdivide correctly |
666 |
+ |
*/ |
667 |
+ |
public void testRecursiveSubSets() { |
668 |
+ |
int setSize = 1000; |
669 |
+ |
Class cl = ConcurrentSkipListSet.class; |
670 |
+ |
|
671 |
+ |
NavigableSet<Integer> set = newSet(cl); |
672 |
+ |
bs = new BitSet(setSize); |
673 |
+ |
|
674 |
+ |
populate(set, setSize); |
675 |
+ |
check(set, 0, setSize - 1, true); |
676 |
+ |
check(set.descendingSet(), 0, setSize - 1, false); |
677 |
+ |
|
678 |
+ |
mutateSet(set, 0, setSize - 1); |
679 |
+ |
check(set, 0, setSize - 1, true); |
680 |
+ |
check(set.descendingSet(), 0, setSize - 1, false); |
681 |
+ |
|
682 |
+ |
bashSubSet(set.subSet(0, true, setSize, false), |
683 |
+ |
0, setSize - 1, true); |
684 |
+ |
} |
685 |
+ |
|
686 |
+ |
static NavigableSet<Integer> newSet(Class cl) { |
687 |
+ |
NavigableSet<Integer> result = null; |
688 |
+ |
try { |
689 |
+ |
result = (NavigableSet<Integer>) cl.newInstance(); |
690 |
+ |
} catch (Exception e) { |
691 |
+ |
fail(); |
692 |
+ |
} |
693 |
+ |
assertEquals(result.size(), 0); |
694 |
+ |
assertFalse(result.iterator().hasNext()); |
695 |
+ |
return result; |
696 |
+ |
} |
697 |
+ |
|
698 |
+ |
void populate(NavigableSet<Integer> set, int limit) { |
699 |
+ |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
700 |
+ |
int element = rnd.nextInt(limit); |
701 |
+ |
put(set, element); |
702 |
+ |
} |
703 |
+ |
} |
704 |
+ |
|
705 |
+ |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
706 |
+ |
int size = set.size(); |
707 |
+ |
int rangeSize = max - min + 1; |
708 |
+ |
|
709 |
+ |
// Remove a bunch of entries directly |
710 |
+ |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
711 |
+ |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
712 |
+ |
} |
713 |
+ |
|
714 |
+ |
// Remove a bunch of entries with iterator |
715 |
+ |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
716 |
+ |
if (rnd.nextBoolean()) { |
717 |
+ |
bs.clear(it.next()); |
718 |
+ |
it.remove(); |
719 |
+ |
} |
720 |
+ |
} |
721 |
+ |
|
722 |
+ |
// Add entries till we're back to original size |
723 |
+ |
while (set.size() < size) { |
724 |
+ |
int element = min + rnd.nextInt(rangeSize); |
725 |
+ |
assertTrue(element >= min && element<= max); |
726 |
+ |
put(set, element); |
727 |
+ |
} |
728 |
+ |
} |
729 |
+ |
|
730 |
+ |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
731 |
+ |
int size = set.size(); |
732 |
+ |
int rangeSize = max - min + 1; |
733 |
+ |
|
734 |
+ |
// Remove a bunch of entries directly |
735 |
+ |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
736 |
+ |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
737 |
+ |
} |
738 |
+ |
|
739 |
+ |
// Remove a bunch of entries with iterator |
740 |
+ |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
741 |
+ |
if (rnd.nextBoolean()) { |
742 |
+ |
bs.clear(it.next()); |
743 |
+ |
it.remove(); |
744 |
+ |
} |
745 |
+ |
} |
746 |
+ |
|
747 |
+ |
// Add entries till we're back to original size |
748 |
+ |
while (set.size() < size) { |
749 |
+ |
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
750 |
+ |
if (element >= min && element<= max) { |
751 |
+ |
put(set, element); |
752 |
+ |
} else { |
753 |
+ |
try { |
754 |
+ |
set.add(element); |
755 |
+ |
fail(); |
756 |
+ |
} catch (IllegalArgumentException e) { |
757 |
+ |
// expected |
758 |
+ |
} |
759 |
+ |
} |
760 |
+ |
} |
761 |
+ |
} |
762 |
+ |
|
763 |
+ |
void put(NavigableSet<Integer> set, int element) { |
764 |
+ |
if (set.add(element)) |
765 |
+ |
bs.set(element); |
766 |
+ |
} |
767 |
+ |
|
768 |
+ |
void remove(NavigableSet<Integer> set, int element) { |
769 |
+ |
if (set.remove(element)) |
770 |
+ |
bs.clear(element); |
771 |
+ |
} |
772 |
+ |
|
773 |
+ |
void bashSubSet(NavigableSet<Integer> set, |
774 |
+ |
int min, int max, boolean ascending) { |
775 |
+ |
check(set, min, max, ascending); |
776 |
+ |
check(set.descendingSet(), min, max, !ascending); |
777 |
+ |
|
778 |
+ |
mutateSubSet(set, min, max); |
779 |
+ |
check(set, min, max, ascending); |
780 |
+ |
check(set.descendingSet(), min, max, !ascending); |
781 |
+ |
|
782 |
+ |
// Recurse |
783 |
+ |
if (max - min < 2) |
784 |
+ |
return; |
785 |
+ |
int midPoint = (min + max) / 2; |
786 |
+ |
|
787 |
+ |
// headSet - pick direction and endpoint inclusion randomly |
788 |
+ |
boolean incl = rnd.nextBoolean(); |
789 |
+ |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
790 |
+ |
if (ascending) { |
791 |
+ |
if (rnd.nextBoolean()) |
792 |
+ |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
793 |
+ |
else |
794 |
+ |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
795 |
+ |
false); |
796 |
+ |
} else { |
797 |
+ |
if (rnd.nextBoolean()) |
798 |
+ |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
799 |
+ |
else |
800 |
+ |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
801 |
+ |
true); |
802 |
+ |
} |
803 |
+ |
|
804 |
+ |
// tailSet - pick direction and endpoint inclusion randomly |
805 |
+ |
incl = rnd.nextBoolean(); |
806 |
+ |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
807 |
+ |
if (ascending) { |
808 |
+ |
if (rnd.nextBoolean()) |
809 |
+ |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
810 |
+ |
else |
811 |
+ |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
812 |
+ |
false); |
813 |
+ |
} else { |
814 |
+ |
if (rnd.nextBoolean()) { |
815 |
+ |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
816 |
+ |
} else { |
817 |
+ |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
818 |
+ |
true); |
819 |
+ |
} |
820 |
+ |
} |
821 |
+ |
|
822 |
+ |
// subSet - pick direction and endpoint inclusion randomly |
823 |
+ |
int rangeSize = max - min + 1; |
824 |
+ |
int[] endpoints = new int[2]; |
825 |
+ |
endpoints[0] = min + rnd.nextInt(rangeSize); |
826 |
+ |
endpoints[1] = min + rnd.nextInt(rangeSize); |
827 |
+ |
Arrays.sort(endpoints); |
828 |
+ |
boolean lowIncl = rnd.nextBoolean(); |
829 |
+ |
boolean highIncl = rnd.nextBoolean(); |
830 |
+ |
if (ascending) { |
831 |
+ |
NavigableSet<Integer> sm = set.subSet( |
832 |
+ |
endpoints[0], lowIncl, endpoints[1], highIncl); |
833 |
+ |
if (rnd.nextBoolean()) |
834 |
+ |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
835 |
+ |
endpoints[1] - (highIncl ? 0 : 1), true); |
836 |
+ |
else |
837 |
+ |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
838 |
+ |
endpoints[1] - (highIncl ? 0 : 1), false); |
839 |
+ |
} else { |
840 |
+ |
NavigableSet<Integer> sm = set.subSet( |
841 |
+ |
endpoints[1], highIncl, endpoints[0], lowIncl); |
842 |
+ |
if (rnd.nextBoolean()) |
843 |
+ |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
844 |
+ |
endpoints[1] - (highIncl ? 0 : 1), false); |
845 |
+ |
else |
846 |
+ |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
847 |
+ |
endpoints[1] - (highIncl ? 0 : 1), true); |
848 |
+ |
} |
849 |
+ |
} |
850 |
+ |
|
851 |
+ |
/** |
852 |
+ |
* min and max are both inclusive. If max < min, interval is empty. |
853 |
+ |
*/ |
854 |
+ |
void check(NavigableSet<Integer> set, |
855 |
+ |
final int min, final int max, final boolean ascending) { |
856 |
+ |
class ReferenceSet { |
857 |
+ |
int lower(int element) { |
858 |
+ |
return ascending ? |
859 |
+ |
lowerAscending(element) : higherAscending(element); |
860 |
+ |
} |
861 |
+ |
int floor(int element) { |
862 |
+ |
return ascending ? |
863 |
+ |
floorAscending(element) : ceilingAscending(element); |
864 |
+ |
} |
865 |
+ |
int ceiling(int element) { |
866 |
+ |
return ascending ? |
867 |
+ |
ceilingAscending(element) : floorAscending(element); |
868 |
+ |
} |
869 |
+ |
int higher(int element) { |
870 |
+ |
return ascending ? |
871 |
+ |
higherAscending(element) : lowerAscending(element); |
872 |
+ |
} |
873 |
+ |
int first() { |
874 |
+ |
return ascending ? firstAscending() : lastAscending(); |
875 |
+ |
} |
876 |
+ |
int last() { |
877 |
+ |
return ascending ? lastAscending() : firstAscending(); |
878 |
+ |
} |
879 |
+ |
int lowerAscending(int element) { |
880 |
+ |
return floorAscending(element - 1); |
881 |
+ |
} |
882 |
+ |
int floorAscending(int element) { |
883 |
+ |
if (element < min) |
884 |
+ |
return -1; |
885 |
+ |
else if (element > max) |
886 |
+ |
element = max; |
887 |
+ |
|
888 |
+ |
// BitSet should support this! Test would run much faster |
889 |
+ |
while (element >= min) { |
890 |
+ |
if (bs.get(element)) |
891 |
+ |
return(element); |
892 |
+ |
element--; |
893 |
+ |
} |
894 |
+ |
return -1; |
895 |
+ |
} |
896 |
+ |
int ceilingAscending(int element) { |
897 |
+ |
if (element < min) |
898 |
+ |
element = min; |
899 |
+ |
else if (element > max) |
900 |
+ |
return -1; |
901 |
+ |
int result = bs.nextSetBit(element); |
902 |
+ |
return result > max ? -1 : result; |
903 |
+ |
} |
904 |
+ |
int higherAscending(int element) { |
905 |
+ |
return ceilingAscending(element + 1); |
906 |
+ |
} |
907 |
+ |
private int firstAscending() { |
908 |
+ |
int result = ceilingAscending(min); |
909 |
+ |
return result > max ? -1 : result; |
910 |
+ |
} |
911 |
+ |
private int lastAscending() { |
912 |
+ |
int result = floorAscending(max); |
913 |
+ |
return result < min ? -1 : result; |
914 |
+ |
} |
915 |
+ |
} |
916 |
+ |
ReferenceSet rs = new ReferenceSet(); |
917 |
+ |
|
918 |
+ |
// Test contents using containsElement |
919 |
+ |
int size = 0; |
920 |
+ |
for (int i = min; i <= max; i++) { |
921 |
+ |
boolean bsContainsI = bs.get(i); |
922 |
+ |
assertEquals(bsContainsI, set.contains(i)); |
923 |
+ |
if (bsContainsI) |
924 |
+ |
size++; |
925 |
+ |
} |
926 |
+ |
assertEquals(set.size(), size); |
927 |
+ |
|
928 |
+ |
// Test contents using contains elementSet iterator |
929 |
+ |
int size2 = 0; |
930 |
+ |
int previousElement = -1; |
931 |
+ |
for (int element : set) { |
932 |
+ |
assertTrue(bs.get(element)); |
933 |
+ |
size2++; |
934 |
+ |
assertTrue(previousElement < 0 || (ascending ? |
935 |
+ |
element - previousElement > 0 : element - previousElement < 0)); |
936 |
+ |
previousElement = element; |
937 |
+ |
} |
938 |
+ |
assertEquals(size2, size); |
939 |
+ |
|
940 |
+ |
// Test navigation ops |
941 |
+ |
for (int element = min - 1; element <= max + 1; element++) { |
942 |
+ |
assertEq(set.lower(element), rs.lower(element)); |
943 |
+ |
assertEq(set.floor(element), rs.floor(element)); |
944 |
+ |
assertEq(set.higher(element), rs.higher(element)); |
945 |
+ |
assertEq(set.ceiling(element), rs.ceiling(element)); |
946 |
+ |
} |
947 |
+ |
|
948 |
+ |
// Test extrema |
949 |
+ |
if (set.size() != 0) { |
950 |
+ |
assertEq(set.first(), rs.first()); |
951 |
+ |
assertEq(set.last(), rs.last()); |
952 |
+ |
} else { |
953 |
+ |
assertEq(rs.first(), -1); |
954 |
+ |
assertEq(rs.last(), -1); |
955 |
+ |
try { |
956 |
+ |
set.first(); |
957 |
+ |
fail(); |
958 |
+ |
} catch (NoSuchElementException e) { |
959 |
+ |
// expected |
960 |
+ |
} |
961 |
+ |
try { |
962 |
+ |
set.last(); |
963 |
+ |
fail(); |
964 |
+ |
} catch (NoSuchElementException e) { |
965 |
+ |
// expected |
966 |
+ |
} |
967 |
+ |
} |
968 |
+ |
} |
969 |
+ |
|
970 |
+ |
static void assertEq(Integer i, int j) { |
971 |
+ |
if (i == null) |
972 |
+ |
assertEquals(j, -1); |
973 |
+ |
else |
974 |
+ |
assertEquals((int) i, j); |
975 |
+ |
} |
976 |
+ |
|
977 |
+ |
static boolean eq(Integer i, int j) { |
978 |
+ |
return i == null ? j == -1 : i == j; |
979 |
+ |
} |
980 |
+ |
|
981 |
|
} |