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.concurrent.ConcurrentNavigableMap; |
16 |
> |
import java.util.concurrent.ConcurrentSkipListMap; |
17 |
> |
|
18 |
> |
import junit.framework.Test; |
19 |
> |
import junit.framework.TestSuite; |
20 |
|
|
21 |
|
public class ConcurrentSkipListSubMapTest extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run (suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(ConcurrentSkipListSubMapTest.class); |
27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
30 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
31 |
|
*/ |
32 |
|
private static ConcurrentNavigableMap map5() { |
33 |
|
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
< |
* Create a map from Integers -5 to -1 to Strings "A"-"E". |
48 |
> |
* Returns a new map from Integers -5 to -1 to Strings "A"-"E". |
49 |
|
*/ |
50 |
|
private static ConcurrentNavigableMap dmap5() { |
51 |
|
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
< |
* clear removes all pairs |
76 |
> |
* clear removes all pairs |
77 |
|
*/ |
78 |
|
public void testClear() { |
79 |
|
ConcurrentNavigableMap map = map5(); |
80 |
|
map.clear(); |
81 |
< |
assertEquals(map.size(), 0); |
81 |
> |
assertEquals(0, map.size()); |
82 |
|
} |
83 |
|
|
75 |
– |
|
84 |
|
/** |
85 |
< |
* Maps with same contents are equal |
85 |
> |
* Maps with same contents are equal |
86 |
|
*/ |
87 |
|
public void testEquals() { |
88 |
|
ConcurrentNavigableMap map1 = map5(); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* containsKey returns true for contained key |
98 |
> |
* containsKey returns true for contained key |
99 |
|
*/ |
100 |
|
public void testContainsKey() { |
101 |
|
ConcurrentNavigableMap map = map5(); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* containsValue returns true for held values |
107 |
> |
* containsValue returns true for held values |
108 |
|
*/ |
109 |
|
public void testContainsValue() { |
110 |
|
ConcurrentNavigableMap map = map5(); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* get returns the correct element at the given key, |
117 |
< |
* or null if not present |
116 |
> |
* get returns the correct element at the given key, |
117 |
> |
* or null if not present |
118 |
|
*/ |
119 |
|
public void testGet() { |
120 |
|
ConcurrentNavigableMap map = map5(); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* isEmpty is true of empty map and false for non-empty |
127 |
> |
* isEmpty is true of empty map and false for non-empty |
128 |
|
*/ |
129 |
|
public void testIsEmpty() { |
130 |
|
ConcurrentNavigableMap empty = map0(); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* firstKey returns first key |
137 |
> |
* firstKey returns first key |
138 |
|
*/ |
139 |
|
public void testFirstKey() { |
140 |
|
ConcurrentNavigableMap map = map5(); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* lastKey returns last key |
145 |
> |
* lastKey returns last key |
146 |
|
*/ |
147 |
|
public void testLastKey() { |
148 |
|
ConcurrentNavigableMap map = map5(); |
149 |
|
assertEquals(five, map.lastKey()); |
150 |
|
} |
151 |
|
|
144 |
– |
|
152 |
|
/** |
153 |
< |
* keySet returns a Set containing all the keys |
153 |
> |
* keySet returns a Set containing all the keys |
154 |
|
*/ |
155 |
|
public void testKeySet() { |
156 |
|
ConcurrentNavigableMap map = map5(); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
< |
* keySet is ordered |
167 |
> |
* keySet is ordered |
168 |
|
*/ |
169 |
|
public void testKeySetOrder() { |
170 |
|
ConcurrentNavigableMap map = map5(); |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
< |
* keySet.toArray returns contains all keys |
197 |
> |
* keySet.toArray returns contains all keys |
198 |
|
*/ |
199 |
|
public void testKeySetToArray() { |
200 |
|
ConcurrentNavigableMap map = map5(); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* descendingkeySet.toArray returns contains all keys |
210 |
> |
* descendingkeySet.toArray returns contains all keys |
211 |
|
*/ |
212 |
|
public void testDescendingKeySetToArray() { |
213 |
|
ConcurrentNavigableMap map = map5(); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* Values.toArray contains all values |
223 |
> |
* Values.toArray contains all values |
224 |
|
*/ |
225 |
|
public void testValuesToArray() { |
226 |
|
ConcurrentNavigableMap map = map5(); |
235 |
|
assertTrue(s.contains("E")); |
236 |
|
} |
237 |
|
|
231 |
– |
|
238 |
|
/** |
239 |
|
* entrySet contains all pairs |
240 |
|
*/ |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* putAll adds all key-value pairs from the given map |
258 |
> |
* putAll adds all key-value pairs from the given map |
259 |
|
*/ |
260 |
|
public void testPutAll() { |
261 |
|
ConcurrentNavigableMap empty = map0(); |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
< |
* putIfAbsent works when the given key is not present |
273 |
> |
* putIfAbsent works when the given key is not present |
274 |
|
*/ |
275 |
|
public void testPutIfAbsent() { |
276 |
|
ConcurrentNavigableMap map = map5(); |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
< |
* putIfAbsent does not add the pair if the key is already present |
282 |
> |
* putIfAbsent does not add the pair if the key is already present |
283 |
|
*/ |
284 |
|
public void testPutIfAbsent2() { |
285 |
|
ConcurrentNavigableMap map = map5(); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
< |
* replace fails when the given key is not present |
290 |
> |
* replace fails when the given key is not present |
291 |
|
*/ |
292 |
|
public void testReplace() { |
293 |
|
ConcurrentNavigableMap map = map5(); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* replace succeeds if the key is already present |
299 |
> |
* replace succeeds if the key is already present |
300 |
|
*/ |
301 |
|
public void testReplace2() { |
302 |
|
ConcurrentNavigableMap map = map5(); |
304 |
|
assertEquals("Z", map.get(one)); |
305 |
|
} |
306 |
|
|
301 |
– |
|
307 |
|
/** |
308 |
|
* replace value fails when the given key not mapped to expected value |
309 |
|
*/ |
324 |
|
assertEquals("Z", map.get(one)); |
325 |
|
} |
326 |
|
|
322 |
– |
|
327 |
|
/** |
328 |
< |
* remove removes the correct key-value pair from the map |
328 |
> |
* remove removes the correct key-value pair from the map |
329 |
|
*/ |
330 |
|
public void testRemove() { |
331 |
|
ConcurrentNavigableMap map = map5(); |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* size returns the correct values |
479 |
> |
* size returns the correct values |
480 |
|
*/ |
481 |
|
public void testSize() { |
482 |
|
ConcurrentNavigableMap map = map5(); |
492 |
|
ConcurrentNavigableMap map = map5(); |
493 |
|
String s = map.toString(); |
494 |
|
for (int i = 1; i <= 5; ++i) { |
495 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
495 |
> |
assertTrue(s.contains(String.valueOf(i))); |
496 |
|
} |
497 |
|
} |
498 |
|
|
531 |
|
} catch (NullPointerException success) {} |
532 |
|
} |
533 |
|
|
530 |
– |
|
534 |
|
/** |
535 |
|
* put(null,x) throws NPE |
536 |
|
*/ |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
< |
* A deserialized map equals original |
601 |
> |
* A deserialized/reserialized map equals original |
602 |
|
*/ |
603 |
|
public void testSerialization() throws Exception { |
604 |
< |
ConcurrentNavigableMap q = map5(); |
604 |
> |
NavigableMap x = map5(); |
605 |
> |
NavigableMap y = serialClone(x); |
606 |
|
|
607 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
608 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
609 |
< |
out.writeObject(q); |
610 |
< |
out.close(); |
611 |
< |
|
608 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
609 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
610 |
< |
ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject(); |
611 |
< |
assertEquals(q.size(), r.size()); |
612 |
< |
assertTrue(q.equals(r)); |
613 |
< |
assertTrue(r.equals(q)); |
607 |
> |
assertNotSame(x, y); |
608 |
> |
assertEquals(x.size(), y.size()); |
609 |
> |
assertEquals(x.toString(), y.toString()); |
610 |
> |
assertEquals(x, y); |
611 |
> |
assertEquals(y, x); |
612 |
|
} |
613 |
|
|
616 |
– |
|
617 |
– |
|
614 |
|
/** |
615 |
|
* subMap returns map with keys in requested range |
616 |
|
*/ |
747 |
|
} |
748 |
|
|
749 |
|
/** |
750 |
< |
* clear removes all pairs |
750 |
> |
* clear removes all pairs |
751 |
|
*/ |
752 |
|
public void testDescendingClear() { |
753 |
|
ConcurrentNavigableMap map = dmap5(); |
754 |
|
map.clear(); |
755 |
< |
assertEquals(map.size(), 0); |
755 |
> |
assertEquals(0, map.size()); |
756 |
|
} |
757 |
|
|
762 |
– |
|
758 |
|
/** |
759 |
< |
* Maps with same contents are equal |
759 |
> |
* Maps with same contents are equal |
760 |
|
*/ |
761 |
|
public void testDescendingEquals() { |
762 |
|
ConcurrentNavigableMap map1 = dmap5(); |
769 |
|
} |
770 |
|
|
771 |
|
/** |
772 |
< |
* containsKey returns true for contained key |
772 |
> |
* containsKey returns true for contained key |
773 |
|
*/ |
774 |
|
public void testDescendingContainsKey() { |
775 |
|
ConcurrentNavigableMap map = dmap5(); |
778 |
|
} |
779 |
|
|
780 |
|
/** |
781 |
< |
* containsValue returns true for held values |
781 |
> |
* containsValue returns true for held values |
782 |
|
*/ |
783 |
|
public void testDescendingContainsValue() { |
784 |
|
ConcurrentNavigableMap map = dmap5(); |
787 |
|
} |
788 |
|
|
789 |
|
/** |
790 |
< |
* get returns the correct element at the given key, |
791 |
< |
* or null if not present |
790 |
> |
* get returns the correct element at the given key, |
791 |
> |
* or null if not present |
792 |
|
*/ |
793 |
|
public void testDescendingGet() { |
794 |
|
ConcurrentNavigableMap map = dmap5(); |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
< |
* isEmpty is true of empty map and false for non-empty |
801 |
> |
* isEmpty is true of empty map and false for non-empty |
802 |
|
*/ |
803 |
|
public void testDescendingIsEmpty() { |
804 |
|
ConcurrentNavigableMap empty = dmap0(); |
808 |
|
} |
809 |
|
|
810 |
|
/** |
811 |
< |
* firstKey returns first key |
811 |
> |
* firstKey returns first key |
812 |
|
*/ |
813 |
|
public void testDescendingFirstKey() { |
814 |
|
ConcurrentNavigableMap map = dmap5(); |
816 |
|
} |
817 |
|
|
818 |
|
/** |
819 |
< |
* lastKey returns last key |
819 |
> |
* lastKey returns last key |
820 |
|
*/ |
821 |
|
public void testDescendingLastKey() { |
822 |
|
ConcurrentNavigableMap map = dmap5(); |
823 |
|
assertEquals(m5, map.lastKey()); |
824 |
|
} |
825 |
|
|
831 |
– |
|
826 |
|
/** |
827 |
< |
* keySet returns a Set containing all the keys |
827 |
> |
* keySet returns a Set containing all the keys |
828 |
|
*/ |
829 |
|
public void testDescendingKeySet() { |
830 |
|
ConcurrentNavigableMap map = dmap5(); |
838 |
|
} |
839 |
|
|
840 |
|
/** |
841 |
< |
* keySet is ordered |
841 |
> |
* keySet is ordered |
842 |
|
*/ |
843 |
|
public void testDescendingKeySetOrder() { |
844 |
|
ConcurrentNavigableMap map = dmap5(); |
868 |
|
} |
869 |
|
|
870 |
|
/** |
871 |
< |
* keySet.toArray returns contains all keys |
871 |
> |
* keySet.toArray returns contains all keys |
872 |
|
*/ |
873 |
|
public void testDescendingAscendingKeySetToArray() { |
874 |
|
ConcurrentNavigableMap map = dmap5(); |
881 |
|
} |
882 |
|
|
883 |
|
/** |
884 |
< |
* descendingkeySet.toArray returns contains all keys |
884 |
> |
* descendingkeySet.toArray returns contains all keys |
885 |
|
*/ |
886 |
|
public void testDescendingDescendingKeySetToArray() { |
887 |
|
ConcurrentNavigableMap map = dmap5(); |
894 |
|
} |
895 |
|
|
896 |
|
/** |
897 |
< |
* Values.toArray contains all values |
897 |
> |
* Values.toArray contains all values |
898 |
|
*/ |
899 |
|
public void testDescendingValuesToArray() { |
900 |
|
ConcurrentNavigableMap map = dmap5(); |
909 |
|
assertTrue(s.contains("E")); |
910 |
|
} |
911 |
|
|
918 |
– |
|
912 |
|
/** |
913 |
|
* entrySet contains all pairs |
914 |
|
*/ |
929 |
|
} |
930 |
|
|
931 |
|
/** |
932 |
< |
* putAll adds all key-value pairs from the given map |
932 |
> |
* putAll adds all key-value pairs from the given map |
933 |
|
*/ |
934 |
|
public void testDescendingPutAll() { |
935 |
|
ConcurrentNavigableMap empty = dmap0(); |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
< |
* putIfAbsent works when the given key is not present |
947 |
> |
* putIfAbsent works when the given key is not present |
948 |
|
*/ |
949 |
|
public void testDescendingPutIfAbsent() { |
950 |
|
ConcurrentNavigableMap map = dmap5(); |
953 |
|
} |
954 |
|
|
955 |
|
/** |
956 |
< |
* putIfAbsent does not add the pair if the key is already present |
956 |
> |
* putIfAbsent does not add the pair if the key is already present |
957 |
|
*/ |
958 |
|
public void testDescendingPutIfAbsent2() { |
959 |
|
ConcurrentNavigableMap map = dmap5(); |
961 |
|
} |
962 |
|
|
963 |
|
/** |
964 |
< |
* replace fails when the given key is not present |
964 |
> |
* replace fails when the given key is not present |
965 |
|
*/ |
966 |
|
public void testDescendingReplace() { |
967 |
|
ConcurrentNavigableMap map = dmap5(); |
970 |
|
} |
971 |
|
|
972 |
|
/** |
973 |
< |
* replace succeeds if the key is already present |
973 |
> |
* replace succeeds if the key is already present |
974 |
|
*/ |
975 |
|
public void testDescendingReplace2() { |
976 |
|
ConcurrentNavigableMap map = dmap5(); |
978 |
|
assertEquals("Z", map.get(m1)); |
979 |
|
} |
980 |
|
|
988 |
– |
|
981 |
|
/** |
982 |
|
* replace value fails when the given key not mapped to expected value |
983 |
|
*/ |
998 |
|
assertEquals("Z", map.get(m1)); |
999 |
|
} |
1000 |
|
|
1009 |
– |
|
1001 |
|
/** |
1002 |
< |
* remove removes the correct key-value pair from the map |
1002 |
> |
* remove removes the correct key-value pair from the map |
1003 |
|
*/ |
1004 |
|
public void testDescendingRemove() { |
1005 |
|
ConcurrentNavigableMap map = dmap5(); |
1150 |
|
} |
1151 |
|
|
1152 |
|
/** |
1153 |
< |
* size returns the correct values |
1153 |
> |
* size returns the correct values |
1154 |
|
*/ |
1155 |
|
public void testDescendingSize() { |
1156 |
|
ConcurrentNavigableMap map = dmap5(); |
1166 |
|
ConcurrentNavigableMap map = dmap5(); |
1167 |
|
String s = map.toString(); |
1168 |
|
for (int i = 1; i <= 5; ++i) { |
1169 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1169 |
> |
assertTrue(s.contains(String.valueOf(i))); |
1170 |
|
} |
1171 |
|
} |
1172 |
|
|
1173 |
|
// Exception testDescendings |
1174 |
|
|
1175 |
|
/** |
1176 |
< |
* get(null) of nm1mpty map throws NPE |
1176 |
> |
* get(null) of empty map throws NPE |
1177 |
|
*/ |
1178 |
|
public void testDescendingGet_NullPointerException() { |
1179 |
|
try { |
1184 |
|
} |
1185 |
|
|
1186 |
|
/** |
1187 |
< |
* containsKey(null) of nm1mpty map throws NPE |
1187 |
> |
* containsKey(null) of empty map throws NPE |
1188 |
|
*/ |
1189 |
|
public void testDescendingContainsKey_NullPointerException() { |
1190 |
|
try { |
1205 |
|
} catch (NullPointerException success) {} |
1206 |
|
} |
1207 |
|
|
1217 |
– |
|
1208 |
|
/** |
1209 |
|
* put(null,x) throws NPE |
1210 |
|
*/ |
1272 |
|
} |
1273 |
|
|
1274 |
|
/** |
1275 |
< |
* A deserialized map equals original |
1275 |
> |
* A deserialized/reserialized map equals original |
1276 |
|
*/ |
1277 |
|
public void testDescendingSerialization() throws Exception { |
1278 |
< |
ConcurrentNavigableMap q = dmap5(); |
1278 |
> |
NavigableMap x = dmap5(); |
1279 |
> |
NavigableMap y = serialClone(x); |
1280 |
|
|
1281 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1282 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1283 |
< |
out.writeObject(q); |
1284 |
< |
out.close(); |
1285 |
< |
|
1295 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1296 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1297 |
< |
ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject(); |
1298 |
< |
assertEquals(q.size(), r.size()); |
1299 |
< |
assertTrue(q.equals(r)); |
1300 |
< |
assertTrue(r.equals(q)); |
1281 |
> |
assertNotSame(x, y); |
1282 |
> |
assertEquals(x.size(), y.size()); |
1283 |
> |
assertEquals(x.toString(), y.toString()); |
1284 |
> |
assertEquals(x, y); |
1285 |
> |
assertEquals(y, x); |
1286 |
|
} |
1287 |
|
|
1303 |
– |
|
1288 |
|
/** |
1289 |
|
* subMap returns map with keys in requested range |
1290 |
|
*/ |