ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSetTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentSkipListSetTest.java (file contents):
Revision 1.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.25 by jsr166, Sat Nov 26 05:42:14 2011 UTC

# Line 1 | Line 1
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
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.*;
8 > import java.util.Arrays;
9 > import java.util.BitSet;
10 > import java.util.Collection;
11 > import java.util.Comparator;
12 > import java.util.Iterator;
13 > import java.util.NavigableSet;
14 > import java.util.NoSuchElementException;
15 > import java.util.Random;
16 > import java.util.Set;
17 > import java.util.SortedSet;
18 > import java.util.concurrent.ConcurrentSkipListSet;
19  
20   public class ConcurrentSkipListSetTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());  
22 >        junit.textui.TestRunner.run(suite());
23      }
24      public static Test suite() {
25 <        return new TestSuite(ConcurrentSkipListSetTest.class);
25 >        return new TestSuite(ConcurrentSkipListSetTest.class);
26      }
27  
28 <    static class MyReverseComparator implements Comparator {
28 >    static class MyReverseComparator implements Comparator {
29          public int compare(Object x, Object y) {
30 <            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;
30 >            return ((Comparable)y).compareTo(x);
31          }
32      }
33  
# Line 31 | Line 35 | public class ConcurrentSkipListSetTest e
35       * Create a set of given size containing consecutive
36       * Integers 0 ... n.
37       */
38 <    private ConcurrentSkipListSet populatedSet(int n) {
39 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
38 >    private ConcurrentSkipListSet<Integer> populatedSet(int n) {
39 >        ConcurrentSkipListSet<Integer> q =
40 >            new ConcurrentSkipListSet<Integer>();
41          assertTrue(q.isEmpty());
42 <        for(int i = n-1; i >= 0; i-=2)
43 <            assertTrue(q.add(new Integer(i)));
44 <        for(int i = (n & 1); i < n; i+=2)
45 <            assertTrue(q.add(new Integer(i)));
42 >        for (int i = n-1; i >= 0; i-=2)
43 >            assertTrue(q.add(new Integer(i)));
44 >        for (int i = (n & 1); i < n; i+=2)
45 >            assertTrue(q.add(new Integer(i)));
46          assertFalse(q.isEmpty());
47 <        assertEquals(n, q.size());
47 >        assertEquals(n, q.size());
48          return q;
49      }
50  
# Line 54 | Line 59 | public class ConcurrentSkipListSetTest e
59          q.add(three);
60          q.add(four);
61          q.add(five);
62 <        assertEquals(5, q.size());
62 >        assertEquals(5, q.size());
63          return q;
64      }
65 <
65 >
66      /**
67       * A new set has unbounded capacity
68       */
# Line 72 | Line 77 | public class ConcurrentSkipListSetTest e
77          try {
78              ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null);
79              shouldThrow();
80 <        }
76 <        catch (NullPointerException success) {}
80 >        } catch (NullPointerException success) {}
81      }
82  
83      /**
# Line 84 | Line 88 | public class ConcurrentSkipListSetTest e
88              Integer[] ints = new Integer[SIZE];
89              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
90              shouldThrow();
91 <        }
88 <        catch (NullPointerException success) {}
91 >        } catch (NullPointerException success) {}
92      }
93  
94      /**
# Line 98 | Line 101 | public class ConcurrentSkipListSetTest e
101                  ints[i] = new Integer(i);
102              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
103              shouldThrow();
104 <        }
102 <        catch (NullPointerException success) {}
104 >        } catch (NullPointerException success) {}
105      }
106  
107      /**
108       * Set contains all elements of collection used to initialize
109       */
110      public void testConstructor6() {
111 <        try {
112 <            Integer[] ints = new Integer[SIZE];
113 <            for (int i = 0; i < SIZE; ++i)
114 <                ints[i] = new Integer(i);
115 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
116 <            for (int i = 0; i < SIZE; ++i)
115 <                assertEquals(ints[i], q.pollFirst());
116 <        }
117 <        finally {}
111 >        Integer[] ints = new Integer[SIZE];
112 >        for (int i = 0; i < SIZE; ++i)
113 >            ints[i] = new Integer(i);
114 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
115 >        for (int i = 0; i < SIZE; ++i)
116 >            assertEquals(ints[i], q.pollFirst());
117      }
118  
119      /**
120       * The comparator used in constructor is used
121       */
122      public void testConstructor7() {
123 <        try {
124 <            MyReverseComparator cmp = new MyReverseComparator();
125 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
126 <            assertEquals(cmp, q.comparator());
127 <            Integer[] ints = new Integer[SIZE];
128 <            for (int i = 0; i < SIZE; ++i)
129 <                ints[i] = new Integer(i);
130 <            q.addAll(Arrays.asList(ints));
131 <            for (int i = SIZE-1; i >= 0; --i)
133 <                assertEquals(ints[i], q.pollFirst());
134 <        }
135 <        finally {}
123 >        MyReverseComparator cmp = new MyReverseComparator();
124 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
125 >        assertEquals(cmp, q.comparator());
126 >        Integer[] ints = new Integer[SIZE];
127 >        for (int i = 0; i < SIZE; ++i)
128 >            ints[i] = new Integer(i);
129 >        q.addAll(Arrays.asList(ints));
130 >        for (int i = SIZE-1; i >= 0; --i)
131 >            assertEquals(ints[i], q.pollFirst());
132      }
133  
134      /**
# Line 168 | Line 164 | public class ConcurrentSkipListSetTest e
164       * add(null) throws NPE
165       */
166      public void testAddNull() {
167 <        try {
167 >        try {
168              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
169              q.add(null);
170              shouldThrow();
171 <        } catch (NullPointerException success) { }  
171 >        } catch (NullPointerException success) {}
172      }
173  
174      /**
# Line 203 | Line 199 | public class ConcurrentSkipListSetTest e
199              q.add(new Object());
200              q.add(new Object());
201              shouldThrow();
202 <        }
207 <        catch(ClassCastException success) {}
202 >        } catch (ClassCastException success) {}
203      }
204  
205      /**
# Line 215 | Line 210 | public class ConcurrentSkipListSetTest e
210              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
211              q.addAll(null);
212              shouldThrow();
213 <        }
219 <        catch (NullPointerException success) {}
213 >        } catch (NullPointerException success) {}
214      }
215 +
216      /**
217       * addAll of a collection with null elements throws NPE
218       */
# Line 227 | Line 222 | public class ConcurrentSkipListSetTest e
222              Integer[] ints = new Integer[SIZE];
223              q.addAll(Arrays.asList(ints));
224              shouldThrow();
225 <        }
231 <        catch (NullPointerException success) {}
225 >        } catch (NullPointerException success) {}
226      }
227 +
228      /**
229       * addAll of a collection with any null elements throws NPE after
230       * possibly adding some elements
# Line 242 | Line 237 | public class ConcurrentSkipListSetTest e
237                  ints[i] = new Integer(i);
238              q.addAll(Arrays.asList(ints));
239              shouldThrow();
240 <        }
246 <        catch (NullPointerException success) {}
240 >        } catch (NullPointerException success) {}
241      }
242  
243      /**
244       * Set contains all elements of successful addAll
245       */
246      public void testAddAll5() {
247 <        try {
248 <            Integer[] empty = new Integer[0];
249 <            Integer[] ints = new Integer[SIZE];
250 <            for (int i = 0; i < SIZE; ++i)
251 <                ints[i] = new Integer(SIZE-1-i);
252 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
253 <            assertFalse(q.addAll(Arrays.asList(empty)));
254 <            assertTrue(q.addAll(Arrays.asList(ints)));
255 <            for (int i = 0; i < SIZE; ++i)
262 <                assertEquals(new Integer(i), q.pollFirst());
263 <        }
264 <        finally {}
247 >        Integer[] empty = new Integer[0];
248 >        Integer[] ints = new Integer[SIZE];
249 >        for (int i = 0; i < SIZE; ++i)
250 >            ints[i] = new Integer(SIZE-1-i);
251 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
252 >        assertFalse(q.addAll(Arrays.asList(empty)));
253 >        assertTrue(q.addAll(Arrays.asList(ints)));
254 >        for (int i = 0; i < SIZE; ++i)
255 >            assertEquals(i, q.pollFirst());
256      }
257  
258      /**
# Line 270 | Line 261 | public class ConcurrentSkipListSetTest e
261      public void testPollFirst() {
262          ConcurrentSkipListSet q = populatedSet(SIZE);
263          for (int i = 0; i < SIZE; ++i) {
264 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
264 >            assertEquals(i, q.pollFirst());
265          }
266 <        assertNull(q.pollFirst());
266 >        assertNull(q.pollFirst());
267      }
268  
269      /**
# Line 281 | Line 272 | public class ConcurrentSkipListSetTest e
272      public void testPollLast() {
273          ConcurrentSkipListSet q = populatedSet(SIZE);
274          for (int i = SIZE-1; i >= 0; --i) {
275 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
275 >            assertEquals(i, q.pollLast());
276          }
277 <        assertNull(q.pollFirst());
277 >        assertNull(q.pollFirst());
278      }
279  
289
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)));
286 >            assertTrue(q.contains(i));
287 >            assertTrue(q.remove(i));
288 >            assertFalse(q.contains(i));
289 >            assertTrue(q.contains(i-1));
290          }
291          for (int i = 0; i < SIZE; i+=2) {
292 <            assertTrue(q.remove(new Integer(i)));
293 <            assertFalse(q.remove(new Integer(i+1)));
292 >            assertTrue(q.contains(i));
293 >            assertTrue(q.remove(i));
294 >            assertFalse(q.contains(i));
295 >            assertFalse(q.remove(i+1));
296 >            assertFalse(q.contains(i+1));
297          }
298          assertTrue(q.isEmpty());
299      }
300 <        
300 >
301      /**
302       * contains(x) reports true when elements added but not yet removed
303       */
# Line 377 | Line 373 | public class ConcurrentSkipListSetTest e
373          }
374      }
375  
380    
381
376      /**
377       * lower returns preceding element
378       */
# Line 395 | Line 389 | public class ConcurrentSkipListSetTest e
389  
390          Object e4 = q.lower(zero);
391          assertNull(e4);
398
392      }
393  
394      /**
# Line 414 | Line 407 | public class ConcurrentSkipListSetTest e
407  
408          Object e4 = q.higher(six);
409          assertNull(e4);
417
410      }
411  
412      /**
# Line 433 | Line 425 | public class ConcurrentSkipListSetTest e
425  
426          Object e4 = q.floor(zero);
427          assertNull(e4);
436
428      }
429  
430      /**
# Line 452 | Line 443 | public class ConcurrentSkipListSetTest e
443  
444          Object e4 = q.ceiling(six);
445          assertNull(e4);
455
446      }
447  
448      /**
449 <     * toArray contains all elements
449 >     * toArray contains all elements in sorted order
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++)
466 <            assertEquals(o[i], q.pollFirst());
453 >        Object[] o = q.toArray();
454 >        for (int i = 0; i < o.length; i++)
455 >            assertSame(o[i], q.pollFirst());
456      }
457  
458      /**
459 <     * toArray(a) contains all elements
459 >     * toArray(a) contains all elements in sorted order
460       */
461      public void testToArray2() {
462 <        ConcurrentSkipListSet q = populatedSet(SIZE);
463 <        Integer[] ints = new Integer[SIZE];
464 <        ints = (Integer[])q.toArray(ints);
465 <        Arrays.sort(ints);
466 <        for(int i = 0; i < ints.length; i++)
467 <            assertEquals(ints[i], q.pollFirst());
462 >        ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
463 >        Integer[] ints = new Integer[SIZE];
464 >        Integer[] array = q.toArray(ints);
465 >        assertSame(ints, array);
466 >        for (int i = 0; i < ints.length; i++)
467 >            assertSame(ints[i], q.pollFirst());
468      }
469 <    
469 >
470      /**
471       * iterator iterates through all elements
472       */
473      public void testIterator() {
474          ConcurrentSkipListSet q = populatedSet(SIZE);
475          int i = 0;
476 <        Iterator it = q.iterator();
477 <        while(it.hasNext()) {
476 >        Iterator it = q.iterator();
477 >        while (it.hasNext()) {
478              assertTrue(q.contains(it.next()));
479              ++i;
480          }
# Line 498 | Line 487 | public class ConcurrentSkipListSetTest e
487      public void testEmptyIterator() {
488          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
489          int i = 0;
490 <        Iterator it = q.iterator();
491 <        while(it.hasNext()) {
490 >        Iterator it = q.iterator();
491 >        while (it.hasNext()) {
492              assertTrue(q.contains(it.next()));
493              ++i;
494          }
495 <        assertEquals(i, 0);
495 >        assertEquals(0, i);
496      }
497  
498      /**
499       * iterator.remove removes current element
500       */
501 <    public void testIteratorRemove () {
501 >    public void testIteratorRemove() {
502          final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
503          q.add(new Integer(2));
504          q.add(new Integer(1));
# Line 525 | Line 514 | public class ConcurrentSkipListSetTest e
514          assertFalse(it.hasNext());
515      }
516  
528
517      /**
518       * toString contains toStrings of elements
519       */
# Line 533 | Line 521 | public class ConcurrentSkipListSetTest e
521          ConcurrentSkipListSet q = populatedSet(SIZE);
522          String s = q.toString();
523          for (int i = 0; i < SIZE; ++i) {
524 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
524 >            assertTrue(s.contains(String.valueOf(i)));
525          }
526 <    }        
526 >    }
527  
528      /**
529 <     * A deserialized serialized set has same elements
529 >     * A deserialized serialized set has same elements
530       */
531 <    public void testSerialization() {
532 <        ConcurrentSkipListSet q = populatedSet(SIZE);
533 <        try {
534 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
535 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
536 <            out.writeObject(q);
537 <            out.close();
538 <
539 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
540 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
541 <            ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
554 <            assertEquals(q.size(), r.size());
555 <            while (!q.isEmpty())
556 <                assertEquals(q.pollFirst(), r.pollFirst());
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
531 >    public void testSerialization() throws Exception {
532 >        NavigableSet x = populatedSet(SIZE);
533 >        NavigableSet y = serialClone(x);
534 >
535 >        assertTrue(x != y);
536 >        assertEquals(x.size(), y.size());
537 >        assertEquals(x, y);
538 >        assertEquals(y, x);
539 >        while (!x.isEmpty()) {
540 >            assertFalse(y.isEmpty());
541 >            assertEquals(x.pollFirst(), y.pollFirst());
542          }
543 +        assertTrue(y.isEmpty());
544      }
545  
546      /**
# Line 679 | Line 662 | public class ConcurrentSkipListSetTest e
662          assertEquals(4, set.size());
663      }
664  
665 +    Random rnd = new Random(666);
666 +
667 +    /**
668 +     * Subsets of subsets subdivide correctly
669 +     */
670 +    public void testRecursiveSubSets() throws Exception {
671 +        int setSize = expensiveTests ? 1000 : 100;
672 +        Class cl = ConcurrentSkipListSet.class;
673 +
674 +        NavigableSet<Integer> set = newSet(cl);
675 +        BitSet bs = new BitSet(setSize);
676 +
677 +        populate(set, setSize, bs);
678 +        check(set,                 0, setSize - 1, true, bs);
679 +        check(set.descendingSet(), 0, setSize - 1, false, bs);
680 +
681 +        mutateSet(set, 0, setSize - 1, bs);
682 +        check(set,                 0, setSize - 1, true, bs);
683 +        check(set.descendingSet(), 0, setSize - 1, false, bs);
684 +
685 +        bashSubSet(set.subSet(0, true, setSize, false),
686 +                   0, setSize - 1, true, bs);
687 +    }
688 +
689 +    static NavigableSet<Integer> newSet(Class cl) throws Exception {
690 +        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
691 +        assertEquals(0, result.size());
692 +        assertFalse(result.iterator().hasNext());
693 +        return result;
694 +    }
695 +
696 +    void populate(NavigableSet<Integer> set, int limit, BitSet bs) {
697 +        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
698 +            int element = rnd.nextInt(limit);
699 +            put(set, element, bs);
700 +        }
701 +    }
702 +
703 +    void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) {
704 +        int size = set.size();
705 +        int rangeSize = max - min + 1;
706 +
707 +        // Remove a bunch of entries directly
708 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
709 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
710 +        }
711 +
712 +        // Remove a bunch of entries with iterator
713 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
714 +            if (rnd.nextBoolean()) {
715 +                bs.clear(it.next());
716 +                it.remove();
717 +            }
718 +        }
719 +
720 +        // Add entries till we're back to original size
721 +        while (set.size() < size) {
722 +            int element = min + rnd.nextInt(rangeSize);
723 +            assertTrue(element >= min && element<= max);
724 +            put(set, element, bs);
725 +        }
726 +    }
727 +
728 +    void mutateSubSet(NavigableSet<Integer> set, int min, int max,
729 +                      BitSet bs) {
730 +        int size = set.size();
731 +        int rangeSize = max - min + 1;
732 +
733 +        // Remove a bunch of entries directly
734 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
735 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
736 +        }
737 +
738 +        // Remove a bunch of entries with iterator
739 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
740 +            if (rnd.nextBoolean()) {
741 +                bs.clear(it.next());
742 +                it.remove();
743 +            }
744 +        }
745 +
746 +        // Add entries till we're back to original size
747 +        while (set.size() < size) {
748 +            int element = min - 5 + rnd.nextInt(rangeSize + 10);
749 +            if (element >= min && element<= max) {
750 +                put(set, element, bs);
751 +            } else {
752 +                try {
753 +                    set.add(element);
754 +                    shouldThrow();
755 +                } catch (IllegalArgumentException success) {}
756 +            }
757 +        }
758 +    }
759 +
760 +    void put(NavigableSet<Integer> set, int element, BitSet bs) {
761 +        if (set.add(element))
762 +            bs.set(element);
763 +    }
764 +
765 +    void remove(NavigableSet<Integer> set, int element, BitSet bs) {
766 +        if (set.remove(element))
767 +            bs.clear(element);
768 +    }
769 +
770 +    void bashSubSet(NavigableSet<Integer> set,
771 +                    int min, int max, boolean ascending,
772 +                    BitSet bs) {
773 +        check(set, min, max, ascending, bs);
774 +        check(set.descendingSet(), min, max, !ascending, bs);
775 +
776 +        mutateSubSet(set, min, max, bs);
777 +        check(set, min, max, ascending, bs);
778 +        check(set.descendingSet(), min, max, !ascending, bs);
779 +
780 +        // Recurse
781 +        if (max - min < 2)
782 +            return;
783 +        int midPoint = (min + max) / 2;
784 +
785 +        // headSet - pick direction and endpoint inclusion randomly
786 +        boolean incl = rnd.nextBoolean();
787 +        NavigableSet<Integer> hm = set.headSet(midPoint, incl);
788 +        if (ascending) {
789 +            if (rnd.nextBoolean())
790 +                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs);
791 +            else
792 +                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
793 +                           false, bs);
794 +        } else {
795 +            if (rnd.nextBoolean())
796 +                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs);
797 +            else
798 +                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
799 +                           true, bs);
800 +        }
801 +
802 +        // tailSet - pick direction and endpoint inclusion randomly
803 +        incl = rnd.nextBoolean();
804 +        NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
805 +        if (ascending) {
806 +            if (rnd.nextBoolean())
807 +                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs);
808 +            else
809 +                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
810 +                           false, bs);
811 +        } else {
812 +            if (rnd.nextBoolean()) {
813 +                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs);
814 +            } else {
815 +                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
816 +                           true, bs);
817 +            }
818 +        }
819 +
820 +        // subSet - pick direction and endpoint inclusion randomly
821 +        int rangeSize = max - min + 1;
822 +        int[] endpoints = new int[2];
823 +        endpoints[0] = min + rnd.nextInt(rangeSize);
824 +        endpoints[1] = min + rnd.nextInt(rangeSize);
825 +        Arrays.sort(endpoints);
826 +        boolean lowIncl = rnd.nextBoolean();
827 +        boolean highIncl = rnd.nextBoolean();
828 +        if (ascending) {
829 +            NavigableSet<Integer> sm = set.subSet(
830 +                endpoints[0], lowIncl, endpoints[1], highIncl);
831 +            if (rnd.nextBoolean())
832 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
833 +                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
834 +            else
835 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
836 +                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
837 +        } else {
838 +            NavigableSet<Integer> sm = set.subSet(
839 +                endpoints[1], highIncl, endpoints[0], lowIncl);
840 +            if (rnd.nextBoolean())
841 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
842 +                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
843 +            else
844 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
845 +                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
846 +        }
847 +    }
848 +
849 +    /**
850 +     * min and max are both inclusive.  If max < min, interval is empty.
851 +     */
852 +    void check(NavigableSet<Integer> set,
853 +               final int min, final int max, final boolean ascending,
854 +               final BitSet bs) {
855 +        class ReferenceSet {
856 +            int lower(int element) {
857 +                return ascending ?
858 +                    lowerAscending(element) : higherAscending(element);
859 +            }
860 +            int floor(int element) {
861 +                return ascending ?
862 +                    floorAscending(element) : ceilingAscending(element);
863 +            }
864 +            int ceiling(int element) {
865 +                return ascending ?
866 +                    ceilingAscending(element) : floorAscending(element);
867 +            }
868 +            int higher(int element) {
869 +                return ascending ?
870 +                    higherAscending(element) : lowerAscending(element);
871 +            }
872 +            int first() {
873 +                return ascending ? firstAscending() : lastAscending();
874 +            }
875 +            int last() {
876 +                return ascending ? lastAscending() : firstAscending();
877 +            }
878 +            int lowerAscending(int element) {
879 +                return floorAscending(element - 1);
880 +            }
881 +            int floorAscending(int element) {
882 +                if (element < min)
883 +                    return -1;
884 +                else if (element > max)
885 +                    element = max;
886 +
887 +                // BitSet should support this! Test would run much faster
888 +                while (element >= min) {
889 +                    if (bs.get(element))
890 +                        return element;
891 +                    element--;
892 +                }
893 +                return -1;
894 +            }
895 +            int ceilingAscending(int element) {
896 +                if (element < min)
897 +                    element = min;
898 +                else if (element > max)
899 +                    return -1;
900 +                int result = bs.nextSetBit(element);
901 +                return result > max ? -1 : result;
902 +            }
903 +            int higherAscending(int element) {
904 +                return ceilingAscending(element + 1);
905 +            }
906 +            private int firstAscending() {
907 +                int result = ceilingAscending(min);
908 +                return result > max ? -1 : result;
909 +            }
910 +            private int lastAscending() {
911 +                int result = floorAscending(max);
912 +                return result < min ? -1 : result;
913 +            }
914 +        }
915 +        ReferenceSet rs = new ReferenceSet();
916 +
917 +        // Test contents using containsElement
918 +        int size = 0;
919 +        for (int i = min; i <= max; i++) {
920 +            boolean bsContainsI = bs.get(i);
921 +            assertEquals(bsContainsI, set.contains(i));
922 +            if (bsContainsI)
923 +                size++;
924 +        }
925 +        assertEquals(size, set.size());
926 +
927 +        // Test contents using contains elementSet iterator
928 +        int size2 = 0;
929 +        int previousElement = -1;
930 +        for (int element : set) {
931 +            assertTrue(bs.get(element));
932 +            size2++;
933 +            assertTrue(previousElement < 0 || (ascending ?
934 +                element - previousElement > 0 : element - previousElement < 0));
935 +            previousElement = element;
936 +        }
937 +        assertEquals(size2, size);
938 +
939 +        // Test navigation ops
940 +        for (int element = min - 1; element <= max + 1; element++) {
941 +            assertEq(set.lower(element), rs.lower(element));
942 +            assertEq(set.floor(element), rs.floor(element));
943 +            assertEq(set.higher(element), rs.higher(element));
944 +            assertEq(set.ceiling(element), rs.ceiling(element));
945 +        }
946 +
947 +        // Test extrema
948 +        if (set.size() != 0) {
949 +            assertEq(set.first(), rs.first());
950 +            assertEq(set.last(), rs.last());
951 +        } else {
952 +            assertEq(rs.first(), -1);
953 +            assertEq(rs.last(),  -1);
954 +            try {
955 +                set.first();
956 +                shouldThrow();
957 +            } catch (NoSuchElementException success) {}
958 +            try {
959 +                set.last();
960 +                shouldThrow();
961 +            } catch (NoSuchElementException success) {}
962 +        }
963 +    }
964 +
965 +    static void assertEq(Integer i, int j) {
966 +        if (i == null)
967 +            assertEquals(j, -1);
968 +        else
969 +            assertEquals((int) i, j);
970 +    }
971 +
972 +    static boolean eq(Integer i, int j) {
973 +        return i == null ? j == -1 : i == j;
974 +    }
975 +
976   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines