1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
14 |
|
|
15 |
|
public class ConcurrentHashMapTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
< |
* clear removes all pairs |
40 |
> |
* clear removes all pairs |
41 |
|
*/ |
42 |
|
public void testClear() { |
43 |
|
ConcurrentHashMap map = map5(); |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
< |
* Maps with same contents are equal |
49 |
> |
* Maps with same contents are equal |
50 |
|
*/ |
51 |
|
public void testEquals() { |
52 |
|
ConcurrentHashMap map1 = map5(); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
< |
* contains returns true for contained value |
62 |
> |
* contains returns true for contained value |
63 |
|
*/ |
64 |
|
public void testContains() { |
65 |
|
ConcurrentHashMap map = map5(); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* containsKey returns true for contained key |
71 |
> |
* containsKey returns true for contained key |
72 |
|
*/ |
73 |
|
public void testContainsKey() { |
74 |
|
ConcurrentHashMap map = map5(); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* containsValue returns true for held values |
80 |
> |
* containsValue returns true for held values |
81 |
|
*/ |
82 |
|
public void testContainsValue() { |
83 |
|
ConcurrentHashMap map = map5(); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* enumeration returns an enumeration containing the correct |
90 |
< |
* elements |
89 |
> |
* enumeration returns an enumeration containing the correct |
90 |
> |
* elements |
91 |
|
*/ |
92 |
|
public void testEnumeration() { |
93 |
|
ConcurrentHashMap map = map5(); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
< |
* get returns the correct element at the given key, |
105 |
< |
* or null if not present |
104 |
> |
* get returns the correct element at the given key, |
105 |
> |
* or null if not present |
106 |
|
*/ |
107 |
|
public void testGet() { |
108 |
|
ConcurrentHashMap map = map5(); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
< |
* isEmpty is true of empty map and false for non-empty |
115 |
> |
* isEmpty is true of empty map and false for non-empty |
116 |
|
*/ |
117 |
|
public void testIsEmpty() { |
118 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
122 |
|
} |
123 |
|
|
124 |
|
/** |
125 |
< |
* keys returns an enumeration containing all the keys from the map |
125 |
> |
* keys returns an enumeration containing all the keys from the map |
126 |
|
*/ |
127 |
|
public void testKeys() { |
128 |
|
ConcurrentHashMap map = map5(); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
< |
* keySet returns a Set containing all the keys |
139 |
> |
* keySet returns a Set containing all the keys |
140 |
|
*/ |
141 |
|
public void testKeySet() { |
142 |
|
ConcurrentHashMap map = map5(); |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
< |
* keySet.toArray returns contains all keys |
153 |
> |
* keySet.toArray returns contains all keys |
154 |
|
*/ |
155 |
|
public void testKeySetToArray() { |
156 |
|
ConcurrentHashMap map = map5(); |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
< |
* Values.toArray contains all values |
166 |
> |
* Values.toArray contains all values |
167 |
|
*/ |
168 |
|
public void testValuesToArray() { |
169 |
|
ConcurrentHashMap map = map5(); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
< |
* entrySet.toArray contains all entries |
182 |
> |
* entrySet.toArray contains all entries |
183 |
|
*/ |
184 |
|
public void testEntrySetToArray() { |
185 |
|
ConcurrentHashMap map = map5(); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
< |
* putAll adds all key-value pairs from the given map |
229 |
> |
* putAll adds all key-value pairs from the given map |
230 |
|
*/ |
231 |
|
public void testPutAll() { |
232 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
< |
* putIfAbsent works when the given key is not present |
244 |
> |
* putIfAbsent works when the given key is not present |
245 |
|
*/ |
246 |
|
public void testPutIfAbsent() { |
247 |
|
ConcurrentHashMap map = map5(); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* putIfAbsent does not add the pair if the key is already present |
253 |
> |
* putIfAbsent does not add the pair if the key is already present |
254 |
|
*/ |
255 |
|
public void testPutIfAbsent2() { |
256 |
|
ConcurrentHashMap map = map5(); |
258 |
|
} |
259 |
|
|
260 |
|
/** |
261 |
< |
* replace fails when the given key is not present |
261 |
> |
* replace fails when the given key is not present |
262 |
|
*/ |
263 |
|
public void testReplace() { |
264 |
|
ConcurrentHashMap map = map5(); |
267 |
|
} |
268 |
|
|
269 |
|
/** |
270 |
< |
* replace succeeds if the key is already present |
270 |
> |
* replace succeeds if the key is already present |
271 |
|
*/ |
272 |
|
public void testReplace2() { |
273 |
|
ConcurrentHashMap map = map5(); |
275 |
|
assertEquals("Z", map.get(one)); |
276 |
|
} |
277 |
|
|
278 |
– |
|
278 |
|
/** |
279 |
|
* replace value fails when the given key not mapped to expected value |
280 |
|
*/ |
295 |
|
assertEquals("Z", map.get(one)); |
296 |
|
} |
297 |
|
|
299 |
– |
|
298 |
|
/** |
299 |
< |
* remove removes the correct key-value pair from the map |
299 |
> |
* remove removes the correct key-value pair from the map |
300 |
|
*/ |
301 |
|
public void testRemove() { |
302 |
|
ConcurrentHashMap map = map5(); |
316 |
|
map.remove(four, "A"); |
317 |
|
assertEquals(4, map.size()); |
318 |
|
assertTrue(map.containsKey(four)); |
321 |
– |
|
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
< |
* size returns the correct values |
322 |
> |
* size returns the correct values |
323 |
|
*/ |
324 |
|
public void testSize() { |
325 |
|
ConcurrentHashMap map = map5(); |
335 |
|
ConcurrentHashMap map = map5(); |
336 |
|
String s = map.toString(); |
337 |
|
for (int i = 1; i <= 5; ++i) { |
338 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
338 |
> |
assertTrue(s.contains(String.valueOf(i))); |
339 |
|
} |
340 |
|
} |
341 |
|
|
348 |
|
try { |
349 |
|
new ConcurrentHashMap(-1,0,1); |
350 |
|
shouldThrow(); |
351 |
< |
} catch (IllegalArgumentException e) {} |
351 |
> |
} catch (IllegalArgumentException success) {} |
352 |
|
} |
353 |
|
|
354 |
|
/** |
358 |
|
try { |
359 |
|
new ConcurrentHashMap(1,0,-1); |
360 |
|
shouldThrow(); |
361 |
< |
} catch (IllegalArgumentException e) {} |
361 |
> |
} catch (IllegalArgumentException success) {} |
362 |
|
} |
363 |
|
|
364 |
|
/** |
368 |
|
try { |
369 |
|
new ConcurrentHashMap(-1); |
370 |
|
shouldThrow(); |
371 |
< |
} catch (IllegalArgumentException e) {} |
371 |
> |
} catch (IllegalArgumentException success) {} |
372 |
|
} |
373 |
|
|
374 |
|
/** |
379 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
380 |
|
c.get(null); |
381 |
|
shouldThrow(); |
382 |
< |
} catch (NullPointerException e) {} |
382 |
> |
} catch (NullPointerException success) {} |
383 |
|
} |
384 |
|
|
385 |
|
/** |
390 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
391 |
|
c.containsKey(null); |
392 |
|
shouldThrow(); |
393 |
< |
} catch (NullPointerException e) {} |
393 |
> |
} catch (NullPointerException success) {} |
394 |
|
} |
395 |
|
|
396 |
|
/** |
401 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
402 |
|
c.containsValue(null); |
403 |
|
shouldThrow(); |
404 |
< |
} catch (NullPointerException e) {} |
404 |
> |
} catch (NullPointerException success) {} |
405 |
|
} |
406 |
|
|
407 |
|
/** |
412 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
413 |
|
c.contains(null); |
414 |
|
shouldThrow(); |
415 |
< |
} catch (NullPointerException e) {} |
415 |
> |
} catch (NullPointerException success) {} |
416 |
|
} |
417 |
|
|
418 |
|
/** |
423 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
424 |
|
c.put(null, "whatever"); |
425 |
|
shouldThrow(); |
426 |
< |
} catch (NullPointerException e) {} |
426 |
> |
} catch (NullPointerException success) {} |
427 |
|
} |
428 |
|
|
429 |
|
/** |
434 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
435 |
|
c.put("whatever", null); |
436 |
|
shouldThrow(); |
437 |
< |
} catch (NullPointerException e) {} |
437 |
> |
} catch (NullPointerException success) {} |
438 |
|
} |
439 |
|
|
440 |
|
/** |
445 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
446 |
|
c.putIfAbsent(null, "whatever"); |
447 |
|
shouldThrow(); |
448 |
< |
} catch (NullPointerException e) {} |
448 |
> |
} catch (NullPointerException success) {} |
449 |
|
} |
450 |
|
|
451 |
|
/** |
456 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
457 |
|
c.replace(null, "whatever"); |
458 |
|
shouldThrow(); |
459 |
< |
} catch (NullPointerException e) {} |
459 |
> |
} catch (NullPointerException success) {} |
460 |
|
} |
461 |
|
|
462 |
|
/** |
467 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
468 |
|
c.replace(null, one, "whatever"); |
469 |
|
shouldThrow(); |
470 |
< |
} catch (NullPointerException e) {} |
470 |
> |
} catch (NullPointerException success) {} |
471 |
|
} |
472 |
|
|
473 |
|
/** |
478 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
479 |
|
c.putIfAbsent("whatever", null); |
480 |
|
shouldThrow(); |
481 |
< |
} catch (NullPointerException e) {} |
481 |
> |
} catch (NullPointerException success) {} |
482 |
|
} |
483 |
|
|
487 |
– |
|
484 |
|
/** |
485 |
|
* replace(x, null) throws NPE |
486 |
|
*/ |
489 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
490 |
|
c.replace("whatever", null); |
491 |
|
shouldThrow(); |
492 |
< |
} catch (NullPointerException e) {} |
492 |
> |
} catch (NullPointerException success) {} |
493 |
|
} |
494 |
|
|
495 |
|
/** |
500 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
501 |
|
c.replace("whatever", null, "A"); |
502 |
|
shouldThrow(); |
503 |
< |
} catch (NullPointerException e) {} |
503 |
> |
} catch (NullPointerException success) {} |
504 |
|
} |
505 |
|
|
506 |
|
/** |
511 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
512 |
|
c.replace("whatever", one, null); |
513 |
|
shouldThrow(); |
514 |
< |
} catch (NullPointerException e) {} |
514 |
> |
} catch (NullPointerException success) {} |
515 |
|
} |
516 |
|
|
521 |
– |
|
517 |
|
/** |
518 |
|
* remove(null) throws NPE |
519 |
|
*/ |
523 |
|
c.put("sadsdf", "asdads"); |
524 |
|
c.remove(null); |
525 |
|
shouldThrow(); |
526 |
< |
} catch (NullPointerException e) {} |
526 |
> |
} catch (NullPointerException success) {} |
527 |
|
} |
528 |
|
|
529 |
|
/** |
535 |
|
c.put("sadsdf", "asdads"); |
536 |
|
c.remove(null, "whatever"); |
537 |
|
shouldThrow(); |
538 |
< |
} catch (NullPointerException e) {} |
538 |
> |
} catch (NullPointerException success) {} |
539 |
|
} |
540 |
|
|
541 |
|
/** |
542 |
|
* remove(x, null) returns false |
543 |
|
*/ |
544 |
|
public void testRemove3() { |
545 |
< |
try { |
546 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
547 |
< |
c.put("sadsdf", "asdads"); |
553 |
< |
assertFalse(c.remove("sadsdf", null)); |
554 |
< |
} catch (NullPointerException e) { |
555 |
< |
fail(); |
556 |
< |
} |
545 |
> |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
546 |
> |
c.put("sadsdf", "asdads"); |
547 |
> |
assertFalse(c.remove("sadsdf", null)); |
548 |
|
} |
549 |
|
|
550 |
|
/** |
566 |
|
assertTrue(r.equals(q)); |
567 |
|
} |
568 |
|
|
578 |
– |
|
569 |
|
/** |
570 |
|
* SetValue of an EntrySet entry sets value in the map. |
571 |
|
*/ |