ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.20
Committed: Tue Dec 1 09:48:13 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.19: +0 -1 lines
Log Message:
whitespace

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