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.8 by jsr166, Sat Nov 21 10:25:05 2009 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();
# Line 34 | Line 34 | public class ConcurrentSkipListSetTest e
34      private ConcurrentSkipListSet populatedSet(int n) {
35          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
36          assertTrue(q.isEmpty());
37 <        for(int i = n-1; i >= 0; i-=2)
38 <            assertTrue(q.add(new Integer(i)));
39 <        for(int i = (n & 1); i < n; i+=2)
40 <            assertTrue(q.add(new Integer(i)));
37 >        for (int i = n-1; i >= 0; i-=2)
38 >            assertTrue(q.add(new Integer(i)));
39 >        for (int i = (n & 1); i < n; i+=2)
40 >            assertTrue(q.add(new Integer(i)));
41          assertFalse(q.isEmpty());
42 <        assertEquals(n, q.size());
42 >        assertEquals(n, q.size());
43          return q;
44      }
45  
# Line 54 | Line 54 | public class ConcurrentSkipListSetTest e
54          q.add(three);
55          q.add(four);
56          q.add(five);
57 <        assertEquals(5, q.size());
57 >        assertEquals(5, q.size());
58          return q;
59      }
60 <
60 >
61      /**
62       * A new set has unbounded capacity
63       */
# Line 72 | Line 72 | public class ConcurrentSkipListSetTest e
72          try {
73              ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null);
74              shouldThrow();
75 <        }
76 <        catch (NullPointerException success) {}
75 >        } catch (NullPointerException success) {}
76      }
77  
78      /**
# Line 84 | Line 83 | public class ConcurrentSkipListSetTest e
83              Integer[] ints = new Integer[SIZE];
84              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
85              shouldThrow();
86 <        }
88 <        catch (NullPointerException success) {}
86 >        } catch (NullPointerException success) {}
87      }
88  
89      /**
# Line 98 | Line 96 | public class ConcurrentSkipListSetTest e
96                  ints[i] = new Integer(i);
97              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
98              shouldThrow();
99 <        }
102 <        catch (NullPointerException success) {}
99 >        } catch (NullPointerException success) {}
100      }
101  
102      /**
103       * Set contains all elements of collection used to initialize
104       */
105      public void testConstructor6() {
106 <        try {
107 <            Integer[] ints = new Integer[SIZE];
108 <            for (int i = 0; i < SIZE; ++i)
109 <                ints[i] = new Integer(i);
110 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
111 <            for (int i = 0; i < SIZE; ++i)
115 <                assertEquals(ints[i], q.pollFirst());
116 <        }
117 <        finally {}
106 >        Integer[] ints = new Integer[SIZE];
107 >        for (int i = 0; i < SIZE; ++i)
108 >            ints[i] = new Integer(i);
109 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
110 >        for (int i = 0; i < SIZE; ++i)
111 >            assertEquals(ints[i], q.pollFirst());
112      }
113  
114      /**
# Line 168 | Line 162 | public class ConcurrentSkipListSetTest e
162       * add(null) throws NPE
163       */
164      public void testAddNull() {
165 <        try {
165 >        try {
166              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
167              q.add(null);
168              shouldThrow();
169 <        } catch (NullPointerException success) { }  
169 >        } catch (NullPointerException success) {}
170      }
171  
172      /**
# Line 203 | Line 197 | public class ConcurrentSkipListSetTest e
197              q.add(new Object());
198              q.add(new Object());
199              shouldThrow();
200 <        }
207 <        catch(ClassCastException success) {}
200 >        } catch (ClassCastException success) {}
201      }
202  
203      /**
# Line 215 | Line 208 | public class ConcurrentSkipListSetTest e
208              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
209              q.addAll(null);
210              shouldThrow();
211 <        }
219 <        catch (NullPointerException success) {}
211 >        } catch (NullPointerException success) {}
212      }
213      /**
214       * addAll of a collection with null elements throws NPE
# Line 227 | Line 219 | public class ConcurrentSkipListSetTest e
219              Integer[] ints = new Integer[SIZE];
220              q.addAll(Arrays.asList(ints));
221              shouldThrow();
222 <        }
231 <        catch (NullPointerException success) {}
222 >        } catch (NullPointerException success) {}
223      }
224      /**
225       * addAll of a collection with any null elements throws NPE after
# Line 242 | Line 233 | public class ConcurrentSkipListSetTest e
233                  ints[i] = new Integer(i);
234              q.addAll(Arrays.asList(ints));
235              shouldThrow();
236 <        }
246 <        catch (NullPointerException success) {}
236 >        } catch (NullPointerException success) {}
237      }
238  
239      /**
# Line 272 | Line 262 | public class ConcurrentSkipListSetTest e
262          for (int i = 0; i < SIZE; ++i) {
263              assertEquals(i, ((Integer)q.pollFirst()).intValue());
264          }
265 <        assertNull(q.pollFirst());
265 >        assertNull(q.pollFirst());
266      }
267  
268      /**
# Line 283 | Line 273 | public class ConcurrentSkipListSetTest e
273          for (int i = SIZE-1; i >= 0; --i) {
274              assertEquals(i, ((Integer)q.pollLast()).intValue());
275          }
276 <        assertNull(q.pollFirst());
276 >        assertNull(q.pollFirst());
277      }
278  
279  
# Line 301 | Line 291 | public class ConcurrentSkipListSetTest e
291          }
292          assertTrue(q.isEmpty());
293      }
294 <        
294 >
295      /**
296       * contains(x) reports true when elements added but not yet removed
297       */
# Line 377 | Line 367 | public class ConcurrentSkipListSetTest e
367          }
368      }
369  
370 <    
370 >
371  
372      /**
373       * lower returns preceding element
# Line 460 | Line 450 | public class ConcurrentSkipListSetTest e
450       */
451      public void testToArray() {
452          ConcurrentSkipListSet q = populatedSet(SIZE);
453 <        Object[] o = q.toArray();
453 >        Object[] o = q.toArray();
454          Arrays.sort(o);
455 <        for(int i = 0; i < o.length; i++)
456 <            assertEquals(o[i], q.pollFirst());
455 >        for (int i = 0; i < o.length; i++)
456 >            assertEquals(o[i], q.pollFirst());
457      }
458  
459      /**
# Line 471 | Line 461 | public class ConcurrentSkipListSetTest e
461       */
462      public void testToArray2() {
463          ConcurrentSkipListSet q = populatedSet(SIZE);
464 <        Integer[] ints = new Integer[SIZE];
465 <        ints = (Integer[])q.toArray(ints);
464 >        Integer[] ints = new Integer[SIZE];
465 >        ints = (Integer[])q.toArray(ints);
466          Arrays.sort(ints);
467 <        for(int i = 0; i < ints.length; i++)
467 >        for (int i = 0; i < ints.length; i++)
468              assertEquals(ints[i], q.pollFirst());
469      }
470 <    
470 >
471      /**
472       * iterator iterates through all elements
473       */
474      public void testIterator() {
475          ConcurrentSkipListSet q = populatedSet(SIZE);
476          int i = 0;
477 <        Iterator it = q.iterator();
478 <        while(it.hasNext()) {
477 >        Iterator it = q.iterator();
478 >        while (it.hasNext()) {
479              assertTrue(q.contains(it.next()));
480              ++i;
481          }
# Line 498 | Line 488 | public class ConcurrentSkipListSetTest e
488      public void testEmptyIterator() {
489          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
490          int i = 0;
491 <        Iterator it = q.iterator();
492 <        while(it.hasNext()) {
491 >        Iterator it = q.iterator();
492 >        while (it.hasNext()) {
493              assertTrue(q.contains(it.next()));
494              ++i;
495          }
# Line 535 | Line 525 | public class ConcurrentSkipListSetTest e
525          for (int i = 0; i < SIZE; ++i) {
526              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
527          }
528 <    }        
528 >    }
529  
530      /**
531 <     * A deserialized serialized set has same elements
531 >     * A deserialized serialized set has same elements
532       */
533 <    public void testSerialization() {
533 >    public void testSerialization() throws Exception {
534          ConcurrentSkipListSet q = populatedSet(SIZE);
535 <        try {
536 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
537 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
538 <            out.writeObject(q);
539 <            out.close();
540 <
541 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
542 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
543 <            ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
544 <            assertEquals(q.size(), r.size());
545 <            while (!q.isEmpty())
556 <                assertEquals(q.pollFirst(), r.pollFirst());
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
560 <        }
535 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
536 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
537 >        out.writeObject(q);
538 >        out.close();
539 >
540 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
541 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
542 >        ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
543 >        assertEquals(q.size(), r.size());
544 >        while (!q.isEmpty())
545 >            assertEquals(q.pollFirst(), r.pollFirst());
546      }
547  
548      /**
# Line 679 | Line 664 | public class ConcurrentSkipListSetTest e
664          assertEquals(4, set.size());
665      }
666  
667 +    Random rnd = new Random(666);
668 +    BitSet bs;
669 +
670 +    /**
671 +     * Subsets of subsets subdivide correctly
672 +     */
673 +    public void testRecursiveSubSets() {
674 +        int setSize = 1000;
675 +        Class cl = ConcurrentSkipListSet.class;
676 +
677 +        NavigableSet<Integer> set = newSet(cl);
678 +        bs = new BitSet(setSize);
679 +
680 +        populate(set, setSize);
681 +        check(set,                 0, setSize - 1, true);
682 +        check(set.descendingSet(), 0, setSize - 1, false);
683 +
684 +        mutateSet(set, 0, setSize - 1);
685 +        check(set,                 0, setSize - 1, true);
686 +        check(set.descendingSet(), 0, setSize - 1, false);
687 +
688 +        bashSubSet(set.subSet(0, true, setSize, false),
689 +                   0, setSize - 1, true);
690 +    }
691 +
692 +    static NavigableSet<Integer> newSet(Class cl) {
693 +        NavigableSet<Integer> result = null;
694 +        try {
695 +            result = (NavigableSet<Integer>) cl.newInstance();
696 +        } catch (Exception e) {
697 +            fail();
698 +        }
699 +        assertEquals(result.size(), 0);
700 +        assertFalse(result.iterator().hasNext());
701 +        return result;
702 +    }
703 +
704 +    void populate(NavigableSet<Integer> set, int limit) {
705 +        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
706 +            int element = rnd.nextInt(limit);
707 +            put(set, element);
708 +        }
709 +    }
710 +
711 +    void mutateSet(NavigableSet<Integer> set, int min, int max) {
712 +        int size = set.size();
713 +        int rangeSize = max - min + 1;
714 +
715 +        // Remove a bunch of entries directly
716 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
717 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
718 +        }
719 +
720 +        // Remove a bunch of entries with iterator
721 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
722 +            if (rnd.nextBoolean()) {
723 +                bs.clear(it.next());
724 +                it.remove();
725 +            }
726 +        }
727 +
728 +        // Add entries till we're back to original size
729 +        while (set.size() < size) {
730 +            int element = min + rnd.nextInt(rangeSize);
731 +            assertTrue(element >= min && element<= max);
732 +            put(set, element);
733 +        }
734 +    }
735 +
736 +    void mutateSubSet(NavigableSet<Integer> set, int min, int max) {
737 +        int size = set.size();
738 +        int rangeSize = max - min + 1;
739 +
740 +        // Remove a bunch of entries directly
741 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
742 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
743 +        }
744 +
745 +        // Remove a bunch of entries with iterator
746 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
747 +            if (rnd.nextBoolean()) {
748 +                bs.clear(it.next());
749 +                it.remove();
750 +            }
751 +        }
752 +
753 +        // Add entries till we're back to original size
754 +        while (set.size() < size) {
755 +            int element = min - 5 + rnd.nextInt(rangeSize + 10);
756 +            if (element >= min && element<= max) {
757 +                put(set, element);
758 +            } else {
759 +                try {
760 +                    set.add(element);
761 +                    fail();
762 +                } catch (IllegalArgumentException e) {
763 +                    // expected
764 +                }
765 +            }
766 +        }
767 +    }
768 +
769 +    void put(NavigableSet<Integer> set, int element) {
770 +        if (set.add(element))
771 +            bs.set(element);
772 +    }
773 +
774 +    void remove(NavigableSet<Integer> set, int element) {
775 +        if (set.remove(element))
776 +            bs.clear(element);
777 +    }
778 +
779 +    void bashSubSet(NavigableSet<Integer> set,
780 +                    int min, int max, boolean ascending) {
781 +        check(set, min, max, ascending);
782 +        check(set.descendingSet(), min, max, !ascending);
783 +
784 +        mutateSubSet(set, min, max);
785 +        check(set, min, max, ascending);
786 +        check(set.descendingSet(), min, max, !ascending);
787 +
788 +        // Recurse
789 +        if (max - min < 2)
790 +            return;
791 +        int midPoint = (min + max) / 2;
792 +
793 +        // headSet - pick direction and endpoint inclusion randomly
794 +        boolean incl = rnd.nextBoolean();
795 +        NavigableSet<Integer> hm = set.headSet(midPoint, incl);
796 +        if (ascending) {
797 +            if (rnd.nextBoolean())
798 +                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
799 +            else
800 +                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
801 +                           false);
802 +        } else {
803 +            if (rnd.nextBoolean())
804 +                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
805 +            else
806 +                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
807 +                           true);
808 +        }
809 +
810 +        // tailSet - pick direction and endpoint inclusion randomly
811 +        incl = rnd.nextBoolean();
812 +        NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
813 +        if (ascending) {
814 +            if (rnd.nextBoolean())
815 +                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
816 +            else
817 +                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
818 +                           false);
819 +        } else {
820 +            if (rnd.nextBoolean()) {
821 +                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
822 +            } else {
823 +                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
824 +                           true);
825 +            }
826 +        }
827 +
828 +        // subSet - pick direction and endpoint inclusion randomly
829 +        int rangeSize = max - min + 1;
830 +        int[] endpoints = new int[2];
831 +        endpoints[0] = min + rnd.nextInt(rangeSize);
832 +        endpoints[1] = min + rnd.nextInt(rangeSize);
833 +        Arrays.sort(endpoints);
834 +        boolean lowIncl = rnd.nextBoolean();
835 +        boolean highIncl = rnd.nextBoolean();
836 +        if (ascending) {
837 +            NavigableSet<Integer> sm = set.subSet(
838 +                endpoints[0], lowIncl, endpoints[1], highIncl);
839 +            if (rnd.nextBoolean())
840 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
841 +                           endpoints[1] - (highIncl ? 0 : 1), true);
842 +            else
843 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
844 +                           endpoints[1] - (highIncl ? 0 : 1), false);
845 +        } else {
846 +            NavigableSet<Integer> sm = set.subSet(
847 +                endpoints[1], highIncl, endpoints[0], lowIncl);
848 +            if (rnd.nextBoolean())
849 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
850 +                           endpoints[1] - (highIncl ? 0 : 1), false);
851 +            else
852 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
853 +                           endpoints[1] - (highIncl ? 0 : 1), true);
854 +        }
855 +    }
856 +
857 +    /**
858 +     * min and max are both inclusive.  If max < min, interval is empty.
859 +     */
860 +    void check(NavigableSet<Integer> set,
861 +                      final int min, final int max, final boolean ascending) {
862 +       class ReferenceSet {
863 +            int lower(int element) {
864 +                return ascending ?
865 +                    lowerAscending(element) : higherAscending(element);
866 +            }
867 +            int floor(int element) {
868 +                return ascending ?
869 +                    floorAscending(element) : ceilingAscending(element);
870 +            }
871 +            int ceiling(int element) {
872 +                return ascending ?
873 +                    ceilingAscending(element) : floorAscending(element);
874 +            }
875 +            int higher(int element) {
876 +                return ascending ?
877 +                    higherAscending(element) : lowerAscending(element);
878 +            }
879 +            int first() {
880 +                return ascending ? firstAscending() : lastAscending();
881 +            }
882 +            int last() {
883 +                return ascending ? lastAscending() : firstAscending();
884 +            }
885 +            int lowerAscending(int element) {
886 +                return floorAscending(element - 1);
887 +            }
888 +            int floorAscending(int element) {
889 +                if (element < min)
890 +                    return -1;
891 +                else if (element > max)
892 +                    element = max;
893 +
894 +                // BitSet should support this! Test would run much faster
895 +                while (element >= min) {
896 +                    if (bs.get(element))
897 +                        return(element);
898 +                    element--;
899 +                }
900 +                return -1;
901 +            }
902 +            int ceilingAscending(int element) {
903 +                if (element < min)
904 +                    element = min;
905 +                else if (element > max)
906 +                    return -1;
907 +                int result = bs.nextSetBit(element);
908 +                return result > max ? -1 : result;
909 +            }
910 +            int higherAscending(int element) {
911 +                return ceilingAscending(element + 1);
912 +            }
913 +            private int firstAscending() {
914 +                int result = ceilingAscending(min);
915 +                return result > max ? -1 : result;
916 +            }
917 +            private int lastAscending() {
918 +                int result = floorAscending(max);
919 +                return result < min ? -1 : result;
920 +            }
921 +        }
922 +        ReferenceSet rs = new ReferenceSet();
923 +
924 +        // Test contents using containsElement
925 +        int size = 0;
926 +        for (int i = min; i <= max; i++) {
927 +            boolean bsContainsI = bs.get(i);
928 +            assertEquals(bsContainsI, set.contains(i));
929 +            if (bsContainsI)
930 +                size++;
931 +        }
932 +        assertEquals(set.size(), size);
933 +
934 +        // Test contents using contains elementSet iterator
935 +        int size2 = 0;
936 +        int previousElement = -1;
937 +        for (int element : set) {
938 +            assertTrue(bs.get(element));
939 +            size2++;
940 +            assertTrue(previousElement < 0 || (ascending ?
941 +                element - previousElement > 0 : element - previousElement < 0));
942 +            previousElement = element;
943 +        }
944 +        assertEquals(size2, size);
945 +
946 +        // Test navigation ops
947 +        for (int element = min - 1; element <= max + 1; element++) {
948 +            assertEq(set.lower(element), rs.lower(element));
949 +            assertEq(set.floor(element), rs.floor(element));
950 +            assertEq(set.higher(element), rs.higher(element));
951 +            assertEq(set.ceiling(element), rs.ceiling(element));
952 +        }
953 +
954 +        // Test extrema
955 +        if (set.size() != 0) {
956 +            assertEq(set.first(), rs.first());
957 +            assertEq(set.last(), rs.last());
958 +        } else {
959 +            assertEq(rs.first(), -1);
960 +            assertEq(rs.last(),  -1);
961 +            try {
962 +                set.first();
963 +                fail();
964 +            } catch (NoSuchElementException e) {
965 +                // expected
966 +            }
967 +            try {
968 +                set.last();
969 +                fail();
970 +            } catch (NoSuchElementException e) {
971 +                // expected
972 +            }
973 +        }
974 +    }
975 +
976 +    static void assertEq(Integer i, int j) {
977 +        if (i == null)
978 +            assertEquals(j, -1);
979 +        else
980 +            assertEquals((int) i, j);
981 +    }
982 +
983 +    static boolean eq(Integer i, int j) {
984 +        return i == null ? j == -1 : i == j;
985 +    }
986 +
987   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines