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 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
|
|
import java.util.*; |
9 |
|
|
import java.util.concurrent.*; |
10 |
|
|
import java.io.*; |
11 |
|
|
|
12 |
|
|
public class ConcurrentSkipListMapTest extends JSR166TestCase { |
13 |
|
|
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(ConcurrentSkipListMapTest.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
|
|
*/ |
23 |
|
|
private static ConcurrentSkipListMap map5() { |
24 |
|
|
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
25 |
|
|
assertTrue(map.isEmpty()); |
26 |
|
|
map.put(one, "A"); |
27 |
|
|
map.put(five, "E"); |
28 |
|
|
map.put(three, "C"); |
29 |
|
|
map.put(two, "B"); |
30 |
|
|
map.put(four, "D"); |
31 |
|
|
assertFalse(map.isEmpty()); |
32 |
|
|
assertEquals(5, map.size()); |
33 |
|
|
return map; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* clear removes all pairs |
38 |
|
|
*/ |
39 |
|
|
public void testClear() { |
40 |
|
|
ConcurrentSkipListMap map = map5(); |
41 |
|
|
map.clear(); |
42 |
|
|
assertEquals(map.size(), 0); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* |
47 |
|
|
*/ |
48 |
|
|
public void testConstructFromSorted() { |
49 |
|
|
ConcurrentSkipListMap map = map5(); |
50 |
|
|
ConcurrentSkipListMap map2 = new ConcurrentSkipListMap(map); |
51 |
|
|
assertEquals(map, map2); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* Maps with same contents are equal |
56 |
|
|
*/ |
57 |
|
|
public void testEquals() { |
58 |
|
|
ConcurrentSkipListMap map1 = map5(); |
59 |
|
|
ConcurrentSkipListMap map2 = map5(); |
60 |
|
|
assertEquals(map1, map2); |
61 |
|
|
assertEquals(map2, map1); |
62 |
|
|
map1.clear(); |
63 |
|
|
assertFalse(map1.equals(map2)); |
64 |
|
|
assertFalse(map2.equals(map1)); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* containsKey returns true for contained key |
69 |
|
|
*/ |
70 |
|
|
public void testContainsKey() { |
71 |
|
|
ConcurrentSkipListMap map = map5(); |
72 |
|
|
assertTrue(map.containsKey(one)); |
73 |
|
|
assertFalse(map.containsKey(zero)); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
* containsValue returns true for held values |
78 |
|
|
*/ |
79 |
|
|
public void testContainsValue() { |
80 |
|
|
ConcurrentSkipListMap map = map5(); |
81 |
|
|
assertTrue(map.containsValue("A")); |
82 |
|
|
assertFalse(map.containsValue("Z")); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* get returns the correct element at the given key, |
87 |
|
|
* or null if not present |
88 |
|
|
*/ |
89 |
|
|
public void testGet() { |
90 |
|
|
ConcurrentSkipListMap map = map5(); |
91 |
|
|
assertEquals("A", (String)map.get(one)); |
92 |
|
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
93 |
|
|
assertNull(empty.get(one)); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* isEmpty is true of empty map and false for non-empty |
98 |
|
|
*/ |
99 |
|
|
public void testIsEmpty() { |
100 |
|
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
101 |
|
|
ConcurrentSkipListMap map = map5(); |
102 |
|
|
assertTrue(empty.isEmpty()); |
103 |
|
|
assertFalse(map.isEmpty()); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
/** |
107 |
|
|
* firstKey returns first key |
108 |
|
|
*/ |
109 |
|
|
public void testFirstKey() { |
110 |
|
|
ConcurrentSkipListMap map = map5(); |
111 |
|
|
assertEquals(one, map.firstKey()); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* lastKey returns last key |
116 |
|
|
*/ |
117 |
|
|
public void testLastKey() { |
118 |
|
|
ConcurrentSkipListMap map = map5(); |
119 |
|
|
assertEquals(five, map.lastKey()); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
/** |
124 |
|
|
* keySet.toArray returns contains all keys |
125 |
|
|
*/ |
126 |
|
|
public void testKeySetToArray() { |
127 |
|
|
ConcurrentSkipListMap map = map5(); |
128 |
|
|
Set s = map.keySet(); |
129 |
|
|
Object[] ar = s.toArray(); |
130 |
|
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
131 |
|
|
assertEquals(5, ar.length); |
132 |
|
|
ar[0] = m10; |
133 |
|
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
|
|
* descendingkeySet.toArray returns contains all keys |
138 |
|
|
*/ |
139 |
|
|
public void testDescendingKeySetToArray() { |
140 |
|
|
ConcurrentSkipListMap map = map5(); |
141 |
|
|
Set s = map.descendingKeySet(); |
142 |
|
|
Object[] ar = s.toArray(); |
143 |
|
|
assertEquals(5, ar.length); |
144 |
|
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
145 |
|
|
ar[0] = m10; |
146 |
|
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
/** |
150 |
|
|
* keySet returns a Set containing all the keys |
151 |
|
|
*/ |
152 |
|
|
public void testKeySet() { |
153 |
|
|
ConcurrentSkipListMap map = map5(); |
154 |
|
|
Set s = map.keySet(); |
155 |
|
|
assertEquals(5, s.size()); |
156 |
|
|
assertTrue(s.contains(one)); |
157 |
|
|
assertTrue(s.contains(two)); |
158 |
|
|
assertTrue(s.contains(three)); |
159 |
|
|
assertTrue(s.contains(four)); |
160 |
|
|
assertTrue(s.contains(five)); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* keySet is ordered |
165 |
|
|
*/ |
166 |
|
|
public void testKeySetOrder() { |
167 |
|
|
ConcurrentSkipListMap map = map5(); |
168 |
|
|
Set s = map.keySet(); |
169 |
|
|
Iterator i = s.iterator(); |
170 |
|
|
Integer last = (Integer)i.next(); |
171 |
|
|
assertEquals(last, one); |
172 |
|
|
while (i.hasNext()) { |
173 |
|
|
Integer k = (Integer)i.next(); |
174 |
|
|
assertTrue(last.compareTo(k) < 0); |
175 |
|
|
last = k; |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
|
|
* descendingKeySet is ordered |
181 |
|
|
*/ |
182 |
|
|
public void testDescendingKeySetOrder() { |
183 |
|
|
ConcurrentSkipListMap map = map5(); |
184 |
|
|
Set s = map.descendingKeySet(); |
185 |
|
|
Iterator i = s.iterator(); |
186 |
|
|
Integer last = (Integer)i.next(); |
187 |
|
|
assertEquals(last, five); |
188 |
|
|
while (i.hasNext()) { |
189 |
|
|
Integer k = (Integer)i.next(); |
190 |
|
|
assertTrue(last.compareTo(k) > 0); |
191 |
|
|
last = k; |
192 |
|
|
} |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
/** |
196 |
|
|
* values collection contains all values |
197 |
|
|
*/ |
198 |
|
|
public void testValues() { |
199 |
|
|
ConcurrentSkipListMap map = map5(); |
200 |
|
|
Collection s = map.values(); |
201 |
|
|
assertEquals(5, s.size()); |
202 |
|
|
assertTrue(s.contains("A")); |
203 |
|
|
assertTrue(s.contains("B")); |
204 |
|
|
assertTrue(s.contains("C")); |
205 |
|
|
assertTrue(s.contains("D")); |
206 |
|
|
assertTrue(s.contains("E")); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
/** |
210 |
|
|
* entrySet contains all pairs |
211 |
|
|
*/ |
212 |
|
|
public void testEntrySet() { |
213 |
|
|
ConcurrentSkipListMap map = map5(); |
214 |
|
|
Set s = map.entrySet(); |
215 |
|
|
assertEquals(5, s.size()); |
216 |
|
|
Iterator it = s.iterator(); |
217 |
|
|
while (it.hasNext()) { |
218 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
219 |
|
|
assertTrue( |
220 |
|
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
221 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
222 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
223 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
224 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
/** |
229 |
|
|
* descendingEntrySet contains all pairs |
230 |
|
|
*/ |
231 |
|
|
public void testDescendingEntrySet() { |
232 |
|
|
ConcurrentSkipListMap map = map5(); |
233 |
|
|
Set s = map.descendingEntrySet(); |
234 |
|
|
assertEquals(5, s.size()); |
235 |
|
|
Iterator it = s.iterator(); |
236 |
|
|
while (it.hasNext()) { |
237 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
238 |
|
|
assertTrue( |
239 |
|
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
240 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
241 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
242 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
243 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
244 |
|
|
} |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
/** |
248 |
|
|
* entrySet.toArray contains all entries |
249 |
|
|
*/ |
250 |
|
|
public void testEntrySetToArray() { |
251 |
|
|
ConcurrentSkipListMap map = map5(); |
252 |
|
|
Set s = map.entrySet(); |
253 |
|
|
Object[] ar = s.toArray(); |
254 |
|
|
assertEquals(5, ar.length); |
255 |
|
|
for (int i = 0; i < 5; ++i) { |
256 |
|
|
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
257 |
|
|
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
258 |
|
|
} |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
/** |
262 |
|
|
* descendingEntrySet.toArray contains all entries |
263 |
|
|
*/ |
264 |
|
|
public void testDescendingEntrySetToArray() { |
265 |
|
|
ConcurrentSkipListMap map = map5(); |
266 |
|
|
Set s = map.descendingEntrySet(); |
267 |
|
|
Object[] ar = s.toArray(); |
268 |
|
|
assertEquals(5, ar.length); |
269 |
|
|
for (int i = 0; i < 5; ++i) { |
270 |
|
|
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
271 |
|
|
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
272 |
|
|
} |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
/** |
276 |
|
|
* putAll adds all key-value pairs from the given map |
277 |
|
|
*/ |
278 |
|
|
public void testPutAll() { |
279 |
|
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
280 |
|
|
ConcurrentSkipListMap map = map5(); |
281 |
|
|
empty.putAll(map); |
282 |
|
|
assertEquals(5, empty.size()); |
283 |
|
|
assertTrue(empty.containsKey(one)); |
284 |
|
|
assertTrue(empty.containsKey(two)); |
285 |
|
|
assertTrue(empty.containsKey(three)); |
286 |
|
|
assertTrue(empty.containsKey(four)); |
287 |
|
|
assertTrue(empty.containsKey(five)); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
/** |
291 |
|
|
* putIfAbsent works when the given key is not present |
292 |
|
|
*/ |
293 |
|
|
public void testPutIfAbsent() { |
294 |
|
|
ConcurrentSkipListMap map = map5(); |
295 |
|
|
map.putIfAbsent(six, "Z"); |
296 |
|
|
assertTrue(map.containsKey(six)); |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
/** |
300 |
|
|
* putIfAbsent does not add the pair if the key is already present |
301 |
|
|
*/ |
302 |
|
|
public void testPutIfAbsent2() { |
303 |
|
|
ConcurrentSkipListMap map = map5(); |
304 |
|
|
assertEquals("A", map.putIfAbsent(one, "Z")); |
305 |
|
|
} |
306 |
|
|
|
307 |
|
|
/** |
308 |
|
|
* replace fails when the given key is not present |
309 |
|
|
*/ |
310 |
|
|
public void testReplace() { |
311 |
|
|
ConcurrentSkipListMap map = map5(); |
312 |
|
|
assertNull(map.replace(six, "Z")); |
313 |
|
|
assertFalse(map.containsKey(six)); |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
/** |
317 |
|
|
* replace succeeds if the key is already present |
318 |
|
|
*/ |
319 |
|
|
public void testReplace2() { |
320 |
|
|
ConcurrentSkipListMap map = map5(); |
321 |
|
|
assertNotNull(map.replace(one, "Z")); |
322 |
|
|
assertEquals("Z", map.get(one)); |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
|
326 |
|
|
/** |
327 |
|
|
* replace value fails when the given key not mapped to expected value |
328 |
|
|
*/ |
329 |
|
|
public void testReplaceValue() { |
330 |
|
|
ConcurrentSkipListMap map = map5(); |
331 |
|
|
assertEquals("A", map.get(one)); |
332 |
|
|
assertFalse(map.replace(one, "Z", "Z")); |
333 |
|
|
assertEquals("A", map.get(one)); |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
/** |
337 |
|
|
* replace value succeeds when the given key mapped to expected value |
338 |
|
|
*/ |
339 |
|
|
public void testReplaceValue2() { |
340 |
|
|
ConcurrentSkipListMap map = map5(); |
341 |
|
|
assertEquals("A", map.get(one)); |
342 |
|
|
assertTrue(map.replace(one, "A", "Z")); |
343 |
|
|
assertEquals("Z", map.get(one)); |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
|
347 |
|
|
/** |
348 |
|
|
* remove removes the correct key-value pair from the map |
349 |
|
|
*/ |
350 |
|
|
public void testRemove() { |
351 |
|
|
ConcurrentSkipListMap map = map5(); |
352 |
|
|
map.remove(five); |
353 |
|
|
assertEquals(4, map.size()); |
354 |
|
|
assertFalse(map.containsKey(five)); |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
/** |
358 |
|
|
* remove(key,value) removes only if pair present |
359 |
|
|
*/ |
360 |
|
|
public void testRemove2() { |
361 |
|
|
ConcurrentSkipListMap map = map5(); |
362 |
|
|
assertTrue(map.containsKey(five)); |
363 |
|
|
assertEquals("E", map.get(five)); |
364 |
|
|
map.remove(five, "E"); |
365 |
|
|
assertEquals(4, map.size()); |
366 |
|
|
assertFalse(map.containsKey(five)); |
367 |
|
|
map.remove(four, "A"); |
368 |
|
|
assertEquals(4, map.size()); |
369 |
|
|
assertTrue(map.containsKey(four)); |
370 |
|
|
|
371 |
|
|
} |
372 |
|
|
|
373 |
|
|
/** |
374 |
|
|
* lowerEntry returns preceding entry. |
375 |
|
|
*/ |
376 |
|
|
public void testLowerEntry() { |
377 |
|
|
ConcurrentSkipListMap map = map5(); |
378 |
|
|
Map.Entry e1 = map.lowerEntry(three); |
379 |
|
|
assertEquals(two, e1.getKey()); |
380 |
|
|
|
381 |
|
|
Map.Entry e2 = map.lowerEntry(six); |
382 |
|
|
assertEquals(five, e2.getKey()); |
383 |
|
|
|
384 |
|
|
Map.Entry e3 = map.lowerEntry(one); |
385 |
|
|
assertNull(e3); |
386 |
|
|
|
387 |
|
|
Map.Entry e4 = map.lowerEntry(zero); |
388 |
|
|
assertNull(e4); |
389 |
|
|
|
390 |
|
|
} |
391 |
|
|
|
392 |
|
|
/** |
393 |
|
|
* higherEntry returns next entry. |
394 |
|
|
*/ |
395 |
|
|
public void testHigherEntry() { |
396 |
|
|
ConcurrentSkipListMap map = map5(); |
397 |
|
|
Map.Entry e1 = map.higherEntry(three); |
398 |
|
|
assertEquals(four, e1.getKey()); |
399 |
|
|
|
400 |
|
|
Map.Entry e2 = map.higherEntry(zero); |
401 |
|
|
assertEquals(one, e2.getKey()); |
402 |
|
|
|
403 |
|
|
Map.Entry e3 = map.higherEntry(five); |
404 |
|
|
assertNull(e3); |
405 |
|
|
|
406 |
|
|
Map.Entry e4 = map.higherEntry(six); |
407 |
|
|
assertNull(e4); |
408 |
|
|
|
409 |
|
|
} |
410 |
|
|
|
411 |
|
|
/** |
412 |
|
|
* floorEntry returns preceding entry. |
413 |
|
|
*/ |
414 |
|
|
public void testFloorEntry() { |
415 |
|
|
ConcurrentSkipListMap map = map5(); |
416 |
|
|
Map.Entry e1 = map.floorEntry(three); |
417 |
|
|
assertEquals(three, e1.getKey()); |
418 |
|
|
|
419 |
|
|
Map.Entry e2 = map.floorEntry(six); |
420 |
|
|
assertEquals(five, e2.getKey()); |
421 |
|
|
|
422 |
|
|
Map.Entry e3 = map.floorEntry(one); |
423 |
|
|
assertEquals(one, e3.getKey()); |
424 |
|
|
|
425 |
|
|
Map.Entry e4 = map.floorEntry(zero); |
426 |
|
|
assertNull(e4); |
427 |
|
|
|
428 |
|
|
} |
429 |
|
|
|
430 |
|
|
/** |
431 |
|
|
* ceilingEntry returns next entry. |
432 |
|
|
*/ |
433 |
|
|
public void testCeilingEntry() { |
434 |
|
|
ConcurrentSkipListMap map = map5(); |
435 |
|
|
Map.Entry e1 = map.ceilingEntry(three); |
436 |
|
|
assertEquals(three, e1.getKey()); |
437 |
|
|
|
438 |
|
|
Map.Entry e2 = map.ceilingEntry(zero); |
439 |
|
|
assertEquals(one, e2.getKey()); |
440 |
|
|
|
441 |
|
|
Map.Entry e3 = map.ceilingEntry(five); |
442 |
|
|
assertEquals(five, e3.getKey()); |
443 |
|
|
|
444 |
|
|
Map.Entry e4 = map.ceilingEntry(six); |
445 |
|
|
assertNull(e4); |
446 |
|
|
|
447 |
|
|
} |
448 |
|
|
|
449 |
|
|
|
450 |
|
|
/** |
451 |
|
|
* lowerKey returns preceding element |
452 |
|
|
*/ |
453 |
|
|
public void testLowerKey() { |
454 |
|
|
ConcurrentSkipListMap q = map5(); |
455 |
|
|
Object e1 = q.lowerKey(three); |
456 |
|
|
assertEquals(two, e1); |
457 |
|
|
|
458 |
|
|
Object e2 = q.lowerKey(six); |
459 |
|
|
assertEquals(five, e2); |
460 |
|
|
|
461 |
|
|
Object e3 = q.lowerKey(one); |
462 |
|
|
assertNull(e3); |
463 |
|
|
|
464 |
|
|
Object e4 = q.lowerKey(zero); |
465 |
|
|
assertNull(e4); |
466 |
|
|
|
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
/** |
470 |
|
|
* higherKey returns next element |
471 |
|
|
*/ |
472 |
|
|
public void testHigherKey() { |
473 |
|
|
ConcurrentSkipListMap q = map5(); |
474 |
|
|
Object e1 = q.higherKey(three); |
475 |
|
|
assertEquals(four, e1); |
476 |
|
|
|
477 |
|
|
Object e2 = q.higherKey(zero); |
478 |
|
|
assertEquals(one, e2); |
479 |
|
|
|
480 |
|
|
Object e3 = q.higherKey(five); |
481 |
|
|
assertNull(e3); |
482 |
|
|
|
483 |
|
|
Object e4 = q.higherKey(six); |
484 |
|
|
assertNull(e4); |
485 |
|
|
|
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
/** |
489 |
|
|
* floorKey returns preceding element |
490 |
|
|
*/ |
491 |
|
|
public void testFloorKey() { |
492 |
|
|
ConcurrentSkipListMap q = map5(); |
493 |
|
|
Object e1 = q.floorKey(three); |
494 |
|
|
assertEquals(three, e1); |
495 |
|
|
|
496 |
|
|
Object e2 = q.floorKey(six); |
497 |
|
|
assertEquals(five, e2); |
498 |
|
|
|
499 |
|
|
Object e3 = q.floorKey(one); |
500 |
|
|
assertEquals(one, e3); |
501 |
|
|
|
502 |
|
|
Object e4 = q.floorKey(zero); |
503 |
|
|
assertNull(e4); |
504 |
|
|
|
505 |
|
|
} |
506 |
|
|
|
507 |
|
|
/** |
508 |
|
|
* ceilingKey returns next element |
509 |
|
|
*/ |
510 |
|
|
public void testCeilingKey() { |
511 |
|
|
ConcurrentSkipListMap q = map5(); |
512 |
|
|
Object e1 = q.ceilingKey(three); |
513 |
|
|
assertEquals(three, e1); |
514 |
|
|
|
515 |
|
|
Object e2 = q.ceilingKey(zero); |
516 |
|
|
assertEquals(one, e2); |
517 |
|
|
|
518 |
|
|
Object e3 = q.ceilingKey(five); |
519 |
|
|
assertEquals(five, e3); |
520 |
|
|
|
521 |
|
|
Object e4 = q.ceilingKey(six); |
522 |
|
|
assertNull(e4); |
523 |
|
|
|
524 |
|
|
} |
525 |
|
|
|
526 |
|
|
/** |
527 |
|
|
* pollFirstEntry returns entries in order |
528 |
|
|
*/ |
529 |
|
|
public void testPollFirstEntry() { |
530 |
|
|
ConcurrentSkipListMap map = map5(); |
531 |
|
|
Map.Entry e = map.pollFirstEntry(); |
532 |
|
|
assertEquals(one, e.getKey()); |
533 |
|
|
assertEquals("A", e.getValue()); |
534 |
|
|
e = map.pollFirstEntry(); |
535 |
|
|
assertEquals(two, e.getKey()); |
536 |
|
|
map.put(one, "A"); |
537 |
|
|
e = map.pollFirstEntry(); |
538 |
|
|
assertEquals(one, e.getKey()); |
539 |
|
|
assertEquals("A", e.getValue()); |
540 |
|
|
e = map.pollFirstEntry(); |
541 |
|
|
assertEquals(three, e.getKey()); |
542 |
|
|
map.remove(four); |
543 |
|
|
e = map.pollFirstEntry(); |
544 |
|
|
assertEquals(five, e.getKey()); |
545 |
|
|
try { |
546 |
|
|
e.setValue("A"); |
547 |
|
|
shouldThrow(); |
548 |
|
|
} catch (Exception ok) { |
549 |
|
|
} |
550 |
|
|
e = map.pollFirstEntry(); |
551 |
|
|
assertNull(e); |
552 |
|
|
} |
553 |
|
|
|
554 |
|
|
/** |
555 |
|
|
* pollLastEntry returns entries in order |
556 |
|
|
*/ |
557 |
|
|
public void testPollLastEntry() { |
558 |
|
|
ConcurrentSkipListMap map = map5(); |
559 |
|
|
Map.Entry e = map.pollLastEntry(); |
560 |
|
|
assertEquals(five, e.getKey()); |
561 |
|
|
assertEquals("E", e.getValue()); |
562 |
|
|
e = map.pollLastEntry(); |
563 |
|
|
assertEquals(four, e.getKey()); |
564 |
|
|
map.put(five, "E"); |
565 |
|
|
e = map.pollLastEntry(); |
566 |
|
|
assertEquals(five, e.getKey()); |
567 |
|
|
assertEquals("E", e.getValue()); |
568 |
|
|
e = map.pollLastEntry(); |
569 |
|
|
assertEquals(three, e.getKey()); |
570 |
|
|
map.remove(two); |
571 |
|
|
e = map.pollLastEntry(); |
572 |
|
|
assertEquals(one, e.getKey()); |
573 |
|
|
try { |
574 |
|
|
e.setValue("E"); |
575 |
|
|
shouldThrow(); |
576 |
|
|
} catch (Exception ok) { |
577 |
|
|
} |
578 |
|
|
e = map.pollLastEntry(); |
579 |
|
|
assertNull(e); |
580 |
|
|
} |
581 |
|
|
|
582 |
|
|
/** |
583 |
|
|
* size returns the correct values |
584 |
|
|
*/ |
585 |
|
|
public void testSize() { |
586 |
|
|
ConcurrentSkipListMap map = map5(); |
587 |
|
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
588 |
|
|
assertEquals(0, empty.size()); |
589 |
|
|
assertEquals(5, map.size()); |
590 |
|
|
} |
591 |
|
|
|
592 |
|
|
/** |
593 |
|
|
* toString contains toString of elements |
594 |
|
|
*/ |
595 |
|
|
public void testToString() { |
596 |
|
|
ConcurrentSkipListMap map = map5(); |
597 |
|
|
String s = map.toString(); |
598 |
|
|
for (int i = 1; i <= 5; ++i) { |
599 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
600 |
|
|
} |
601 |
|
|
} |
602 |
|
|
|
603 |
|
|
// Exception tests |
604 |
|
|
|
605 |
|
|
/** |
606 |
|
|
* get(null) of nonempty map throws NPE |
607 |
|
|
*/ |
608 |
|
|
public void testGet_NullPointerException() { |
609 |
|
|
try { |
610 |
|
|
ConcurrentSkipListMap c = map5(); |
611 |
|
|
c.get(null); |
612 |
|
|
shouldThrow(); |
613 |
|
|
} catch(NullPointerException e){} |
614 |
|
|
} |
615 |
|
|
|
616 |
|
|
/** |
617 |
|
|
* containsKey(null) of nonempty map throws NPE |
618 |
|
|
*/ |
619 |
|
|
public void testContainsKey_NullPointerException() { |
620 |
|
|
try { |
621 |
|
|
ConcurrentSkipListMap c = map5(); |
622 |
|
|
c.containsKey(null); |
623 |
|
|
shouldThrow(); |
624 |
|
|
} catch(NullPointerException e){} |
625 |
|
|
} |
626 |
|
|
|
627 |
|
|
/** |
628 |
|
|
* containsValue(null) throws NPE |
629 |
|
|
*/ |
630 |
|
|
public void testContainsValue_NullPointerException() { |
631 |
|
|
try { |
632 |
|
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
633 |
|
|
c.containsValue(null); |
634 |
|
|
shouldThrow(); |
635 |
|
|
} catch(NullPointerException e){} |
636 |
|
|
} |
637 |
|
|
|
638 |
|
|
|
639 |
|
|
/** |
640 |
|
|
* put(null,x) throws NPE |
641 |
|
|
*/ |
642 |
|
|
public void testPut1_NullPointerException() { |
643 |
|
|
try { |
644 |
|
|
ConcurrentSkipListMap c = map5(); |
645 |
|
|
c.put(null, "whatever"); |
646 |
|
|
shouldThrow(); |
647 |
|
|
} catch(NullPointerException e){} |
648 |
|
|
} |
649 |
|
|
|
650 |
|
|
/** |
651 |
|
|
* putIfAbsent(null, x) throws NPE |
652 |
|
|
*/ |
653 |
|
|
public void testPutIfAbsent1_NullPointerException() { |
654 |
|
|
try { |
655 |
|
|
ConcurrentSkipListMap c = map5(); |
656 |
|
|
c.putIfAbsent(null, "whatever"); |
657 |
|
|
shouldThrow(); |
658 |
|
|
} catch(NullPointerException e){} |
659 |
|
|
} |
660 |
|
|
|
661 |
|
|
/** |
662 |
|
|
* replace(null, x) throws NPE |
663 |
|
|
*/ |
664 |
|
|
public void testReplace_NullPointerException() { |
665 |
|
|
try { |
666 |
|
|
ConcurrentSkipListMap c = map5(); |
667 |
|
|
c.replace(null, "whatever"); |
668 |
|
|
shouldThrow(); |
669 |
|
|
} catch(NullPointerException e){} |
670 |
|
|
} |
671 |
|
|
|
672 |
|
|
/** |
673 |
|
|
* replace(null, x, y) throws NPE |
674 |
|
|
*/ |
675 |
|
|
public void testReplaceValue_NullPointerException() { |
676 |
|
|
try { |
677 |
|
|
ConcurrentSkipListMap c = map5(); |
678 |
|
|
c.replace(null, one, "whatever"); |
679 |
|
|
shouldThrow(); |
680 |
|
|
} catch(NullPointerException e){} |
681 |
|
|
} |
682 |
|
|
|
683 |
|
|
/** |
684 |
|
|
* remove(null) throws NPE |
685 |
|
|
*/ |
686 |
|
|
public void testRemove1_NullPointerException() { |
687 |
|
|
try { |
688 |
|
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
689 |
|
|
c.put("sadsdf", "asdads"); |
690 |
|
|
c.remove(null); |
691 |
|
|
shouldThrow(); |
692 |
|
|
} catch(NullPointerException e){} |
693 |
|
|
} |
694 |
|
|
|
695 |
|
|
/** |
696 |
|
|
* remove(null, x) throws NPE |
697 |
|
|
*/ |
698 |
|
|
public void testRemove2_NullPointerException() { |
699 |
|
|
try { |
700 |
|
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
701 |
|
|
c.put("sadsdf", "asdads"); |
702 |
|
|
c.remove(null, "whatever"); |
703 |
|
|
shouldThrow(); |
704 |
|
|
} catch(NullPointerException e){} |
705 |
|
|
} |
706 |
|
|
|
707 |
|
|
/** |
708 |
dl |
1.3 |
* remove(x, null) returns false |
709 |
|
|
*/ |
710 |
|
|
public void testRemove3() { |
711 |
|
|
try { |
712 |
|
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
713 |
|
|
c.put("sadsdf", "asdads"); |
714 |
|
|
assertFalse(c.remove("sadsdf", null)); |
715 |
|
|
} catch(NullPointerException e){ |
716 |
|
|
fail(); |
717 |
|
|
} |
718 |
|
|
} |
719 |
|
|
|
720 |
|
|
/** |
721 |
dl |
1.1 |
* A deserialized map equals original |
722 |
|
|
*/ |
723 |
|
|
public void testSerialization() { |
724 |
|
|
ConcurrentSkipListMap q = map5(); |
725 |
|
|
|
726 |
|
|
try { |
727 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
728 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
729 |
|
|
out.writeObject(q); |
730 |
|
|
out.close(); |
731 |
|
|
|
732 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
733 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
734 |
|
|
ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject(); |
735 |
|
|
assertEquals(q.size(), r.size()); |
736 |
|
|
assertTrue(q.equals(r)); |
737 |
|
|
assertTrue(r.equals(q)); |
738 |
|
|
} catch(Exception e){ |
739 |
|
|
e.printStackTrace(); |
740 |
|
|
unexpectedException(); |
741 |
|
|
} |
742 |
|
|
} |
743 |
|
|
|
744 |
|
|
|
745 |
|
|
|
746 |
|
|
/** |
747 |
|
|
* subMap returns map with keys in requested range |
748 |
|
|
*/ |
749 |
|
|
public void testSubMapContents() { |
750 |
|
|
ConcurrentSkipListMap map = map5(); |
751 |
dl |
1.2 |
NavigableMap sm = map.navigableSubMap(two, four); |
752 |
dl |
1.1 |
assertEquals(two, sm.firstKey()); |
753 |
|
|
assertEquals(three, sm.lastKey()); |
754 |
|
|
assertEquals(2, sm.size()); |
755 |
|
|
assertFalse(sm.containsKey(one)); |
756 |
|
|
assertTrue(sm.containsKey(two)); |
757 |
|
|
assertTrue(sm.containsKey(three)); |
758 |
|
|
assertFalse(sm.containsKey(four)); |
759 |
|
|
assertFalse(sm.containsKey(five)); |
760 |
|
|
Iterator i = sm.keySet().iterator(); |
761 |
|
|
Object k; |
762 |
|
|
k = (Integer)(i.next()); |
763 |
|
|
assertEquals(two, k); |
764 |
|
|
k = (Integer)(i.next()); |
765 |
|
|
assertEquals(three, k); |
766 |
|
|
assertFalse(i.hasNext()); |
767 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
768 |
|
|
k = (Integer)(r.next()); |
769 |
|
|
assertEquals(three, k); |
770 |
|
|
k = (Integer)(r.next()); |
771 |
|
|
assertEquals(two, k); |
772 |
|
|
assertFalse(r.hasNext()); |
773 |
|
|
|
774 |
|
|
Iterator j = sm.keySet().iterator(); |
775 |
|
|
j.next(); |
776 |
|
|
j.remove(); |
777 |
|
|
assertFalse(map.containsKey(two)); |
778 |
|
|
assertEquals(4, map.size()); |
779 |
|
|
assertEquals(1, sm.size()); |
780 |
|
|
assertEquals(three, sm.firstKey()); |
781 |
|
|
assertEquals(three, sm.lastKey()); |
782 |
|
|
assertTrue(sm.remove(three) != null); |
783 |
|
|
assertTrue(sm.isEmpty()); |
784 |
|
|
assertEquals(3, map.size()); |
785 |
|
|
} |
786 |
|
|
|
787 |
|
|
public void testSubMapContents2() { |
788 |
|
|
ConcurrentSkipListMap map = map5(); |
789 |
dl |
1.2 |
NavigableMap sm = map.navigableSubMap(two, three); |
790 |
dl |
1.1 |
assertEquals(1, sm.size()); |
791 |
|
|
assertEquals(two, sm.firstKey()); |
792 |
|
|
assertEquals(two, sm.lastKey()); |
793 |
|
|
assertFalse(sm.containsKey(one)); |
794 |
|
|
assertTrue(sm.containsKey(two)); |
795 |
|
|
assertFalse(sm.containsKey(three)); |
796 |
|
|
assertFalse(sm.containsKey(four)); |
797 |
|
|
assertFalse(sm.containsKey(five)); |
798 |
|
|
Iterator i = sm.keySet().iterator(); |
799 |
|
|
Object k; |
800 |
|
|
k = (Integer)(i.next()); |
801 |
|
|
assertEquals(two, k); |
802 |
|
|
assertFalse(i.hasNext()); |
803 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
804 |
|
|
k = (Integer)(r.next()); |
805 |
|
|
assertEquals(two, k); |
806 |
|
|
assertFalse(r.hasNext()); |
807 |
|
|
|
808 |
|
|
Iterator j = sm.keySet().iterator(); |
809 |
|
|
j.next(); |
810 |
|
|
j.remove(); |
811 |
|
|
assertFalse(map.containsKey(two)); |
812 |
|
|
assertEquals(4, map.size()); |
813 |
|
|
assertEquals(0, sm.size()); |
814 |
|
|
assertTrue(sm.isEmpty()); |
815 |
|
|
assertTrue(sm.remove(three) == null); |
816 |
|
|
assertEquals(4, map.size()); |
817 |
|
|
} |
818 |
|
|
|
819 |
|
|
/** |
820 |
|
|
* headMap returns map with keys in requested range |
821 |
|
|
*/ |
822 |
|
|
public void testHeadMapContents() { |
823 |
|
|
ConcurrentSkipListMap map = map5(); |
824 |
dl |
1.2 |
NavigableMap sm = map.navigableHeadMap(four); |
825 |
dl |
1.1 |
assertTrue(sm.containsKey(one)); |
826 |
|
|
assertTrue(sm.containsKey(two)); |
827 |
|
|
assertTrue(sm.containsKey(three)); |
828 |
|
|
assertFalse(sm.containsKey(four)); |
829 |
|
|
assertFalse(sm.containsKey(five)); |
830 |
|
|
Iterator i = sm.keySet().iterator(); |
831 |
|
|
Object k; |
832 |
|
|
k = (Integer)(i.next()); |
833 |
|
|
assertEquals(one, k); |
834 |
|
|
k = (Integer)(i.next()); |
835 |
|
|
assertEquals(two, k); |
836 |
|
|
k = (Integer)(i.next()); |
837 |
|
|
assertEquals(three, k); |
838 |
|
|
assertFalse(i.hasNext()); |
839 |
|
|
sm.clear(); |
840 |
|
|
assertTrue(sm.isEmpty()); |
841 |
|
|
assertEquals(2, map.size()); |
842 |
|
|
assertEquals(four, map.firstKey()); |
843 |
|
|
} |
844 |
|
|
|
845 |
|
|
/** |
846 |
|
|
* headMap returns map with keys in requested range |
847 |
|
|
*/ |
848 |
|
|
public void testTailMapContents() { |
849 |
|
|
ConcurrentSkipListMap map = map5(); |
850 |
dl |
1.2 |
NavigableMap sm = map.navigableTailMap(two); |
851 |
dl |
1.1 |
assertFalse(sm.containsKey(one)); |
852 |
|
|
assertTrue(sm.containsKey(two)); |
853 |
|
|
assertTrue(sm.containsKey(three)); |
854 |
|
|
assertTrue(sm.containsKey(four)); |
855 |
|
|
assertTrue(sm.containsKey(five)); |
856 |
|
|
Iterator i = sm.keySet().iterator(); |
857 |
|
|
Object k; |
858 |
|
|
k = (Integer)(i.next()); |
859 |
|
|
assertEquals(two, k); |
860 |
|
|
k = (Integer)(i.next()); |
861 |
|
|
assertEquals(three, k); |
862 |
|
|
k = (Integer)(i.next()); |
863 |
|
|
assertEquals(four, k); |
864 |
|
|
k = (Integer)(i.next()); |
865 |
|
|
assertEquals(five, k); |
866 |
|
|
assertFalse(i.hasNext()); |
867 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
868 |
|
|
k = (Integer)(r.next()); |
869 |
|
|
assertEquals(five, k); |
870 |
|
|
k = (Integer)(r.next()); |
871 |
|
|
assertEquals(four, k); |
872 |
|
|
k = (Integer)(r.next()); |
873 |
|
|
assertEquals(three, k); |
874 |
|
|
k = (Integer)(r.next()); |
875 |
|
|
assertEquals(two, k); |
876 |
|
|
assertFalse(r.hasNext()); |
877 |
|
|
|
878 |
|
|
Iterator ei = sm.entrySet().iterator(); |
879 |
|
|
Map.Entry e; |
880 |
|
|
e = (Map.Entry)(ei.next()); |
881 |
|
|
assertEquals(two, e.getKey()); |
882 |
|
|
assertEquals("B", e.getValue()); |
883 |
|
|
e = (Map.Entry)(ei.next()); |
884 |
|
|
assertEquals(three, e.getKey()); |
885 |
|
|
assertEquals("C", e.getValue()); |
886 |
|
|
e = (Map.Entry)(ei.next()); |
887 |
|
|
assertEquals(four, e.getKey()); |
888 |
|
|
assertEquals("D", e.getValue()); |
889 |
|
|
e = (Map.Entry)(ei.next()); |
890 |
|
|
assertEquals(five, e.getKey()); |
891 |
|
|
assertEquals("E", e.getValue()); |
892 |
|
|
assertFalse(i.hasNext()); |
893 |
|
|
|
894 |
dl |
1.2 |
NavigableMap ssm = sm.navigableTailMap(four); |
895 |
dl |
1.1 |
assertEquals(four, ssm.firstKey()); |
896 |
|
|
assertEquals(five, ssm.lastKey()); |
897 |
|
|
assertTrue(ssm.remove(four) != null); |
898 |
|
|
assertEquals(1, ssm.size()); |
899 |
|
|
assertEquals(3, sm.size()); |
900 |
|
|
assertEquals(4, map.size()); |
901 |
|
|
} |
902 |
|
|
|
903 |
|
|
} |