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

Comparing jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java (file contents):
Revision 1.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.10 by jsr166, Sat Nov 21 10:29:50 2009 UTC

# Line 11 | Line 11 | import java.io.*;
11  
12   public class ConcurrentSkipListSubSetTest 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(ConcurrentSkipListSubSetTest.class);
17 >        return new TestSuite(ConcurrentSkipListSubSetTest.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 35 | Line 35 | public class ConcurrentSkipListSubSetTes
35          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
36          assertTrue(q.isEmpty());
37  
38 <        for(int i = n-1; i >= 0; i-=2)
39 <            assertTrue(q.add(new Integer(i)));
40 <        for(int i = (n & 1); i < n; i+=2)
41 <            assertTrue(q.add(new Integer(i)));
38 >        for (int i = n-1; i >= 0; i-=2)
39 >            assertTrue(q.add(new Integer(i)));
40 >        for (int i = (n & 1); i < n; i+=2)
41 >            assertTrue(q.add(new Integer(i)));
42          assertTrue(q.add(new Integer(-n)));
43          assertTrue(q.add(new Integer(n)));
44 <        NavigableSet s = q.subSet(new Integer(0), new Integer(n));
44 >        NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false);
45          assertFalse(s.isEmpty());
46 <        assertEquals(n, s.size());
46 >        assertEquals(n, s.size());
47          return s;
48      }
49  
# Line 60 | Line 60 | public class ConcurrentSkipListSubSetTes
60          q.add(five);
61          q.add(zero);
62          q.add(seven);
63 <        NavigableSet s = q.subSet(one, seven);
64 <        assertEquals(5, s.size());
63 >        NavigableSet s = q.subSet(one, true, seven, false);
64 >        assertEquals(5, s.size());
65          return s;
66      }
67  
68 <    private static NavigableSet set0() {  
69 <        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
68 >    /**
69 >     * Create set of first 5 negative ints
70 >     */
71 >    private NavigableSet dset5() {
72 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
73 >        assertTrue(q.isEmpty());
74 >        q.add(m1);
75 >        q.add(m2);
76 >        q.add(m3);
77 >        q.add(m4);
78 >        q.add(m5);
79 >        NavigableSet s = q.descendingSet();
80 >        assertEquals(5, s.size());
81 >        return s;
82 >    }
83 >
84 >    private static NavigableSet set0() {
85 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
86 >        assertTrue(set.isEmpty());
87 >        return set.tailSet(m1, true);
88 >    }
89 >
90 >    private static NavigableSet dset0() {
91 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
92          assertTrue(set.isEmpty());
93 <        return set.tailSet(m1);
93 >        return set;
94      }
95 <
95 >
96      /**
97       * A new set has unbounded capacity
98       */
# Line 112 | Line 134 | public class ConcurrentSkipListSubSetTes
134       * add(null) throws NPE
135       */
136      public void testAddNull() {
137 <        try {
137 >        try {
138              NavigableSet q = set0();
139              q.add(null);
140              shouldThrow();
141 <        } catch (NullPointerException success) { }  
141 >        } catch (NullPointerException success) {}
142      }
143  
144      /**
# Line 146 | Line 168 | public class ConcurrentSkipListSubSetTes
168              q.add(new Object());
169              q.add(new Object());
170              shouldThrow();
171 <        }
150 <        catch(ClassCastException success) {}
171 >        } catch (ClassCastException success) {}
172      }
173  
174  
# Line 159 | Line 180 | public class ConcurrentSkipListSubSetTes
180              NavigableSet q = set0();
181              q.addAll(null);
182              shouldThrow();
183 <        }
163 <        catch (NullPointerException success) {}
183 >        } catch (NullPointerException success) {}
184      }
185      /**
186       * addAll of a collection with null elements throws NPE
# Line 171 | Line 191 | public class ConcurrentSkipListSubSetTes
191              Integer[] ints = new Integer[SIZE];
192              q.addAll(Arrays.asList(ints));
193              shouldThrow();
194 <        }
175 <        catch (NullPointerException success) {}
194 >        } catch (NullPointerException success) {}
195      }
196      /**
197       * addAll of a collection with any null elements throws NPE after
# Line 186 | Line 205 | public class ConcurrentSkipListSubSetTes
205                  ints[i] = new Integer(i+SIZE);
206              q.addAll(Arrays.asList(ints));
207              shouldThrow();
208 <        }
190 <        catch (NullPointerException success) {}
208 >        } catch (NullPointerException success) {}
209      }
210  
211      /**
212       * Set contains all elements of successful addAll
213       */
214      public void testAddAll5() {
215 <        try {
216 <            Integer[] empty = new Integer[0];
217 <            Integer[] ints = new Integer[SIZE];
218 <            for (int i = 0; i < SIZE; ++i)
219 <                ints[i] = new Integer(SIZE-1- i);
220 <            NavigableSet q = set0();
221 <            assertFalse(q.addAll(Arrays.asList(empty)));
222 <            assertTrue(q.addAll(Arrays.asList(ints)));
223 <            for (int i = 0; i < SIZE; ++i)
206 <                assertEquals(new Integer(i), q.pollFirst());
207 <        }
208 <        finally {}
215 >        Integer[] empty = new Integer[0];
216 >        Integer[] ints = new Integer[SIZE];
217 >        for (int i = 0; i < SIZE; ++i)
218 >            ints[i] = new Integer(SIZE-1- i);
219 >        NavigableSet q = set0();
220 >        assertFalse(q.addAll(Arrays.asList(empty)));
221 >        assertTrue(q.addAll(Arrays.asList(ints)));
222 >        for (int i = 0; i < SIZE; ++i)
223 >            assertEquals(new Integer(i), q.pollFirst());
224      }
225  
226      /**
# Line 216 | Line 231 | public class ConcurrentSkipListSubSetTes
231          for (int i = 0; i < SIZE; ++i) {
232              assertEquals(i, ((Integer)q.pollFirst()).intValue());
233          }
234 <        assertNull(q.pollFirst());
234 >        assertNull(q.pollFirst());
235      }
236  
237      /**
# Line 233 | Line 248 | public class ConcurrentSkipListSubSetTes
248          }
249          assertTrue(q.isEmpty());
250      }
251 <        
251 >
252      /**
253       * contains(x) reports true when elements added but not yet removed
254       */
# Line 309 | Line 324 | public class ConcurrentSkipListSubSetTes
324          }
325      }
326  
327 <    
327 >
328  
329      /**
330       * lower returns preceding element
# Line 392 | Line 407 | public class ConcurrentSkipListSubSetTes
407       */
408      public void testToArray() {
409          NavigableSet q = populatedSet(SIZE);
410 <        Object[] o = q.toArray();
410 >        Object[] o = q.toArray();
411          Arrays.sort(o);
412 <        for(int i = 0; i < o.length; i++)
413 <            assertEquals(o[i], q.pollFirst());
412 >        for (int i = 0; i < o.length; i++)
413 >            assertEquals(o[i], q.pollFirst());
414      }
415  
416      /**
# Line 403 | Line 418 | public class ConcurrentSkipListSubSetTes
418       */
419      public void testToArray2() {
420          NavigableSet q = populatedSet(SIZE);
421 <        Integer[] ints = new Integer[SIZE];
422 <        ints = (Integer[])q.toArray(ints);
421 >        Integer[] ints = new Integer[SIZE];
422 >        ints = (Integer[])q.toArray(ints);
423          Arrays.sort(ints);
424 <        for(int i = 0; i < ints.length; i++)
424 >        for (int i = 0; i < ints.length; i++)
425              assertEquals(ints[i], q.pollFirst());
426      }
427 <    
427 >
428      /**
429       * iterator iterates through all elements
430       */
431      public void testIterator() {
432          NavigableSet q = populatedSet(SIZE);
433          int i = 0;
434 <        Iterator it = q.iterator();
435 <        while(it.hasNext()) {
434 >        Iterator it = q.iterator();
435 >        while (it.hasNext()) {
436              assertTrue(q.contains(it.next()));
437              ++i;
438          }
# Line 430 | Line 445 | public class ConcurrentSkipListSubSetTes
445      public void testEmptyIterator() {
446          NavigableSet q = set0();
447          int i = 0;
448 <        Iterator it = q.iterator();
449 <        while(it.hasNext()) {
448 >        Iterator it = q.iterator();
449 >        while (it.hasNext()) {
450              assertTrue(q.contains(it.next()));
451              ++i;
452          }
# Line 467 | Line 482 | public class ConcurrentSkipListSubSetTes
482          for (int i = 0; i < SIZE; ++i) {
483              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
484          }
485 <    }        
485 >    }
486  
487      /**
488 <     * A deserialized serialized set has same elements
488 >     * A deserialized serialized set has same elements
489       */
490 <    public void testSerialization() {
490 >    public void testSerialization() throws Exception {
491          NavigableSet q = populatedSet(SIZE);
492 <        try {
493 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
494 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
495 <            out.writeObject(q);
496 <            out.close();
497 <
498 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
499 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
500 <            NavigableSet r = (NavigableSet)in.readObject();
501 <            assertEquals(q.size(), r.size());
502 <            while (!q.isEmpty())
488 <                assertEquals(q.pollFirst(), r.pollFirst());
489 <        } catch(Exception e){
490 <            e.printStackTrace();
491 <            unexpectedException();
492 <        }
492 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
493 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
494 >        out.writeObject(q);
495 >        out.close();
496 >
497 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
498 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
499 >        NavigableSet r = (NavigableSet)in.readObject();
500 >        assertEquals(q.size(), r.size());
501 >        while (!q.isEmpty())
502 >            assertEquals(q.pollFirst(), r.pollFirst());
503      }
504  
505      /**
# Line 609 | Line 619 | public class ConcurrentSkipListSubSetTes
619          assertEquals(1, ssm.size());
620          assertEquals(3, sm.size());
621          assertEquals(4, set.size());
622 +    }
623 +
624 +    /**
625 +     * size changes when elements added and removed
626 +     */
627 +    public void testDescendingSize() {
628 +        NavigableSet q = populatedSet(SIZE);
629 +        for (int i = 0; i < SIZE; ++i) {
630 +            assertEquals(SIZE-i, q.size());
631 +            q.pollFirst();
632 +        }
633 +        for (int i = 0; i < SIZE; ++i) {
634 +            assertEquals(i, q.size());
635 +            q.add(new Integer(i));
636 +        }
637 +    }
638 +
639 +    /**
640 +     * add(null) throws NPE
641 +     */
642 +    public void testDescendingAddNull() {
643 +        try {
644 +            NavigableSet q = dset0();
645 +            q.add(null);
646 +            shouldThrow();
647 +        } catch (NullPointerException success) {}
648 +    }
649 +
650 +    /**
651 +     * Add of comparable element succeeds
652 +     */
653 +    public void testDescendingAdd() {
654 +        NavigableSet q = dset0();
655 +        assertTrue(q.add(m6));
656 +    }
657 +
658 +    /**
659 +     * Add of duplicate element fails
660 +     */
661 +    public void testDescendingAddDup() {
662 +        NavigableSet q = dset0();
663 +        assertTrue(q.add(m6));
664 +        assertFalse(q.add(m6));
665 +    }
666 +
667 +    /**
668 +     * Add of non-Comparable throws CCE
669 +     */
670 +    public void testDescendingAddNonComparable() {
671 +        try {
672 +            NavigableSet q = dset0();
673 +            q.add(new Object());
674 +            q.add(new Object());
675 +            q.add(new Object());
676 +            shouldThrow();
677 +        } catch (ClassCastException success) {}
678 +    }
679 +
680 +
681 +    /**
682 +     * addAll(null) throws NPE
683 +     */
684 +    public void testDescendingAddAll1() {
685 +        try {
686 +            NavigableSet q = dset0();
687 +            q.addAll(null);
688 +            shouldThrow();
689 +        } catch (NullPointerException success) {}
690 +    }
691 +    /**
692 +     * addAll of a collection with null elements throws NPE
693 +     */
694 +    public void testDescendingAddAll2() {
695 +        try {
696 +            NavigableSet q = dset0();
697 +            Integer[] ints = new Integer[SIZE];
698 +            q.addAll(Arrays.asList(ints));
699 +            shouldThrow();
700 +        } catch (NullPointerException success) {}
701 +    }
702 +    /**
703 +     * addAll of a collection with any null elements throws NPE after
704 +     * possibly adding some elements
705 +     */
706 +    public void testDescendingAddAll3() {
707 +        try {
708 +            NavigableSet q = dset0();
709 +            Integer[] ints = new Integer[SIZE];
710 +            for (int i = 0; i < SIZE-1; ++i)
711 +                ints[i] = new Integer(i+SIZE);
712 +            q.addAll(Arrays.asList(ints));
713 +            shouldThrow();
714 +        } catch (NullPointerException success) {}
715 +    }
716 +
717 +    /**
718 +     * Set contains all elements of successful addAll
719 +     */
720 +    public void testDescendingAddAll5() {
721 +        Integer[] empty = new Integer[0];
722 +        Integer[] ints = new Integer[SIZE];
723 +        for (int i = 0; i < SIZE; ++i)
724 +            ints[i] = new Integer(SIZE-1- i);
725 +        NavigableSet q = dset0();
726 +        assertFalse(q.addAll(Arrays.asList(empty)));
727 +        assertTrue(q.addAll(Arrays.asList(ints)));
728 +        for (int i = 0; i < SIZE; ++i)
729 +            assertEquals(new Integer(i), q.pollFirst());
730 +    }
731 +
732 +    /**
733 +     * poll succeeds unless empty
734 +     */
735 +    public void testDescendingPoll() {
736 +        NavigableSet q = populatedSet(SIZE);
737 +        for (int i = 0; i < SIZE; ++i) {
738 +            assertEquals(i, ((Integer)q.pollFirst()).intValue());
739 +        }
740 +        assertNull(q.pollFirst());
741 +    }
742 +
743 +    /**
744 +     * remove(x) removes x and returns true if present
745 +     */
746 +    public void testDescendingRemoveElement() {
747 +        NavigableSet q = populatedSet(SIZE);
748 +        for (int i = 1; i < SIZE; i+=2) {
749 +            assertTrue(q.remove(new Integer(i)));
750 +        }
751 +        for (int i = 0; i < SIZE; i+=2) {
752 +            assertTrue(q.remove(new Integer(i)));
753 +            assertFalse(q.remove(new Integer(i+1)));
754 +        }
755 +        assertTrue(q.isEmpty());
756 +    }
757 +
758 +    /**
759 +     * contains(x) reports true when elements added but not yet removed
760 +     */
761 +    public void testDescendingContains() {
762 +        NavigableSet q = populatedSet(SIZE);
763 +        for (int i = 0; i < SIZE; ++i) {
764 +            assertTrue(q.contains(new Integer(i)));
765 +            q.pollFirst();
766 +            assertFalse(q.contains(new Integer(i)));
767 +        }
768 +    }
769 +
770 +    /**
771 +     * clear removes all elements
772 +     */
773 +    public void testDescendingClear() {
774 +        NavigableSet q = populatedSet(SIZE);
775 +        q.clear();
776 +        assertTrue(q.isEmpty());
777 +        assertEquals(0, q.size());
778 +        q.add(new Integer(1));
779 +        assertFalse(q.isEmpty());
780 +        q.clear();
781 +        assertTrue(q.isEmpty());
782 +    }
783 +
784 +    /**
785 +     * containsAll(c) is true when c contains a subset of elements
786 +     */
787 +    public void testDescendingContainsAll() {
788 +        NavigableSet q = populatedSet(SIZE);
789 +        NavigableSet p = dset0();
790 +        for (int i = 0; i < SIZE; ++i) {
791 +            assertTrue(q.containsAll(p));
792 +            assertFalse(p.containsAll(q));
793 +            p.add(new Integer(i));
794 +        }
795 +        assertTrue(p.containsAll(q));
796 +    }
797 +
798 +    /**
799 +     * retainAll(c) retains only those elements of c and reports true if changed
800 +     */
801 +    public void testDescendingRetainAll() {
802 +        NavigableSet q = populatedSet(SIZE);
803 +        NavigableSet p = populatedSet(SIZE);
804 +        for (int i = 0; i < SIZE; ++i) {
805 +            boolean changed = q.retainAll(p);
806 +            if (i == 0)
807 +                assertFalse(changed);
808 +            else
809 +                assertTrue(changed);
810 +
811 +            assertTrue(q.containsAll(p));
812 +            assertEquals(SIZE-i, q.size());
813 +            p.pollFirst();
814 +        }
815 +    }
816 +
817 +    /**
818 +     * removeAll(c) removes only those elements of c and reports true if changed
819 +     */
820 +    public void testDescendingRemoveAll() {
821 +        for (int i = 1; i < SIZE; ++i) {
822 +            NavigableSet q = populatedSet(SIZE);
823 +            NavigableSet p = populatedSet(i);
824 +            assertTrue(q.removeAll(p));
825 +            assertEquals(SIZE-i, q.size());
826 +            for (int j = 0; j < i; ++j) {
827 +                Integer I = (Integer)(p.pollFirst());
828 +                assertFalse(q.contains(I));
829 +            }
830 +        }
831 +    }
832 +
833 +
834 +
835 +    /**
836 +     * lower returns preceding element
837 +     */
838 +    public void testDescendingLower() {
839 +        NavigableSet q = dset5();
840 +        Object e1 = q.lower(m3);
841 +        assertEquals(m2, e1);
842 +
843 +        Object e2 = q.lower(m6);
844 +        assertEquals(m5, e2);
845 +
846 +        Object e3 = q.lower(m1);
847 +        assertNull(e3);
848 +
849 +        Object e4 = q.lower(zero);
850 +        assertNull(e4);
851 +
852 +    }
853 +
854 +    /**
855 +     * higher returns next element
856 +     */
857 +    public void testDescendingHigher() {
858 +        NavigableSet q = dset5();
859 +        Object e1 = q.higher(m3);
860 +        assertEquals(m4, e1);
861 +
862 +        Object e2 = q.higher(zero);
863 +        assertEquals(m1, e2);
864 +
865 +        Object e3 = q.higher(m5);
866 +        assertNull(e3);
867 +
868 +        Object e4 = q.higher(m6);
869 +        assertNull(e4);
870 +
871 +    }
872 +
873 +    /**
874 +     * floor returns preceding element
875 +     */
876 +    public void testDescendingFloor() {
877 +        NavigableSet q = dset5();
878 +        Object e1 = q.floor(m3);
879 +        assertEquals(m3, e1);
880 +
881 +        Object e2 = q.floor(m6);
882 +        assertEquals(m5, e2);
883 +
884 +        Object e3 = q.floor(m1);
885 +        assertEquals(m1, e3);
886 +
887 +        Object e4 = q.floor(zero);
888 +        assertNull(e4);
889 +
890 +    }
891 +
892 +    /**
893 +     * ceiling returns next element
894 +     */
895 +    public void testDescendingCeiling() {
896 +        NavigableSet q = dset5();
897 +        Object e1 = q.ceiling(m3);
898 +        assertEquals(m3, e1);
899 +
900 +        Object e2 = q.ceiling(zero);
901 +        assertEquals(m1, e2);
902 +
903 +        Object e3 = q.ceiling(m5);
904 +        assertEquals(m5, e3);
905 +
906 +        Object e4 = q.ceiling(m6);
907 +        assertNull(e4);
908 +
909 +    }
910 +
911 +    /**
912 +     * toArray contains all elements
913 +     */
914 +    public void testDescendingToArray() {
915 +        NavigableSet q = populatedSet(SIZE);
916 +        Object[] o = q.toArray();
917 +        Arrays.sort(o);
918 +        for (int i = 0; i < o.length; i++)
919 +            assertEquals(o[i], q.pollFirst());
920 +    }
921 +
922 +    /**
923 +     * toArray(a) contains all elements
924 +     */
925 +    public void testDescendingToArray2() {
926 +        NavigableSet q = populatedSet(SIZE);
927 +        Integer[] ints = new Integer[SIZE];
928 +        ints = (Integer[])q.toArray(ints);
929 +        Arrays.sort(ints);
930 +        for (int i = 0; i < ints.length; i++)
931 +            assertEquals(ints[i], q.pollFirst());
932 +    }
933 +
934 +    /**
935 +     * iterator iterates through all elements
936 +     */
937 +    public void testDescendingIterator() {
938 +        NavigableSet q = populatedSet(SIZE);
939 +        int i = 0;
940 +        Iterator it = q.iterator();
941 +        while (it.hasNext()) {
942 +            assertTrue(q.contains(it.next()));
943 +            ++i;
944 +        }
945 +        assertEquals(i, SIZE);
946 +    }
947 +
948 +    /**
949 +     * iterator of empty set has no elements
950 +     */
951 +    public void testDescendingEmptyIterator() {
952 +        NavigableSet q = dset0();
953 +        int i = 0;
954 +        Iterator it = q.iterator();
955 +        while (it.hasNext()) {
956 +            assertTrue(q.contains(it.next()));
957 +            ++i;
958 +        }
959 +        assertEquals(i, 0);
960 +    }
961 +
962 +    /**
963 +     * iterator.remove removes current element
964 +     */
965 +    public void testDescendingIteratorRemove () {
966 +        final NavigableSet q = dset0();
967 +        q.add(new Integer(2));
968 +        q.add(new Integer(1));
969 +        q.add(new Integer(3));
970 +
971 +        Iterator it = q.iterator();
972 +        it.next();
973 +        it.remove();
974 +
975 +        it = q.iterator();
976 +        assertEquals(it.next(), new Integer(2));
977 +        assertEquals(it.next(), new Integer(3));
978 +        assertFalse(it.hasNext());
979 +    }
980 +
981 +
982 +    /**
983 +     * toString contains toStrings of elements
984 +     */
985 +    public void testDescendingToString() {
986 +        NavigableSet q = populatedSet(SIZE);
987 +        String s = q.toString();
988 +        for (int i = 0; i < SIZE; ++i) {
989 +            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
990 +        }
991 +    }
992 +
993 +    /**
994 +     * A deserialized serialized set has same elements
995 +     */
996 +    public void testDescendingSerialization() throws Exception {
997 +        NavigableSet q = populatedSet(SIZE);
998 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
999 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1000 +        out.writeObject(q);
1001 +        out.close();
1002 +
1003 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1004 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1005 +        NavigableSet r = (NavigableSet)in.readObject();
1006 +        assertEquals(q.size(), r.size());
1007 +        while (!q.isEmpty())
1008 +            assertEquals(q.pollFirst(), r.pollFirst());
1009 +    }
1010 +
1011 +    /**
1012 +     * subSet returns set with keys in requested range
1013 +     */
1014 +    public void testDescendingSubSetContents() {
1015 +        NavigableSet set = dset5();
1016 +        SortedSet sm = set.subSet(m2, m4);
1017 +        assertEquals(m2, sm.first());
1018 +        assertEquals(m3, sm.last());
1019 +        assertEquals(2, sm.size());
1020 +        assertFalse(sm.contains(m1));
1021 +        assertTrue(sm.contains(m2));
1022 +        assertTrue(sm.contains(m3));
1023 +        assertFalse(sm.contains(m4));
1024 +        assertFalse(sm.contains(m5));
1025 +        Iterator i = sm.iterator();
1026 +        Object k;
1027 +        k = (Integer)(i.next());
1028 +        assertEquals(m2, k);
1029 +        k = (Integer)(i.next());
1030 +        assertEquals(m3, k);
1031 +        assertFalse(i.hasNext());
1032 +        Iterator j = sm.iterator();
1033 +        j.next();
1034 +        j.remove();
1035 +        assertFalse(set.contains(m2));
1036 +        assertEquals(4, set.size());
1037 +        assertEquals(1, sm.size());
1038 +        assertEquals(m3, sm.first());
1039 +        assertEquals(m3, sm.last());
1040 +        assertTrue(sm.remove(m3));
1041 +        assertTrue(sm.isEmpty());
1042 +        assertEquals(3, set.size());
1043 +    }
1044 +
1045 +    public void testDescendingSubSetContents2() {
1046 +        NavigableSet set = dset5();
1047 +        SortedSet sm = set.subSet(m2, m3);
1048 +        assertEquals(1, sm.size());
1049 +        assertEquals(m2, sm.first());
1050 +        assertEquals(m2, sm.last());
1051 +        assertFalse(sm.contains(m1));
1052 +        assertTrue(sm.contains(m2));
1053 +        assertFalse(sm.contains(m3));
1054 +        assertFalse(sm.contains(m4));
1055 +        assertFalse(sm.contains(m5));
1056 +        Iterator i = sm.iterator();
1057 +        Object k;
1058 +        k = (Integer)(i.next());
1059 +        assertEquals(m2, k);
1060 +        assertFalse(i.hasNext());
1061 +        Iterator j = sm.iterator();
1062 +        j.next();
1063 +        j.remove();
1064 +        assertFalse(set.contains(m2));
1065 +        assertEquals(4, set.size());
1066 +        assertEquals(0, sm.size());
1067 +        assertTrue(sm.isEmpty());
1068 +        assertFalse(sm.remove(m3));
1069 +        assertEquals(4, set.size());
1070 +    }
1071 +
1072 +    /**
1073 +     * headSet returns set with keys in requested range
1074 +     */
1075 +    public void testDescendingHeadSetContents() {
1076 +        NavigableSet set = dset5();
1077 +        SortedSet sm = set.headSet(m4);
1078 +        assertTrue(sm.contains(m1));
1079 +        assertTrue(sm.contains(m2));
1080 +        assertTrue(sm.contains(m3));
1081 +        assertFalse(sm.contains(m4));
1082 +        assertFalse(sm.contains(m5));
1083 +        Iterator i = sm.iterator();
1084 +        Object k;
1085 +        k = (Integer)(i.next());
1086 +        assertEquals(m1, k);
1087 +        k = (Integer)(i.next());
1088 +        assertEquals(m2, k);
1089 +        k = (Integer)(i.next());
1090 +        assertEquals(m3, k);
1091 +        assertFalse(i.hasNext());
1092 +        sm.clear();
1093 +        assertTrue(sm.isEmpty());
1094 +        assertEquals(2, set.size());
1095 +        assertEquals(m4, set.first());
1096 +    }
1097 +
1098 +    /**
1099 +     * tailSet returns set with keys in requested range
1100 +     */
1101 +    public void testDescendingTailSetContents() {
1102 +        NavigableSet set = dset5();
1103 +        SortedSet sm = set.tailSet(m2);
1104 +        assertFalse(sm.contains(m1));
1105 +        assertTrue(sm.contains(m2));
1106 +        assertTrue(sm.contains(m3));
1107 +        assertTrue(sm.contains(m4));
1108 +        assertTrue(sm.contains(m5));
1109 +        Iterator i = sm.iterator();
1110 +        Object k;
1111 +        k = (Integer)(i.next());
1112 +        assertEquals(m2, k);
1113 +        k = (Integer)(i.next());
1114 +        assertEquals(m3, k);
1115 +        k = (Integer)(i.next());
1116 +        assertEquals(m4, k);
1117 +        k = (Integer)(i.next());
1118 +        assertEquals(m5, k);
1119 +        assertFalse(i.hasNext());
1120 +
1121 +        SortedSet ssm = sm.tailSet(m4);
1122 +        assertEquals(m4, ssm.first());
1123 +        assertEquals(m5, ssm.last());
1124 +        assertTrue(ssm.remove(m4));
1125 +        assertEquals(1, ssm.size());
1126 +        assertEquals(3, sm.size());
1127 +        assertEquals(4, set.size());
1128      }
1129  
1130   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines