ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
Revision: 1.49
Committed: Sun Jan 7 22:59:18 2018 UTC (6 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +1 -2 lines
Log Message:
use <>

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