ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
Revision: 1.8
Committed: Sat Nov 21 10:25:05 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +26 -41 lines
Log Message:
improve exception handling

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