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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines