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.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.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 36 | Line 40 | public class TreeSetTest extends JSR166T
40       * Create a 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  
# 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 +    static NavigableSet<Integer> newSet(Class cl) throws Exception {
695 +        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
696 +        assertEquals(0, result.size());
697 +        assertFalse(result.iterator().hasNext());
698 +        return result;
699 +    }
700 +
701 +    void populate(NavigableSet<Integer> set, int limit) {
702 +        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
703 +            int element = rnd.nextInt(limit);
704 +            put(set, element);
705 +        }
706 +    }
707 +
708 +    void mutateSet(NavigableSet<Integer> set, int min, int max) {
709 +        int size = set.size();
710 +        int rangeSize = max - min + 1;
711 +
712 +        // Remove a bunch of entries directly
713 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
714 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
715 +        }
716 +
717 +        // Remove a bunch of entries with iterator
718 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
719 +            if (rnd.nextBoolean()) {
720 +                bs.clear(it.next());
721 +                it.remove();
722 +            }
723 +        }
724 +
725 +        // Add entries till we're back to original size
726 +        while (set.size() < size) {
727 +            int element = min + rnd.nextInt(rangeSize);
728 +            assertTrue(element >= min && element<= max);
729 +            put(set, element);
730 +        }
731 +    }
732 +
733 +    void mutateSubSet(NavigableSet<Integer> set, int min, int max) {
734 +        int size = set.size();
735 +        int rangeSize = max - min + 1;
736 +
737 +        // Remove a bunch of entries directly
738 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
739 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
740 +        }
741 +
742 +        // Remove a bunch of entries with iterator
743 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
744 +            if (rnd.nextBoolean()) {
745 +                bs.clear(it.next());
746 +                it.remove();
747 +            }
748 +        }
749 +
750 +        // Add entries till we're back to original size
751 +        while (set.size() < size) {
752 +            int element = min - 5 + rnd.nextInt(rangeSize + 10);
753 +            if (element >= min && element<= max) {
754 +                put(set, element);
755 +            } else {
756 +                try {
757 +                    set.add(element);
758 +                    shouldThrow();
759 +                } catch (IllegalArgumentException success) {}
760 +            }
761 +        }
762 +    }
763 +
764 +    void put(NavigableSet<Integer> set, int element) {
765 +        if (set.add(element))
766 +            bs.set(element);
767 +    }
768 +
769 +    void remove(NavigableSet<Integer> set, int element) {
770 +        if (set.remove(element))
771 +            bs.clear(element);
772 +    }
773 +
774 +    void bashSubSet(NavigableSet<Integer> set,
775 +                    int min, int max, boolean ascending) {
776 +        check(set, min, max, ascending);
777 +        check(set.descendingSet(), min, max, !ascending);
778 +
779 +        mutateSubSet(set, min, max);
780 +        check(set, min, max, ascending);
781 +        check(set.descendingSet(), min, max, !ascending);
782 +
783 +        // Recurse
784 +        if (max - min < 2)
785 +            return;
786 +        int midPoint = (min + max) / 2;
787 +
788 +        // headSet - pick direction and endpoint inclusion randomly
789 +        boolean incl = rnd.nextBoolean();
790 +        NavigableSet<Integer> hm = set.headSet(midPoint, incl);
791 +        if (ascending) {
792 +            if (rnd.nextBoolean())
793 +                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
794 +            else
795 +                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
796 +                           false);
797 +        } else {
798 +            if (rnd.nextBoolean())
799 +                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
800 +            else
801 +                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
802 +                           true);
803 +        }
804 +
805 +        // tailSet - pick direction and endpoint inclusion randomly
806 +        incl = rnd.nextBoolean();
807 +        NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
808 +        if (ascending) {
809 +            if (rnd.nextBoolean())
810 +                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
811 +            else
812 +                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
813 +                           false);
814 +        } else {
815 +            if (rnd.nextBoolean()) {
816 +                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
817 +            } else {
818 +                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
819 +                           true);
820 +            }
821 +        }
822 +
823 +        // subSet - pick direction and endpoint inclusion randomly
824 +        int rangeSize = max - min + 1;
825 +        int[] endpoints = new int[2];
826 +        endpoints[0] = min + rnd.nextInt(rangeSize);
827 +        endpoints[1] = min + rnd.nextInt(rangeSize);
828 +        Arrays.sort(endpoints);
829 +        boolean lowIncl = rnd.nextBoolean();
830 +        boolean highIncl = rnd.nextBoolean();
831 +        if (ascending) {
832 +            NavigableSet<Integer> sm = set.subSet(
833 +                endpoints[0], lowIncl, endpoints[1], highIncl);
834 +            if (rnd.nextBoolean())
835 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
836 +                           endpoints[1] - (highIncl ? 0 : 1), true);
837 +            else
838 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
839 +                           endpoints[1] - (highIncl ? 0 : 1), false);
840 +        } else {
841 +            NavigableSet<Integer> sm = set.subSet(
842 +                endpoints[1], highIncl, endpoints[0], lowIncl);
843 +            if (rnd.nextBoolean())
844 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
845 +                           endpoints[1] - (highIncl ? 0 : 1), false);
846 +            else
847 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
848 +                           endpoints[1] - (highIncl ? 0 : 1), true);
849 +        }
850 +    }
851 +
852 +    /**
853 +     * min and max are both inclusive.  If max < min, interval is empty.
854 +     */
855 +    void check(NavigableSet<Integer> set,
856 +                      final int min, final int max, final boolean ascending) {
857 +        class ReferenceSet {
858 +            int lower(int element) {
859 +                return ascending ?
860 +                    lowerAscending(element) : higherAscending(element);
861 +            }
862 +            int floor(int element) {
863 +                return ascending ?
864 +                    floorAscending(element) : ceilingAscending(element);
865 +            }
866 +            int ceiling(int element) {
867 +                return ascending ?
868 +                    ceilingAscending(element) : floorAscending(element);
869 +            }
870 +            int higher(int element) {
871 +                return ascending ?
872 +                    higherAscending(element) : lowerAscending(element);
873 +            }
874 +            int first() {
875 +                return ascending ? firstAscending() : lastAscending();
876 +            }
877 +            int last() {
878 +                return ascending ? lastAscending() : firstAscending();
879 +            }
880 +            int lowerAscending(int element) {
881 +                return floorAscending(element - 1);
882 +            }
883 +            int floorAscending(int element) {
884 +                if (element < min)
885 +                    return -1;
886 +                else if (element > max)
887 +                    element = max;
888 +
889 +                // BitSet should support this! Test would run much faster
890 +                while (element >= min) {
891 +                    if (bs.get(element))
892 +                        return element;
893 +                    element--;
894 +                }
895 +                return -1;
896 +            }
897 +            int ceilingAscending(int element) {
898 +                if (element < min)
899 +                    element = min;
900 +                else if (element > max)
901 +                    return -1;
902 +                int result = bs.nextSetBit(element);
903 +                return result > max ? -1 : result;
904 +            }
905 +            int higherAscending(int element) {
906 +                return ceilingAscending(element + 1);
907 +            }
908 +            private int firstAscending() {
909 +                int result = ceilingAscending(min);
910 +                return result > max ? -1 : result;
911 +            }
912 +            private int lastAscending() {
913 +                int result = floorAscending(max);
914 +                return result < min ? -1 : result;
915 +            }
916 +        }
917 +        ReferenceSet rs = new ReferenceSet();
918 +
919 +        // Test contents using containsElement
920 +        int size = 0;
921 +        for (int i = min; i <= max; i++) {
922 +            boolean bsContainsI = bs.get(i);
923 +            assertEquals(bsContainsI, set.contains(i));
924 +            if (bsContainsI)
925 +                size++;
926 +        }
927 +        assertEquals(size, set.size());
928 +
929 +        // Test contents using contains elementSet iterator
930 +        int size2 = 0;
931 +        int previousElement = -1;
932 +        for (int element : set) {
933 +            assertTrue(bs.get(element));
934 +            size2++;
935 +            assertTrue(previousElement < 0 || (ascending ?
936 +                element - previousElement > 0 : element - previousElement < 0));
937 +            previousElement = element;
938 +        }
939 +        assertEquals(size2, size);
940 +
941 +        // Test navigation ops
942 +        for (int element = min - 1; element <= max + 1; element++) {
943 +            assertEq(set.lower(element), rs.lower(element));
944 +            assertEq(set.floor(element), rs.floor(element));
945 +            assertEq(set.higher(element), rs.higher(element));
946 +            assertEq(set.ceiling(element), rs.ceiling(element));
947 +        }
948 +
949 +        // Test extrema
950 +        if (set.size() != 0) {
951 +            assertEq(set.first(), rs.first());
952 +            assertEq(set.last(), rs.last());
953 +        } else {
954 +            assertEq(rs.first(), -1);
955 +            assertEq(rs.last(),  -1);
956 +            try {
957 +                set.first();
958 +                shouldThrow();
959 +            } catch (NoSuchElementException success) {}
960 +            try {
961 +                set.last();
962 +                shouldThrow();
963 +            } catch (NoSuchElementException success) {}
964 +        }
965 +    }
966 +
967 +    static void assertEq(Integer i, int j) {
968 +        if (i == null)
969 +            assertEquals(j, -1);
970 +        else
971 +            assertEquals((int) i, j);
972 +    }
973 +
974 +    static boolean eq(Integer i, int j) {
975 +        return i == null ? j == -1 : i == j;
976 +    }
977 +
978   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines