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.14 by jsr166, Wed Aug 25 01:44:48 2010 UTC

# Line 11 | Line 11 | import java.io.*;
11  
12   public class ConcurrentSkipListSetTest extends JSR166TestCase {
13      public static void main(String[] args) {
14 <        junit.textui.TestRunner.run (suite());  
14 >        junit.textui.TestRunner.run(suite());
15      }
16      public static Test suite() {
17 <        return new TestSuite(ConcurrentSkipListSetTest.class);
17 >        return new TestSuite(ConcurrentSkipListSetTest.class);
18      }
19  
20 <    static class MyReverseComparator implements Comparator {
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;
22 >            return ((Comparable)y).compareTo(x);
23          }
24      }
25  
# Line 34 | Line 30 | public class ConcurrentSkipListSetTest e
30      private ConcurrentSkipListSet populatedSet(int n) {
31          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
32          assertTrue(q.isEmpty());
33 <        for(int i = n-1; i >= 0; i-=2)
34 <            assertTrue(q.add(new Integer(i)));
35 <        for(int i = (n & 1); i < n; i+=2)
36 <            assertTrue(q.add(new Integer(i)));
33 >        for (int i = n-1; i >= 0; i-=2)
34 >            assertTrue(q.add(new Integer(i)));
35 >        for (int i = (n & 1); i < n; i+=2)
36 >            assertTrue(q.add(new Integer(i)));
37          assertFalse(q.isEmpty());
38 <        assertEquals(n, q.size());
38 >        assertEquals(n, q.size());
39          return q;
40      }
41  
# Line 54 | Line 50 | public class ConcurrentSkipListSetTest e
50          q.add(three);
51          q.add(four);
52          q.add(five);
53 <        assertEquals(5, q.size());
53 >        assertEquals(5, q.size());
54          return q;
55      }
56 <
56 >
57      /**
58       * A new set has unbounded capacity
59       */
# Line 72 | Line 68 | public class ConcurrentSkipListSetTest e
68          try {
69              ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null);
70              shouldThrow();
71 <        }
76 <        catch (NullPointerException success) {}
71 >        } catch (NullPointerException success) {}
72      }
73  
74      /**
# Line 84 | Line 79 | public class ConcurrentSkipListSetTest e
79              Integer[] ints = new Integer[SIZE];
80              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
81              shouldThrow();
82 <        }
88 <        catch (NullPointerException success) {}
82 >        } catch (NullPointerException success) {}
83      }
84  
85      /**
# Line 98 | Line 92 | public class ConcurrentSkipListSetTest e
92                  ints[i] = new Integer(i);
93              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
94              shouldThrow();
95 <        }
102 <        catch (NullPointerException success) {}
95 >        } catch (NullPointerException success) {}
96      }
97  
98      /**
99       * Set contains all elements of collection used to initialize
100       */
101      public void testConstructor6() {
102 <        try {
103 <            Integer[] ints = new Integer[SIZE];
104 <            for (int i = 0; i < SIZE; ++i)
105 <                ints[i] = new Integer(i);
106 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
107 <            for (int i = 0; i < SIZE; ++i)
115 <                assertEquals(ints[i], q.pollFirst());
116 <        }
117 <        finally {}
102 >        Integer[] ints = new Integer[SIZE];
103 >        for (int i = 0; i < SIZE; ++i)
104 >            ints[i] = new Integer(i);
105 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
106 >        for (int i = 0; i < SIZE; ++i)
107 >            assertEquals(ints[i], q.pollFirst());
108      }
109  
110      /**
111       * The comparator used in constructor is used
112       */
113      public void testConstructor7() {
114 <        try {
115 <            MyReverseComparator cmp = new MyReverseComparator();
116 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
117 <            assertEquals(cmp, q.comparator());
118 <            Integer[] ints = new Integer[SIZE];
119 <            for (int i = 0; i < SIZE; ++i)
120 <                ints[i] = new Integer(i);
121 <            q.addAll(Arrays.asList(ints));
122 <            for (int i = SIZE-1; i >= 0; --i)
133 <                assertEquals(ints[i], q.pollFirst());
134 <        }
135 <        finally {}
114 >        MyReverseComparator cmp = new MyReverseComparator();
115 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
116 >        assertEquals(cmp, q.comparator());
117 >        Integer[] ints = new Integer[SIZE];
118 >        for (int i = 0; i < SIZE; ++i)
119 >            ints[i] = new Integer(i);
120 >        q.addAll(Arrays.asList(ints));
121 >        for (int i = SIZE-1; i >= 0; --i)
122 >            assertEquals(ints[i], q.pollFirst());
123      }
124  
125      /**
# Line 168 | Line 155 | public class ConcurrentSkipListSetTest e
155       * add(null) throws NPE
156       */
157      public void testAddNull() {
158 <        try {
158 >        try {
159              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
160              q.add(null);
161              shouldThrow();
162 <        } catch (NullPointerException success) { }  
162 >        } catch (NullPointerException success) {}
163      }
164  
165      /**
# Line 203 | Line 190 | public class ConcurrentSkipListSetTest e
190              q.add(new Object());
191              q.add(new Object());
192              shouldThrow();
193 <        }
207 <        catch(ClassCastException success) {}
193 >        } catch (ClassCastException success) {}
194      }
195  
196      /**
# Line 215 | Line 201 | public class ConcurrentSkipListSetTest e
201              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
202              q.addAll(null);
203              shouldThrow();
204 <        }
219 <        catch (NullPointerException success) {}
204 >        } catch (NullPointerException success) {}
205      }
206 +
207      /**
208       * addAll of a collection with null elements throws NPE
209       */
# Line 227 | Line 213 | public class ConcurrentSkipListSetTest e
213              Integer[] ints = new Integer[SIZE];
214              q.addAll(Arrays.asList(ints));
215              shouldThrow();
216 <        }
231 <        catch (NullPointerException success) {}
216 >        } catch (NullPointerException success) {}
217      }
218 +
219      /**
220       * addAll of a collection with any null elements throws NPE after
221       * possibly adding some elements
# Line 242 | Line 228 | public class ConcurrentSkipListSetTest e
228                  ints[i] = new Integer(i);
229              q.addAll(Arrays.asList(ints));
230              shouldThrow();
231 <        }
246 <        catch (NullPointerException success) {}
231 >        } catch (NullPointerException success) {}
232      }
233  
234      /**
235       * Set contains all elements of successful addAll
236       */
237      public void testAddAll5() {
238 <        try {
239 <            Integer[] empty = new Integer[0];
240 <            Integer[] ints = new Integer[SIZE];
241 <            for (int i = 0; i < SIZE; ++i)
242 <                ints[i] = new Integer(SIZE-1-i);
243 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
244 <            assertFalse(q.addAll(Arrays.asList(empty)));
245 <            assertTrue(q.addAll(Arrays.asList(ints)));
246 <            for (int i = 0; i < SIZE; ++i)
262 <                assertEquals(new Integer(i), q.pollFirst());
263 <        }
264 <        finally {}
238 >        Integer[] empty = new Integer[0];
239 >        Integer[] ints = new Integer[SIZE];
240 >        for (int i = 0; i < SIZE; ++i)
241 >            ints[i] = new Integer(SIZE-1-i);
242 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
243 >        assertFalse(q.addAll(Arrays.asList(empty)));
244 >        assertTrue(q.addAll(Arrays.asList(ints)));
245 >        for (int i = 0; i < SIZE; ++i)
246 >            assertEquals(i, q.pollFirst());
247      }
248  
249      /**
# Line 270 | Line 252 | public class ConcurrentSkipListSetTest e
252      public void testPollFirst() {
253          ConcurrentSkipListSet q = populatedSet(SIZE);
254          for (int i = 0; i < SIZE; ++i) {
255 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
255 >            assertEquals(i, q.pollFirst());
256          }
257 <        assertNull(q.pollFirst());
257 >        assertNull(q.pollFirst());
258      }
259  
260      /**
# Line 281 | Line 263 | public class ConcurrentSkipListSetTest e
263      public void testPollLast() {
264          ConcurrentSkipListSet q = populatedSet(SIZE);
265          for (int i = SIZE-1; i >= 0; --i) {
266 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
266 >            assertEquals(i, q.pollLast());
267          }
268 <        assertNull(q.pollFirst());
268 >        assertNull(q.pollFirst());
269      }
270  
271  
# Line 301 | Line 283 | public class ConcurrentSkipListSetTest e
283          }
284          assertTrue(q.isEmpty());
285      }
286 <        
286 >
287      /**
288       * contains(x) reports true when elements added but not yet removed
289       */
# Line 377 | Line 359 | public class ConcurrentSkipListSetTest e
359          }
360      }
361  
362 <    
362 >
363  
364      /**
365       * lower returns preceding element
# Line 395 | Line 377 | public class ConcurrentSkipListSetTest e
377  
378          Object e4 = q.lower(zero);
379          assertNull(e4);
398
380      }
381  
382      /**
# Line 414 | Line 395 | public class ConcurrentSkipListSetTest e
395  
396          Object e4 = q.higher(six);
397          assertNull(e4);
417
398      }
399  
400      /**
# Line 433 | Line 413 | public class ConcurrentSkipListSetTest e
413  
414          Object e4 = q.floor(zero);
415          assertNull(e4);
436
416      }
417  
418      /**
# Line 452 | Line 431 | public class ConcurrentSkipListSetTest e
431  
432          Object e4 = q.ceiling(six);
433          assertNull(e4);
455
434      }
435  
436      /**
# Line 460 | Line 438 | public class ConcurrentSkipListSetTest e
438       */
439      public void testToArray() {
440          ConcurrentSkipListSet q = populatedSet(SIZE);
441 <        Object[] o = q.toArray();
441 >        Object[] o = q.toArray();
442          Arrays.sort(o);
443 <        for(int i = 0; i < o.length; i++)
444 <            assertEquals(o[i], q.pollFirst());
443 >        for (int i = 0; i < o.length; i++)
444 >            assertEquals(o[i], q.pollFirst());
445      }
446  
447      /**
# Line 471 | Line 449 | public class ConcurrentSkipListSetTest e
449       */
450      public void testToArray2() {
451          ConcurrentSkipListSet q = populatedSet(SIZE);
452 <        Integer[] ints = new Integer[SIZE];
453 <        ints = (Integer[])q.toArray(ints);
452 >        Integer[] ints = new Integer[SIZE];
453 >        ints = (Integer[])q.toArray(ints);
454          Arrays.sort(ints);
455 <        for(int i = 0; i < ints.length; i++)
455 >        for (int i = 0; i < ints.length; i++)
456              assertEquals(ints[i], q.pollFirst());
457      }
458 <    
458 >
459      /**
460       * iterator iterates through all elements
461       */
462      public void testIterator() {
463          ConcurrentSkipListSet q = populatedSet(SIZE);
464          int i = 0;
465 <        Iterator it = q.iterator();
466 <        while(it.hasNext()) {
465 >        Iterator it = q.iterator();
466 >        while (it.hasNext()) {
467              assertTrue(q.contains(it.next()));
468              ++i;
469          }
# Line 498 | Line 476 | public class ConcurrentSkipListSetTest e
476      public void testEmptyIterator() {
477          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
478          int i = 0;
479 <        Iterator it = q.iterator();
480 <        while(it.hasNext()) {
479 >        Iterator it = q.iterator();
480 >        while (it.hasNext()) {
481              assertTrue(q.contains(it.next()));
482              ++i;
483          }
# Line 509 | Line 487 | public class ConcurrentSkipListSetTest e
487      /**
488       * iterator.remove removes current element
489       */
490 <    public void testIteratorRemove () {
490 >    public void testIteratorRemove() {
491          final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
492          q.add(new Integer(2));
493          q.add(new Integer(1));
# Line 535 | Line 513 | public class ConcurrentSkipListSetTest e
513          for (int i = 0; i < SIZE; ++i) {
514              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
515          }
516 <    }        
516 >    }
517  
518      /**
519 <     * A deserialized serialized set has same elements
519 >     * A deserialized serialized set has same elements
520       */
521 <    public void testSerialization() {
521 >    public void testSerialization() throws Exception {
522          ConcurrentSkipListSet q = populatedSet(SIZE);
523 <        try {
524 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
525 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
526 <            out.writeObject(q);
527 <            out.close();
528 <
529 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
530 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
531 <            ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
532 <            assertEquals(q.size(), r.size());
533 <            while (!q.isEmpty())
556 <                assertEquals(q.pollFirst(), r.pollFirst());
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
560 <        }
523 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
524 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
525 >        out.writeObject(q);
526 >        out.close();
527 >
528 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
529 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
530 >        ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
531 >        assertEquals(q.size(), r.size());
532 >        while (!q.isEmpty())
533 >            assertEquals(q.pollFirst(), r.pollFirst());
534      }
535  
536      /**
# Line 679 | Line 652 | public class ConcurrentSkipListSetTest e
652          assertEquals(4, set.size());
653      }
654  
655 +    Random rnd = new Random(666);
656 +    BitSet bs;
657 +
658 +    /**
659 +     * Subsets of subsets subdivide correctly
660 +     */
661 +    public void testRecursiveSubSets() throws Exception {
662 +        int setSize = 1000;
663 +        Class cl = ConcurrentSkipListSet.class;
664 +
665 +        NavigableSet<Integer> set = newSet(cl);
666 +        bs = new BitSet(setSize);
667 +
668 +        populate(set, setSize);
669 +        check(set,                 0, setSize - 1, true);
670 +        check(set.descendingSet(), 0, setSize - 1, false);
671 +
672 +        mutateSet(set, 0, setSize - 1);
673 +        check(set,                 0, setSize - 1, true);
674 +        check(set.descendingSet(), 0, setSize - 1, false);
675 +
676 +        bashSubSet(set.subSet(0, true, setSize, false),
677 +                   0, setSize - 1, true);
678 +    }
679 +
680 +    static NavigableSet<Integer> newSet(Class cl) throws Exception {
681 +        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
682 +        assertEquals(result.size(), 0);
683 +        assertFalse(result.iterator().hasNext());
684 +        return result;
685 +    }
686 +
687 +    void populate(NavigableSet<Integer> set, int limit) {
688 +        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
689 +            int element = rnd.nextInt(limit);
690 +            put(set, element);
691 +        }
692 +    }
693 +
694 +    void mutateSet(NavigableSet<Integer> set, int min, int max) {
695 +        int size = set.size();
696 +        int rangeSize = max - min + 1;
697 +
698 +        // Remove a bunch of entries directly
699 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
700 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
701 +        }
702 +
703 +        // Remove a bunch of entries with iterator
704 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
705 +            if (rnd.nextBoolean()) {
706 +                bs.clear(it.next());
707 +                it.remove();
708 +            }
709 +        }
710 +
711 +        // Add entries till we're back to original size
712 +        while (set.size() < size) {
713 +            int element = min + rnd.nextInt(rangeSize);
714 +            assertTrue(element >= min && element<= max);
715 +            put(set, element);
716 +        }
717 +    }
718 +
719 +    void mutateSubSet(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 - 5 + rnd.nextInt(rangeSize + 10);
739 +            if (element >= min && element<= max) {
740 +                put(set, element);
741 +            } else {
742 +                try {
743 +                    set.add(element);
744 +                    shouldThrow();
745 +                } catch (IllegalArgumentException success) {}
746 +            }
747 +        }
748 +    }
749 +
750 +    void put(NavigableSet<Integer> set, int element) {
751 +        if (set.add(element))
752 +            bs.set(element);
753 +    }
754 +
755 +    void remove(NavigableSet<Integer> set, int element) {
756 +        if (set.remove(element))
757 +            bs.clear(element);
758 +    }
759 +
760 +    void bashSubSet(NavigableSet<Integer> set,
761 +                    int min, int max, boolean ascending) {
762 +        check(set, min, max, ascending);
763 +        check(set.descendingSet(), min, max, !ascending);
764 +
765 +        mutateSubSet(set, min, max);
766 +        check(set, min, max, ascending);
767 +        check(set.descendingSet(), min, max, !ascending);
768 +
769 +        // Recurse
770 +        if (max - min < 2)
771 +            return;
772 +        int midPoint = (min + max) / 2;
773 +
774 +        // headSet - pick direction and endpoint inclusion randomly
775 +        boolean incl = rnd.nextBoolean();
776 +        NavigableSet<Integer> hm = set.headSet(midPoint, incl);
777 +        if (ascending) {
778 +            if (rnd.nextBoolean())
779 +                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
780 +            else
781 +                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
782 +                           false);
783 +        } else {
784 +            if (rnd.nextBoolean())
785 +                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
786 +            else
787 +                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
788 +                           true);
789 +        }
790 +
791 +        // tailSet - pick direction and endpoint inclusion randomly
792 +        incl = rnd.nextBoolean();
793 +        NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
794 +        if (ascending) {
795 +            if (rnd.nextBoolean())
796 +                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
797 +            else
798 +                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
799 +                           false);
800 +        } else {
801 +            if (rnd.nextBoolean()) {
802 +                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
803 +            } else {
804 +                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
805 +                           true);
806 +            }
807 +        }
808 +
809 +        // subSet - pick direction and endpoint inclusion randomly
810 +        int rangeSize = max - min + 1;
811 +        int[] endpoints = new int[2];
812 +        endpoints[0] = min + rnd.nextInt(rangeSize);
813 +        endpoints[1] = min + rnd.nextInt(rangeSize);
814 +        Arrays.sort(endpoints);
815 +        boolean lowIncl = rnd.nextBoolean();
816 +        boolean highIncl = rnd.nextBoolean();
817 +        if (ascending) {
818 +            NavigableSet<Integer> sm = set.subSet(
819 +                endpoints[0], lowIncl, endpoints[1], highIncl);
820 +            if (rnd.nextBoolean())
821 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
822 +                           endpoints[1] - (highIncl ? 0 : 1), true);
823 +            else
824 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
825 +                           endpoints[1] - (highIncl ? 0 : 1), false);
826 +        } else {
827 +            NavigableSet<Integer> sm = set.subSet(
828 +                endpoints[1], highIncl, endpoints[0], lowIncl);
829 +            if (rnd.nextBoolean())
830 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
831 +                           endpoints[1] - (highIncl ? 0 : 1), false);
832 +            else
833 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
834 +                           endpoints[1] - (highIncl ? 0 : 1), true);
835 +        }
836 +    }
837 +
838 +    /**
839 +     * min and max are both inclusive.  If max < min, interval is empty.
840 +     */
841 +    void check(NavigableSet<Integer> set,
842 +                      final int min, final int max, final boolean ascending) {
843 +       class ReferenceSet {
844 +            int lower(int element) {
845 +                return ascending ?
846 +                    lowerAscending(element) : higherAscending(element);
847 +            }
848 +            int floor(int element) {
849 +                return ascending ?
850 +                    floorAscending(element) : ceilingAscending(element);
851 +            }
852 +            int ceiling(int element) {
853 +                return ascending ?
854 +                    ceilingAscending(element) : floorAscending(element);
855 +            }
856 +            int higher(int element) {
857 +                return ascending ?
858 +                    higherAscending(element) : lowerAscending(element);
859 +            }
860 +            int first() {
861 +                return ascending ? firstAscending() : lastAscending();
862 +            }
863 +            int last() {
864 +                return ascending ? lastAscending() : firstAscending();
865 +            }
866 +            int lowerAscending(int element) {
867 +                return floorAscending(element - 1);
868 +            }
869 +            int floorAscending(int element) {
870 +                if (element < min)
871 +                    return -1;
872 +                else if (element > max)
873 +                    element = max;
874 +
875 +                // BitSet should support this! Test would run much faster
876 +                while (element >= min) {
877 +                    if (bs.get(element))
878 +                        return(element);
879 +                    element--;
880 +                }
881 +                return -1;
882 +            }
883 +            int ceilingAscending(int element) {
884 +                if (element < min)
885 +                    element = min;
886 +                else if (element > max)
887 +                    return -1;
888 +                int result = bs.nextSetBit(element);
889 +                return result > max ? -1 : result;
890 +            }
891 +            int higherAscending(int element) {
892 +                return ceilingAscending(element + 1);
893 +            }
894 +            private int firstAscending() {
895 +                int result = ceilingAscending(min);
896 +                return result > max ? -1 : result;
897 +            }
898 +            private int lastAscending() {
899 +                int result = floorAscending(max);
900 +                return result < min ? -1 : result;
901 +            }
902 +        }
903 +        ReferenceSet rs = new ReferenceSet();
904 +
905 +        // Test contents using containsElement
906 +        int size = 0;
907 +        for (int i = min; i <= max; i++) {
908 +            boolean bsContainsI = bs.get(i);
909 +            assertEquals(bsContainsI, set.contains(i));
910 +            if (bsContainsI)
911 +                size++;
912 +        }
913 +        assertEquals(set.size(), size);
914 +
915 +        // Test contents using contains elementSet iterator
916 +        int size2 = 0;
917 +        int previousElement = -1;
918 +        for (int element : set) {
919 +            assertTrue(bs.get(element));
920 +            size2++;
921 +            assertTrue(previousElement < 0 || (ascending ?
922 +                element - previousElement > 0 : element - previousElement < 0));
923 +            previousElement = element;
924 +        }
925 +        assertEquals(size2, size);
926 +
927 +        // Test navigation ops
928 +        for (int element = min - 1; element <= max + 1; element++) {
929 +            assertEq(set.lower(element), rs.lower(element));
930 +            assertEq(set.floor(element), rs.floor(element));
931 +            assertEq(set.higher(element), rs.higher(element));
932 +            assertEq(set.ceiling(element), rs.ceiling(element));
933 +        }
934 +
935 +        // Test extrema
936 +        if (set.size() != 0) {
937 +            assertEq(set.first(), rs.first());
938 +            assertEq(set.last(), rs.last());
939 +        } else {
940 +            assertEq(rs.first(), -1);
941 +            assertEq(rs.last(),  -1);
942 +            try {
943 +                set.first();
944 +                shouldThrow();
945 +            } catch (NoSuchElementException success) {}
946 +            try {
947 +                set.last();
948 +                shouldThrow();
949 +            } catch (NoSuchElementException success) {}
950 +        }
951 +    }
952 +
953 +    static void assertEq(Integer i, int j) {
954 +        if (i == null)
955 +            assertEquals(j, -1);
956 +        else
957 +            assertEquals((int) i, j);
958 +    }
959 +
960 +    static boolean eq(Integer i, int j) {
961 +        return i == null ? j == -1 : i == j;
962 +    }
963 +
964   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines