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 |
/** |
197 |
* Values.toArray contains all values |
198 |
*/ |
199 |
public void testValuesToArray() { |
200 |
ConcurrentSkipListMap map = map5(); |
201 |
Collection v = map.values(); |
202 |
Object[] ar = v.toArray(); |
203 |
ArrayList s = new ArrayList(Arrays.asList(ar)); |
204 |
assertEquals(5, ar.length); |
205 |
assertTrue(s.contains("A")); |
206 |
assertTrue(s.contains("B")); |
207 |
assertTrue(s.contains("C")); |
208 |
assertTrue(s.contains("D")); |
209 |
assertTrue(s.contains("E")); |
210 |
} |
211 |
|
212 |
/** |
213 |
* values collection contains all values |
214 |
*/ |
215 |
public void testValues() { |
216 |
ConcurrentSkipListMap map = map5(); |
217 |
Collection s = map.values(); |
218 |
assertEquals(5, s.size()); |
219 |
assertTrue(s.contains("A")); |
220 |
assertTrue(s.contains("B")); |
221 |
assertTrue(s.contains("C")); |
222 |
assertTrue(s.contains("D")); |
223 |
assertTrue(s.contains("E")); |
224 |
} |
225 |
|
226 |
/** |
227 |
* entrySet contains all pairs |
228 |
*/ |
229 |
public void testEntrySet() { |
230 |
ConcurrentSkipListMap map = map5(); |
231 |
Set s = map.entrySet(); |
232 |
assertEquals(5, s.size()); |
233 |
Iterator it = s.iterator(); |
234 |
while (it.hasNext()) { |
235 |
Map.Entry e = (Map.Entry) it.next(); |
236 |
assertTrue( |
237 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
238 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
239 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
240 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
241 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
242 |
} |
243 |
} |
244 |
|
245 |
/** |
246 |
* descendingEntrySet contains all pairs |
247 |
*/ |
248 |
public void testDescendingEntrySet() { |
249 |
ConcurrentSkipListMap map = map5(); |
250 |
Set s = map.descendingEntrySet(); |
251 |
assertEquals(5, s.size()); |
252 |
Iterator it = s.iterator(); |
253 |
while (it.hasNext()) { |
254 |
Map.Entry e = (Map.Entry) it.next(); |
255 |
assertTrue( |
256 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
257 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
258 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
259 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
260 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
261 |
} |
262 |
} |
263 |
|
264 |
/** |
265 |
* entrySet.toArray contains all entries |
266 |
*/ |
267 |
public void testEntrySetToArray() { |
268 |
ConcurrentSkipListMap map = map5(); |
269 |
Set s = map.entrySet(); |
270 |
Object[] ar = s.toArray(); |
271 |
assertEquals(5, ar.length); |
272 |
for (int i = 0; i < 5; ++i) { |
273 |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
274 |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
275 |
} |
276 |
} |
277 |
|
278 |
/** |
279 |
* descendingEntrySet.toArray contains all entries |
280 |
*/ |
281 |
public void testDescendingEntrySetToArray() { |
282 |
ConcurrentSkipListMap map = map5(); |
283 |
Set s = map.descendingEntrySet(); |
284 |
Object[] ar = s.toArray(); |
285 |
assertEquals(5, ar.length); |
286 |
for (int i = 0; i < 5; ++i) { |
287 |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
288 |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
289 |
} |
290 |
} |
291 |
|
292 |
/** |
293 |
* putAll adds all key-value pairs from the given map |
294 |
*/ |
295 |
public void testPutAll() { |
296 |
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
297 |
ConcurrentSkipListMap map = map5(); |
298 |
empty.putAll(map); |
299 |
assertEquals(5, empty.size()); |
300 |
assertTrue(empty.containsKey(one)); |
301 |
assertTrue(empty.containsKey(two)); |
302 |
assertTrue(empty.containsKey(three)); |
303 |
assertTrue(empty.containsKey(four)); |
304 |
assertTrue(empty.containsKey(five)); |
305 |
} |
306 |
|
307 |
/** |
308 |
* putIfAbsent works when the given key is not present |
309 |
*/ |
310 |
public void testPutIfAbsent() { |
311 |
ConcurrentSkipListMap map = map5(); |
312 |
map.putIfAbsent(six, "Z"); |
313 |
assertTrue(map.containsKey(six)); |
314 |
} |
315 |
|
316 |
/** |
317 |
* putIfAbsent does not add the pair if the key is already present |
318 |
*/ |
319 |
public void testPutIfAbsent2() { |
320 |
ConcurrentSkipListMap map = map5(); |
321 |
assertEquals("A", map.putIfAbsent(one, "Z")); |
322 |
} |
323 |
|
324 |
/** |
325 |
* replace fails when the given key is not present |
326 |
*/ |
327 |
public void testReplace() { |
328 |
ConcurrentSkipListMap map = map5(); |
329 |
assertNull(map.replace(six, "Z")); |
330 |
assertFalse(map.containsKey(six)); |
331 |
} |
332 |
|
333 |
/** |
334 |
* replace succeeds if the key is already present |
335 |
*/ |
336 |
public void testReplace2() { |
337 |
ConcurrentSkipListMap map = map5(); |
338 |
assertNotNull(map.replace(one, "Z")); |
339 |
assertEquals("Z", map.get(one)); |
340 |
} |
341 |
|
342 |
|
343 |
/** |
344 |
* replace value fails when the given key not mapped to expected value |
345 |
*/ |
346 |
public void testReplaceValue() { |
347 |
ConcurrentSkipListMap map = map5(); |
348 |
assertEquals("A", map.get(one)); |
349 |
assertFalse(map.replace(one, "Z", "Z")); |
350 |
assertEquals("A", map.get(one)); |
351 |
} |
352 |
|
353 |
/** |
354 |
* replace value succeeds when the given key mapped to expected value |
355 |
*/ |
356 |
public void testReplaceValue2() { |
357 |
ConcurrentSkipListMap map = map5(); |
358 |
assertEquals("A", map.get(one)); |
359 |
assertTrue(map.replace(one, "A", "Z")); |
360 |
assertEquals("Z", map.get(one)); |
361 |
} |
362 |
|
363 |
|
364 |
/** |
365 |
* remove removes the correct key-value pair from the map |
366 |
*/ |
367 |
public void testRemove() { |
368 |
ConcurrentSkipListMap map = map5(); |
369 |
map.remove(five); |
370 |
assertEquals(4, map.size()); |
371 |
assertFalse(map.containsKey(five)); |
372 |
} |
373 |
|
374 |
/** |
375 |
* remove(key,value) removes only if pair present |
376 |
*/ |
377 |
public void testRemove2() { |
378 |
ConcurrentSkipListMap map = map5(); |
379 |
assertTrue(map.containsKey(five)); |
380 |
assertEquals("E", map.get(five)); |
381 |
map.remove(five, "E"); |
382 |
assertEquals(4, map.size()); |
383 |
assertFalse(map.containsKey(five)); |
384 |
map.remove(four, "A"); |
385 |
assertEquals(4, map.size()); |
386 |
assertTrue(map.containsKey(four)); |
387 |
|
388 |
} |
389 |
|
390 |
/** |
391 |
* lowerEntry returns preceding entry. |
392 |
*/ |
393 |
public void testLowerEntry() { |
394 |
ConcurrentSkipListMap map = map5(); |
395 |
Map.Entry e1 = map.lowerEntry(three); |
396 |
assertEquals(two, e1.getKey()); |
397 |
|
398 |
Map.Entry e2 = map.lowerEntry(six); |
399 |
assertEquals(five, e2.getKey()); |
400 |
|
401 |
Map.Entry e3 = map.lowerEntry(one); |
402 |
assertNull(e3); |
403 |
|
404 |
Map.Entry e4 = map.lowerEntry(zero); |
405 |
assertNull(e4); |
406 |
|
407 |
} |
408 |
|
409 |
/** |
410 |
* higherEntry returns next entry. |
411 |
*/ |
412 |
public void testHigherEntry() { |
413 |
ConcurrentSkipListMap map = map5(); |
414 |
Map.Entry e1 = map.higherEntry(three); |
415 |
assertEquals(four, e1.getKey()); |
416 |
|
417 |
Map.Entry e2 = map.higherEntry(zero); |
418 |
assertEquals(one, e2.getKey()); |
419 |
|
420 |
Map.Entry e3 = map.higherEntry(five); |
421 |
assertNull(e3); |
422 |
|
423 |
Map.Entry e4 = map.higherEntry(six); |
424 |
assertNull(e4); |
425 |
|
426 |
} |
427 |
|
428 |
/** |
429 |
* floorEntry returns preceding entry. |
430 |
*/ |
431 |
public void testFloorEntry() { |
432 |
ConcurrentSkipListMap map = map5(); |
433 |
Map.Entry e1 = map.floorEntry(three); |
434 |
assertEquals(three, e1.getKey()); |
435 |
|
436 |
Map.Entry e2 = map.floorEntry(six); |
437 |
assertEquals(five, e2.getKey()); |
438 |
|
439 |
Map.Entry e3 = map.floorEntry(one); |
440 |
assertEquals(one, e3.getKey()); |
441 |
|
442 |
Map.Entry e4 = map.floorEntry(zero); |
443 |
assertNull(e4); |
444 |
|
445 |
} |
446 |
|
447 |
/** |
448 |
* ceilingEntry returns next entry. |
449 |
*/ |
450 |
public void testCeilingEntry() { |
451 |
ConcurrentSkipListMap map = map5(); |
452 |
Map.Entry e1 = map.ceilingEntry(three); |
453 |
assertEquals(three, e1.getKey()); |
454 |
|
455 |
Map.Entry e2 = map.ceilingEntry(zero); |
456 |
assertEquals(one, e2.getKey()); |
457 |
|
458 |
Map.Entry e3 = map.ceilingEntry(five); |
459 |
assertEquals(five, e3.getKey()); |
460 |
|
461 |
Map.Entry e4 = map.ceilingEntry(six); |
462 |
assertNull(e4); |
463 |
|
464 |
} |
465 |
|
466 |
|
467 |
/** |
468 |
* lowerKey returns preceding element |
469 |
*/ |
470 |
public void testLowerKey() { |
471 |
ConcurrentSkipListMap q = map5(); |
472 |
Object e1 = q.lowerKey(three); |
473 |
assertEquals(two, e1); |
474 |
|
475 |
Object e2 = q.lowerKey(six); |
476 |
assertEquals(five, e2); |
477 |
|
478 |
Object e3 = q.lowerKey(one); |
479 |
assertNull(e3); |
480 |
|
481 |
Object e4 = q.lowerKey(zero); |
482 |
assertNull(e4); |
483 |
|
484 |
} |
485 |
|
486 |
/** |
487 |
* higherKey returns next element |
488 |
*/ |
489 |
public void testHigherKey() { |
490 |
ConcurrentSkipListMap q = map5(); |
491 |
Object e1 = q.higherKey(three); |
492 |
assertEquals(four, e1); |
493 |
|
494 |
Object e2 = q.higherKey(zero); |
495 |
assertEquals(one, e2); |
496 |
|
497 |
Object e3 = q.higherKey(five); |
498 |
assertNull(e3); |
499 |
|
500 |
Object e4 = q.higherKey(six); |
501 |
assertNull(e4); |
502 |
|
503 |
} |
504 |
|
505 |
/** |
506 |
* floorKey returns preceding element |
507 |
*/ |
508 |
public void testFloorKey() { |
509 |
ConcurrentSkipListMap q = map5(); |
510 |
Object e1 = q.floorKey(three); |
511 |
assertEquals(three, e1); |
512 |
|
513 |
Object e2 = q.floorKey(six); |
514 |
assertEquals(five, e2); |
515 |
|
516 |
Object e3 = q.floorKey(one); |
517 |
assertEquals(one, e3); |
518 |
|
519 |
Object e4 = q.floorKey(zero); |
520 |
assertNull(e4); |
521 |
|
522 |
} |
523 |
|
524 |
/** |
525 |
* ceilingKey returns next element |
526 |
*/ |
527 |
public void testCeilingKey() { |
528 |
ConcurrentSkipListMap q = map5(); |
529 |
Object e1 = q.ceilingKey(three); |
530 |
assertEquals(three, e1); |
531 |
|
532 |
Object e2 = q.ceilingKey(zero); |
533 |
assertEquals(one, e2); |
534 |
|
535 |
Object e3 = q.ceilingKey(five); |
536 |
assertEquals(five, e3); |
537 |
|
538 |
Object e4 = q.ceilingKey(six); |
539 |
assertNull(e4); |
540 |
|
541 |
} |
542 |
|
543 |
/** |
544 |
* pollFirstEntry returns entries in order |
545 |
*/ |
546 |
public void testPollFirstEntry() { |
547 |
ConcurrentSkipListMap map = map5(); |
548 |
Map.Entry e = map.pollFirstEntry(); |
549 |
assertEquals(one, e.getKey()); |
550 |
assertEquals("A", e.getValue()); |
551 |
e = map.pollFirstEntry(); |
552 |
assertEquals(two, e.getKey()); |
553 |
map.put(one, "A"); |
554 |
e = map.pollFirstEntry(); |
555 |
assertEquals(one, e.getKey()); |
556 |
assertEquals("A", e.getValue()); |
557 |
e = map.pollFirstEntry(); |
558 |
assertEquals(three, e.getKey()); |
559 |
map.remove(four); |
560 |
e = map.pollFirstEntry(); |
561 |
assertEquals(five, e.getKey()); |
562 |
try { |
563 |
e.setValue("A"); |
564 |
shouldThrow(); |
565 |
} catch (Exception ok) { |
566 |
} |
567 |
e = map.pollFirstEntry(); |
568 |
assertNull(e); |
569 |
} |
570 |
|
571 |
/** |
572 |
* pollLastEntry returns entries in order |
573 |
*/ |
574 |
public void testPollLastEntry() { |
575 |
ConcurrentSkipListMap map = map5(); |
576 |
Map.Entry e = map.pollLastEntry(); |
577 |
assertEquals(five, e.getKey()); |
578 |
assertEquals("E", e.getValue()); |
579 |
e = map.pollLastEntry(); |
580 |
assertEquals(four, e.getKey()); |
581 |
map.put(five, "E"); |
582 |
e = map.pollLastEntry(); |
583 |
assertEquals(five, e.getKey()); |
584 |
assertEquals("E", e.getValue()); |
585 |
e = map.pollLastEntry(); |
586 |
assertEquals(three, e.getKey()); |
587 |
map.remove(two); |
588 |
e = map.pollLastEntry(); |
589 |
assertEquals(one, e.getKey()); |
590 |
try { |
591 |
e.setValue("E"); |
592 |
shouldThrow(); |
593 |
} catch (Exception ok) { |
594 |
} |
595 |
e = map.pollLastEntry(); |
596 |
assertNull(e); |
597 |
} |
598 |
|
599 |
/** |
600 |
* size returns the correct values |
601 |
*/ |
602 |
public void testSize() { |
603 |
ConcurrentSkipListMap map = map5(); |
604 |
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
605 |
assertEquals(0, empty.size()); |
606 |
assertEquals(5, map.size()); |
607 |
} |
608 |
|
609 |
/** |
610 |
* toString contains toString of elements |
611 |
*/ |
612 |
public void testToString() { |
613 |
ConcurrentSkipListMap map = map5(); |
614 |
String s = map.toString(); |
615 |
for (int i = 1; i <= 5; ++i) { |
616 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
617 |
} |
618 |
} |
619 |
|
620 |
// Exception tests |
621 |
|
622 |
/** |
623 |
* get(null) of nonempty map throws NPE |
624 |
*/ |
625 |
public void testGet_NullPointerException() { |
626 |
try { |
627 |
ConcurrentSkipListMap c = map5(); |
628 |
c.get(null); |
629 |
shouldThrow(); |
630 |
} catch(NullPointerException e){} |
631 |
} |
632 |
|
633 |
/** |
634 |
* containsKey(null) of nonempty map throws NPE |
635 |
*/ |
636 |
public void testContainsKey_NullPointerException() { |
637 |
try { |
638 |
ConcurrentSkipListMap c = map5(); |
639 |
c.containsKey(null); |
640 |
shouldThrow(); |
641 |
} catch(NullPointerException e){} |
642 |
} |
643 |
|
644 |
/** |
645 |
* containsValue(null) throws NPE |
646 |
*/ |
647 |
public void testContainsValue_NullPointerException() { |
648 |
try { |
649 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
650 |
c.containsValue(null); |
651 |
shouldThrow(); |
652 |
} catch(NullPointerException e){} |
653 |
} |
654 |
|
655 |
|
656 |
/** |
657 |
* put(null,x) throws NPE |
658 |
*/ |
659 |
public void testPut1_NullPointerException() { |
660 |
try { |
661 |
ConcurrentSkipListMap c = map5(); |
662 |
c.put(null, "whatever"); |
663 |
shouldThrow(); |
664 |
} catch(NullPointerException e){} |
665 |
} |
666 |
|
667 |
/** |
668 |
* putIfAbsent(null, x) throws NPE |
669 |
*/ |
670 |
public void testPutIfAbsent1_NullPointerException() { |
671 |
try { |
672 |
ConcurrentSkipListMap c = map5(); |
673 |
c.putIfAbsent(null, "whatever"); |
674 |
shouldThrow(); |
675 |
} catch(NullPointerException e){} |
676 |
} |
677 |
|
678 |
/** |
679 |
* replace(null, x) throws NPE |
680 |
*/ |
681 |
public void testReplace_NullPointerException() { |
682 |
try { |
683 |
ConcurrentSkipListMap c = map5(); |
684 |
c.replace(null, "whatever"); |
685 |
shouldThrow(); |
686 |
} catch(NullPointerException e){} |
687 |
} |
688 |
|
689 |
/** |
690 |
* replace(null, x, y) throws NPE |
691 |
*/ |
692 |
public void testReplaceValue_NullPointerException() { |
693 |
try { |
694 |
ConcurrentSkipListMap c = map5(); |
695 |
c.replace(null, one, "whatever"); |
696 |
shouldThrow(); |
697 |
} catch(NullPointerException e){} |
698 |
} |
699 |
|
700 |
/** |
701 |
* remove(null) throws NPE |
702 |
*/ |
703 |
public void testRemove1_NullPointerException() { |
704 |
try { |
705 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
706 |
c.put("sadsdf", "asdads"); |
707 |
c.remove(null); |
708 |
shouldThrow(); |
709 |
} catch(NullPointerException e){} |
710 |
} |
711 |
|
712 |
/** |
713 |
* remove(null, x) throws NPE |
714 |
*/ |
715 |
public void testRemove2_NullPointerException() { |
716 |
try { |
717 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
718 |
c.put("sadsdf", "asdads"); |
719 |
c.remove(null, "whatever"); |
720 |
shouldThrow(); |
721 |
} catch(NullPointerException e){} |
722 |
} |
723 |
|
724 |
/** |
725 |
* remove(x, null) returns false |
726 |
*/ |
727 |
public void testRemove3() { |
728 |
try { |
729 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
730 |
c.put("sadsdf", "asdads"); |
731 |
assertFalse(c.remove("sadsdf", null)); |
732 |
} catch(NullPointerException e){ |
733 |
fail(); |
734 |
} |
735 |
} |
736 |
|
737 |
/** |
738 |
* A deserialized map equals original |
739 |
*/ |
740 |
public void testSerialization() { |
741 |
ConcurrentSkipListMap q = map5(); |
742 |
|
743 |
try { |
744 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
745 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
746 |
out.writeObject(q); |
747 |
out.close(); |
748 |
|
749 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
750 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
751 |
ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject(); |
752 |
assertEquals(q.size(), r.size()); |
753 |
assertTrue(q.equals(r)); |
754 |
assertTrue(r.equals(q)); |
755 |
} catch(Exception e){ |
756 |
e.printStackTrace(); |
757 |
unexpectedException(); |
758 |
} |
759 |
} |
760 |
|
761 |
|
762 |
|
763 |
/** |
764 |
* subMap returns map with keys in requested range |
765 |
*/ |
766 |
public void testSubMapContents() { |
767 |
ConcurrentSkipListMap map = map5(); |
768 |
NavigableMap sm = map.navigableSubMap(two, four); |
769 |
assertEquals(two, sm.firstKey()); |
770 |
assertEquals(three, sm.lastKey()); |
771 |
assertEquals(2, sm.size()); |
772 |
assertFalse(sm.containsKey(one)); |
773 |
assertTrue(sm.containsKey(two)); |
774 |
assertTrue(sm.containsKey(three)); |
775 |
assertFalse(sm.containsKey(four)); |
776 |
assertFalse(sm.containsKey(five)); |
777 |
Iterator i = sm.keySet().iterator(); |
778 |
Object k; |
779 |
k = (Integer)(i.next()); |
780 |
assertEquals(two, k); |
781 |
k = (Integer)(i.next()); |
782 |
assertEquals(three, k); |
783 |
assertFalse(i.hasNext()); |
784 |
Iterator r = sm.descendingKeySet().iterator(); |
785 |
k = (Integer)(r.next()); |
786 |
assertEquals(three, k); |
787 |
k = (Integer)(r.next()); |
788 |
assertEquals(two, k); |
789 |
assertFalse(r.hasNext()); |
790 |
|
791 |
Iterator j = sm.keySet().iterator(); |
792 |
j.next(); |
793 |
j.remove(); |
794 |
assertFalse(map.containsKey(two)); |
795 |
assertEquals(4, map.size()); |
796 |
assertEquals(1, sm.size()); |
797 |
assertEquals(three, sm.firstKey()); |
798 |
assertEquals(three, sm.lastKey()); |
799 |
assertTrue(sm.remove(three) != null); |
800 |
assertTrue(sm.isEmpty()); |
801 |
assertEquals(3, map.size()); |
802 |
} |
803 |
|
804 |
public void testSubMapContents2() { |
805 |
ConcurrentSkipListMap map = map5(); |
806 |
NavigableMap sm = map.navigableSubMap(two, three); |
807 |
assertEquals(1, sm.size()); |
808 |
assertEquals(two, sm.firstKey()); |
809 |
assertEquals(two, sm.lastKey()); |
810 |
assertFalse(sm.containsKey(one)); |
811 |
assertTrue(sm.containsKey(two)); |
812 |
assertFalse(sm.containsKey(three)); |
813 |
assertFalse(sm.containsKey(four)); |
814 |
assertFalse(sm.containsKey(five)); |
815 |
Iterator i = sm.keySet().iterator(); |
816 |
Object k; |
817 |
k = (Integer)(i.next()); |
818 |
assertEquals(two, k); |
819 |
assertFalse(i.hasNext()); |
820 |
Iterator r = sm.descendingKeySet().iterator(); |
821 |
k = (Integer)(r.next()); |
822 |
assertEquals(two, k); |
823 |
assertFalse(r.hasNext()); |
824 |
|
825 |
Iterator j = sm.keySet().iterator(); |
826 |
j.next(); |
827 |
j.remove(); |
828 |
assertFalse(map.containsKey(two)); |
829 |
assertEquals(4, map.size()); |
830 |
assertEquals(0, sm.size()); |
831 |
assertTrue(sm.isEmpty()); |
832 |
assertTrue(sm.remove(three) == null); |
833 |
assertEquals(4, map.size()); |
834 |
} |
835 |
|
836 |
/** |
837 |
* headMap returns map with keys in requested range |
838 |
*/ |
839 |
public void testHeadMapContents() { |
840 |
ConcurrentSkipListMap map = map5(); |
841 |
NavigableMap sm = map.navigableHeadMap(four); |
842 |
assertTrue(sm.containsKey(one)); |
843 |
assertTrue(sm.containsKey(two)); |
844 |
assertTrue(sm.containsKey(three)); |
845 |
assertFalse(sm.containsKey(four)); |
846 |
assertFalse(sm.containsKey(five)); |
847 |
Iterator i = sm.keySet().iterator(); |
848 |
Object k; |
849 |
k = (Integer)(i.next()); |
850 |
assertEquals(one, k); |
851 |
k = (Integer)(i.next()); |
852 |
assertEquals(two, k); |
853 |
k = (Integer)(i.next()); |
854 |
assertEquals(three, k); |
855 |
assertFalse(i.hasNext()); |
856 |
sm.clear(); |
857 |
assertTrue(sm.isEmpty()); |
858 |
assertEquals(2, map.size()); |
859 |
assertEquals(four, map.firstKey()); |
860 |
} |
861 |
|
862 |
/** |
863 |
* headMap returns map with keys in requested range |
864 |
*/ |
865 |
public void testTailMapContents() { |
866 |
ConcurrentSkipListMap map = map5(); |
867 |
NavigableMap sm = map.navigableTailMap(two); |
868 |
assertFalse(sm.containsKey(one)); |
869 |
assertTrue(sm.containsKey(two)); |
870 |
assertTrue(sm.containsKey(three)); |
871 |
assertTrue(sm.containsKey(four)); |
872 |
assertTrue(sm.containsKey(five)); |
873 |
Iterator i = sm.keySet().iterator(); |
874 |
Object k; |
875 |
k = (Integer)(i.next()); |
876 |
assertEquals(two, k); |
877 |
k = (Integer)(i.next()); |
878 |
assertEquals(three, k); |
879 |
k = (Integer)(i.next()); |
880 |
assertEquals(four, k); |
881 |
k = (Integer)(i.next()); |
882 |
assertEquals(five, k); |
883 |
assertFalse(i.hasNext()); |
884 |
Iterator r = sm.descendingKeySet().iterator(); |
885 |
k = (Integer)(r.next()); |
886 |
assertEquals(five, k); |
887 |
k = (Integer)(r.next()); |
888 |
assertEquals(four, k); |
889 |
k = (Integer)(r.next()); |
890 |
assertEquals(three, k); |
891 |
k = (Integer)(r.next()); |
892 |
assertEquals(two, k); |
893 |
assertFalse(r.hasNext()); |
894 |
|
895 |
Iterator ei = sm.entrySet().iterator(); |
896 |
Map.Entry e; |
897 |
e = (Map.Entry)(ei.next()); |
898 |
assertEquals(two, e.getKey()); |
899 |
assertEquals("B", e.getValue()); |
900 |
e = (Map.Entry)(ei.next()); |
901 |
assertEquals(three, e.getKey()); |
902 |
assertEquals("C", e.getValue()); |
903 |
e = (Map.Entry)(ei.next()); |
904 |
assertEquals(four, e.getKey()); |
905 |
assertEquals("D", e.getValue()); |
906 |
e = (Map.Entry)(ei.next()); |
907 |
assertEquals(five, e.getKey()); |
908 |
assertEquals("E", e.getValue()); |
909 |
assertFalse(i.hasNext()); |
910 |
|
911 |
NavigableMap ssm = sm.navigableTailMap(four); |
912 |
assertEquals(four, ssm.firstKey()); |
913 |
assertEquals(five, ssm.lastKey()); |
914 |
assertTrue(ssm.remove(four) != null); |
915 |
assertEquals(1, ssm.size()); |
916 |
assertEquals(3, sm.size()); |
917 |
assertEquals(4, map.size()); |
918 |
} |
919 |
|
920 |
} |