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

# User Rev Content
1 dl 1.1 /*
2 dl 1.9 * 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.23 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.14 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11 jsr166 1.25 import java.util.concurrent.ConcurrentHashMap;
12 dl 1.1
13 jsr166 1.16 public class ConcurrentHashMapTest extends JSR166TestCase {
14 dl 1.1 public static void main(String[] args) {
15 jsr166 1.21 junit.textui.TestRunner.run(suite());
16 dl 1.1 }
17     public static Test suite() {
18 jsr166 1.17 return new TestSuite(ConcurrentHashMapTest.class);
19 dl 1.1 }
20    
21 dl 1.5 /**
22     * Create a map from Integers 1-5 to Strings "A"-"E".
23     */
24 jsr166 1.14 private static ConcurrentHashMap map5() {
25 jsr166 1.17 ConcurrentHashMap map = new ConcurrentHashMap(5);
26 dl 1.1 assertTrue(map.isEmpty());
27 jsr166 1.17 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 dl 1.1 assertFalse(map.isEmpty());
33     assertEquals(5, map.size());
34 jsr166 1.17 return map;
35 dl 1.1 }
36    
37     /**
38 jsr166 1.22 * clear removes all pairs
39 dl 1.1 */
40 dl 1.5 public void testClear() {
41 dl 1.1 ConcurrentHashMap map = map5();
42 jsr166 1.17 map.clear();
43 jsr166 1.26 assertEquals(0, map.size());
44 dl 1.1 }
45    
46     /**
47 jsr166 1.22 * Maps with same contents are equal
48 dl 1.5 */
49     public void testEquals() {
50     ConcurrentHashMap map1 = map5();
51     ConcurrentHashMap map2 = map5();
52     assertEquals(map1, map2);
53     assertEquals(map2, map1);
54 jsr166 1.17 map1.clear();
55 dl 1.5 assertFalse(map1.equals(map2));
56     assertFalse(map2.equals(map1));
57     }
58    
59     /**
60 jsr166 1.22 * contains returns true for contained value
61 dl 1.1 */
62 dl 1.5 public void testContains() {
63 dl 1.1 ConcurrentHashMap map = map5();
64 jsr166 1.17 assertTrue(map.contains("A"));
65 dl 1.1 assertFalse(map.contains("Z"));
66     }
67 jsr166 1.14
68 dl 1.1 /**
69 jsr166 1.22 * containsKey returns true for contained key
70 dl 1.1 */
71 dl 1.5 public void testContainsKey() {
72 dl 1.1 ConcurrentHashMap map = map5();
73 jsr166 1.17 assertTrue(map.containsKey(one));
74 dl 1.5 assertFalse(map.containsKey(zero));
75 dl 1.1 }
76    
77     /**
78 jsr166 1.22 * containsValue returns true for held values
79 dl 1.1 */
80 dl 1.5 public void testContainsValue() {
81 dl 1.1 ConcurrentHashMap map = map5();
82 jsr166 1.17 assertTrue(map.containsValue("A"));
83 dl 1.11 assertFalse(map.containsValue("Z"));
84 dl 1.1 }
85    
86     /**
87 jsr166 1.22 * enumeration returns an enumeration containing the correct
88     * elements
89 dl 1.1 */
90 dl 1.5 public void testEnumeration() {
91 dl 1.1 ConcurrentHashMap map = map5();
92 jsr166 1.17 Enumeration e = map.elements();
93     int count = 0;
94     while (e.hasMoreElements()) {
95     count++;
96     e.nextElement();
97     }
98     assertEquals(5, count);
99 dl 1.4 }
100    
101     /**
102 jsr166 1.22 * get returns the correct element at the given key,
103     * or null if not present
104 dl 1.1 */
105 dl 1.5 public void testGet() {
106 dl 1.1 ConcurrentHashMap map = map5();
107 jsr166 1.17 assertEquals("A", (String)map.get(one));
108 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
109 dl 1.5 assertNull(map.get("anything"));
110 dl 1.1 }
111    
112     /**
113 jsr166 1.22 * isEmpty is true of empty map and false for non-empty
114 dl 1.1 */
115 dl 1.5 public void testIsEmpty() {
116 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
117     ConcurrentHashMap map = map5();
118 jsr166 1.17 assertTrue(empty.isEmpty());
119 dl 1.1 assertFalse(map.isEmpty());
120     }
121    
122     /**
123 jsr166 1.22 * keys returns an enumeration containing all the keys from the map
124 dl 1.1 */
125 dl 1.5 public void testKeys() {
126 dl 1.1 ConcurrentHashMap map = map5();
127 jsr166 1.17 Enumeration e = map.keys();
128     int count = 0;
129     while (e.hasMoreElements()) {
130     count++;
131     e.nextElement();
132     }
133     assertEquals(5, count);
134 dl 1.1 }
135    
136     /**
137 jsr166 1.22 * keySet returns a Set containing all the keys
138 dl 1.1 */
139 dl 1.5 public void testKeySet() {
140 dl 1.1 ConcurrentHashMap map = map5();
141 jsr166 1.17 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 dl 1.1 }
149    
150 dl 1.5 /**
151 jsr166 1.22 * keySet.toArray returns contains all keys
152 dl 1.13 */
153     public void testKeySetToArray() {
154     ConcurrentHashMap map = map5();
155 jsr166 1.17 Set s = map.keySet();
156 dl 1.13 Object[] ar = s.toArray();
157     assertTrue(s.containsAll(Arrays.asList(ar)));
158 jsr166 1.17 assertEquals(5, ar.length);
159 dl 1.13 ar[0] = m10;
160     assertFalse(s.containsAll(Arrays.asList(ar)));
161     }
162    
163     /**
164 jsr166 1.22 * Values.toArray contains all values
165 dl 1.13 */
166     public void testValuesToArray() {
167     ConcurrentHashMap map = map5();
168 jsr166 1.17 Collection v = map.values();
169 dl 1.13 Object[] ar = v.toArray();
170     ArrayList s = new ArrayList(Arrays.asList(ar));
171 jsr166 1.17 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 dl 1.13 }
178    
179     /**
180 jsr166 1.22 * entrySet.toArray contains all entries
181 dl 1.13 */
182     public void testEntrySetToArray() {
183     ConcurrentHashMap map = map5();
184 jsr166 1.17 Set s = map.entrySet();
185 dl 1.13 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 dl 1.5 * values collection contains all values
195     */
196     public void testValues() {
197 dl 1.1 ConcurrentHashMap map = map5();
198 jsr166 1.17 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 dl 1.1 }
206    
207 dl 1.5 /**
208     * entrySet contains all pairs
209     */
210     public void testEntrySet() {
211 dl 1.1 ConcurrentHashMap map = map5();
212 jsr166 1.17 Set s = map.entrySet();
213     assertEquals(5, s.size());
214 dl 1.1 Iterator it = s.iterator();
215     while (it.hasNext()) {
216     Map.Entry e = (Map.Entry) it.next();
217 jsr166 1.14 assertTrue(
218 dl 1.1 (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 jsr166 1.22 * putAll adds all key-value pairs from the given map
228 dl 1.1 */
229 dl 1.5 public void testPutAll() {
230 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
231     ConcurrentHashMap map = map5();
232 jsr166 1.17 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 dl 1.1 }
240    
241     /**
242 jsr166 1.22 * putIfAbsent works when the given key is not present
243 dl 1.1 */
244 dl 1.5 public void testPutIfAbsent() {
245 dl 1.1 ConcurrentHashMap map = map5();
246 jsr166 1.17 map.putIfAbsent(six, "Z");
247 dl 1.7 assertTrue(map.containsKey(six));
248 dl 1.1 }
249    
250     /**
251 jsr166 1.22 * putIfAbsent does not add the pair if the key is already present
252 dl 1.1 */
253 dl 1.5 public void testPutIfAbsent2() {
254 dl 1.1 ConcurrentHashMap map = map5();
255     assertEquals("A", map.putIfAbsent(one, "Z"));
256     }
257    
258     /**
259 jsr166 1.22 * replace fails when the given key is not present
260 dl 1.7 */
261     public void testReplace() {
262     ConcurrentHashMap map = map5();
263 jsr166 1.17 assertNull(map.replace(six, "Z"));
264 dl 1.7 assertFalse(map.containsKey(six));
265     }
266    
267     /**
268 jsr166 1.22 * replace succeeds if the key is already present
269 dl 1.7 */
270     public void testReplace2() {
271     ConcurrentHashMap map = map5();
272 dl 1.8 assertNotNull(map.replace(one, "Z"));
273 dl 1.7 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 jsr166 1.17 assertFalse(map.replace(one, "Z", "Z"));
283 dl 1.7 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 jsr166 1.17 assertTrue(map.replace(one, "A", "Z"));
293 dl 1.7 assertEquals("Z", map.get(one));
294     }
295    
296     /**
297 jsr166 1.22 * remove removes the correct key-value pair from the map
298 dl 1.1 */
299 dl 1.5 public void testRemove() {
300 dl 1.1 ConcurrentHashMap map = map5();
301 jsr166 1.17 map.remove(five);
302     assertEquals(4, map.size());
303     assertFalse(map.containsKey(five));
304 dl 1.1 }
305    
306 dl 1.5 /**
307     * remove(key,value) removes only if pair present
308     */
309     public void testRemove2() {
310 dl 1.1 ConcurrentHashMap map = map5();
311 jsr166 1.17 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 dl 1.1 }
318    
319     /**
320 jsr166 1.22 * size returns the correct values
321 dl 1.1 */
322 dl 1.5 public void testSize() {
323 dl 1.1 ConcurrentHashMap map = map5();
324     ConcurrentHashMap empty = new ConcurrentHashMap();
325 jsr166 1.17 assertEquals(0, empty.size());
326     assertEquals(5, map.size());
327 dl 1.1 }
328    
329 dl 1.5 /**
330     * toString contains toString of elements
331     */
332     public void testToString() {
333 dl 1.1 ConcurrentHashMap map = map5();
334     String s = map.toString();
335     for (int i = 1; i <= 5; ++i) {
336 jsr166 1.24 assertTrue(s.contains(String.valueOf(i)));
337 dl 1.1 }
338 jsr166 1.14 }
339 dl 1.1
340     // Exception tests
341 jsr166 1.14
342 dl 1.5 /**
343 jsr166 1.14 * Cannot create with negative capacity
344 dl 1.5 */
345     public void testConstructor1() {
346     try {
347 dl 1.1 new ConcurrentHashMap(-1,0,1);
348 dl 1.5 shouldThrow();
349 jsr166 1.19 } catch (IllegalArgumentException success) {}
350 dl 1.1 }
351    
352 dl 1.5 /**
353     * Cannot create with negative concurrency level
354     */
355     public void testConstructor2() {
356     try {
357 dl 1.1 new ConcurrentHashMap(1,0,-1);
358 dl 1.5 shouldThrow();
359 jsr166 1.19 } catch (IllegalArgumentException success) {}
360 dl 1.1 }
361    
362 dl 1.5 /**
363     * Cannot create with only negative capacity
364     */
365     public void testConstructor3() {
366     try {
367 dl 1.1 new ConcurrentHashMap(-1);
368 dl 1.5 shouldThrow();
369 jsr166 1.19 } catch (IllegalArgumentException success) {}
370 dl 1.1 }
371    
372 dl 1.5 /**
373     * get(null) throws NPE
374     */
375     public void testGet_NullPointerException() {
376     try {
377 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
378     c.get(null);
379 dl 1.5 shouldThrow();
380 jsr166 1.19 } catch (NullPointerException success) {}
381 dl 1.1 }
382    
383 dl 1.5 /**
384     * containsKey(null) throws NPE
385     */
386     public void testContainsKey_NullPointerException() {
387     try {
388 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
389     c.containsKey(null);
390 dl 1.5 shouldThrow();
391 jsr166 1.19 } catch (NullPointerException success) {}
392 dl 1.1 }
393    
394 dl 1.5 /**
395     * containsValue(null) throws NPE
396     */
397     public void testContainsValue_NullPointerException() {
398     try {
399 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
400     c.containsValue(null);
401 dl 1.5 shouldThrow();
402 jsr166 1.19 } catch (NullPointerException success) {}
403 dl 1.1 }
404    
405 dl 1.5 /**
406     * contains(null) throws NPE
407     */
408     public void testContains_NullPointerException() {
409     try {
410 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
411     c.contains(null);
412 dl 1.5 shouldThrow();
413 jsr166 1.19 } catch (NullPointerException success) {}
414 dl 1.1 }
415    
416 dl 1.5 /**
417     * put(null,x) throws NPE
418     */
419     public void testPut1_NullPointerException() {
420     try {
421 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
422     c.put(null, "whatever");
423 dl 1.5 shouldThrow();
424 jsr166 1.19 } catch (NullPointerException success) {}
425 dl 1.1 }
426    
427 dl 1.5 /**
428     * put(x, null) throws NPE
429     */
430     public void testPut2_NullPointerException() {
431     try {
432 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
433     c.put("whatever", null);
434 dl 1.5 shouldThrow();
435 jsr166 1.19 } catch (NullPointerException success) {}
436 dl 1.1 }
437    
438 dl 1.5 /**
439     * putIfAbsent(null, x) throws NPE
440     */
441     public void testPutIfAbsent1_NullPointerException() {
442     try {
443 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
444     c.putIfAbsent(null, "whatever");
445 dl 1.5 shouldThrow();
446 jsr166 1.19 } catch (NullPointerException success) {}
447 dl 1.1 }
448    
449 dl 1.5 /**
450 dl 1.7 * 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 jsr166 1.19 } catch (NullPointerException success) {}
458 dl 1.7 }
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 jsr166 1.19 } catch (NullPointerException success) {}
469 dl 1.7 }
470    
471     /**
472 dl 1.5 * putIfAbsent(x, null) throws NPE
473     */
474     public void testPutIfAbsent2_NullPointerException() {
475     try {
476 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
477     c.putIfAbsent("whatever", null);
478 dl 1.7 shouldThrow();
479 jsr166 1.19 } catch (NullPointerException success) {}
480 dl 1.7 }
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 jsr166 1.19 } catch (NullPointerException success) {}
491 dl 1.7 }
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 jsr166 1.19 } catch (NullPointerException success) {}
502 dl 1.7 }
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 dl 1.5 shouldThrow();
512 jsr166 1.19 } catch (NullPointerException success) {}
513 dl 1.1 }
514    
515 dl 1.5 /**
516     * remove(null) throws NPE
517     */
518     public void testRemove1_NullPointerException() {
519     try {
520 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
521     c.put("sadsdf", "asdads");
522     c.remove(null);
523 dl 1.5 shouldThrow();
524 jsr166 1.19 } catch (NullPointerException success) {}
525 dl 1.1 }
526    
527 dl 1.5 /**
528     * remove(null, x) throws NPE
529     */
530     public void testRemove2_NullPointerException() {
531     try {
532 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
533     c.put("sadsdf", "asdads");
534     c.remove(null, "whatever");
535 dl 1.5 shouldThrow();
536 jsr166 1.19 } catch (NullPointerException success) {}
537 dl 1.1 }
538 dl 1.2
539 dl 1.5 /**
540 dl 1.12 * remove(x, null) returns false
541     */
542     public void testRemove3() {
543 jsr166 1.19 ConcurrentHashMap c = new ConcurrentHashMap(5);
544     c.put("sadsdf", "asdads");
545     assertFalse(c.remove("sadsdf", null));
546 dl 1.12 }
547    
548     /**
549 dl 1.5 * A deserialized map equals original
550     */
551 jsr166 1.18 public void testSerialization() throws Exception {
552 jsr166 1.25 Map x = map5();
553     Map y = serialClone(x);
554 dl 1.2
555 jsr166 1.25 assertTrue(x != y);
556     assertEquals(x.size(), y.size());
557     assertEquals(x, y);
558     assertEquals(y, x);
559 dl 1.2 }
560 dl 1.6
561     /**
562     * SetValue of an EntrySet entry sets value in the map.
563     */
564     public void testSetValueWriteThrough() {
565 jsr166 1.14 // Adapted from a bug report by Eric Zoerner
566 dl 1.6 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 jsr166 1.14
573 dl 1.6 // assert that entry1 is not 16
574     assertTrue("entry is 16, test not valid",
575     !entry1.getKey().equals(new Integer(16)));
576 jsr166 1.14
577     // remove 16 (a different key) from map
578 dl 1.6 // 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 jsr166 1.14
584 dl 1.1 }