ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
Revision: 1.52
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.51: +16 -16 lines
Log Message:
use diamond <> pervasively

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