ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.9
Committed: Sat Dec 27 19:26:43 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.8: +5 -4 lines
Log Message:
Headers reference Creative Commons

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