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.*; |
9 |
|
|
10 |
|
public class TreeSubMapTest extends JSR166TestCase { |
11 |
|
public static void main(String[] args) { |
12 |
< |
junit.textui.TestRunner.run (suite()); |
12 |
> |
junit.textui.TestRunner.run(suite()); |
13 |
|
} |
14 |
|
public static Test suite() { |
15 |
|
return new TestSuite(TreeSubMapTest.class); |
16 |
|
} |
17 |
|
|
18 |
|
/** |
19 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
19 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
20 |
|
*/ |
21 |
|
private static NavigableMap map5() { |
22 |
|
TreeMap map = new TreeMap(); |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* Create a map from Integers -5 to -1 to Strings "A"-"E". |
43 |
> |
* Returns a new map from Integers -5 to -1 to Strings "A"-"E". |
44 |
|
*/ |
45 |
|
private static NavigableMap dmap5() { |
46 |
|
TreeMap map = new TreeMap(); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* clear removes all pairs |
65 |
> |
* clear removes all pairs |
66 |
|
*/ |
67 |
|
public void testClear() { |
68 |
|
NavigableMap map = map5(); |
69 |
|
map.clear(); |
70 |
< |
assertEquals(map.size(), 0); |
70 |
> |
assertEquals(0, map.size()); |
71 |
|
} |
72 |
|
|
75 |
– |
|
73 |
|
/** |
74 |
< |
* Maps with same contents are equal |
74 |
> |
* Maps with same contents are equal |
75 |
|
*/ |
76 |
|
public void testEquals() { |
77 |
|
NavigableMap map1 = map5(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* containsKey returns true for contained key |
87 |
> |
* containsKey returns true for contained key |
88 |
|
*/ |
89 |
|
public void testContainsKey() { |
90 |
|
NavigableMap map = map5(); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* containsValue returns true for held values |
96 |
> |
* containsValue returns true for held values |
97 |
|
*/ |
98 |
|
public void testContainsValue() { |
99 |
|
NavigableMap map = map5(); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* get returns the correct element at the given key, |
106 |
< |
* or null if not present |
105 |
> |
* get returns the correct element at the given key, |
106 |
> |
* or null if not present |
107 |
|
*/ |
108 |
|
public void testGet() { |
109 |
|
NavigableMap map = map5(); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* isEmpty is true of empty map and false for non-empty |
116 |
> |
* isEmpty is true of empty map and false for non-empty |
117 |
|
*/ |
118 |
|
public void testIsEmpty() { |
119 |
|
NavigableMap empty = map0(); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
< |
* firstKey returns first key |
126 |
> |
* firstKey returns first key |
127 |
|
*/ |
128 |
|
public void testFirstKey() { |
129 |
|
NavigableMap map = map5(); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* lastKey returns last key |
134 |
> |
* lastKey returns last key |
135 |
|
*/ |
136 |
|
public void testLastKey() { |
137 |
|
NavigableMap map = map5(); |
138 |
|
assertEquals(five, map.lastKey()); |
139 |
|
} |
140 |
|
|
144 |
– |
|
141 |
|
/** |
142 |
< |
* keySet returns a Set containing all the keys |
142 |
> |
* keySet returns a Set containing all the keys |
143 |
|
*/ |
144 |
|
public void testKeySet() { |
145 |
|
NavigableMap map = map5(); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* keySet is ordered |
156 |
> |
* keySet is ordered |
157 |
|
*/ |
158 |
|
public void testKeySetOrder() { |
159 |
|
NavigableMap map = map5(); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* putAll adds all key-value pairs from the given map |
205 |
> |
* putAll adds all key-value pairs from the given map |
206 |
|
*/ |
207 |
|
public void testPutAll() { |
208 |
|
NavigableMap empty = map0(); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* remove removes the correct key-value pair from the map |
220 |
> |
* remove removes the correct key-value pair from the map |
221 |
|
*/ |
222 |
|
public void testRemove() { |
223 |
|
NavigableMap map = map5(); |
242 |
|
|
243 |
|
Map.Entry e4 = map.lowerEntry(zero); |
244 |
|
assertNull(e4); |
249 |
– |
|
245 |
|
} |
246 |
|
|
247 |
|
/** |
260 |
|
|
261 |
|
Map.Entry e4 = map.higherEntry(six); |
262 |
|
assertNull(e4); |
268 |
– |
|
263 |
|
} |
264 |
|
|
265 |
|
/** |
278 |
|
|
279 |
|
Map.Entry e4 = map.floorEntry(zero); |
280 |
|
assertNull(e4); |
287 |
– |
|
281 |
|
} |
282 |
|
|
283 |
|
/** |
296 |
|
|
297 |
|
Map.Entry e4 = map.ceilingEntry(six); |
298 |
|
assertNull(e4); |
306 |
– |
|
299 |
|
} |
300 |
|
|
301 |
|
/** |
320 |
|
try { |
321 |
|
e.setValue("A"); |
322 |
|
shouldThrow(); |
323 |
< |
} catch (Exception ok) { |
332 |
< |
} |
323 |
> |
} catch (UnsupportedOperationException success) {} |
324 |
|
assertTrue(map.isEmpty()); |
325 |
|
Map.Entry f = map.firstEntry(); |
326 |
|
assertNull(f); |
350 |
|
try { |
351 |
|
e.setValue("E"); |
352 |
|
shouldThrow(); |
353 |
< |
} catch (Exception ok) { |
363 |
< |
} |
353 |
> |
} catch (UnsupportedOperationException success) {} |
354 |
|
e = map.pollLastEntry(); |
355 |
|
assertNull(e); |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
< |
* size returns the correct values |
359 |
> |
* size returns the correct values |
360 |
|
*/ |
361 |
|
public void testSize() { |
362 |
|
NavigableMap map = map5(); |
372 |
|
NavigableMap map = map5(); |
373 |
|
String s = map.toString(); |
374 |
|
for (int i = 1; i <= 5; ++i) { |
375 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
375 |
> |
assertTrue(s.contains(String.valueOf(i))); |
376 |
|
} |
377 |
|
} |
378 |
|
|
386 |
|
NavigableMap c = map5(); |
387 |
|
c.get(null); |
388 |
|
shouldThrow(); |
389 |
< |
} catch (NullPointerException e) {} |
389 |
> |
} catch (NullPointerException success) {} |
390 |
|
} |
391 |
|
|
392 |
|
/** |
397 |
|
NavigableMap c = map5(); |
398 |
|
c.containsKey(null); |
399 |
|
shouldThrow(); |
400 |
< |
} catch (NullPointerException e) {} |
400 |
> |
} catch (NullPointerException success) {} |
401 |
|
} |
402 |
|
|
403 |
|
/** |
408 |
|
NavigableMap c = map5(); |
409 |
|
c.put(null, "whatever"); |
410 |
|
shouldThrow(); |
411 |
< |
} catch (NullPointerException e) {} |
411 |
> |
} catch (NullPointerException success) {} |
412 |
|
} |
413 |
|
|
414 |
|
/** |
419 |
|
NavigableMap c = map5(); |
420 |
|
c.remove(null); |
421 |
|
shouldThrow(); |
422 |
< |
} catch (NullPointerException e) {} |
422 |
> |
} catch (NullPointerException success) {} |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* A deserialized map equals original |
427 |
|
*/ |
428 |
< |
public void testSerialization() { |
429 |
< |
NavigableMap q = map5(); |
430 |
< |
|
431 |
< |
try { |
432 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
433 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
434 |
< |
out.writeObject(q); |
435 |
< |
out.close(); |
436 |
< |
|
447 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
448 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
449 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
450 |
< |
assertFalse(r.isEmpty()); |
451 |
< |
assertEquals(q.size(), r.size()); |
452 |
< |
assertTrue(q.equals(r)); |
453 |
< |
assertTrue(r.equals(q)); |
454 |
< |
} catch (Exception e) { |
455 |
< |
e.printStackTrace(); |
456 |
< |
unexpectedException(); |
457 |
< |
} |
428 |
> |
public void testSerialization() throws Exception { |
429 |
> |
NavigableMap x = map5(); |
430 |
> |
NavigableMap y = serialClone(x); |
431 |
> |
|
432 |
> |
assertNotSame(x, y); |
433 |
> |
assertEquals(x.size(), y.size()); |
434 |
> |
assertEquals(x.toString(), y.toString()); |
435 |
> |
assertEquals(x, y); |
436 |
> |
assertEquals(y, x); |
437 |
|
} |
438 |
|
|
460 |
– |
|
461 |
– |
|
439 |
|
/** |
440 |
|
* subMap returns map with keys in requested range |
441 |
|
*/ |
465 |
|
assertEquals(1, sm.size()); |
466 |
|
assertEquals(three, sm.firstKey()); |
467 |
|
assertEquals(three, sm.lastKey()); |
468 |
< |
assertTrue(sm.remove(three) != null); |
468 |
> |
assertEquals("C", sm.remove(three)); |
469 |
|
assertTrue(sm.isEmpty()); |
470 |
|
assertEquals(3, map.size()); |
471 |
|
} |
493 |
|
assertEquals(4, map.size()); |
494 |
|
assertEquals(0, sm.size()); |
495 |
|
assertTrue(sm.isEmpty()); |
496 |
< |
assertTrue(sm.remove(three) == null); |
496 |
> |
assertSame(sm.remove(three), null); |
497 |
|
assertEquals(4, map.size()); |
498 |
|
} |
499 |
|
|
565 |
|
SortedMap ssm = sm.tailMap(four); |
566 |
|
assertEquals(four, ssm.firstKey()); |
567 |
|
assertEquals(five, ssm.lastKey()); |
568 |
< |
assertTrue(ssm.remove(four) != null); |
568 |
> |
assertEquals("D", ssm.remove(four)); |
569 |
|
assertEquals(1, ssm.size()); |
570 |
|
assertEquals(3, sm.size()); |
571 |
|
assertEquals(4, map.size()); |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
< |
* clear removes all pairs |
575 |
> |
* clear removes all pairs |
576 |
|
*/ |
577 |
|
public void testDescendingClear() { |
578 |
|
NavigableMap map = dmap5(); |
579 |
|
map.clear(); |
580 |
< |
assertEquals(map.size(), 0); |
580 |
> |
assertEquals(0, map.size()); |
581 |
|
} |
582 |
|
|
606 |
– |
|
583 |
|
/** |
584 |
< |
* Maps with same contents are equal |
584 |
> |
* Maps with same contents are equal |
585 |
|
*/ |
586 |
|
public void testDescendingEquals() { |
587 |
|
NavigableMap map1 = dmap5(); |
594 |
|
} |
595 |
|
|
596 |
|
/** |
597 |
< |
* containsKey returns true for contained key |
597 |
> |
* containsKey returns true for contained key |
598 |
|
*/ |
599 |
|
public void testDescendingContainsKey() { |
600 |
|
NavigableMap map = dmap5(); |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
< |
* containsValue returns true for held values |
606 |
> |
* containsValue returns true for held values |
607 |
|
*/ |
608 |
|
public void testDescendingContainsValue() { |
609 |
|
NavigableMap map = dmap5(); |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
< |
* get returns the correct element at the given key, |
616 |
< |
* or null if not present |
615 |
> |
* get returns the correct element at the given key, |
616 |
> |
* or null if not present |
617 |
|
*/ |
618 |
|
public void testDescendingGet() { |
619 |
|
NavigableMap map = dmap5(); |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
< |
* isEmpty is true of empty map and false for non-empty |
626 |
> |
* isEmpty is true of empty map and false for non-empty |
627 |
|
*/ |
628 |
|
public void testDescendingIsEmpty() { |
629 |
|
NavigableMap empty = dmap0(); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
< |
* firstKey returns first key |
636 |
> |
* firstKey returns first key |
637 |
|
*/ |
638 |
|
public void testDescendingFirstKey() { |
639 |
|
NavigableMap map = dmap5(); |
641 |
|
} |
642 |
|
|
643 |
|
/** |
644 |
< |
* lastKey returns last key |
644 |
> |
* lastKey returns last key |
645 |
|
*/ |
646 |
|
public void testDescendingLastKey() { |
647 |
|
NavigableMap map = dmap5(); |
648 |
|
assertEquals(m5, map.lastKey()); |
649 |
|
} |
650 |
|
|
675 |
– |
|
651 |
|
/** |
652 |
< |
* keySet returns a Set containing all the keys |
652 |
> |
* keySet returns a Set containing all the keys |
653 |
|
*/ |
654 |
|
public void testDescendingKeySet() { |
655 |
|
NavigableMap map = dmap5(); |
663 |
|
} |
664 |
|
|
665 |
|
/** |
666 |
< |
* keySet is ordered |
666 |
> |
* keySet is ordered |
667 |
|
*/ |
668 |
|
public void testDescendingKeySetOrder() { |
669 |
|
NavigableMap map = dmap5(); |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* keySet.toArray returns contains all keys |
696 |
> |
* keySet.toArray returns contains all keys |
697 |
|
*/ |
698 |
|
public void testDescendingAscendingKeySetToArray() { |
699 |
|
NavigableMap map = dmap5(); |
706 |
|
} |
707 |
|
|
708 |
|
/** |
709 |
< |
* descendingkeySet.toArray returns contains all keys |
709 |
> |
* descendingkeySet.toArray returns contains all keys |
710 |
|
*/ |
711 |
|
public void testDescendingDescendingKeySetToArray() { |
712 |
|
NavigableMap map = dmap5(); |
719 |
|
} |
720 |
|
|
721 |
|
/** |
722 |
< |
* Values.toArray contains all values |
722 |
> |
* Values.toArray contains all values |
723 |
|
*/ |
724 |
|
public void testDescendingValuesToArray() { |
725 |
|
NavigableMap map = dmap5(); |
734 |
|
assertTrue(s.contains("E")); |
735 |
|
} |
736 |
|
|
762 |
– |
|
737 |
|
/** |
738 |
|
* entrySet contains all pairs |
739 |
|
*/ |
754 |
|
} |
755 |
|
|
756 |
|
/** |
757 |
< |
* putAll adds all key-value pairs from the given map |
757 |
> |
* putAll adds all key-value pairs from the given map |
758 |
|
*/ |
759 |
|
public void testDescendingPutAll() { |
760 |
|
NavigableMap empty = dmap0(); |
768 |
|
assertTrue(empty.containsKey(m5)); |
769 |
|
} |
770 |
|
|
797 |
– |
|
771 |
|
/** |
772 |
< |
* remove removes the correct key-value pair from the map |
772 |
> |
* remove removes the correct key-value pair from the map |
773 |
|
*/ |
774 |
|
public void testDescendingRemove() { |
775 |
|
NavigableMap map = dmap5(); |
794 |
|
|
795 |
|
Map.Entry e4 = map.lowerEntry(zero); |
796 |
|
assertNull(e4); |
824 |
– |
|
797 |
|
} |
798 |
|
|
799 |
|
/** |
812 |
|
|
813 |
|
Map.Entry e4 = map.higherEntry(m6); |
814 |
|
assertNull(e4); |
843 |
– |
|
815 |
|
} |
816 |
|
|
817 |
|
/** |
830 |
|
|
831 |
|
Map.Entry e4 = map.floorEntry(zero); |
832 |
|
assertNull(e4); |
862 |
– |
|
833 |
|
} |
834 |
|
|
835 |
|
/** |
848 |
|
|
849 |
|
Map.Entry e4 = map.ceilingEntry(m6); |
850 |
|
assertNull(e4); |
881 |
– |
|
851 |
|
} |
852 |
|
|
853 |
|
/** |
872 |
|
try { |
873 |
|
e.setValue("A"); |
874 |
|
shouldThrow(); |
875 |
< |
} catch (Exception ok) { |
907 |
< |
} |
875 |
> |
} catch (UnsupportedOperationException success) {} |
876 |
|
e = map.pollFirstEntry(); |
877 |
|
assertNull(e); |
878 |
|
} |
899 |
|
try { |
900 |
|
e.setValue("E"); |
901 |
|
shouldThrow(); |
902 |
< |
} catch (Exception ok) { |
935 |
< |
} |
902 |
> |
} catch (UnsupportedOperationException success) {} |
903 |
|
e = map.pollLastEntry(); |
904 |
|
assertNull(e); |
905 |
|
} |
906 |
|
|
907 |
|
/** |
908 |
< |
* size returns the correct values |
908 |
> |
* size returns the correct values |
909 |
|
*/ |
910 |
|
public void testDescendingSize() { |
911 |
|
NavigableMap map = dmap5(); |
921 |
|
NavigableMap map = dmap5(); |
922 |
|
String s = map.toString(); |
923 |
|
for (int i = 1; i <= 5; ++i) { |
924 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
924 |
> |
assertTrue(s.contains(String.valueOf(i))); |
925 |
|
} |
926 |
|
} |
927 |
|
|
935 |
|
NavigableMap c = dmap5(); |
936 |
|
c.get(null); |
937 |
|
shouldThrow(); |
938 |
< |
} catch (NullPointerException e) {} |
938 |
> |
} catch (NullPointerException success) {} |
939 |
|
} |
940 |
|
|
941 |
|
/** |
946 |
|
NavigableMap c = dmap5(); |
947 |
|
c.put(null, "whatever"); |
948 |
|
shouldThrow(); |
949 |
< |
} catch (NullPointerException e) {} |
949 |
> |
} catch (NullPointerException success) {} |
950 |
|
} |
951 |
|
|
952 |
|
/** |
953 |
|
* A deserialized map equals original |
954 |
|
*/ |
955 |
< |
public void testDescendingSerialization() { |
956 |
< |
NavigableMap q = dmap5(); |
957 |
< |
|
958 |
< |
try { |
959 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
960 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
961 |
< |
out.writeObject(q); |
962 |
< |
out.close(); |
963 |
< |
|
997 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
998 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
999 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
1000 |
< |
assertEquals(q.size(), r.size()); |
1001 |
< |
assertTrue(q.equals(r)); |
1002 |
< |
assertTrue(r.equals(q)); |
1003 |
< |
} catch (Exception e) { |
1004 |
< |
e.printStackTrace(); |
1005 |
< |
unexpectedException(); |
1006 |
< |
} |
955 |
> |
public void testDescendingSerialization() throws Exception { |
956 |
> |
NavigableMap x = dmap5(); |
957 |
> |
NavigableMap y = serialClone(x); |
958 |
> |
|
959 |
> |
assertNotSame(x, y); |
960 |
> |
assertEquals(x.size(), y.size()); |
961 |
> |
assertEquals(x.toString(), y.toString()); |
962 |
> |
assertEquals(x, y); |
963 |
> |
assertEquals(y, x); |
964 |
|
} |
965 |
|
|
1009 |
– |
|
1010 |
– |
|
966 |
|
/** |
967 |
|
* subMap returns map with keys in requested range |
968 |
|
*/ |
992 |
|
assertEquals(1, sm.size()); |
993 |
|
assertEquals(m3, sm.firstKey()); |
994 |
|
assertEquals(m3, sm.lastKey()); |
995 |
< |
assertTrue(sm.remove(m3) != null); |
995 |
> |
assertEquals("C", sm.remove(m3)); |
996 |
|
assertTrue(sm.isEmpty()); |
997 |
|
assertEquals(3, map.size()); |
998 |
|
} |
1020 |
|
assertEquals(4, map.size()); |
1021 |
|
assertEquals(0, sm.size()); |
1022 |
|
assertTrue(sm.isEmpty()); |
1023 |
< |
assertTrue(sm.remove(m3) == null); |
1023 |
> |
assertSame(sm.remove(m3), null); |
1024 |
|
assertEquals(4, map.size()); |
1025 |
|
} |
1026 |
|
|
1092 |
|
SortedMap ssm = sm.tailMap(m4); |
1093 |
|
assertEquals(m4, ssm.firstKey()); |
1094 |
|
assertEquals(m5, ssm.lastKey()); |
1095 |
< |
assertTrue(ssm.remove(m4) != null); |
1095 |
> |
assertEquals("D", ssm.remove(m4)); |
1096 |
|
assertEquals(1, ssm.size()); |
1097 |
|
assertEquals(3, sm.size()); |
1098 |
|
assertEquals(4, map.size()); |