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) { |
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(); |
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 |
86 |
|
*/ |
149 |
|
assertEquals(five, map.lastKey()); |
150 |
|
} |
151 |
|
|
144 |
– |
|
152 |
|
/** |
153 |
|
* keySet returns a Set containing all the keys |
154 |
|
*/ |
235 |
|
assertTrue(s.contains("E")); |
236 |
|
} |
237 |
|
|
231 |
– |
|
238 |
|
/** |
239 |
|
* entrySet contains all pairs |
240 |
|
*/ |
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 |
329 |
|
*/ |
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 |
|
*/ |
601 |
|
* A deserialized 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 |
|
*/ |
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 |
760 |
|
*/ |
823 |
|
assertEquals(m5, map.lastKey()); |
824 |
|
} |
825 |
|
|
831 |
– |
|
826 |
|
/** |
827 |
|
* keySet returns a Set containing all the keys |
828 |
|
*/ |
909 |
|
assertTrue(s.contains("E")); |
910 |
|
} |
911 |
|
|
918 |
– |
|
912 |
|
/** |
913 |
|
* entrySet contains all pairs |
914 |
|
*/ |
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 |
1003 |
|
*/ |
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 |
|
|
1205 |
|
} catch (NullPointerException success) {} |
1206 |
|
} |
1207 |
|
|
1217 |
– |
|
1208 |
|
/** |
1209 |
|
* put(null,x) throws NPE |
1210 |
|
*/ |
1275 |
|
* A deserialized 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 |
|
*/ |