ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
Revision: 1.12
Committed: Tue Dec 1 09:48:13 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +0 -4 lines
Log Message:
whitespace

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