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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines