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 |
> |
import java.util.concurrent.ConcurrentSkipListMap; |
10 |
|
|
11 |
|
public class ConcurrentSkipListMapTest extends JSR166TestCase { |
12 |
|
public static void main(String[] args) { |
17 |
|
} |
18 |
|
|
19 |
|
/** |
20 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
20 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
21 |
|
*/ |
22 |
|
private static ConcurrentSkipListMap map5() { |
23 |
|
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
33 |
|
} |
34 |
|
|
35 |
|
/** |
36 |
< |
* clear removes all pairs |
36 |
> |
* clear removes all pairs |
37 |
|
*/ |
38 |
|
public void testClear() { |
39 |
|
ConcurrentSkipListMap map = map5(); |
40 |
|
map.clear(); |
41 |
< |
assertEquals(map.size(), 0); |
41 |
> |
assertEquals(0, map.size()); |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* |
45 |
> |
* copy constructor creates map equal to source map |
46 |
|
*/ |
47 |
|
public void testConstructFromSorted() { |
48 |
|
ConcurrentSkipListMap map = map5(); |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* Maps with same contents are equal |
54 |
> |
* Maps with same contents are equal |
55 |
|
*/ |
56 |
|
public void testEquals() { |
57 |
|
ConcurrentSkipListMap map1 = map5(); |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* containsKey returns true for contained key |
67 |
> |
* containsKey returns true for contained key |
68 |
|
*/ |
69 |
|
public void testContainsKey() { |
70 |
|
ConcurrentSkipListMap map = map5(); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
< |
* containsValue returns true for held values |
76 |
> |
* containsValue returns true for held values |
77 |
|
*/ |
78 |
|
public void testContainsValue() { |
79 |
|
ConcurrentSkipListMap map = map5(); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* get returns the correct element at the given key, |
86 |
< |
* or null if not present |
85 |
> |
* get returns the correct element at the given key, |
86 |
> |
* or null if not present |
87 |
|
*/ |
88 |
|
public void testGet() { |
89 |
|
ConcurrentSkipListMap map = map5(); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* isEmpty is true of empty map and false for non-empty |
96 |
> |
* isEmpty is true of empty map and false for non-empty |
97 |
|
*/ |
98 |
|
public void testIsEmpty() { |
99 |
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
< |
* firstKey returns first key |
106 |
> |
* firstKey returns first key |
107 |
|
*/ |
108 |
|
public void testFirstKey() { |
109 |
|
ConcurrentSkipListMap map = map5(); |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
< |
* lastKey returns last key |
114 |
> |
* lastKey returns last key |
115 |
|
*/ |
116 |
|
public void testLastKey() { |
117 |
|
ConcurrentSkipListMap map = map5(); |
118 |
|
assertEquals(five, map.lastKey()); |
119 |
|
} |
120 |
|
|
122 |
– |
|
121 |
|
/** |
122 |
< |
* keySet.toArray returns contains all keys |
122 |
> |
* keySet.toArray returns contains all keys |
123 |
|
*/ |
124 |
|
public void testKeySetToArray() { |
125 |
|
ConcurrentSkipListMap map = map5(); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
135 |
< |
* descendingkeySet.toArray returns contains all keys |
135 |
> |
* descendingkeySet.toArray returns contains all keys |
136 |
|
*/ |
137 |
|
public void testDescendingKeySetToArray() { |
138 |
|
ConcurrentSkipListMap map = map5(); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* keySet returns a Set containing all the keys |
148 |
> |
* keySet returns a Set containing all the keys |
149 |
|
*/ |
150 |
|
public void testKeySet() { |
151 |
|
ConcurrentSkipListMap map = map5(); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* keySet is ordered |
162 |
> |
* keySet is ordered |
163 |
|
*/ |
164 |
|
public void testKeySetOrder() { |
165 |
|
ConcurrentSkipListMap map = map5(); |
174 |
|
last = k; |
175 |
|
++count; |
176 |
|
} |
177 |
< |
assertEquals(count ,5); |
177 |
> |
assertEquals(5, count); |
178 |
|
} |
179 |
|
|
180 |
|
/** |
193 |
|
last = k; |
194 |
|
++count; |
195 |
|
} |
196 |
< |
assertEquals(count ,5); |
196 |
> |
assertEquals(5, count); |
197 |
|
} |
198 |
|
|
199 |
|
/** |
200 |
< |
* descendingKeySet is ordered |
200 |
> |
* descendingKeySet is ordered |
201 |
|
*/ |
202 |
|
public void testDescendingKeySetOrder() { |
203 |
|
ConcurrentSkipListMap map = map5(); |
212 |
|
last = k; |
213 |
|
++count; |
214 |
|
} |
215 |
< |
assertEquals(count, 5); |
215 |
> |
assertEquals(5, count); |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* descending iterator of descendingKeySet is ordered |
219 |
> |
* descending iterator of descendingKeySet is ordered |
220 |
|
*/ |
221 |
|
public void testDescendingKeySetDescendingIteratorOrder() { |
222 |
|
ConcurrentSkipListMap map = map5(); |
231 |
|
last = k; |
232 |
|
++count; |
233 |
|
} |
234 |
< |
assertEquals(count, 5); |
234 |
> |
assertEquals(5, count); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* Values.toArray contains all values |
238 |
> |
* Values.toArray contains all values |
239 |
|
*/ |
240 |
|
public void testValuesToArray() { |
241 |
|
ConcurrentSkipListMap map = map5(); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* entrySet.toArray contains all entries |
306 |
> |
* entrySet.toArray contains all entries |
307 |
|
*/ |
308 |
|
public void testEntrySetToArray() { |
309 |
|
ConcurrentSkipListMap map = map5(); |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* descendingEntrySet.toArray contains all entries |
320 |
> |
* descendingEntrySet.toArray contains all entries |
321 |
|
*/ |
322 |
|
public void testDescendingEntrySetToArray() { |
323 |
|
ConcurrentSkipListMap map = map5(); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* putAll adds all key-value pairs from the given map |
334 |
> |
* putAll adds all key-value pairs from the given map |
335 |
|
*/ |
336 |
|
public void testPutAll() { |
337 |
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
< |
* putIfAbsent works when the given key is not present |
349 |
> |
* putIfAbsent works when the given key is not present |
350 |
|
*/ |
351 |
|
public void testPutIfAbsent() { |
352 |
|
ConcurrentSkipListMap map = map5(); |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
< |
* putIfAbsent does not add the pair if the key is already present |
358 |
> |
* putIfAbsent does not add the pair if the key is already present |
359 |
|
*/ |
360 |
|
public void testPutIfAbsent2() { |
361 |
|
ConcurrentSkipListMap map = map5(); |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
< |
* replace fails when the given key is not present |
366 |
> |
* replace fails when the given key is not present |
367 |
|
*/ |
368 |
|
public void testReplace() { |
369 |
|
ConcurrentSkipListMap map = map5(); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* replace succeeds if the key is already present |
375 |
> |
* replace succeeds if the key is already present |
376 |
|
*/ |
377 |
|
public void testReplace2() { |
378 |
|
ConcurrentSkipListMap map = map5(); |
380 |
|
assertEquals("Z", map.get(one)); |
381 |
|
} |
382 |
|
|
385 |
– |
|
383 |
|
/** |
384 |
|
* replace value fails when the given key not mapped to expected value |
385 |
|
*/ |
400 |
|
assertEquals("Z", map.get(one)); |
401 |
|
} |
402 |
|
|
406 |
– |
|
403 |
|
/** |
404 |
< |
* remove removes the correct key-value pair from the map |
404 |
> |
* remove removes the correct key-value pair from the map |
405 |
|
*/ |
406 |
|
public void testRemove() { |
407 |
|
ConcurrentSkipListMap map = map5(); |
501 |
|
* lowerEntry, higherEntry, ceilingEntry, and floorEntry return |
502 |
|
* immutable entries |
503 |
|
*/ |
504 |
< |
public void testEntryImmutablity() { |
504 |
> |
public void testEntryImmutability() { |
505 |
|
ConcurrentSkipListMap map = map5(); |
506 |
|
Map.Entry e = map.lowerEntry(three); |
507 |
|
assertEquals(two, e.getKey()); |
529 |
|
} catch (UnsupportedOperationException success) {} |
530 |
|
} |
531 |
|
|
536 |
– |
|
537 |
– |
|
532 |
|
/** |
533 |
|
* lowerKey returns preceding element |
534 |
|
*/ |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* size returns the correct values |
659 |
> |
* size returns the correct values |
660 |
|
*/ |
661 |
|
public void testSize() { |
662 |
|
ConcurrentSkipListMap map = map5(); |
672 |
|
ConcurrentSkipListMap map = map5(); |
673 |
|
String s = map.toString(); |
674 |
|
for (int i = 1; i <= 5; ++i) { |
675 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
675 |
> |
assertTrue(s.contains(String.valueOf(i))); |
676 |
|
} |
677 |
|
} |
678 |
|
|
711 |
|
} catch (NullPointerException success) {} |
712 |
|
} |
713 |
|
|
720 |
– |
|
714 |
|
/** |
715 |
|
* put(null,x) throws NPE |
716 |
|
*/ |
792 |
|
* A deserialized map equals original |
793 |
|
*/ |
794 |
|
public void testSerialization() throws Exception { |
795 |
< |
ConcurrentSkipListMap q = map5(); |
795 |
> |
NavigableMap x = map5(); |
796 |
> |
NavigableMap y = serialClone(x); |
797 |
|
|
798 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
799 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
800 |
< |
out.writeObject(q); |
801 |
< |
out.close(); |
802 |
< |
|
809 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
810 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
811 |
< |
ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject(); |
812 |
< |
assertEquals(q.size(), r.size()); |
813 |
< |
assertTrue(q.equals(r)); |
814 |
< |
assertTrue(r.equals(q)); |
798 |
> |
assertTrue(x != y); |
799 |
> |
assertEquals(x.size(), y.size()); |
800 |
> |
assertEquals(x.toString(), y.toString()); |
801 |
> |
assertEquals(x, y); |
802 |
> |
assertEquals(y, x); |
803 |
|
} |
804 |
|
|
817 |
– |
|
818 |
– |
|
805 |
|
/** |
806 |
|
* subMap returns map with keys in requested range |
807 |
|
*/ |
966 |
|
* Submaps of submaps subdivide correctly |
967 |
|
*/ |
968 |
|
public void testRecursiveSubMaps() throws Exception { |
969 |
< |
int mapSize = 1000; |
969 |
> |
int mapSize = expensiveTests ? 1000 : 100; |
970 |
|
Class cl = ConcurrentSkipListMap.class; |
971 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
972 |
|
bs = new BitSet(mapSize); |
986 |
|
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
987 |
|
NavigableMap<Integer, Integer> result = |
988 |
|
(NavigableMap<Integer, Integer>) cl.newInstance(); |
989 |
< |
assertEquals(result.size(), 0); |
989 |
> |
assertEquals(0, result.size()); |
990 |
|
assertFalse(result.keySet().iterator().hasNext()); |
991 |
|
return result; |
992 |
|
} |
1147 |
|
*/ |
1148 |
|
void check(NavigableMap<Integer, Integer> map, |
1149 |
|
final int min, final int max, final boolean ascending) { |
1150 |
< |
class ReferenceSet { |
1150 |
> |
class ReferenceSet { |
1151 |
|
int lower(int key) { |
1152 |
|
return ascending ? lowerAscending(key) : higherAscending(key); |
1153 |
|
} |
1178 |
|
// BitSet should support this! Test would run much faster |
1179 |
|
while (key >= min) { |
1180 |
|
if (bs.get(key)) |
1181 |
< |
return(key); |
1181 |
> |
return key; |
1182 |
|
key--; |
1183 |
|
} |
1184 |
|
return -1; |
1213 |
|
if (bsContainsI) |
1214 |
|
size++; |
1215 |
|
} |
1216 |
< |
assertEquals(map.size(), size); |
1216 |
> |
assertEquals(size, map.size()); |
1217 |
|
|
1218 |
|
// Test contents using contains keySet iterator |
1219 |
|
int size2 = 0; |