ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
Revision: 1.22
Committed: Fri May 27 19:21:27 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +1 -5 lines
Log Message:
indexOf => contains

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