ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
Revision: 1.48
Committed: Fri Aug 4 04:59:23 2017 UTC (6 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.47: +19 -1 lines
Log Message:
8185830: ConcurrentSkipListSet.clone() fails with UnsupportedOperationException

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