ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.26
Committed: Sat Nov 26 05:19:17 2011 UTC (12 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +1 -1 lines
Log Message:
assertEquals argument order

File Contents

# Content
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/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.ConcurrentHashMap;
12
13 public class ConcurrentHashMapTest extends JSR166TestCase {
14 public static void main(String[] args) {
15 junit.textui.TestRunner.run(suite());
16 }
17 public static Test suite() {
18 return new TestSuite(ConcurrentHashMapTest.class);
19 }
20
21 /**
22 * Create a map from Integers 1-5 to Strings "A"-"E".
23 */
24 private static ConcurrentHashMap map5() {
25 ConcurrentHashMap map = new ConcurrentHashMap(5);
26 assertTrue(map.isEmpty());
27 map.put(one, "A");
28 map.put(two, "B");
29 map.put(three, "C");
30 map.put(four, "D");
31 map.put(five, "E");
32 assertFalse(map.isEmpty());
33 assertEquals(5, map.size());
34 return map;
35 }
36
37 /**
38 * clear removes all pairs
39 */
40 public void testClear() {
41 ConcurrentHashMap map = map5();
42 map.clear();
43 assertEquals(0, map.size());
44 }
45
46 /**
47 * Maps with same contents are equal
48 */
49 public void testEquals() {
50 ConcurrentHashMap map1 = map5();
51 ConcurrentHashMap map2 = map5();
52 assertEquals(map1, map2);
53 assertEquals(map2, map1);
54 map1.clear();
55 assertFalse(map1.equals(map2));
56 assertFalse(map2.equals(map1));
57 }
58
59 /**
60 * contains returns true for contained value
61 */
62 public void testContains() {
63 ConcurrentHashMap map = map5();
64 assertTrue(map.contains("A"));
65 assertFalse(map.contains("Z"));
66 }
67
68 /**
69 * containsKey returns true for contained key
70 */
71 public void testContainsKey() {
72 ConcurrentHashMap map = map5();
73 assertTrue(map.containsKey(one));
74 assertFalse(map.containsKey(zero));
75 }
76
77 /**
78 * containsValue returns true for held values
79 */
80 public void testContainsValue() {
81 ConcurrentHashMap map = map5();
82 assertTrue(map.containsValue("A"));
83 assertFalse(map.containsValue("Z"));
84 }
85
86 /**
87 * enumeration returns an enumeration containing the correct
88 * elements
89 */
90 public void testEnumeration() {
91 ConcurrentHashMap map = map5();
92 Enumeration e = map.elements();
93 int count = 0;
94 while (e.hasMoreElements()) {
95 count++;
96 e.nextElement();
97 }
98 assertEquals(5, count);
99 }
100
101 /**
102 * get returns the correct element at the given key,
103 * or null if not present
104 */
105 public void testGet() {
106 ConcurrentHashMap map = map5();
107 assertEquals("A", (String)map.get(one));
108 ConcurrentHashMap empty = new ConcurrentHashMap();
109 assertNull(map.get("anything"));
110 }
111
112 /**
113 * isEmpty is true of empty map and false for non-empty
114 */
115 public void testIsEmpty() {
116 ConcurrentHashMap empty = new ConcurrentHashMap();
117 ConcurrentHashMap map = map5();
118 assertTrue(empty.isEmpty());
119 assertFalse(map.isEmpty());
120 }
121
122 /**
123 * keys returns an enumeration containing all the keys from the map
124 */
125 public void testKeys() {
126 ConcurrentHashMap map = map5();
127 Enumeration e = map.keys();
128 int count = 0;
129 while (e.hasMoreElements()) {
130 count++;
131 e.nextElement();
132 }
133 assertEquals(5, count);
134 }
135
136 /**
137 * keySet returns a Set containing all the keys
138 */
139 public void testKeySet() {
140 ConcurrentHashMap map = map5();
141 Set s = map.keySet();
142 assertEquals(5, s.size());
143 assertTrue(s.contains(one));
144 assertTrue(s.contains(two));
145 assertTrue(s.contains(three));
146 assertTrue(s.contains(four));
147 assertTrue(s.contains(five));
148 }
149
150 /**
151 * keySet.toArray returns contains all keys
152 */
153 public void testKeySetToArray() {
154 ConcurrentHashMap map = map5();
155 Set s = map.keySet();
156 Object[] ar = s.toArray();
157 assertTrue(s.containsAll(Arrays.asList(ar)));
158 assertEquals(5, ar.length);
159 ar[0] = m10;
160 assertFalse(s.containsAll(Arrays.asList(ar)));
161 }
162
163 /**
164 * Values.toArray contains all values
165 */
166 public void testValuesToArray() {
167 ConcurrentHashMap map = map5();
168 Collection v = map.values();
169 Object[] ar = v.toArray();
170 ArrayList s = new ArrayList(Arrays.asList(ar));
171 assertEquals(5, ar.length);
172 assertTrue(s.contains("A"));
173 assertTrue(s.contains("B"));
174 assertTrue(s.contains("C"));
175 assertTrue(s.contains("D"));
176 assertTrue(s.contains("E"));
177 }
178
179 /**
180 * entrySet.toArray contains all entries
181 */
182 public void testEntrySetToArray() {
183 ConcurrentHashMap map = map5();
184 Set s = map.entrySet();
185 Object[] ar = s.toArray();
186 assertEquals(5, ar.length);
187 for (int i = 0; i < 5; ++i) {
188 assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
189 assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
190 }
191 }
192
193 /**
194 * values collection contains all values
195 */
196 public void testValues() {
197 ConcurrentHashMap map = map5();
198 Collection s = map.values();
199 assertEquals(5, s.size());
200 assertTrue(s.contains("A"));
201 assertTrue(s.contains("B"));
202 assertTrue(s.contains("C"));
203 assertTrue(s.contains("D"));
204 assertTrue(s.contains("E"));
205 }
206
207 /**
208 * entrySet contains all pairs
209 */
210 public void testEntrySet() {
211 ConcurrentHashMap map = map5();
212 Set s = map.entrySet();
213 assertEquals(5, s.size());
214 Iterator it = s.iterator();
215 while (it.hasNext()) {
216 Map.Entry e = (Map.Entry) it.next();
217 assertTrue(
218 (e.getKey().equals(one) && e.getValue().equals("A")) ||
219 (e.getKey().equals(two) && e.getValue().equals("B")) ||
220 (e.getKey().equals(three) && e.getValue().equals("C")) ||
221 (e.getKey().equals(four) && e.getValue().equals("D")) ||
222 (e.getKey().equals(five) && e.getValue().equals("E")));
223 }
224 }
225
226 /**
227 * putAll adds all key-value pairs from the given map
228 */
229 public void testPutAll() {
230 ConcurrentHashMap empty = new ConcurrentHashMap();
231 ConcurrentHashMap map = map5();
232 empty.putAll(map);
233 assertEquals(5, empty.size());
234 assertTrue(empty.containsKey(one));
235 assertTrue(empty.containsKey(two));
236 assertTrue(empty.containsKey(three));
237 assertTrue(empty.containsKey(four));
238 assertTrue(empty.containsKey(five));
239 }
240
241 /**
242 * putIfAbsent works when the given key is not present
243 */
244 public void testPutIfAbsent() {
245 ConcurrentHashMap map = map5();
246 map.putIfAbsent(six, "Z");
247 assertTrue(map.containsKey(six));
248 }
249
250 /**
251 * putIfAbsent does not add the pair if the key is already present
252 */
253 public void testPutIfAbsent2() {
254 ConcurrentHashMap map = map5();
255 assertEquals("A", map.putIfAbsent(one, "Z"));
256 }
257
258 /**
259 * replace fails when the given key is not present
260 */
261 public void testReplace() {
262 ConcurrentHashMap map = map5();
263 assertNull(map.replace(six, "Z"));
264 assertFalse(map.containsKey(six));
265 }
266
267 /**
268 * replace succeeds if the key is already present
269 */
270 public void testReplace2() {
271 ConcurrentHashMap map = map5();
272 assertNotNull(map.replace(one, "Z"));
273 assertEquals("Z", map.get(one));
274 }
275
276 /**
277 * replace value fails when the given key not mapped to expected value
278 */
279 public void testReplaceValue() {
280 ConcurrentHashMap map = map5();
281 assertEquals("A", map.get(one));
282 assertFalse(map.replace(one, "Z", "Z"));
283 assertEquals("A", map.get(one));
284 }
285
286 /**
287 * replace value succeeds when the given key mapped to expected value
288 */
289 public void testReplaceValue2() {
290 ConcurrentHashMap map = map5();
291 assertEquals("A", map.get(one));
292 assertTrue(map.replace(one, "A", "Z"));
293 assertEquals("Z", map.get(one));
294 }
295
296 /**
297 * remove removes the correct key-value pair from the map
298 */
299 public void testRemove() {
300 ConcurrentHashMap map = map5();
301 map.remove(five);
302 assertEquals(4, map.size());
303 assertFalse(map.containsKey(five));
304 }
305
306 /**
307 * remove(key,value) removes only if pair present
308 */
309 public void testRemove2() {
310 ConcurrentHashMap map = map5();
311 map.remove(five, "E");
312 assertEquals(4, map.size());
313 assertFalse(map.containsKey(five));
314 map.remove(four, "A");
315 assertEquals(4, map.size());
316 assertTrue(map.containsKey(four));
317 }
318
319 /**
320 * size returns the correct values
321 */
322 public void testSize() {
323 ConcurrentHashMap map = map5();
324 ConcurrentHashMap empty = new ConcurrentHashMap();
325 assertEquals(0, empty.size());
326 assertEquals(5, map.size());
327 }
328
329 /**
330 * toString contains toString of elements
331 */
332 public void testToString() {
333 ConcurrentHashMap map = map5();
334 String s = map.toString();
335 for (int i = 1; i <= 5; ++i) {
336 assertTrue(s.contains(String.valueOf(i)));
337 }
338 }
339
340 // Exception tests
341
342 /**
343 * Cannot create with negative capacity
344 */
345 public void testConstructor1() {
346 try {
347 new ConcurrentHashMap(-1,0,1);
348 shouldThrow();
349 } catch (IllegalArgumentException success) {}
350 }
351
352 /**
353 * Cannot create with negative concurrency level
354 */
355 public void testConstructor2() {
356 try {
357 new ConcurrentHashMap(1,0,-1);
358 shouldThrow();
359 } catch (IllegalArgumentException success) {}
360 }
361
362 /**
363 * Cannot create with only negative capacity
364 */
365 public void testConstructor3() {
366 try {
367 new ConcurrentHashMap(-1);
368 shouldThrow();
369 } catch (IllegalArgumentException success) {}
370 }
371
372 /**
373 * get(null) throws NPE
374 */
375 public void testGet_NullPointerException() {
376 try {
377 ConcurrentHashMap c = new ConcurrentHashMap(5);
378 c.get(null);
379 shouldThrow();
380 } catch (NullPointerException success) {}
381 }
382
383 /**
384 * containsKey(null) throws NPE
385 */
386 public void testContainsKey_NullPointerException() {
387 try {
388 ConcurrentHashMap c = new ConcurrentHashMap(5);
389 c.containsKey(null);
390 shouldThrow();
391 } catch (NullPointerException success) {}
392 }
393
394 /**
395 * containsValue(null) throws NPE
396 */
397 public void testContainsValue_NullPointerException() {
398 try {
399 ConcurrentHashMap c = new ConcurrentHashMap(5);
400 c.containsValue(null);
401 shouldThrow();
402 } catch (NullPointerException success) {}
403 }
404
405 /**
406 * contains(null) throws NPE
407 */
408 public void testContains_NullPointerException() {
409 try {
410 ConcurrentHashMap c = new ConcurrentHashMap(5);
411 c.contains(null);
412 shouldThrow();
413 } catch (NullPointerException success) {}
414 }
415
416 /**
417 * put(null,x) throws NPE
418 */
419 public void testPut1_NullPointerException() {
420 try {
421 ConcurrentHashMap c = new ConcurrentHashMap(5);
422 c.put(null, "whatever");
423 shouldThrow();
424 } catch (NullPointerException success) {}
425 }
426
427 /**
428 * put(x, null) throws NPE
429 */
430 public void testPut2_NullPointerException() {
431 try {
432 ConcurrentHashMap c = new ConcurrentHashMap(5);
433 c.put("whatever", null);
434 shouldThrow();
435 } catch (NullPointerException success) {}
436 }
437
438 /**
439 * putIfAbsent(null, x) throws NPE
440 */
441 public void testPutIfAbsent1_NullPointerException() {
442 try {
443 ConcurrentHashMap c = new ConcurrentHashMap(5);
444 c.putIfAbsent(null, "whatever");
445 shouldThrow();
446 } catch (NullPointerException success) {}
447 }
448
449 /**
450 * replace(null, x) throws NPE
451 */
452 public void testReplace_NullPointerException() {
453 try {
454 ConcurrentHashMap c = new ConcurrentHashMap(5);
455 c.replace(null, "whatever");
456 shouldThrow();
457 } catch (NullPointerException success) {}
458 }
459
460 /**
461 * replace(null, x, y) throws NPE
462 */
463 public void testReplaceValue_NullPointerException() {
464 try {
465 ConcurrentHashMap c = new ConcurrentHashMap(5);
466 c.replace(null, one, "whatever");
467 shouldThrow();
468 } catch (NullPointerException success) {}
469 }
470
471 /**
472 * putIfAbsent(x, null) throws NPE
473 */
474 public void testPutIfAbsent2_NullPointerException() {
475 try {
476 ConcurrentHashMap c = new ConcurrentHashMap(5);
477 c.putIfAbsent("whatever", null);
478 shouldThrow();
479 } catch (NullPointerException success) {}
480 }
481
482 /**
483 * replace(x, null) throws NPE
484 */
485 public void testReplace2_NullPointerException() {
486 try {
487 ConcurrentHashMap c = new ConcurrentHashMap(5);
488 c.replace("whatever", null);
489 shouldThrow();
490 } catch (NullPointerException success) {}
491 }
492
493 /**
494 * replace(x, null, y) throws NPE
495 */
496 public void testReplaceValue2_NullPointerException() {
497 try {
498 ConcurrentHashMap c = new ConcurrentHashMap(5);
499 c.replace("whatever", null, "A");
500 shouldThrow();
501 } catch (NullPointerException success) {}
502 }
503
504 /**
505 * replace(x, y, null) throws NPE
506 */
507 public void testReplaceValue3_NullPointerException() {
508 try {
509 ConcurrentHashMap c = new ConcurrentHashMap(5);
510 c.replace("whatever", one, null);
511 shouldThrow();
512 } catch (NullPointerException success) {}
513 }
514
515 /**
516 * remove(null) throws NPE
517 */
518 public void testRemove1_NullPointerException() {
519 try {
520 ConcurrentHashMap c = new ConcurrentHashMap(5);
521 c.put("sadsdf", "asdads");
522 c.remove(null);
523 shouldThrow();
524 } catch (NullPointerException success) {}
525 }
526
527 /**
528 * remove(null, x) throws NPE
529 */
530 public void testRemove2_NullPointerException() {
531 try {
532 ConcurrentHashMap c = new ConcurrentHashMap(5);
533 c.put("sadsdf", "asdads");
534 c.remove(null, "whatever");
535 shouldThrow();
536 } catch (NullPointerException success) {}
537 }
538
539 /**
540 * remove(x, null) returns false
541 */
542 public void testRemove3() {
543 ConcurrentHashMap c = new ConcurrentHashMap(5);
544 c.put("sadsdf", "asdads");
545 assertFalse(c.remove("sadsdf", null));
546 }
547
548 /**
549 * A deserialized map equals original
550 */
551 public void testSerialization() throws Exception {
552 Map x = map5();
553 Map y = serialClone(x);
554
555 assertTrue(x != y);
556 assertEquals(x.size(), y.size());
557 assertEquals(x, y);
558 assertEquals(y, x);
559 }
560
561 /**
562 * SetValue of an EntrySet entry sets value in the map.
563 */
564 public void testSetValueWriteThrough() {
565 // Adapted from a bug report by Eric Zoerner
566 ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
567 assertTrue(map.isEmpty());
568 for (int i = 0; i < 20; i++)
569 map.put(new Integer(i), new Integer(i));
570 assertFalse(map.isEmpty());
571 Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
572
573 // assert that entry1 is not 16
574 assertTrue("entry is 16, test not valid",
575 !entry1.getKey().equals(new Integer(16)));
576
577 // remove 16 (a different key) from map
578 // which just happens to cause entry1 to be cloned in map
579 map.remove(new Integer(16));
580 entry1.setValue("XYZ");
581 assertTrue(map.containsValue("XYZ")); // fails
582 }
583
584 }