1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
7 |
> |
import java.util.ArrayList; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.Map; |
12 |
> |
import java.util.NavigableMap; |
13 |
> |
import java.util.Set; |
14 |
> |
import java.util.SortedMap; |
15 |
> |
import java.util.TreeMap; |
16 |
> |
|
17 |
> |
import junit.framework.Test; |
18 |
> |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class TreeSubMapTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run (suite()); |
22 |
> |
junit.textui.TestRunner.run(suite()); |
23 |
|
} |
24 |
|
public static Test suite() { |
25 |
|
return new TestSuite(TreeSubMapTest.class); |
26 |
|
} |
27 |
|
|
28 |
|
/** |
29 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
29 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
30 |
|
*/ |
31 |
|
private static NavigableMap map5() { |
32 |
|
TreeMap map = new TreeMap(); |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Create a map from Integers -5 to -1 to Strings "A"-"E". |
53 |
> |
* Returns a new map from Integers -5 to -1 to Strings "A"-"E". |
54 |
|
*/ |
55 |
|
private static NavigableMap dmap5() { |
56 |
|
TreeMap map = new TreeMap(); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* clear removes all pairs |
75 |
> |
* clear removes all pairs |
76 |
|
*/ |
77 |
|
public void testClear() { |
78 |
|
NavigableMap map = map5(); |
79 |
|
map.clear(); |
80 |
< |
assertEquals(map.size(), 0); |
80 |
> |
assertEquals(0, map.size()); |
81 |
|
} |
82 |
|
|
75 |
– |
|
83 |
|
/** |
84 |
< |
* Maps with same contents are equal |
84 |
> |
* Maps with same contents are equal |
85 |
|
*/ |
86 |
|
public void testEquals() { |
87 |
|
NavigableMap map1 = map5(); |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
< |
* containsKey returns true for contained key |
97 |
> |
* containsKey returns true for contained key |
98 |
|
*/ |
99 |
|
public void testContainsKey() { |
100 |
|
NavigableMap map = map5(); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
< |
* containsValue returns true for held values |
106 |
> |
* containsValue returns true for held values |
107 |
|
*/ |
108 |
|
public void testContainsValue() { |
109 |
|
NavigableMap map = map5(); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
< |
* get returns the correct element at the given key, |
116 |
< |
* or null if not present |
115 |
> |
* get returns the correct element at the given key, |
116 |
> |
* or null if not present |
117 |
|
*/ |
118 |
|
public void testGet() { |
119 |
|
NavigableMap map = map5(); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
< |
* isEmpty is true of empty map and false for non-empty |
126 |
> |
* isEmpty is true of empty map and false for non-empty |
127 |
|
*/ |
128 |
|
public void testIsEmpty() { |
129 |
|
NavigableMap empty = map0(); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* firstKey returns first key |
136 |
> |
* firstKey returns first key |
137 |
|
*/ |
138 |
|
public void testFirstKey() { |
139 |
|
NavigableMap map = map5(); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* lastKey returns last key |
144 |
> |
* lastKey returns last key |
145 |
|
*/ |
146 |
|
public void testLastKey() { |
147 |
|
NavigableMap map = map5(); |
148 |
|
assertEquals(five, map.lastKey()); |
149 |
|
} |
150 |
|
|
144 |
– |
|
151 |
|
/** |
152 |
< |
* keySet returns a Set containing all the keys |
152 |
> |
* keySet returns a Set containing all the keys |
153 |
|
*/ |
154 |
|
public void testKeySet() { |
155 |
|
NavigableMap map = map5(); |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
< |
* keySet is ordered |
166 |
> |
* keySet is ordered |
167 |
|
*/ |
168 |
|
public void testKeySetOrder() { |
169 |
|
NavigableMap map = map5(); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* putAll adds all key-value pairs from the given map |
215 |
> |
* putAll adds all key-value pairs from the given map |
216 |
|
*/ |
217 |
|
public void testPutAll() { |
218 |
|
NavigableMap empty = map0(); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* remove removes the correct key-value pair from the map |
230 |
> |
* remove removes the correct key-value pair from the map |
231 |
|
*/ |
232 |
|
public void testRemove() { |
233 |
|
NavigableMap map = map5(); |
252 |
|
|
253 |
|
Map.Entry e4 = map.lowerEntry(zero); |
254 |
|
assertNull(e4); |
249 |
– |
|
255 |
|
} |
256 |
|
|
257 |
|
/** |
270 |
|
|
271 |
|
Map.Entry e4 = map.higherEntry(six); |
272 |
|
assertNull(e4); |
268 |
– |
|
273 |
|
} |
274 |
|
|
275 |
|
/** |
288 |
|
|
289 |
|
Map.Entry e4 = map.floorEntry(zero); |
290 |
|
assertNull(e4); |
287 |
– |
|
291 |
|
} |
292 |
|
|
293 |
|
/** |
306 |
|
|
307 |
|
Map.Entry e4 = map.ceilingEntry(six); |
308 |
|
assertNull(e4); |
306 |
– |
|
309 |
|
} |
310 |
|
|
311 |
|
/** |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
< |
* size returns the correct values |
369 |
> |
* size returns the correct values |
370 |
|
*/ |
371 |
|
public void testSize() { |
372 |
|
NavigableMap map = map5(); |
382 |
|
NavigableMap map = map5(); |
383 |
|
String s = map.toString(); |
384 |
|
for (int i = 1; i <= 5; ++i) { |
385 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
385 |
> |
assertTrue(s.contains(String.valueOf(i))); |
386 |
|
} |
387 |
|
} |
388 |
|
|
392 |
|
* get(null) of nonempty map throws NPE |
393 |
|
*/ |
394 |
|
public void testGet_NullPointerException() { |
395 |
+ |
NavigableMap c = map5(); |
396 |
|
try { |
394 |
– |
NavigableMap c = map5(); |
397 |
|
c.get(null); |
398 |
|
shouldThrow(); |
399 |
|
} catch (NullPointerException success) {} |
403 |
|
* containsKey(null) of nonempty map throws NPE |
404 |
|
*/ |
405 |
|
public void testContainsKey_NullPointerException() { |
406 |
+ |
NavigableMap c = map5(); |
407 |
|
try { |
405 |
– |
NavigableMap c = map5(); |
408 |
|
c.containsKey(null); |
409 |
|
shouldThrow(); |
410 |
|
} catch (NullPointerException success) {} |
414 |
|
* put(null,x) throws NPE |
415 |
|
*/ |
416 |
|
public void testPut1_NullPointerException() { |
417 |
+ |
NavigableMap c = map5(); |
418 |
|
try { |
416 |
– |
NavigableMap c = map5(); |
419 |
|
c.put(null, "whatever"); |
420 |
|
shouldThrow(); |
421 |
|
} catch (NullPointerException success) {} |
425 |
|
* remove(null) throws NPE |
426 |
|
*/ |
427 |
|
public void testRemove1_NullPointerException() { |
428 |
+ |
NavigableMap c = map5(); |
429 |
|
try { |
427 |
– |
NavigableMap c = map5(); |
430 |
|
c.remove(null); |
431 |
|
shouldThrow(); |
432 |
|
} catch (NullPointerException success) {} |
436 |
|
* A deserialized map equals original |
437 |
|
*/ |
438 |
|
public void testSerialization() throws Exception { |
439 |
< |
NavigableMap q = map5(); |
439 |
> |
NavigableMap x = map5(); |
440 |
> |
NavigableMap y = serialClone(x); |
441 |
|
|
442 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
443 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
444 |
< |
out.writeObject(q); |
445 |
< |
out.close(); |
446 |
< |
|
444 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
445 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
446 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
447 |
< |
assertFalse(r.isEmpty()); |
448 |
< |
assertEquals(q.size(), r.size()); |
449 |
< |
assertTrue(q.equals(r)); |
450 |
< |
assertTrue(r.equals(q)); |
442 |
> |
assertNotSame(x, y); |
443 |
> |
assertEquals(x.size(), y.size()); |
444 |
> |
assertEquals(x.toString(), y.toString()); |
445 |
> |
assertEquals(x, y); |
446 |
> |
assertEquals(y, x); |
447 |
|
} |
448 |
|
|
453 |
– |
|
454 |
– |
|
449 |
|
/** |
450 |
|
* subMap returns map with keys in requested range |
451 |
|
*/ |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
< |
* clear removes all pairs |
585 |
> |
* clear removes all pairs |
586 |
|
*/ |
587 |
|
public void testDescendingClear() { |
588 |
|
NavigableMap map = dmap5(); |
589 |
|
map.clear(); |
590 |
< |
assertEquals(map.size(), 0); |
590 |
> |
assertEquals(0, map.size()); |
591 |
|
} |
592 |
|
|
599 |
– |
|
593 |
|
/** |
594 |
< |
* Maps with same contents are equal |
594 |
> |
* Maps with same contents are equal |
595 |
|
*/ |
596 |
|
public void testDescendingEquals() { |
597 |
|
NavigableMap map1 = dmap5(); |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* containsKey returns true for contained key |
607 |
> |
* containsKey returns true for contained key |
608 |
|
*/ |
609 |
|
public void testDescendingContainsKey() { |
610 |
|
NavigableMap map = dmap5(); |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
< |
* containsValue returns true for held values |
616 |
> |
* containsValue returns true for held values |
617 |
|
*/ |
618 |
|
public void testDescendingContainsValue() { |
619 |
|
NavigableMap map = dmap5(); |
622 |
|
} |
623 |
|
|
624 |
|
/** |
625 |
< |
* get returns the correct element at the given key, |
626 |
< |
* or null if not present |
625 |
> |
* get returns the correct element at the given key, |
626 |
> |
* or null if not present |
627 |
|
*/ |
628 |
|
public void testDescendingGet() { |
629 |
|
NavigableMap map = dmap5(); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
< |
* isEmpty is true of empty map and false for non-empty |
636 |
> |
* isEmpty is true of empty map and false for non-empty |
637 |
|
*/ |
638 |
|
public void testDescendingIsEmpty() { |
639 |
|
NavigableMap empty = dmap0(); |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
< |
* firstKey returns first key |
646 |
> |
* firstKey returns first key |
647 |
|
*/ |
648 |
|
public void testDescendingFirstKey() { |
649 |
|
NavigableMap map = dmap5(); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
654 |
< |
* lastKey returns last key |
654 |
> |
* lastKey returns last key |
655 |
|
*/ |
656 |
|
public void testDescendingLastKey() { |
657 |
|
NavigableMap map = dmap5(); |
658 |
|
assertEquals(m5, map.lastKey()); |
659 |
|
} |
660 |
|
|
668 |
– |
|
661 |
|
/** |
662 |
< |
* keySet returns a Set containing all the keys |
662 |
> |
* keySet returns a Set containing all the keys |
663 |
|
*/ |
664 |
|
public void testDescendingKeySet() { |
665 |
|
NavigableMap map = dmap5(); |
673 |
|
} |
674 |
|
|
675 |
|
/** |
676 |
< |
* keySet is ordered |
676 |
> |
* keySet is ordered |
677 |
|
*/ |
678 |
|
public void testDescendingKeySetOrder() { |
679 |
|
NavigableMap map = dmap5(); |
703 |
|
} |
704 |
|
|
705 |
|
/** |
706 |
< |
* keySet.toArray returns contains all keys |
706 |
> |
* keySet.toArray returns contains all keys |
707 |
|
*/ |
708 |
|
public void testDescendingAscendingKeySetToArray() { |
709 |
|
NavigableMap map = dmap5(); |
716 |
|
} |
717 |
|
|
718 |
|
/** |
719 |
< |
* descendingkeySet.toArray returns contains all keys |
719 |
> |
* descendingkeySet.toArray returns contains all keys |
720 |
|
*/ |
721 |
|
public void testDescendingDescendingKeySetToArray() { |
722 |
|
NavigableMap map = dmap5(); |
729 |
|
} |
730 |
|
|
731 |
|
/** |
732 |
< |
* Values.toArray contains all values |
732 |
> |
* Values.toArray contains all values |
733 |
|
*/ |
734 |
|
public void testDescendingValuesToArray() { |
735 |
|
NavigableMap map = dmap5(); |
744 |
|
assertTrue(s.contains("E")); |
745 |
|
} |
746 |
|
|
755 |
– |
|
747 |
|
/** |
748 |
|
* entrySet contains all pairs |
749 |
|
*/ |
764 |
|
} |
765 |
|
|
766 |
|
/** |
767 |
< |
* putAll adds all key-value pairs from the given map |
767 |
> |
* putAll adds all key-value pairs from the given map |
768 |
|
*/ |
769 |
|
public void testDescendingPutAll() { |
770 |
|
NavigableMap empty = dmap0(); |
778 |
|
assertTrue(empty.containsKey(m5)); |
779 |
|
} |
780 |
|
|
790 |
– |
|
781 |
|
/** |
782 |
< |
* remove removes the correct key-value pair from the map |
782 |
> |
* remove removes the correct key-value pair from the map |
783 |
|
*/ |
784 |
|
public void testDescendingRemove() { |
785 |
|
NavigableMap map = dmap5(); |
804 |
|
|
805 |
|
Map.Entry e4 = map.lowerEntry(zero); |
806 |
|
assertNull(e4); |
817 |
– |
|
807 |
|
} |
808 |
|
|
809 |
|
/** |
822 |
|
|
823 |
|
Map.Entry e4 = map.higherEntry(m6); |
824 |
|
assertNull(e4); |
836 |
– |
|
825 |
|
} |
826 |
|
|
827 |
|
/** |
840 |
|
|
841 |
|
Map.Entry e4 = map.floorEntry(zero); |
842 |
|
assertNull(e4); |
855 |
– |
|
843 |
|
} |
844 |
|
|
845 |
|
/** |
858 |
|
|
859 |
|
Map.Entry e4 = map.ceilingEntry(m6); |
860 |
|
assertNull(e4); |
874 |
– |
|
861 |
|
} |
862 |
|
|
863 |
|
/** |
915 |
|
} |
916 |
|
|
917 |
|
/** |
918 |
< |
* size returns the correct values |
918 |
> |
* size returns the correct values |
919 |
|
*/ |
920 |
|
public void testDescendingSize() { |
921 |
|
NavigableMap map = dmap5(); |
931 |
|
NavigableMap map = dmap5(); |
932 |
|
String s = map.toString(); |
933 |
|
for (int i = 1; i <= 5; ++i) { |
934 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
934 |
> |
assertTrue(s.contains(String.valueOf(i))); |
935 |
|
} |
936 |
|
} |
937 |
|
|
941 |
|
* get(null) of nonempty map throws NPE |
942 |
|
*/ |
943 |
|
public void testDescendingGet_NullPointerException() { |
944 |
+ |
NavigableMap c = dmap5(); |
945 |
|
try { |
959 |
– |
NavigableMap c = dmap5(); |
946 |
|
c.get(null); |
947 |
|
shouldThrow(); |
948 |
|
} catch (NullPointerException success) {} |
952 |
|
* put(null,x) throws NPE |
953 |
|
*/ |
954 |
|
public void testDescendingPut1_NullPointerException() { |
955 |
+ |
NavigableMap c = dmap5(); |
956 |
|
try { |
970 |
– |
NavigableMap c = dmap5(); |
957 |
|
c.put(null, "whatever"); |
958 |
|
shouldThrow(); |
959 |
|
} catch (NullPointerException success) {} |
963 |
|
* A deserialized map equals original |
964 |
|
*/ |
965 |
|
public void testDescendingSerialization() throws Exception { |
966 |
< |
NavigableMap q = dmap5(); |
966 |
> |
NavigableMap x = dmap5(); |
967 |
> |
NavigableMap y = serialClone(x); |
968 |
|
|
969 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
970 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
971 |
< |
out.writeObject(q); |
972 |
< |
out.close(); |
973 |
< |
|
987 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
988 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
989 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
990 |
< |
assertEquals(q.size(), r.size()); |
991 |
< |
assertTrue(q.equals(r)); |
992 |
< |
assertTrue(r.equals(q)); |
969 |
> |
assertNotSame(x, y); |
970 |
> |
assertEquals(x.size(), y.size()); |
971 |
> |
assertEquals(x.toString(), y.toString()); |
972 |
> |
assertEquals(x, y); |
973 |
> |
assertEquals(y, x); |
974 |
|
} |
975 |
|
|
995 |
– |
|
976 |
|
/** |
977 |
|
* subMap returns map with keys in requested range |
978 |
|
*/ |