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