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

Comparing jsr166/src/test/tck/TreeSetTest.java (file contents):
Revision 1.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.28 by jsr166, Tue Apr 2 22:18:26 2013 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.TreeSet;
19  
20   public class TreeSetTest 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(TreeSetTest.class);
25 >        return new TestSuite(TreeSetTest.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 33 | Line 37 | public class TreeSetTest extends JSR166T
37      static final int SIZE = 20;
38  
39      /**
40 <     * Create a set of given size containing consecutive
40 >     * Returns a new set of given size containing consecutive
41       * Integers 0 ... n.
42       */
43 <    private TreeSet populatedSet(int n) {
44 <        TreeSet q = new TreeSet();
43 >    private TreeSet<Integer> populatedSet(int n) {
44 >        TreeSet<Integer> q = new TreeSet<Integer>();
45          assertTrue(q.isEmpty());
46 <        for(int i = n-1; i >= 0; i-=2)
47 <            assertTrue(q.add(new Integer(i)));
48 <        for(int i = (n & 1); i < n; i+=2)
49 <            assertTrue(q.add(new Integer(i)));
46 >        for (int i = n-1; i >= 0; i-=2)
47 >            assertTrue(q.add(new Integer(i)));
48 >        for (int i = (n & 1); i < n; i+=2)
49 >            assertTrue(q.add(new Integer(i)));
50          assertFalse(q.isEmpty());
51 <        assertEquals(n, q.size());
51 >        assertEquals(n, q.size());
52          return q;
53      }
54  
55      /**
56 <     * Create set of first 5 ints
56 >     * Returns a new set of first 5 ints.
57       */
58      private TreeSet set5() {
59          TreeSet q = new TreeSet();
# Line 59 | Line 63 | public class TreeSetTest extends JSR166T
63          q.add(three);
64          q.add(four);
65          q.add(five);
66 <        assertEquals(5, q.size());
66 >        assertEquals(5, q.size());
67          return q;
68      }
69 <
69 >
70      /**
71       * A new set has unbounded capacity
72       */
# Line 77 | Line 81 | public class TreeSetTest extends JSR166T
81          try {
82              TreeSet q = new TreeSet((Collection)null);
83              shouldThrow();
84 <        }
81 <        catch (NullPointerException success) {}
84 >        } catch (NullPointerException success) {}
85      }
86  
87      /**
# Line 89 | Line 92 | public class TreeSetTest extends JSR166T
92              Integer[] ints = new Integer[SIZE];
93              TreeSet q = new TreeSet(Arrays.asList(ints));
94              shouldThrow();
95 <        }
93 <        catch (NullPointerException success) {}
95 >        } catch (NullPointerException success) {}
96      }
97  
98      /**
# Line 103 | Line 105 | public class TreeSetTest extends JSR166T
105                  ints[i] = new Integer(i);
106              TreeSet q = new TreeSet(Arrays.asList(ints));
107              shouldThrow();
108 <        }
107 <        catch (NullPointerException success) {}
108 >        } catch (NullPointerException success) {}
109      }
110  
111      /**
112       * Set contains all elements of collection used to initialize
113       */
114      public void testConstructor6() {
115 <        try {
116 <            Integer[] ints = new Integer[SIZE];
117 <            for (int i = 0; i < SIZE; ++i)
118 <                ints[i] = new Integer(i);
119 <            TreeSet q = new TreeSet(Arrays.asList(ints));
120 <            for (int i = 0; i < SIZE; ++i)
120 <                assertEquals(ints[i], q.pollFirst());
121 <        }
122 <        finally {}
115 >        Integer[] ints = new Integer[SIZE];
116 >        for (int i = 0; i < SIZE; ++i)
117 >            ints[i] = new Integer(i);
118 >        TreeSet q = new TreeSet(Arrays.asList(ints));
119 >        for (int i = 0; i < SIZE; ++i)
120 >            assertEquals(ints[i], q.pollFirst());
121      }
122  
123      /**
124       * The comparator used in constructor is used
125       */
126      public void testConstructor7() {
127 <        try {
128 <            MyReverseComparator cmp = new MyReverseComparator();
129 <            TreeSet q = new TreeSet(cmp);
130 <            assertEquals(cmp, q.comparator());
131 <            Integer[] ints = new Integer[SIZE];
132 <            for (int i = 0; i < SIZE; ++i)
133 <                ints[i] = new Integer(i);
134 <            q.addAll(Arrays.asList(ints));
135 <            for (int i = SIZE-1; i >= 0; --i)
138 <                assertEquals(ints[i], q.pollFirst());
139 <        }
140 <        finally {}
127 >        MyReverseComparator cmp = new MyReverseComparator();
128 >        TreeSet q = new TreeSet(cmp);
129 >        assertEquals(cmp, q.comparator());
130 >        Integer[] ints = new Integer[SIZE];
131 >        for (int i = 0; i < SIZE; ++i)
132 >            ints[i] = new Integer(i);
133 >        q.addAll(Arrays.asList(ints));
134 >        for (int i = SIZE-1; i >= 0; --i)
135 >            assertEquals(ints[i], q.pollFirst());
136      }
137  
138      /**
# Line 173 | Line 168 | public class TreeSetTest extends JSR166T
168       * add(null) throws NPE if nonempty
169       */
170      public void testAddNull() {
171 <        try {
171 >        try {
172              TreeSet q = populatedSet(SIZE);
173              q.add(null);
174              shouldThrow();
175 <        } catch (NullPointerException success) { }  
175 >        } catch (NullPointerException success) {}
176      }
177  
178      /**
# Line 208 | Line 203 | public class TreeSetTest extends JSR166T
203              q.add(new Object());
204              q.add(new Object());
205              shouldThrow();
206 <        }
212 <        catch(ClassCastException success) {}
206 >        } catch (ClassCastException success) {}
207      }
208  
209      /**
# Line 220 | Line 214 | public class TreeSetTest extends JSR166T
214              TreeSet q = new TreeSet();
215              q.addAll(null);
216              shouldThrow();
217 <        }
224 <        catch (NullPointerException success) {}
217 >        } catch (NullPointerException success) {}
218      }
219 +
220      /**
221       * addAll of a collection with null elements throws NPE
222       */
# Line 232 | Line 226 | public class TreeSetTest extends JSR166T
226              Integer[] ints = new Integer[SIZE];
227              q.addAll(Arrays.asList(ints));
228              shouldThrow();
229 <        }
236 <        catch (NullPointerException success) {}
229 >        } catch (NullPointerException success) {}
230      }
231 +
232      /**
233       * addAll of a collection with any null elements throws NPE after
234       * possibly adding some elements
# Line 247 | Line 241 | public class TreeSetTest extends JSR166T
241                  ints[i] = new Integer(i);
242              q.addAll(Arrays.asList(ints));
243              shouldThrow();
244 <        }
251 <        catch (NullPointerException success) {}
244 >        } catch (NullPointerException success) {}
245      }
246  
247      /**
248       * Set contains all elements of successful addAll
249       */
250      public void testAddAll5() {
251 <        try {
252 <            Integer[] empty = new Integer[0];
253 <            Integer[] ints = new Integer[SIZE];
254 <            for (int i = 0; i < SIZE; ++i)
255 <                ints[i] = new Integer(SIZE-1-i);
256 <            TreeSet q = new TreeSet();
257 <            assertFalse(q.addAll(Arrays.asList(empty)));
258 <            assertTrue(q.addAll(Arrays.asList(ints)));
259 <            for (int i = 0; i < SIZE; ++i)
267 <                assertEquals(new Integer(i), q.pollFirst());
268 <        }
269 <        finally {}
251 >        Integer[] empty = new Integer[0];
252 >        Integer[] ints = new Integer[SIZE];
253 >        for (int i = 0; i < SIZE; ++i)
254 >            ints[i] = new Integer(SIZE-1-i);
255 >        TreeSet q = new TreeSet();
256 >        assertFalse(q.addAll(Arrays.asList(empty)));
257 >        assertTrue(q.addAll(Arrays.asList(ints)));
258 >        for (int i = 0; i < SIZE; ++i)
259 >            assertEquals(new Integer(i), q.pollFirst());
260      }
261  
262      /**
# Line 275 | Line 265 | public class TreeSetTest extends JSR166T
265      public void testPollFirst() {
266          TreeSet q = populatedSet(SIZE);
267          for (int i = 0; i < SIZE; ++i) {
268 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
268 >            assertEquals(i, q.pollFirst());
269          }
270 <        assertNull(q.pollFirst());
270 >        assertNull(q.pollFirst());
271      }
272  
273      /**
# Line 286 | Line 276 | public class TreeSetTest extends JSR166T
276      public void testPollLast() {
277          TreeSet q = populatedSet(SIZE);
278          for (int i = SIZE-1; i >= 0; --i) {
279 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
279 >            assertEquals(i, q.pollLast());
280          }
281 <        assertNull(q.pollFirst());
281 >        assertNull(q.pollFirst());
282      }
283  
294
284      /**
285       * remove(x) removes x and returns true if present
286       */
287      public void testRemoveElement() {
288          TreeSet q = populatedSet(SIZE);
289          for (int i = 1; i < SIZE; i+=2) {
290 <            assertTrue(q.remove(new Integer(i)));
290 >            assertTrue(q.contains(i));
291 >            assertTrue(q.remove(i));
292 >            assertFalse(q.contains(i));
293 >            assertTrue(q.contains(i-1));
294          }
295          for (int i = 0; i < SIZE; i+=2) {
296 <            assertTrue(q.remove(new Integer(i)));
297 <            assertFalse(q.remove(new Integer(i+1)));
296 >            assertTrue(q.contains(i));
297 >            assertTrue(q.remove(i));
298 >            assertFalse(q.contains(i));
299 >            assertFalse(q.remove(i+1));
300 >            assertFalse(q.contains(i+1));
301          }
302          assertTrue(q.isEmpty());
303      }
304 <        
304 >
305      /**
306       * contains(x) reports true when elements added but not yet removed
307       */
# Line 382 | Line 377 | public class TreeSetTest extends JSR166T
377          }
378      }
379  
385    
386
380      /**
381       * lower returns preceding element
382       */
# Line 400 | Line 393 | public class TreeSetTest extends JSR166T
393  
394          Object e4 = q.lower(zero);
395          assertNull(e4);
403
396      }
397  
398      /**
# Line 419 | Line 411 | public class TreeSetTest extends JSR166T
411  
412          Object e4 = q.higher(six);
413          assertNull(e4);
422
414      }
415  
416      /**
# Line 438 | Line 429 | public class TreeSetTest extends JSR166T
429  
430          Object e4 = q.floor(zero);
431          assertNull(e4);
441
432      }
433  
434      /**
# Line 457 | Line 447 | public class TreeSetTest extends JSR166T
447  
448          Object e4 = q.ceiling(six);
449          assertNull(e4);
460
450      }
451  
452      /**
453 <     * toArray contains all elements
453 >     * toArray contains all elements in sorted order
454       */
455      public void testToArray() {
456          TreeSet q = populatedSet(SIZE);
457 <        Object[] o = q.toArray();
458 <        Arrays.sort(o);
459 <        for(int i = 0; i < o.length; i++)
471 <            assertEquals(o[i], q.pollFirst());
457 >        Object[] o = q.toArray();
458 >        for (int i = 0; i < o.length; i++)
459 >            assertSame(o[i], q.pollFirst());
460      }
461  
462      /**
463 <     * toArray(a) contains all elements
463 >     * toArray(a) contains all elements in sorted order
464       */
465      public void testToArray2() {
466 <        TreeSet q = populatedSet(SIZE);
467 <        Integer[] ints = new Integer[SIZE];
468 <        ints = (Integer[])q.toArray(ints);
469 <        Arrays.sort(ints);
470 <        for(int i = 0; i < ints.length; i++)
471 <            assertEquals(ints[i], q.pollFirst());
466 >        TreeSet<Integer> q = populatedSet(SIZE);
467 >        Integer[] ints = new Integer[SIZE];
468 >        Integer[] array = q.toArray(ints);
469 >        assertSame(ints, array);
470 >        for (int i = 0; i < ints.length; i++)
471 >            assertSame(ints[i], q.pollFirst());
472      }
473 <    
473 >
474      /**
475       * iterator iterates through all elements
476       */
477      public void testIterator() {
478          TreeSet q = populatedSet(SIZE);
479          int i = 0;
480 <        Iterator it = q.iterator();
481 <        while(it.hasNext()) {
480 >        Iterator it = q.iterator();
481 >        while (it.hasNext()) {
482              assertTrue(q.contains(it.next()));
483              ++i;
484          }
# Line 503 | Line 491 | public class TreeSetTest extends JSR166T
491      public void testEmptyIterator() {
492          TreeSet q = new TreeSet();
493          int i = 0;
494 <        Iterator it = q.iterator();
495 <        while(it.hasNext()) {
494 >        Iterator it = q.iterator();
495 >        while (it.hasNext()) {
496              assertTrue(q.contains(it.next()));
497              ++i;
498          }
499 <        assertEquals(i, 0);
499 >        assertEquals(0, i);
500      }
501  
502      /**
503       * iterator.remove removes current element
504       */
505 <    public void testIteratorRemove () {
505 >    public void testIteratorRemove() {
506          final TreeSet q = new TreeSet();
507          q.add(new Integer(2));
508          q.add(new Integer(1));
# Line 530 | Line 518 | public class TreeSetTest extends JSR166T
518          assertFalse(it.hasNext());
519      }
520  
533
521      /**
522       * toString contains toStrings of elements
523       */
# Line 538 | Line 525 | public class TreeSetTest extends JSR166T
525          TreeSet q = populatedSet(SIZE);
526          String s = q.toString();
527          for (int i = 0; i < SIZE; ++i) {
528 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
528 >            assertTrue(s.contains(String.valueOf(i)));
529          }
530 <    }        
530 >    }
531  
532      /**
533 <     * A deserialized serialized set has same elements
533 >     * A deserialized serialized set has same elements
534       */
535 <    public void testSerialization() {
536 <        TreeSet q = populatedSet(SIZE);
537 <        try {
538 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
539 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
540 <            out.writeObject(q);
541 <            out.close();
542 <
543 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
544 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
545 <            TreeSet r = (TreeSet)in.readObject();
559 <            assertEquals(q.size(), r.size());
560 <            while (!q.isEmpty())
561 <                assertEquals(q.pollFirst(), r.pollFirst());
562 <        } catch(Exception e){
563 <            e.printStackTrace();
564 <            unexpectedException();
535 >    public void testSerialization() throws Exception {
536 >        NavigableSet x = populatedSet(SIZE);
537 >        NavigableSet y = serialClone(x);
538 >
539 >        assertTrue(x != y);
540 >        assertEquals(x.size(), y.size());
541 >        assertEquals(x, y);
542 >        assertEquals(y, x);
543 >        while (!x.isEmpty()) {
544 >            assertFalse(y.isEmpty());
545 >            assertEquals(x.pollFirst(), y.pollFirst());
546          }
547 +        assertTrue(y.isEmpty());
548      }
549  
550      /**
# Line 684 | Line 666 | public class TreeSetTest extends JSR166T
666          assertEquals(4, set.size());
667      }
668  
669 +    Random rnd = new Random(666);
670 +    BitSet bs;
671 +
672 +    /**
673 +     * Subsets of subsets subdivide correctly
674 +     */
675 +    public void testRecursiveSubSets() throws Exception {
676 +        int setSize = expensiveTests ? 1000 : 100;
677 +        Class cl = TreeSet.class;
678 +
679 +        NavigableSet<Integer> set = newSet(cl);
680 +        bs = new BitSet(setSize);
681 +
682 +        populate(set, setSize);
683 +        check(set,                 0, setSize - 1, true);
684 +        check(set.descendingSet(), 0, setSize - 1, false);
685 +
686 +        mutateSet(set, 0, setSize - 1);
687 +        check(set,                 0, setSize - 1, true);
688 +        check(set.descendingSet(), 0, setSize - 1, false);
689 +
690 +        bashSubSet(set.subSet(0, true, setSize, false),
691 +                   0, setSize - 1, true);
692 +    }
693 +
694 +    /**
695 +     * addAll is idempotent
696 +     */
697 +    public void testAddAll_idempotent() throws Exception {
698 +        Set x = populatedSet(SIZE);
699 +        Set y = new TreeSet(x);
700 +        y.addAll(x);
701 +        assertEquals(x, y);
702 +        assertEquals(y, x);
703 +    }
704 +
705 +    static NavigableSet<Integer> newSet(Class cl) throws Exception {
706 +        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
707 +        assertEquals(0, result.size());
708 +        assertFalse(result.iterator().hasNext());
709 +        return result;
710 +    }
711 +
712 +    void populate(NavigableSet<Integer> set, int limit) {
713 +        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
714 +            int element = rnd.nextInt(limit);
715 +            put(set, element);
716 +        }
717 +    }
718 +
719 +    void mutateSet(NavigableSet<Integer> set, int min, int max) {
720 +        int size = set.size();
721 +        int rangeSize = max - min + 1;
722 +
723 +        // Remove a bunch of entries directly
724 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
725 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
726 +        }
727 +
728 +        // Remove a bunch of entries with iterator
729 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
730 +            if (rnd.nextBoolean()) {
731 +                bs.clear(it.next());
732 +                it.remove();
733 +            }
734 +        }
735 +
736 +        // Add entries till we're back to original size
737 +        while (set.size() < size) {
738 +            int element = min + rnd.nextInt(rangeSize);
739 +            assertTrue(element >= min && element<= max);
740 +            put(set, element);
741 +        }
742 +    }
743 +
744 +    void mutateSubSet(NavigableSet<Integer> set, int min, int max) {
745 +        int size = set.size();
746 +        int rangeSize = max - min + 1;
747 +
748 +        // Remove a bunch of entries directly
749 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
750 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
751 +        }
752 +
753 +        // Remove a bunch of entries with iterator
754 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
755 +            if (rnd.nextBoolean()) {
756 +                bs.clear(it.next());
757 +                it.remove();
758 +            }
759 +        }
760 +
761 +        // Add entries till we're back to original size
762 +        while (set.size() < size) {
763 +            int element = min - 5 + rnd.nextInt(rangeSize + 10);
764 +            if (element >= min && element<= max) {
765 +                put(set, element);
766 +            } else {
767 +                try {
768 +                    set.add(element);
769 +                    shouldThrow();
770 +                } catch (IllegalArgumentException success) {}
771 +            }
772 +        }
773 +    }
774 +
775 +    void put(NavigableSet<Integer> set, int element) {
776 +        if (set.add(element))
777 +            bs.set(element);
778 +    }
779 +
780 +    void remove(NavigableSet<Integer> set, int element) {
781 +        if (set.remove(element))
782 +            bs.clear(element);
783 +    }
784 +
785 +    void bashSubSet(NavigableSet<Integer> set,
786 +                    int min, int max, boolean ascending) {
787 +        check(set, min, max, ascending);
788 +        check(set.descendingSet(), min, max, !ascending);
789 +
790 +        mutateSubSet(set, min, max);
791 +        check(set, min, max, ascending);
792 +        check(set.descendingSet(), min, max, !ascending);
793 +
794 +        // Recurse
795 +        if (max - min < 2)
796 +            return;
797 +        int midPoint = (min + max) / 2;
798 +
799 +        // headSet - pick direction and endpoint inclusion randomly
800 +        boolean incl = rnd.nextBoolean();
801 +        NavigableSet<Integer> hm = set.headSet(midPoint, incl);
802 +        if (ascending) {
803 +            if (rnd.nextBoolean())
804 +                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
805 +            else
806 +                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
807 +                           false);
808 +        } else {
809 +            if (rnd.nextBoolean())
810 +                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
811 +            else
812 +                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
813 +                           true);
814 +        }
815 +
816 +        // tailSet - pick direction and endpoint inclusion randomly
817 +        incl = rnd.nextBoolean();
818 +        NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
819 +        if (ascending) {
820 +            if (rnd.nextBoolean())
821 +                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
822 +            else
823 +                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
824 +                           false);
825 +        } else {
826 +            if (rnd.nextBoolean()) {
827 +                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
828 +            } else {
829 +                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
830 +                           true);
831 +            }
832 +        }
833 +
834 +        // subSet - pick direction and endpoint inclusion randomly
835 +        int rangeSize = max - min + 1;
836 +        int[] endpoints = new int[2];
837 +        endpoints[0] = min + rnd.nextInt(rangeSize);
838 +        endpoints[1] = min + rnd.nextInt(rangeSize);
839 +        Arrays.sort(endpoints);
840 +        boolean lowIncl = rnd.nextBoolean();
841 +        boolean highIncl = rnd.nextBoolean();
842 +        if (ascending) {
843 +            NavigableSet<Integer> sm = set.subSet(
844 +                endpoints[0], lowIncl, endpoints[1], highIncl);
845 +            if (rnd.nextBoolean())
846 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
847 +                           endpoints[1] - (highIncl ? 0 : 1), true);
848 +            else
849 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
850 +                           endpoints[1] - (highIncl ? 0 : 1), false);
851 +        } else {
852 +            NavigableSet<Integer> sm = set.subSet(
853 +                endpoints[1], highIncl, endpoints[0], lowIncl);
854 +            if (rnd.nextBoolean())
855 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
856 +                           endpoints[1] - (highIncl ? 0 : 1), false);
857 +            else
858 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
859 +                           endpoints[1] - (highIncl ? 0 : 1), true);
860 +        }
861 +    }
862 +
863 +    /**
864 +     * min and max are both inclusive.  If max < min, interval is empty.
865 +     */
866 +    void check(NavigableSet<Integer> set,
867 +                      final int min, final int max, final boolean ascending) {
868 +        class ReferenceSet {
869 +            int lower(int element) {
870 +                return ascending ?
871 +                    lowerAscending(element) : higherAscending(element);
872 +            }
873 +            int floor(int element) {
874 +                return ascending ?
875 +                    floorAscending(element) : ceilingAscending(element);
876 +            }
877 +            int ceiling(int element) {
878 +                return ascending ?
879 +                    ceilingAscending(element) : floorAscending(element);
880 +            }
881 +            int higher(int element) {
882 +                return ascending ?
883 +                    higherAscending(element) : lowerAscending(element);
884 +            }
885 +            int first() {
886 +                return ascending ? firstAscending() : lastAscending();
887 +            }
888 +            int last() {
889 +                return ascending ? lastAscending() : firstAscending();
890 +            }
891 +            int lowerAscending(int element) {
892 +                return floorAscending(element - 1);
893 +            }
894 +            int floorAscending(int element) {
895 +                if (element < min)
896 +                    return -1;
897 +                else if (element > max)
898 +                    element = max;
899 +
900 +                // BitSet should support this! Test would run much faster
901 +                while (element >= min) {
902 +                    if (bs.get(element))
903 +                        return element;
904 +                    element--;
905 +                }
906 +                return -1;
907 +            }
908 +            int ceilingAscending(int element) {
909 +                if (element < min)
910 +                    element = min;
911 +                else if (element > max)
912 +                    return -1;
913 +                int result = bs.nextSetBit(element);
914 +                return result > max ? -1 : result;
915 +            }
916 +            int higherAscending(int element) {
917 +                return ceilingAscending(element + 1);
918 +            }
919 +            private int firstAscending() {
920 +                int result = ceilingAscending(min);
921 +                return result > max ? -1 : result;
922 +            }
923 +            private int lastAscending() {
924 +                int result = floorAscending(max);
925 +                return result < min ? -1 : result;
926 +            }
927 +        }
928 +        ReferenceSet rs = new ReferenceSet();
929 +
930 +        // Test contents using containsElement
931 +        int size = 0;
932 +        for (int i = min; i <= max; i++) {
933 +            boolean bsContainsI = bs.get(i);
934 +            assertEquals(bsContainsI, set.contains(i));
935 +            if (bsContainsI)
936 +                size++;
937 +        }
938 +        assertEquals(size, set.size());
939 +
940 +        // Test contents using contains elementSet iterator
941 +        int size2 = 0;
942 +        int previousElement = -1;
943 +        for (int element : set) {
944 +            assertTrue(bs.get(element));
945 +            size2++;
946 +            assertTrue(previousElement < 0 || (ascending ?
947 +                element - previousElement > 0 : element - previousElement < 0));
948 +            previousElement = element;
949 +        }
950 +        assertEquals(size2, size);
951 +
952 +        // Test navigation ops
953 +        for (int element = min - 1; element <= max + 1; element++) {
954 +            assertEq(set.lower(element), rs.lower(element));
955 +            assertEq(set.floor(element), rs.floor(element));
956 +            assertEq(set.higher(element), rs.higher(element));
957 +            assertEq(set.ceiling(element), rs.ceiling(element));
958 +        }
959 +
960 +        // Test extrema
961 +        if (set.size() != 0) {
962 +            assertEq(set.first(), rs.first());
963 +            assertEq(set.last(), rs.last());
964 +        } else {
965 +            assertEq(rs.first(), -1);
966 +            assertEq(rs.last(),  -1);
967 +            try {
968 +                set.first();
969 +                shouldThrow();
970 +            } catch (NoSuchElementException success) {}
971 +            try {
972 +                set.last();
973 +                shouldThrow();
974 +            } catch (NoSuchElementException success) {}
975 +        }
976 +    }
977 +
978 +    static void assertEq(Integer i, int j) {
979 +        if (i == null)
980 +            assertEquals(j, -1);
981 +        else
982 +            assertEquals((int) i, j);
983 +    }
984 +
985 +    static boolean eq(Integer i, int j) {
986 +        return i == null ? j == -1 : i == j;
987 +    }
988 +
989   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines