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 |
> |
main(suite(), args); |
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 |
|
/** |
330 |
|
try { |
331 |
|
e.setValue("A"); |
332 |
|
shouldThrow(); |
333 |
< |
} catch (Exception ok) { |
332 |
< |
} |
333 |
> |
} catch (UnsupportedOperationException success) {} |
334 |
|
assertTrue(map.isEmpty()); |
335 |
|
Map.Entry f = map.firstEntry(); |
336 |
|
assertNull(f); |
360 |
|
try { |
361 |
|
e.setValue("E"); |
362 |
|
shouldThrow(); |
363 |
< |
} catch (Exception ok) { |
363 |
< |
} |
363 |
> |
} catch (UnsupportedOperationException success) {} |
364 |
|
e = map.pollLastEntry(); |
365 |
|
assertNull(e); |
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 { |
396 |
– |
NavigableMap c = map5(); |
397 |
|
c.get(null); |
398 |
|
shouldThrow(); |
399 |
< |
} catch (NullPointerException e) {} |
399 |
> |
} catch (NullPointerException success) {} |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
|
* containsKey(null) of nonempty map throws NPE |
404 |
|
*/ |
405 |
|
public void testContainsKey_NullPointerException() { |
406 |
+ |
NavigableMap c = map5(); |
407 |
|
try { |
407 |
– |
NavigableMap c = map5(); |
408 |
|
c.containsKey(null); |
409 |
|
shouldThrow(); |
410 |
< |
} catch (NullPointerException e) {} |
410 |
> |
} catch (NullPointerException success) {} |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
|
* put(null,x) throws NPE |
415 |
|
*/ |
416 |
|
public void testPut1_NullPointerException() { |
417 |
+ |
NavigableMap c = map5(); |
418 |
|
try { |
418 |
– |
NavigableMap c = map5(); |
419 |
|
c.put(null, "whatever"); |
420 |
|
shouldThrow(); |
421 |
< |
} catch (NullPointerException e) {} |
421 |
> |
} catch (NullPointerException success) {} |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
|
* remove(null) throws NPE |
426 |
|
*/ |
427 |
|
public void testRemove1_NullPointerException() { |
428 |
+ |
NavigableMap c = map5(); |
429 |
|
try { |
429 |
– |
NavigableMap c = map5(); |
430 |
|
c.remove(null); |
431 |
|
shouldThrow(); |
432 |
< |
} catch (NullPointerException e) {} |
432 |
> |
} catch (NullPointerException success) {} |
433 |
|
} |
434 |
|
|
435 |
|
/** |
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 |
< |
|
446 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
447 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
448 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
449 |
< |
assertFalse(r.isEmpty()); |
450 |
< |
assertEquals(q.size(), r.size()); |
451 |
< |
assertTrue(q.equals(r)); |
452 |
< |
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 |
|
|
455 |
– |
|
456 |
– |
|
449 |
|
/** |
450 |
|
* subMap returns map with keys in requested range |
451 |
|
*/ |
475 |
|
assertEquals(1, sm.size()); |
476 |
|
assertEquals(three, sm.firstKey()); |
477 |
|
assertEquals(three, sm.lastKey()); |
478 |
< |
assertTrue(sm.remove(three) != null); |
478 |
> |
assertEquals("C", sm.remove(three)); |
479 |
|
assertTrue(sm.isEmpty()); |
480 |
|
assertEquals(3, map.size()); |
481 |
|
} |
503 |
|
assertEquals(4, map.size()); |
504 |
|
assertEquals(0, sm.size()); |
505 |
|
assertTrue(sm.isEmpty()); |
506 |
< |
assertTrue(sm.remove(three) == null); |
506 |
> |
assertSame(sm.remove(three), null); |
507 |
|
assertEquals(4, map.size()); |
508 |
|
} |
509 |
|
|
575 |
|
SortedMap ssm = sm.tailMap(four); |
576 |
|
assertEquals(four, ssm.firstKey()); |
577 |
|
assertEquals(five, ssm.lastKey()); |
578 |
< |
assertTrue(ssm.remove(four) != null); |
578 |
> |
assertEquals("D", ssm.remove(four)); |
579 |
|
assertEquals(1, ssm.size()); |
580 |
|
assertEquals(3, sm.size()); |
581 |
|
assertEquals(4, map.size()); |
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 |
|
|
601 |
– |
|
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 |
|
|
670 |
– |
|
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 |
|
|
757 |
– |
|
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 |
|
|
792 |
– |
|
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); |
819 |
– |
|
807 |
|
} |
808 |
|
|
809 |
|
/** |
822 |
|
|
823 |
|
Map.Entry e4 = map.higherEntry(m6); |
824 |
|
assertNull(e4); |
838 |
– |
|
825 |
|
} |
826 |
|
|
827 |
|
/** |
840 |
|
|
841 |
|
Map.Entry e4 = map.floorEntry(zero); |
842 |
|
assertNull(e4); |
857 |
– |
|
843 |
|
} |
844 |
|
|
845 |
|
/** |
858 |
|
|
859 |
|
Map.Entry e4 = map.ceilingEntry(m6); |
860 |
|
assertNull(e4); |
876 |
– |
|
861 |
|
} |
862 |
|
|
863 |
|
/** |
882 |
|
try { |
883 |
|
e.setValue("A"); |
884 |
|
shouldThrow(); |
885 |
< |
} catch (Exception ok) { |
902 |
< |
} |
885 |
> |
} catch (UnsupportedOperationException success) {} |
886 |
|
e = map.pollFirstEntry(); |
887 |
|
assertNull(e); |
888 |
|
} |
909 |
|
try { |
910 |
|
e.setValue("E"); |
911 |
|
shouldThrow(); |
912 |
< |
} catch (Exception ok) { |
930 |
< |
} |
912 |
> |
} catch (UnsupportedOperationException success) {} |
913 |
|
e = map.pollLastEntry(); |
914 |
|
assertNull(e); |
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 { |
963 |
– |
NavigableMap c = dmap5(); |
946 |
|
c.get(null); |
947 |
|
shouldThrow(); |
948 |
< |
} catch (NullPointerException e) {} |
948 |
> |
} catch (NullPointerException success) {} |
949 |
|
} |
950 |
|
|
951 |
|
/** |
952 |
|
* put(null,x) throws NPE |
953 |
|
*/ |
954 |
|
public void testDescendingPut1_NullPointerException() { |
955 |
+ |
NavigableMap c = dmap5(); |
956 |
|
try { |
974 |
– |
NavigableMap c = dmap5(); |
957 |
|
c.put(null, "whatever"); |
958 |
|
shouldThrow(); |
959 |
< |
} catch (NullPointerException e) {} |
959 |
> |
} catch (NullPointerException success) {} |
960 |
|
} |
961 |
|
|
962 |
|
/** |
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 |
< |
|
991 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
992 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
993 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
994 |
< |
assertEquals(q.size(), r.size()); |
995 |
< |
assertTrue(q.equals(r)); |
996 |
< |
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 |
|
|
999 |
– |
|
976 |
|
/** |
977 |
|
* subMap returns map with keys in requested range |
978 |
|
*/ |
1002 |
|
assertEquals(1, sm.size()); |
1003 |
|
assertEquals(m3, sm.firstKey()); |
1004 |
|
assertEquals(m3, sm.lastKey()); |
1005 |
< |
assertTrue(sm.remove(m3) != null); |
1005 |
> |
assertEquals("C", sm.remove(m3)); |
1006 |
|
assertTrue(sm.isEmpty()); |
1007 |
|
assertEquals(3, map.size()); |
1008 |
|
} |
1030 |
|
assertEquals(4, map.size()); |
1031 |
|
assertEquals(0, sm.size()); |
1032 |
|
assertTrue(sm.isEmpty()); |
1033 |
< |
assertTrue(sm.remove(m3) == null); |
1033 |
> |
assertSame(sm.remove(m3), null); |
1034 |
|
assertEquals(4, map.size()); |
1035 |
|
} |
1036 |
|
|
1102 |
|
SortedMap ssm = sm.tailMap(m4); |
1103 |
|
assertEquals(m4, ssm.firstKey()); |
1104 |
|
assertEquals(m5, ssm.lastKey()); |
1105 |
< |
assertTrue(ssm.remove(m4) != null); |
1105 |
> |
assertEquals("D", ssm.remove(m4)); |
1106 |
|
assertEquals(1, ssm.size()); |
1107 |
|
assertEquals(3, sm.size()); |
1108 |
|
assertEquals(4, map.size()); |