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