1 |
dl |
1.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 |
jsr166 |
1.22 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
|
|
import java.util.*; |
9 |
jsr166 |
1.25 |
import java.util.concurrent.ConcurrentSkipListMap; |
10 |
dl |
1.1 |
|
11 |
|
|
public class ConcurrentSkipListMapTest extends JSR166TestCase { |
12 |
|
|
public static void main(String[] args) { |
13 |
jsr166 |
1.18 |
junit.textui.TestRunner.run(suite()); |
14 |
dl |
1.1 |
} |
15 |
|
|
public static Test suite() { |
16 |
jsr166 |
1.12 |
return new TestSuite(ConcurrentSkipListMapTest.class); |
17 |
dl |
1.1 |
} |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
21 |
|
|
*/ |
22 |
jsr166 |
1.9 |
private static ConcurrentSkipListMap map5() { |
23 |
jsr166 |
1.12 |
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
24 |
dl |
1.1 |
assertTrue(map.isEmpty()); |
25 |
jsr166 |
1.12 |
map.put(one, "A"); |
26 |
|
|
map.put(five, "E"); |
27 |
|
|
map.put(three, "C"); |
28 |
|
|
map.put(two, "B"); |
29 |
|
|
map.put(four, "D"); |
30 |
dl |
1.1 |
assertFalse(map.isEmpty()); |
31 |
|
|
assertEquals(5, map.size()); |
32 |
jsr166 |
1.12 |
return map; |
33 |
dl |
1.1 |
} |
34 |
|
|
|
35 |
|
|
/** |
36 |
jsr166 |
1.20 |
* clear removes all pairs |
37 |
dl |
1.1 |
*/ |
38 |
|
|
public void testClear() { |
39 |
|
|
ConcurrentSkipListMap map = map5(); |
40 |
jsr166 |
1.12 |
map.clear(); |
41 |
jsr166 |
1.27 |
assertEquals(0, map.size()); |
42 |
dl |
1.1 |
} |
43 |
|
|
|
44 |
|
|
/** |
45 |
jsr166 |
1.9 |
* |
46 |
dl |
1.1 |
*/ |
47 |
|
|
public void testConstructFromSorted() { |
48 |
|
|
ConcurrentSkipListMap map = map5(); |
49 |
|
|
ConcurrentSkipListMap map2 = new ConcurrentSkipListMap(map); |
50 |
|
|
assertEquals(map, map2); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
/** |
54 |
jsr166 |
1.20 |
* Maps with same contents are equal |
55 |
dl |
1.1 |
*/ |
56 |
|
|
public void testEquals() { |
57 |
|
|
ConcurrentSkipListMap map1 = map5(); |
58 |
|
|
ConcurrentSkipListMap map2 = map5(); |
59 |
|
|
assertEquals(map1, map2); |
60 |
|
|
assertEquals(map2, map1); |
61 |
jsr166 |
1.12 |
map1.clear(); |
62 |
dl |
1.1 |
assertFalse(map1.equals(map2)); |
63 |
|
|
assertFalse(map2.equals(map1)); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/** |
67 |
jsr166 |
1.20 |
* containsKey returns true for contained key |
68 |
dl |
1.1 |
*/ |
69 |
|
|
public void testContainsKey() { |
70 |
|
|
ConcurrentSkipListMap map = map5(); |
71 |
jsr166 |
1.12 |
assertTrue(map.containsKey(one)); |
72 |
dl |
1.1 |
assertFalse(map.containsKey(zero)); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
jsr166 |
1.20 |
* containsValue returns true for held values |
77 |
dl |
1.1 |
*/ |
78 |
|
|
public void testContainsValue() { |
79 |
|
|
ConcurrentSkipListMap map = map5(); |
80 |
jsr166 |
1.12 |
assertTrue(map.containsValue("A")); |
81 |
dl |
1.1 |
assertFalse(map.containsValue("Z")); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
/** |
85 |
jsr166 |
1.20 |
* get returns the correct element at the given key, |
86 |
|
|
* or null if not present |
87 |
dl |
1.1 |
*/ |
88 |
|
|
public void testGet() { |
89 |
|
|
ConcurrentSkipListMap map = map5(); |
90 |
jsr166 |
1.12 |
assertEquals("A", (String)map.get(one)); |
91 |
dl |
1.1 |
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
92 |
|
|
assertNull(empty.get(one)); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/** |
96 |
jsr166 |
1.20 |
* isEmpty is true of empty map and false for non-empty |
97 |
dl |
1.1 |
*/ |
98 |
|
|
public void testIsEmpty() { |
99 |
|
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
100 |
|
|
ConcurrentSkipListMap map = map5(); |
101 |
jsr166 |
1.12 |
assertTrue(empty.isEmpty()); |
102 |
dl |
1.1 |
assertFalse(map.isEmpty()); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
/** |
106 |
jsr166 |
1.20 |
* firstKey returns first key |
107 |
dl |
1.1 |
*/ |
108 |
|
|
public void testFirstKey() { |
109 |
|
|
ConcurrentSkipListMap map = map5(); |
110 |
jsr166 |
1.12 |
assertEquals(one, map.firstKey()); |
111 |
dl |
1.1 |
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
jsr166 |
1.20 |
* lastKey returns last key |
115 |
dl |
1.1 |
*/ |
116 |
|
|
public void testLastKey() { |
117 |
|
|
ConcurrentSkipListMap map = map5(); |
118 |
jsr166 |
1.12 |
assertEquals(five, map.lastKey()); |
119 |
dl |
1.1 |
} |
120 |
|
|
|
121 |
|
|
/** |
122 |
jsr166 |
1.20 |
* keySet.toArray returns contains all keys |
123 |
dl |
1.1 |
*/ |
124 |
|
|
public void testKeySetToArray() { |
125 |
|
|
ConcurrentSkipListMap map = map5(); |
126 |
jsr166 |
1.12 |
Set s = map.keySet(); |
127 |
dl |
1.1 |
Object[] ar = s.toArray(); |
128 |
|
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
129 |
jsr166 |
1.12 |
assertEquals(5, ar.length); |
130 |
dl |
1.1 |
ar[0] = m10; |
131 |
|
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
/** |
135 |
jsr166 |
1.20 |
* descendingkeySet.toArray returns contains all keys |
136 |
dl |
1.1 |
*/ |
137 |
|
|
public void testDescendingKeySetToArray() { |
138 |
|
|
ConcurrentSkipListMap map = map5(); |
139 |
jsr166 |
1.12 |
Set s = map.descendingKeySet(); |
140 |
dl |
1.1 |
Object[] ar = s.toArray(); |
141 |
jsr166 |
1.12 |
assertEquals(5, ar.length); |
142 |
dl |
1.1 |
assertTrue(s.containsAll(Arrays.asList(ar))); |
143 |
|
|
ar[0] = m10; |
144 |
|
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
/** |
148 |
jsr166 |
1.20 |
* keySet returns a Set containing all the keys |
149 |
dl |
1.1 |
*/ |
150 |
|
|
public void testKeySet() { |
151 |
|
|
ConcurrentSkipListMap map = map5(); |
152 |
jsr166 |
1.12 |
Set s = map.keySet(); |
153 |
|
|
assertEquals(5, s.size()); |
154 |
|
|
assertTrue(s.contains(one)); |
155 |
|
|
assertTrue(s.contains(two)); |
156 |
|
|
assertTrue(s.contains(three)); |
157 |
|
|
assertTrue(s.contains(four)); |
158 |
|
|
assertTrue(s.contains(five)); |
159 |
dl |
1.1 |
} |
160 |
|
|
|
161 |
|
|
/** |
162 |
jsr166 |
1.20 |
* keySet is ordered |
163 |
dl |
1.1 |
*/ |
164 |
|
|
public void testKeySetOrder() { |
165 |
|
|
ConcurrentSkipListMap map = map5(); |
166 |
jsr166 |
1.12 |
Set s = map.keySet(); |
167 |
dl |
1.1 |
Iterator i = s.iterator(); |
168 |
|
|
Integer last = (Integer)i.next(); |
169 |
|
|
assertEquals(last, one); |
170 |
dl |
1.8 |
int count = 1; |
171 |
dl |
1.1 |
while (i.hasNext()) { |
172 |
|
|
Integer k = (Integer)i.next(); |
173 |
|
|
assertTrue(last.compareTo(k) < 0); |
174 |
|
|
last = k; |
175 |
dl |
1.8 |
++count; |
176 |
dl |
1.1 |
} |
177 |
jsr166 |
1.27 |
assertEquals(5, count); |
178 |
dl |
1.8 |
} |
179 |
|
|
|
180 |
|
|
/** |
181 |
|
|
* descending iterator of key set is inverse ordered |
182 |
|
|
*/ |
183 |
|
|
public void testKeySetDescendingIteratorOrder() { |
184 |
|
|
ConcurrentSkipListMap map = map5(); |
185 |
jsr166 |
1.12 |
NavigableSet s = map.navigableKeySet(); |
186 |
dl |
1.8 |
Iterator i = s.descendingIterator(); |
187 |
|
|
Integer last = (Integer)i.next(); |
188 |
|
|
assertEquals(last, five); |
189 |
|
|
int count = 1; |
190 |
|
|
while (i.hasNext()) { |
191 |
|
|
Integer k = (Integer)i.next(); |
192 |
|
|
assertTrue(last.compareTo(k) > 0); |
193 |
|
|
last = k; |
194 |
|
|
++count; |
195 |
|
|
} |
196 |
jsr166 |
1.27 |
assertEquals(5, count); |
197 |
dl |
1.1 |
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
jsr166 |
1.20 |
* descendingKeySet is ordered |
201 |
dl |
1.1 |
*/ |
202 |
|
|
public void testDescendingKeySetOrder() { |
203 |
|
|
ConcurrentSkipListMap map = map5(); |
204 |
jsr166 |
1.12 |
Set s = map.descendingKeySet(); |
205 |
dl |
1.1 |
Iterator i = s.iterator(); |
206 |
|
|
Integer last = (Integer)i.next(); |
207 |
|
|
assertEquals(last, five); |
208 |
dl |
1.8 |
int count = 1; |
209 |
dl |
1.1 |
while (i.hasNext()) { |
210 |
|
|
Integer k = (Integer)i.next(); |
211 |
|
|
assertTrue(last.compareTo(k) > 0); |
212 |
|
|
last = k; |
213 |
dl |
1.8 |
++count; |
214 |
dl |
1.1 |
} |
215 |
jsr166 |
1.27 |
assertEquals(5, count); |
216 |
dl |
1.1 |
} |
217 |
|
|
|
218 |
dl |
1.8 |
/** |
219 |
jsr166 |
1.20 |
* descending iterator of descendingKeySet is ordered |
220 |
dl |
1.8 |
*/ |
221 |
|
|
public void testDescendingKeySetDescendingIteratorOrder() { |
222 |
|
|
ConcurrentSkipListMap map = map5(); |
223 |
jsr166 |
1.12 |
NavigableSet s = map.descendingKeySet(); |
224 |
dl |
1.8 |
Iterator i = s.descendingIterator(); |
225 |
|
|
Integer last = (Integer)i.next(); |
226 |
|
|
assertEquals(last, one); |
227 |
|
|
int count = 1; |
228 |
|
|
while (i.hasNext()) { |
229 |
|
|
Integer k = (Integer)i.next(); |
230 |
|
|
assertTrue(last.compareTo(k) < 0); |
231 |
|
|
last = k; |
232 |
|
|
++count; |
233 |
|
|
} |
234 |
jsr166 |
1.27 |
assertEquals(5, count); |
235 |
dl |
1.8 |
} |
236 |
dl |
1.4 |
|
237 |
|
|
/** |
238 |
jsr166 |
1.20 |
* Values.toArray contains all values |
239 |
dl |
1.4 |
*/ |
240 |
|
|
public void testValuesToArray() { |
241 |
|
|
ConcurrentSkipListMap map = map5(); |
242 |
jsr166 |
1.12 |
Collection v = map.values(); |
243 |
dl |
1.4 |
Object[] ar = v.toArray(); |
244 |
|
|
ArrayList s = new ArrayList(Arrays.asList(ar)); |
245 |
jsr166 |
1.12 |
assertEquals(5, ar.length); |
246 |
|
|
assertTrue(s.contains("A")); |
247 |
|
|
assertTrue(s.contains("B")); |
248 |
|
|
assertTrue(s.contains("C")); |
249 |
|
|
assertTrue(s.contains("D")); |
250 |
|
|
assertTrue(s.contains("E")); |
251 |
dl |
1.4 |
} |
252 |
|
|
|
253 |
dl |
1.1 |
/** |
254 |
|
|
* values collection contains all values |
255 |
|
|
*/ |
256 |
|
|
public void testValues() { |
257 |
|
|
ConcurrentSkipListMap map = map5(); |
258 |
jsr166 |
1.12 |
Collection s = map.values(); |
259 |
|
|
assertEquals(5, s.size()); |
260 |
|
|
assertTrue(s.contains("A")); |
261 |
|
|
assertTrue(s.contains("B")); |
262 |
|
|
assertTrue(s.contains("C")); |
263 |
|
|
assertTrue(s.contains("D")); |
264 |
|
|
assertTrue(s.contains("E")); |
265 |
dl |
1.1 |
} |
266 |
|
|
|
267 |
|
|
/** |
268 |
|
|
* entrySet contains all pairs |
269 |
|
|
*/ |
270 |
|
|
public void testEntrySet() { |
271 |
|
|
ConcurrentSkipListMap map = map5(); |
272 |
jsr166 |
1.12 |
Set s = map.entrySet(); |
273 |
|
|
assertEquals(5, s.size()); |
274 |
dl |
1.1 |
Iterator it = s.iterator(); |
275 |
|
|
while (it.hasNext()) { |
276 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
277 |
jsr166 |
1.9 |
assertTrue( |
278 |
dl |
1.1 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
279 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
280 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
281 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
282 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
283 |
|
|
} |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
/** |
287 |
|
|
* descendingEntrySet contains all pairs |
288 |
|
|
*/ |
289 |
|
|
public void testDescendingEntrySet() { |
290 |
|
|
ConcurrentSkipListMap map = map5(); |
291 |
jsr166 |
1.12 |
Set s = map.descendingMap().entrySet(); |
292 |
|
|
assertEquals(5, s.size()); |
293 |
dl |
1.1 |
Iterator it = s.iterator(); |
294 |
|
|
while (it.hasNext()) { |
295 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
296 |
jsr166 |
1.9 |
assertTrue( |
297 |
dl |
1.1 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
298 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
299 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
300 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
301 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
302 |
|
|
} |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
/** |
306 |
jsr166 |
1.20 |
* entrySet.toArray contains all entries |
307 |
dl |
1.1 |
*/ |
308 |
|
|
public void testEntrySetToArray() { |
309 |
|
|
ConcurrentSkipListMap map = map5(); |
310 |
jsr166 |
1.12 |
Set s = map.entrySet(); |
311 |
dl |
1.1 |
Object[] ar = s.toArray(); |
312 |
|
|
assertEquals(5, ar.length); |
313 |
|
|
for (int i = 0; i < 5; ++i) { |
314 |
|
|
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
315 |
|
|
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
316 |
|
|
} |
317 |
|
|
} |
318 |
|
|
|
319 |
|
|
/** |
320 |
jsr166 |
1.20 |
* descendingEntrySet.toArray contains all entries |
321 |
dl |
1.1 |
*/ |
322 |
|
|
public void testDescendingEntrySetToArray() { |
323 |
|
|
ConcurrentSkipListMap map = map5(); |
324 |
jsr166 |
1.12 |
Set s = map.descendingMap().entrySet(); |
325 |
dl |
1.1 |
Object[] ar = s.toArray(); |
326 |
|
|
assertEquals(5, ar.length); |
327 |
|
|
for (int i = 0; i < 5; ++i) { |
328 |
|
|
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
329 |
|
|
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
330 |
|
|
} |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
/** |
334 |
jsr166 |
1.20 |
* putAll adds all key-value pairs from the given map |
335 |
dl |
1.1 |
*/ |
336 |
|
|
public void testPutAll() { |
337 |
|
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
338 |
|
|
ConcurrentSkipListMap map = map5(); |
339 |
jsr166 |
1.12 |
empty.putAll(map); |
340 |
|
|
assertEquals(5, empty.size()); |
341 |
|
|
assertTrue(empty.containsKey(one)); |
342 |
|
|
assertTrue(empty.containsKey(two)); |
343 |
|
|
assertTrue(empty.containsKey(three)); |
344 |
|
|
assertTrue(empty.containsKey(four)); |
345 |
|
|
assertTrue(empty.containsKey(five)); |
346 |
dl |
1.1 |
} |
347 |
|
|
|
348 |
|
|
/** |
349 |
jsr166 |
1.20 |
* putIfAbsent works when the given key is not present |
350 |
dl |
1.1 |
*/ |
351 |
|
|
public void testPutIfAbsent() { |
352 |
|
|
ConcurrentSkipListMap map = map5(); |
353 |
jsr166 |
1.12 |
map.putIfAbsent(six, "Z"); |
354 |
dl |
1.1 |
assertTrue(map.containsKey(six)); |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
/** |
358 |
jsr166 |
1.20 |
* putIfAbsent does not add the pair if the key is already present |
359 |
dl |
1.1 |
*/ |
360 |
|
|
public void testPutIfAbsent2() { |
361 |
|
|
ConcurrentSkipListMap map = map5(); |
362 |
|
|
assertEquals("A", map.putIfAbsent(one, "Z")); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
/** |
366 |
jsr166 |
1.20 |
* replace fails when the given key is not present |
367 |
dl |
1.1 |
*/ |
368 |
|
|
public void testReplace() { |
369 |
|
|
ConcurrentSkipListMap map = map5(); |
370 |
jsr166 |
1.12 |
assertNull(map.replace(six, "Z")); |
371 |
dl |
1.1 |
assertFalse(map.containsKey(six)); |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
/** |
375 |
jsr166 |
1.20 |
* replace succeeds if the key is already present |
376 |
dl |
1.1 |
*/ |
377 |
|
|
public void testReplace2() { |
378 |
|
|
ConcurrentSkipListMap map = map5(); |
379 |
|
|
assertNotNull(map.replace(one, "Z")); |
380 |
|
|
assertEquals("Z", map.get(one)); |
381 |
|
|
} |
382 |
|
|
|
383 |
|
|
/** |
384 |
|
|
* replace value fails when the given key not mapped to expected value |
385 |
|
|
*/ |
386 |
|
|
public void testReplaceValue() { |
387 |
|
|
ConcurrentSkipListMap map = map5(); |
388 |
|
|
assertEquals("A", map.get(one)); |
389 |
jsr166 |
1.12 |
assertFalse(map.replace(one, "Z", "Z")); |
390 |
dl |
1.1 |
assertEquals("A", map.get(one)); |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
/** |
394 |
|
|
* replace value succeeds when the given key mapped to expected value |
395 |
|
|
*/ |
396 |
|
|
public void testReplaceValue2() { |
397 |
|
|
ConcurrentSkipListMap map = map5(); |
398 |
|
|
assertEquals("A", map.get(one)); |
399 |
jsr166 |
1.12 |
assertTrue(map.replace(one, "A", "Z")); |
400 |
dl |
1.1 |
assertEquals("Z", map.get(one)); |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
/** |
404 |
jsr166 |
1.20 |
* remove removes the correct key-value pair from the map |
405 |
dl |
1.1 |
*/ |
406 |
|
|
public void testRemove() { |
407 |
|
|
ConcurrentSkipListMap map = map5(); |
408 |
jsr166 |
1.12 |
map.remove(five); |
409 |
|
|
assertEquals(4, map.size()); |
410 |
|
|
assertFalse(map.containsKey(five)); |
411 |
dl |
1.1 |
} |
412 |
|
|
|
413 |
|
|
/** |
414 |
|
|
* remove(key,value) removes only if pair present |
415 |
|
|
*/ |
416 |
|
|
public void testRemove2() { |
417 |
|
|
ConcurrentSkipListMap map = map5(); |
418 |
jsr166 |
1.12 |
assertTrue(map.containsKey(five)); |
419 |
dl |
1.1 |
assertEquals("E", map.get(five)); |
420 |
jsr166 |
1.12 |
map.remove(five, "E"); |
421 |
|
|
assertEquals(4, map.size()); |
422 |
|
|
assertFalse(map.containsKey(five)); |
423 |
|
|
map.remove(four, "A"); |
424 |
|
|
assertEquals(4, map.size()); |
425 |
|
|
assertTrue(map.containsKey(four)); |
426 |
dl |
1.1 |
} |
427 |
|
|
|
428 |
|
|
/** |
429 |
|
|
* lowerEntry returns preceding entry. |
430 |
|
|
*/ |
431 |
|
|
public void testLowerEntry() { |
432 |
|
|
ConcurrentSkipListMap map = map5(); |
433 |
|
|
Map.Entry e1 = map.lowerEntry(three); |
434 |
|
|
assertEquals(two, e1.getKey()); |
435 |
|
|
|
436 |
|
|
Map.Entry e2 = map.lowerEntry(six); |
437 |
|
|
assertEquals(five, e2.getKey()); |
438 |
|
|
|
439 |
|
|
Map.Entry e3 = map.lowerEntry(one); |
440 |
|
|
assertNull(e3); |
441 |
|
|
|
442 |
|
|
Map.Entry e4 = map.lowerEntry(zero); |
443 |
|
|
assertNull(e4); |
444 |
|
|
} |
445 |
|
|
|
446 |
|
|
/** |
447 |
|
|
* higherEntry returns next entry. |
448 |
|
|
*/ |
449 |
|
|
public void testHigherEntry() { |
450 |
|
|
ConcurrentSkipListMap map = map5(); |
451 |
|
|
Map.Entry e1 = map.higherEntry(three); |
452 |
|
|
assertEquals(four, e1.getKey()); |
453 |
|
|
|
454 |
|
|
Map.Entry e2 = map.higherEntry(zero); |
455 |
|
|
assertEquals(one, e2.getKey()); |
456 |
|
|
|
457 |
|
|
Map.Entry e3 = map.higherEntry(five); |
458 |
|
|
assertNull(e3); |
459 |
|
|
|
460 |
|
|
Map.Entry e4 = map.higherEntry(six); |
461 |
|
|
assertNull(e4); |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
/** |
465 |
|
|
* floorEntry returns preceding entry. |
466 |
|
|
*/ |
467 |
|
|
public void testFloorEntry() { |
468 |
|
|
ConcurrentSkipListMap map = map5(); |
469 |
|
|
Map.Entry e1 = map.floorEntry(three); |
470 |
|
|
assertEquals(three, e1.getKey()); |
471 |
|
|
|
472 |
|
|
Map.Entry e2 = map.floorEntry(six); |
473 |
|
|
assertEquals(five, e2.getKey()); |
474 |
|
|
|
475 |
|
|
Map.Entry e3 = map.floorEntry(one); |
476 |
|
|
assertEquals(one, e3.getKey()); |
477 |
|
|
|
478 |
|
|
Map.Entry e4 = map.floorEntry(zero); |
479 |
|
|
assertNull(e4); |
480 |
|
|
} |
481 |
|
|
|
482 |
|
|
/** |
483 |
|
|
* ceilingEntry returns next entry. |
484 |
|
|
*/ |
485 |
|
|
public void testCeilingEntry() { |
486 |
|
|
ConcurrentSkipListMap map = map5(); |
487 |
|
|
Map.Entry e1 = map.ceilingEntry(three); |
488 |
|
|
assertEquals(three, e1.getKey()); |
489 |
|
|
|
490 |
|
|
Map.Entry e2 = map.ceilingEntry(zero); |
491 |
|
|
assertEquals(one, e2.getKey()); |
492 |
|
|
|
493 |
|
|
Map.Entry e3 = map.ceilingEntry(five); |
494 |
|
|
assertEquals(five, e3.getKey()); |
495 |
|
|
|
496 |
|
|
Map.Entry e4 = map.ceilingEntry(six); |
497 |
|
|
assertNull(e4); |
498 |
|
|
} |
499 |
|
|
|
500 |
dl |
1.5 |
/** |
501 |
|
|
* lowerEntry, higherEntry, ceilingEntry, and floorEntry return |
502 |
jsr166 |
1.17 |
* immutable entries |
503 |
dl |
1.5 |
*/ |
504 |
jsr166 |
1.26 |
public void testEntryImmutability() { |
505 |
dl |
1.5 |
ConcurrentSkipListMap map = map5(); |
506 |
|
|
Map.Entry e = map.lowerEntry(three); |
507 |
|
|
assertEquals(two, e.getKey()); |
508 |
|
|
try { |
509 |
|
|
e.setValue("X"); |
510 |
jsr166 |
1.14 |
shouldThrow(); |
511 |
jsr166 |
1.10 |
} catch (UnsupportedOperationException success) {} |
512 |
dl |
1.5 |
e = map.higherEntry(zero); |
513 |
|
|
assertEquals(one, e.getKey()); |
514 |
|
|
try { |
515 |
|
|
e.setValue("X"); |
516 |
jsr166 |
1.14 |
shouldThrow(); |
517 |
jsr166 |
1.10 |
} catch (UnsupportedOperationException success) {} |
518 |
dl |
1.5 |
e = map.floorEntry(one); |
519 |
|
|
assertEquals(one, e.getKey()); |
520 |
|
|
try { |
521 |
|
|
e.setValue("X"); |
522 |
jsr166 |
1.14 |
shouldThrow(); |
523 |
jsr166 |
1.10 |
} catch (UnsupportedOperationException success) {} |
524 |
dl |
1.5 |
e = map.ceilingEntry(five); |
525 |
|
|
assertEquals(five, e.getKey()); |
526 |
|
|
try { |
527 |
|
|
e.setValue("X"); |
528 |
jsr166 |
1.14 |
shouldThrow(); |
529 |
jsr166 |
1.10 |
} catch (UnsupportedOperationException success) {} |
530 |
dl |
1.5 |
} |
531 |
|
|
|
532 |
dl |
1.1 |
/** |
533 |
|
|
* lowerKey returns preceding element |
534 |
|
|
*/ |
535 |
|
|
public void testLowerKey() { |
536 |
|
|
ConcurrentSkipListMap q = map5(); |
537 |
|
|
Object e1 = q.lowerKey(three); |
538 |
|
|
assertEquals(two, e1); |
539 |
|
|
|
540 |
|
|
Object e2 = q.lowerKey(six); |
541 |
|
|
assertEquals(five, e2); |
542 |
|
|
|
543 |
|
|
Object e3 = q.lowerKey(one); |
544 |
|
|
assertNull(e3); |
545 |
|
|
|
546 |
|
|
Object e4 = q.lowerKey(zero); |
547 |
|
|
assertNull(e4); |
548 |
|
|
} |
549 |
|
|
|
550 |
|
|
/** |
551 |
|
|
* higherKey returns next element |
552 |
|
|
*/ |
553 |
|
|
public void testHigherKey() { |
554 |
|
|
ConcurrentSkipListMap q = map5(); |
555 |
|
|
Object e1 = q.higherKey(three); |
556 |
|
|
assertEquals(four, e1); |
557 |
|
|
|
558 |
|
|
Object e2 = q.higherKey(zero); |
559 |
|
|
assertEquals(one, e2); |
560 |
|
|
|
561 |
|
|
Object e3 = q.higherKey(five); |
562 |
|
|
assertNull(e3); |
563 |
|
|
|
564 |
|
|
Object e4 = q.higherKey(six); |
565 |
|
|
assertNull(e4); |
566 |
|
|
} |
567 |
|
|
|
568 |
|
|
/** |
569 |
|
|
* floorKey returns preceding element |
570 |
|
|
*/ |
571 |
|
|
public void testFloorKey() { |
572 |
|
|
ConcurrentSkipListMap q = map5(); |
573 |
|
|
Object e1 = q.floorKey(three); |
574 |
|
|
assertEquals(three, e1); |
575 |
|
|
|
576 |
|
|
Object e2 = q.floorKey(six); |
577 |
|
|
assertEquals(five, e2); |
578 |
|
|
|
579 |
|
|
Object e3 = q.floorKey(one); |
580 |
|
|
assertEquals(one, e3); |
581 |
|
|
|
582 |
|
|
Object e4 = q.floorKey(zero); |
583 |
|
|
assertNull(e4); |
584 |
|
|
} |
585 |
|
|
|
586 |
|
|
/** |
587 |
|
|
* ceilingKey returns next element |
588 |
|
|
*/ |
589 |
|
|
public void testCeilingKey() { |
590 |
|
|
ConcurrentSkipListMap q = map5(); |
591 |
|
|
Object e1 = q.ceilingKey(three); |
592 |
|
|
assertEquals(three, e1); |
593 |
|
|
|
594 |
|
|
Object e2 = q.ceilingKey(zero); |
595 |
|
|
assertEquals(one, e2); |
596 |
|
|
|
597 |
|
|
Object e3 = q.ceilingKey(five); |
598 |
|
|
assertEquals(five, e3); |
599 |
|
|
|
600 |
|
|
Object e4 = q.ceilingKey(six); |
601 |
|
|
assertNull(e4); |
602 |
|
|
} |
603 |
|
|
|
604 |
|
|
/** |
605 |
|
|
* pollFirstEntry returns entries in order |
606 |
|
|
*/ |
607 |
|
|
public void testPollFirstEntry() { |
608 |
|
|
ConcurrentSkipListMap map = map5(); |
609 |
|
|
Map.Entry e = map.pollFirstEntry(); |
610 |
|
|
assertEquals(one, e.getKey()); |
611 |
|
|
assertEquals("A", e.getValue()); |
612 |
|
|
e = map.pollFirstEntry(); |
613 |
|
|
assertEquals(two, e.getKey()); |
614 |
|
|
map.put(one, "A"); |
615 |
|
|
e = map.pollFirstEntry(); |
616 |
|
|
assertEquals(one, e.getKey()); |
617 |
|
|
assertEquals("A", e.getValue()); |
618 |
|
|
e = map.pollFirstEntry(); |
619 |
|
|
assertEquals(three, e.getKey()); |
620 |
|
|
map.remove(four); |
621 |
|
|
e = map.pollFirstEntry(); |
622 |
|
|
assertEquals(five, e.getKey()); |
623 |
|
|
try { |
624 |
|
|
e.setValue("A"); |
625 |
|
|
shouldThrow(); |
626 |
jsr166 |
1.14 |
} catch (UnsupportedOperationException success) {} |
627 |
dl |
1.1 |
e = map.pollFirstEntry(); |
628 |
|
|
assertNull(e); |
629 |
|
|
} |
630 |
|
|
|
631 |
|
|
/** |
632 |
|
|
* pollLastEntry returns entries in order |
633 |
|
|
*/ |
634 |
|
|
public void testPollLastEntry() { |
635 |
|
|
ConcurrentSkipListMap map = map5(); |
636 |
|
|
Map.Entry e = map.pollLastEntry(); |
637 |
|
|
assertEquals(five, e.getKey()); |
638 |
|
|
assertEquals("E", e.getValue()); |
639 |
|
|
e = map.pollLastEntry(); |
640 |
|
|
assertEquals(four, e.getKey()); |
641 |
|
|
map.put(five, "E"); |
642 |
|
|
e = map.pollLastEntry(); |
643 |
|
|
assertEquals(five, e.getKey()); |
644 |
|
|
assertEquals("E", e.getValue()); |
645 |
|
|
e = map.pollLastEntry(); |
646 |
|
|
assertEquals(three, e.getKey()); |
647 |
|
|
map.remove(two); |
648 |
|
|
e = map.pollLastEntry(); |
649 |
|
|
assertEquals(one, e.getKey()); |
650 |
|
|
try { |
651 |
|
|
e.setValue("E"); |
652 |
|
|
shouldThrow(); |
653 |
jsr166 |
1.14 |
} catch (UnsupportedOperationException success) {} |
654 |
dl |
1.1 |
e = map.pollLastEntry(); |
655 |
|
|
assertNull(e); |
656 |
|
|
} |
657 |
|
|
|
658 |
|
|
/** |
659 |
jsr166 |
1.20 |
* size returns the correct values |
660 |
dl |
1.1 |
*/ |
661 |
|
|
public void testSize() { |
662 |
|
|
ConcurrentSkipListMap map = map5(); |
663 |
|
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
664 |
jsr166 |
1.12 |
assertEquals(0, empty.size()); |
665 |
|
|
assertEquals(5, map.size()); |
666 |
dl |
1.1 |
} |
667 |
|
|
|
668 |
|
|
/** |
669 |
|
|
* toString contains toString of elements |
670 |
|
|
*/ |
671 |
|
|
public void testToString() { |
672 |
|
|
ConcurrentSkipListMap map = map5(); |
673 |
|
|
String s = map.toString(); |
674 |
|
|
for (int i = 1; i <= 5; ++i) { |
675 |
jsr166 |
1.24 |
assertTrue(s.contains(String.valueOf(i))); |
676 |
dl |
1.1 |
} |
677 |
jsr166 |
1.9 |
} |
678 |
dl |
1.1 |
|
679 |
|
|
// Exception tests |
680 |
|
|
|
681 |
|
|
/** |
682 |
|
|
* get(null) of nonempty map throws NPE |
683 |
|
|
*/ |
684 |
|
|
public void testGet_NullPointerException() { |
685 |
|
|
try { |
686 |
|
|
ConcurrentSkipListMap c = map5(); |
687 |
|
|
c.get(null); |
688 |
|
|
shouldThrow(); |
689 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
690 |
dl |
1.1 |
} |
691 |
|
|
|
692 |
|
|
/** |
693 |
|
|
* containsKey(null) of nonempty map throws NPE |
694 |
|
|
*/ |
695 |
|
|
public void testContainsKey_NullPointerException() { |
696 |
|
|
try { |
697 |
|
|
ConcurrentSkipListMap c = map5(); |
698 |
|
|
c.containsKey(null); |
699 |
|
|
shouldThrow(); |
700 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
701 |
dl |
1.1 |
} |
702 |
|
|
|
703 |
|
|
/** |
704 |
|
|
* containsValue(null) throws NPE |
705 |
|
|
*/ |
706 |
|
|
public void testContainsValue_NullPointerException() { |
707 |
|
|
try { |
708 |
|
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
709 |
|
|
c.containsValue(null); |
710 |
|
|
shouldThrow(); |
711 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
712 |
dl |
1.1 |
} |
713 |
|
|
|
714 |
|
|
/** |
715 |
|
|
* put(null,x) throws NPE |
716 |
|
|
*/ |
717 |
|
|
public void testPut1_NullPointerException() { |
718 |
|
|
try { |
719 |
|
|
ConcurrentSkipListMap c = map5(); |
720 |
|
|
c.put(null, "whatever"); |
721 |
|
|
shouldThrow(); |
722 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
723 |
dl |
1.1 |
} |
724 |
|
|
|
725 |
|
|
/** |
726 |
|
|
* putIfAbsent(null, x) throws NPE |
727 |
|
|
*/ |
728 |
|
|
public void testPutIfAbsent1_NullPointerException() { |
729 |
|
|
try { |
730 |
|
|
ConcurrentSkipListMap c = map5(); |
731 |
|
|
c.putIfAbsent(null, "whatever"); |
732 |
|
|
shouldThrow(); |
733 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
734 |
dl |
1.1 |
} |
735 |
|
|
|
736 |
|
|
/** |
737 |
|
|
* replace(null, x) throws NPE |
738 |
|
|
*/ |
739 |
|
|
public void testReplace_NullPointerException() { |
740 |
|
|
try { |
741 |
|
|
ConcurrentSkipListMap c = map5(); |
742 |
|
|
c.replace(null, "whatever"); |
743 |
|
|
shouldThrow(); |
744 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
745 |
dl |
1.1 |
} |
746 |
|
|
|
747 |
|
|
/** |
748 |
|
|
* replace(null, x, y) throws NPE |
749 |
|
|
*/ |
750 |
|
|
public void testReplaceValue_NullPointerException() { |
751 |
|
|
try { |
752 |
|
|
ConcurrentSkipListMap c = map5(); |
753 |
|
|
c.replace(null, one, "whatever"); |
754 |
|
|
shouldThrow(); |
755 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
756 |
dl |
1.1 |
} |
757 |
|
|
|
758 |
|
|
/** |
759 |
|
|
* remove(null) throws NPE |
760 |
|
|
*/ |
761 |
|
|
public void testRemove1_NullPointerException() { |
762 |
|
|
try { |
763 |
|
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
764 |
|
|
c.put("sadsdf", "asdads"); |
765 |
|
|
c.remove(null); |
766 |
|
|
shouldThrow(); |
767 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
768 |
dl |
1.1 |
} |
769 |
|
|
|
770 |
|
|
/** |
771 |
|
|
* remove(null, x) throws NPE |
772 |
|
|
*/ |
773 |
|
|
public void testRemove2_NullPointerException() { |
774 |
|
|
try { |
775 |
|
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
776 |
|
|
c.put("sadsdf", "asdads"); |
777 |
|
|
c.remove(null, "whatever"); |
778 |
|
|
shouldThrow(); |
779 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
780 |
dl |
1.1 |
} |
781 |
|
|
|
782 |
|
|
/** |
783 |
dl |
1.3 |
* remove(x, null) returns false |
784 |
|
|
*/ |
785 |
|
|
public void testRemove3() { |
786 |
jsr166 |
1.14 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
787 |
|
|
c.put("sadsdf", "asdads"); |
788 |
|
|
assertFalse(c.remove("sadsdf", null)); |
789 |
dl |
1.3 |
} |
790 |
|
|
|
791 |
|
|
/** |
792 |
dl |
1.1 |
* A deserialized map equals original |
793 |
|
|
*/ |
794 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
795 |
jsr166 |
1.25 |
NavigableMap x = map5(); |
796 |
|
|
NavigableMap y = serialClone(x); |
797 |
dl |
1.1 |
|
798 |
jsr166 |
1.25 |
assertTrue(x != y); |
799 |
|
|
assertEquals(x.size(), y.size()); |
800 |
|
|
assertEquals(x.toString(), y.toString()); |
801 |
|
|
assertEquals(x, y); |
802 |
|
|
assertEquals(y, x); |
803 |
dl |
1.1 |
} |
804 |
|
|
|
805 |
|
|
/** |
806 |
|
|
* subMap returns map with keys in requested range |
807 |
|
|
*/ |
808 |
|
|
public void testSubMapContents() { |
809 |
|
|
ConcurrentSkipListMap map = map5(); |
810 |
dl |
1.7 |
NavigableMap sm = map.subMap(two, true, four, false); |
811 |
dl |
1.1 |
assertEquals(two, sm.firstKey()); |
812 |
|
|
assertEquals(three, sm.lastKey()); |
813 |
|
|
assertEquals(2, sm.size()); |
814 |
|
|
assertFalse(sm.containsKey(one)); |
815 |
|
|
assertTrue(sm.containsKey(two)); |
816 |
|
|
assertTrue(sm.containsKey(three)); |
817 |
|
|
assertFalse(sm.containsKey(four)); |
818 |
|
|
assertFalse(sm.containsKey(five)); |
819 |
|
|
Iterator i = sm.keySet().iterator(); |
820 |
|
|
Object k; |
821 |
|
|
k = (Integer)(i.next()); |
822 |
|
|
assertEquals(two, k); |
823 |
|
|
k = (Integer)(i.next()); |
824 |
|
|
assertEquals(three, k); |
825 |
|
|
assertFalse(i.hasNext()); |
826 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
827 |
|
|
k = (Integer)(r.next()); |
828 |
|
|
assertEquals(three, k); |
829 |
|
|
k = (Integer)(r.next()); |
830 |
|
|
assertEquals(two, k); |
831 |
|
|
assertFalse(r.hasNext()); |
832 |
jsr166 |
1.9 |
|
833 |
dl |
1.1 |
Iterator j = sm.keySet().iterator(); |
834 |
|
|
j.next(); |
835 |
|
|
j.remove(); |
836 |
|
|
assertFalse(map.containsKey(two)); |
837 |
|
|
assertEquals(4, map.size()); |
838 |
|
|
assertEquals(1, sm.size()); |
839 |
|
|
assertEquals(three, sm.firstKey()); |
840 |
|
|
assertEquals(three, sm.lastKey()); |
841 |
jsr166 |
1.15 |
assertEquals("C", sm.remove(three)); |
842 |
dl |
1.1 |
assertTrue(sm.isEmpty()); |
843 |
|
|
assertEquals(3, map.size()); |
844 |
|
|
} |
845 |
|
|
|
846 |
|
|
public void testSubMapContents2() { |
847 |
|
|
ConcurrentSkipListMap map = map5(); |
848 |
dl |
1.7 |
NavigableMap sm = map.subMap(two, true, three, false); |
849 |
dl |
1.1 |
assertEquals(1, sm.size()); |
850 |
|
|
assertEquals(two, sm.firstKey()); |
851 |
|
|
assertEquals(two, sm.lastKey()); |
852 |
|
|
assertFalse(sm.containsKey(one)); |
853 |
|
|
assertTrue(sm.containsKey(two)); |
854 |
|
|
assertFalse(sm.containsKey(three)); |
855 |
|
|
assertFalse(sm.containsKey(four)); |
856 |
|
|
assertFalse(sm.containsKey(five)); |
857 |
|
|
Iterator i = sm.keySet().iterator(); |
858 |
|
|
Object k; |
859 |
|
|
k = (Integer)(i.next()); |
860 |
|
|
assertEquals(two, k); |
861 |
|
|
assertFalse(i.hasNext()); |
862 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
863 |
|
|
k = (Integer)(r.next()); |
864 |
|
|
assertEquals(two, k); |
865 |
|
|
assertFalse(r.hasNext()); |
866 |
jsr166 |
1.9 |
|
867 |
dl |
1.1 |
Iterator j = sm.keySet().iterator(); |
868 |
|
|
j.next(); |
869 |
|
|
j.remove(); |
870 |
|
|
assertFalse(map.containsKey(two)); |
871 |
|
|
assertEquals(4, map.size()); |
872 |
|
|
assertEquals(0, sm.size()); |
873 |
|
|
assertTrue(sm.isEmpty()); |
874 |
jsr166 |
1.15 |
assertSame(sm.remove(three), null); |
875 |
dl |
1.1 |
assertEquals(4, map.size()); |
876 |
|
|
} |
877 |
|
|
|
878 |
|
|
/** |
879 |
|
|
* headMap returns map with keys in requested range |
880 |
|
|
*/ |
881 |
|
|
public void testHeadMapContents() { |
882 |
|
|
ConcurrentSkipListMap map = map5(); |
883 |
dl |
1.7 |
NavigableMap sm = map.headMap(four, false); |
884 |
dl |
1.1 |
assertTrue(sm.containsKey(one)); |
885 |
|
|
assertTrue(sm.containsKey(two)); |
886 |
|
|
assertTrue(sm.containsKey(three)); |
887 |
|
|
assertFalse(sm.containsKey(four)); |
888 |
|
|
assertFalse(sm.containsKey(five)); |
889 |
|
|
Iterator i = sm.keySet().iterator(); |
890 |
|
|
Object k; |
891 |
|
|
k = (Integer)(i.next()); |
892 |
|
|
assertEquals(one, k); |
893 |
|
|
k = (Integer)(i.next()); |
894 |
|
|
assertEquals(two, k); |
895 |
|
|
k = (Integer)(i.next()); |
896 |
|
|
assertEquals(three, k); |
897 |
|
|
assertFalse(i.hasNext()); |
898 |
|
|
sm.clear(); |
899 |
|
|
assertTrue(sm.isEmpty()); |
900 |
|
|
assertEquals(2, map.size()); |
901 |
|
|
assertEquals(four, map.firstKey()); |
902 |
|
|
} |
903 |
|
|
|
904 |
|
|
/** |
905 |
dl |
1.6 |
* tailMap returns map with keys in requested range |
906 |
dl |
1.1 |
*/ |
907 |
|
|
public void testTailMapContents() { |
908 |
|
|
ConcurrentSkipListMap map = map5(); |
909 |
dl |
1.7 |
NavigableMap sm = map.tailMap(two, true); |
910 |
dl |
1.1 |
assertFalse(sm.containsKey(one)); |
911 |
|
|
assertTrue(sm.containsKey(two)); |
912 |
|
|
assertTrue(sm.containsKey(three)); |
913 |
|
|
assertTrue(sm.containsKey(four)); |
914 |
|
|
assertTrue(sm.containsKey(five)); |
915 |
|
|
Iterator i = sm.keySet().iterator(); |
916 |
|
|
Object k; |
917 |
|
|
k = (Integer)(i.next()); |
918 |
|
|
assertEquals(two, k); |
919 |
|
|
k = (Integer)(i.next()); |
920 |
|
|
assertEquals(three, k); |
921 |
|
|
k = (Integer)(i.next()); |
922 |
|
|
assertEquals(four, k); |
923 |
|
|
k = (Integer)(i.next()); |
924 |
|
|
assertEquals(five, k); |
925 |
|
|
assertFalse(i.hasNext()); |
926 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
927 |
|
|
k = (Integer)(r.next()); |
928 |
|
|
assertEquals(five, k); |
929 |
|
|
k = (Integer)(r.next()); |
930 |
|
|
assertEquals(four, k); |
931 |
|
|
k = (Integer)(r.next()); |
932 |
|
|
assertEquals(three, k); |
933 |
|
|
k = (Integer)(r.next()); |
934 |
|
|
assertEquals(two, k); |
935 |
|
|
assertFalse(r.hasNext()); |
936 |
|
|
|
937 |
|
|
Iterator ei = sm.entrySet().iterator(); |
938 |
|
|
Map.Entry e; |
939 |
|
|
e = (Map.Entry)(ei.next()); |
940 |
|
|
assertEquals(two, e.getKey()); |
941 |
|
|
assertEquals("B", e.getValue()); |
942 |
|
|
e = (Map.Entry)(ei.next()); |
943 |
|
|
assertEquals(three, e.getKey()); |
944 |
|
|
assertEquals("C", e.getValue()); |
945 |
|
|
e = (Map.Entry)(ei.next()); |
946 |
|
|
assertEquals(four, e.getKey()); |
947 |
|
|
assertEquals("D", e.getValue()); |
948 |
|
|
e = (Map.Entry)(ei.next()); |
949 |
|
|
assertEquals(five, e.getKey()); |
950 |
|
|
assertEquals("E", e.getValue()); |
951 |
|
|
assertFalse(i.hasNext()); |
952 |
|
|
|
953 |
dl |
1.7 |
NavigableMap ssm = sm.tailMap(four, true); |
954 |
dl |
1.1 |
assertEquals(four, ssm.firstKey()); |
955 |
|
|
assertEquals(five, ssm.lastKey()); |
956 |
jsr166 |
1.15 |
assertEquals("D", ssm.remove(four)); |
957 |
dl |
1.1 |
assertEquals(1, ssm.size()); |
958 |
|
|
assertEquals(3, sm.size()); |
959 |
|
|
assertEquals(4, map.size()); |
960 |
|
|
} |
961 |
dl |
1.6 |
|
962 |
|
|
Random rnd = new Random(666); |
963 |
|
|
BitSet bs; |
964 |
|
|
|
965 |
|
|
/** |
966 |
|
|
* Submaps of submaps subdivide correctly |
967 |
|
|
*/ |
968 |
jsr166 |
1.14 |
public void testRecursiveSubMaps() throws Exception { |
969 |
jsr166 |
1.21 |
int mapSize = expensiveTests ? 1000 : 100; |
970 |
jsr166 |
1.12 |
Class cl = ConcurrentSkipListMap.class; |
971 |
dl |
1.6 |
NavigableMap<Integer, Integer> map = newMap(cl); |
972 |
|
|
bs = new BitSet(mapSize); |
973 |
|
|
|
974 |
|
|
populate(map, mapSize); |
975 |
|
|
check(map, 0, mapSize - 1, true); |
976 |
|
|
check(map.descendingMap(), 0, mapSize - 1, false); |
977 |
|
|
|
978 |
|
|
mutateMap(map, 0, mapSize - 1); |
979 |
|
|
check(map, 0, mapSize - 1, true); |
980 |
|
|
check(map.descendingMap(), 0, mapSize - 1, false); |
981 |
|
|
|
982 |
dl |
1.7 |
bashSubMap(map.subMap(0, true, mapSize, false), |
983 |
dl |
1.6 |
0, mapSize - 1, true); |
984 |
|
|
} |
985 |
|
|
|
986 |
jsr166 |
1.14 |
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
987 |
|
|
NavigableMap<Integer, Integer> result = |
988 |
|
|
(NavigableMap<Integer, Integer>) cl.newInstance(); |
989 |
jsr166 |
1.27 |
assertEquals(0, result.size()); |
990 |
dl |
1.6 |
assertFalse(result.keySet().iterator().hasNext()); |
991 |
|
|
return result; |
992 |
|
|
} |
993 |
|
|
|
994 |
|
|
void populate(NavigableMap<Integer, Integer> map, int limit) { |
995 |
|
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
996 |
|
|
int key = rnd.nextInt(limit); |
997 |
|
|
put(map, key); |
998 |
|
|
} |
999 |
|
|
} |
1000 |
|
|
|
1001 |
|
|
void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) { |
1002 |
|
|
int size = map.size(); |
1003 |
|
|
int rangeSize = max - min + 1; |
1004 |
|
|
|
1005 |
|
|
// Remove a bunch of entries directly |
1006 |
|
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
1007 |
|
|
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
1008 |
|
|
} |
1009 |
|
|
|
1010 |
|
|
// Remove a bunch of entries with iterator |
1011 |
jsr166 |
1.10 |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
1012 |
dl |
1.6 |
if (rnd.nextBoolean()) { |
1013 |
|
|
bs.clear(it.next()); |
1014 |
|
|
it.remove(); |
1015 |
|
|
} |
1016 |
|
|
} |
1017 |
|
|
|
1018 |
|
|
// Add entries till we're back to original size |
1019 |
|
|
while (map.size() < size) { |
1020 |
|
|
int key = min + rnd.nextInt(rangeSize); |
1021 |
|
|
assertTrue(key >= min && key<= max); |
1022 |
|
|
put(map, key); |
1023 |
|
|
} |
1024 |
|
|
} |
1025 |
|
|
|
1026 |
|
|
void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) { |
1027 |
|
|
int size = map.size(); |
1028 |
|
|
int rangeSize = max - min + 1; |
1029 |
|
|
|
1030 |
|
|
// Remove a bunch of entries directly |
1031 |
|
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
1032 |
|
|
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
1033 |
|
|
} |
1034 |
|
|
|
1035 |
|
|
// Remove a bunch of entries with iterator |
1036 |
jsr166 |
1.10 |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
1037 |
dl |
1.6 |
if (rnd.nextBoolean()) { |
1038 |
|
|
bs.clear(it.next()); |
1039 |
|
|
it.remove(); |
1040 |
|
|
} |
1041 |
|
|
} |
1042 |
|
|
|
1043 |
|
|
// Add entries till we're back to original size |
1044 |
|
|
while (map.size() < size) { |
1045 |
|
|
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
1046 |
|
|
if (key >= min && key<= max) { |
1047 |
|
|
put(map, key); |
1048 |
|
|
} else { |
1049 |
|
|
try { |
1050 |
|
|
map.put(key, 2 * key); |
1051 |
jsr166 |
1.14 |
shouldThrow(); |
1052 |
|
|
} catch (IllegalArgumentException success) {} |
1053 |
dl |
1.6 |
} |
1054 |
|
|
} |
1055 |
|
|
} |
1056 |
|
|
|
1057 |
|
|
void put(NavigableMap<Integer, Integer> map, int key) { |
1058 |
|
|
if (map.put(key, 2 * key) == null) |
1059 |
|
|
bs.set(key); |
1060 |
|
|
} |
1061 |
|
|
|
1062 |
|
|
void remove(NavigableMap<Integer, Integer> map, int key) { |
1063 |
|
|
if (map.remove(key) != null) |
1064 |
|
|
bs.clear(key); |
1065 |
|
|
} |
1066 |
|
|
|
1067 |
|
|
void bashSubMap(NavigableMap<Integer, Integer> map, |
1068 |
|
|
int min, int max, boolean ascending) { |
1069 |
|
|
check(map, min, max, ascending); |
1070 |
|
|
check(map.descendingMap(), min, max, !ascending); |
1071 |
|
|
|
1072 |
|
|
mutateSubMap(map, min, max); |
1073 |
|
|
check(map, min, max, ascending); |
1074 |
|
|
check(map.descendingMap(), min, max, !ascending); |
1075 |
|
|
|
1076 |
|
|
// Recurse |
1077 |
|
|
if (max - min < 2) |
1078 |
|
|
return; |
1079 |
|
|
int midPoint = (min + max) / 2; |
1080 |
|
|
|
1081 |
|
|
// headMap - pick direction and endpoint inclusion randomly |
1082 |
|
|
boolean incl = rnd.nextBoolean(); |
1083 |
dl |
1.7 |
NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl); |
1084 |
dl |
1.6 |
if (ascending) { |
1085 |
|
|
if (rnd.nextBoolean()) |
1086 |
|
|
bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true); |
1087 |
|
|
else |
1088 |
|
|
bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
1089 |
|
|
false); |
1090 |
|
|
} else { |
1091 |
|
|
if (rnd.nextBoolean()) |
1092 |
|
|
bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false); |
1093 |
|
|
else |
1094 |
|
|
bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
1095 |
|
|
true); |
1096 |
|
|
} |
1097 |
|
|
|
1098 |
|
|
// tailMap - pick direction and endpoint inclusion randomly |
1099 |
|
|
incl = rnd.nextBoolean(); |
1100 |
dl |
1.7 |
NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl); |
1101 |
dl |
1.6 |
if (ascending) { |
1102 |
|
|
if (rnd.nextBoolean()) |
1103 |
|
|
bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true); |
1104 |
|
|
else |
1105 |
|
|
bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
1106 |
|
|
false); |
1107 |
|
|
} else { |
1108 |
|
|
if (rnd.nextBoolean()) { |
1109 |
|
|
bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false); |
1110 |
|
|
} else { |
1111 |
|
|
bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
1112 |
|
|
true); |
1113 |
|
|
} |
1114 |
|
|
} |
1115 |
|
|
|
1116 |
|
|
// subMap - pick direction and endpoint inclusion randomly |
1117 |
|
|
int rangeSize = max - min + 1; |
1118 |
|
|
int[] endpoints = new int[2]; |
1119 |
|
|
endpoints[0] = min + rnd.nextInt(rangeSize); |
1120 |
|
|
endpoints[1] = min + rnd.nextInt(rangeSize); |
1121 |
|
|
Arrays.sort(endpoints); |
1122 |
|
|
boolean lowIncl = rnd.nextBoolean(); |
1123 |
|
|
boolean highIncl = rnd.nextBoolean(); |
1124 |
|
|
if (ascending) { |
1125 |
dl |
1.7 |
NavigableMap<Integer,Integer> sm = map.subMap( |
1126 |
dl |
1.6 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
1127 |
|
|
if (rnd.nextBoolean()) |
1128 |
|
|
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
1129 |
|
|
endpoints[1] - (highIncl ? 0 : 1), true); |
1130 |
|
|
else |
1131 |
|
|
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
1132 |
|
|
endpoints[1] - (highIncl ? 0 : 1), false); |
1133 |
|
|
} else { |
1134 |
dl |
1.7 |
NavigableMap<Integer,Integer> sm = map.subMap( |
1135 |
dl |
1.6 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
1136 |
|
|
if (rnd.nextBoolean()) |
1137 |
|
|
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
1138 |
|
|
endpoints[1] - (highIncl ? 0 : 1), false); |
1139 |
|
|
else |
1140 |
|
|
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
1141 |
|
|
endpoints[1] - (highIncl ? 0 : 1), true); |
1142 |
|
|
} |
1143 |
|
|
} |
1144 |
|
|
|
1145 |
|
|
/** |
1146 |
|
|
* min and max are both inclusive. If max < min, interval is empty. |
1147 |
|
|
*/ |
1148 |
|
|
void check(NavigableMap<Integer, Integer> map, |
1149 |
|
|
final int min, final int max, final boolean ascending) { |
1150 |
jsr166 |
1.23 |
class ReferenceSet { |
1151 |
dl |
1.6 |
int lower(int key) { |
1152 |
|
|
return ascending ? lowerAscending(key) : higherAscending(key); |
1153 |
|
|
} |
1154 |
|
|
int floor(int key) { |
1155 |
|
|
return ascending ? floorAscending(key) : ceilingAscending(key); |
1156 |
|
|
} |
1157 |
|
|
int ceiling(int key) { |
1158 |
|
|
return ascending ? ceilingAscending(key) : floorAscending(key); |
1159 |
|
|
} |
1160 |
|
|
int higher(int key) { |
1161 |
|
|
return ascending ? higherAscending(key) : lowerAscending(key); |
1162 |
|
|
} |
1163 |
|
|
int first() { |
1164 |
|
|
return ascending ? firstAscending() : lastAscending(); |
1165 |
|
|
} |
1166 |
|
|
int last() { |
1167 |
|
|
return ascending ? lastAscending() : firstAscending(); |
1168 |
|
|
} |
1169 |
|
|
int lowerAscending(int key) { |
1170 |
|
|
return floorAscending(key - 1); |
1171 |
|
|
} |
1172 |
|
|
int floorAscending(int key) { |
1173 |
|
|
if (key < min) |
1174 |
|
|
return -1; |
1175 |
|
|
else if (key > max) |
1176 |
|
|
key = max; |
1177 |
|
|
|
1178 |
|
|
// BitSet should support this! Test would run much faster |
1179 |
|
|
while (key >= min) { |
1180 |
|
|
if (bs.get(key)) |
1181 |
jsr166 |
1.19 |
return key; |
1182 |
dl |
1.6 |
key--; |
1183 |
|
|
} |
1184 |
|
|
return -1; |
1185 |
|
|
} |
1186 |
|
|
int ceilingAscending(int key) { |
1187 |
|
|
if (key < min) |
1188 |
|
|
key = min; |
1189 |
|
|
else if (key > max) |
1190 |
|
|
return -1; |
1191 |
|
|
int result = bs.nextSetBit(key); |
1192 |
|
|
return result > max ? -1 : result; |
1193 |
|
|
} |
1194 |
|
|
int higherAscending(int key) { |
1195 |
|
|
return ceilingAscending(key + 1); |
1196 |
|
|
} |
1197 |
|
|
private int firstAscending() { |
1198 |
|
|
int result = ceilingAscending(min); |
1199 |
|
|
return result > max ? -1 : result; |
1200 |
|
|
} |
1201 |
|
|
private int lastAscending() { |
1202 |
|
|
int result = floorAscending(max); |
1203 |
|
|
return result < min ? -1 : result; |
1204 |
|
|
} |
1205 |
|
|
} |
1206 |
|
|
ReferenceSet rs = new ReferenceSet(); |
1207 |
|
|
|
1208 |
|
|
// Test contents using containsKey |
1209 |
|
|
int size = 0; |
1210 |
|
|
for (int i = min; i <= max; i++) { |
1211 |
|
|
boolean bsContainsI = bs.get(i); |
1212 |
|
|
assertEquals(bsContainsI, map.containsKey(i)); |
1213 |
|
|
if (bsContainsI) |
1214 |
|
|
size++; |
1215 |
|
|
} |
1216 |
jsr166 |
1.28 |
assertEquals(size, map.size()); |
1217 |
dl |
1.6 |
|
1218 |
|
|
// Test contents using contains keySet iterator |
1219 |
|
|
int size2 = 0; |
1220 |
|
|
int previousKey = -1; |
1221 |
|
|
for (int key : map.keySet()) { |
1222 |
|
|
assertTrue(bs.get(key)); |
1223 |
|
|
size2++; |
1224 |
|
|
assertTrue(previousKey < 0 || |
1225 |
|
|
(ascending ? key - previousKey > 0 : key - previousKey < 0)); |
1226 |
|
|
previousKey = key; |
1227 |
|
|
} |
1228 |
|
|
assertEquals(size2, size); |
1229 |
|
|
|
1230 |
|
|
// Test navigation ops |
1231 |
|
|
for (int key = min - 1; key <= max + 1; key++) { |
1232 |
|
|
assertEq(map.lowerKey(key), rs.lower(key)); |
1233 |
|
|
assertEq(map.floorKey(key), rs.floor(key)); |
1234 |
|
|
assertEq(map.higherKey(key), rs.higher(key)); |
1235 |
|
|
assertEq(map.ceilingKey(key), rs.ceiling(key)); |
1236 |
|
|
} |
1237 |
|
|
|
1238 |
|
|
// Test extrema |
1239 |
|
|
if (map.size() != 0) { |
1240 |
|
|
assertEq(map.firstKey(), rs.first()); |
1241 |
|
|
assertEq(map.lastKey(), rs.last()); |
1242 |
|
|
} else { |
1243 |
|
|
assertEq(rs.first(), -1); |
1244 |
|
|
assertEq(rs.last(), -1); |
1245 |
|
|
try { |
1246 |
|
|
map.firstKey(); |
1247 |
jsr166 |
1.14 |
shouldThrow(); |
1248 |
|
|
} catch (NoSuchElementException success) {} |
1249 |
dl |
1.6 |
try { |
1250 |
|
|
map.lastKey(); |
1251 |
jsr166 |
1.14 |
shouldThrow(); |
1252 |
|
|
} catch (NoSuchElementException success) {} |
1253 |
dl |
1.6 |
} |
1254 |
|
|
} |
1255 |
|
|
|
1256 |
|
|
static void assertEq(Integer i, int j) { |
1257 |
|
|
if (i == null) |
1258 |
|
|
assertEquals(j, -1); |
1259 |
|
|
else |
1260 |
|
|
assertEquals((int) i, j); |
1261 |
|
|
} |
1262 |
|
|
|
1263 |
|
|
static boolean eq(Integer i, int j) { |
1264 |
|
|
return i == null ? j == -1 : i == j; |
1265 |
|
|
} |
1266 |
jsr166 |
1.9 |
|
1267 |
dl |
1.1 |
} |