ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
Revision: 1.41
Committed: Sun May 24 01:23:17 2015 UTC (8 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +1 -1 lines
Log Message:
coding style

File Contents

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