ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.24
Committed: Fri May 27 19:26:42 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.23: +1 -6 lines
Log Message:
indexOf => contains

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     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.21 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 jsr166 1.22 * 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 jsr166 1.22 * Maps with same contents are equal
50 dl 1.5 */
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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * keySet.toArray returns contains all keys
154 dl 1.13 */
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 jsr166 1.22 * Values.toArray contains all values
167 dl 1.13 */
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 jsr166 1.22 * entrySet.toArray contains all entries
183 dl 1.13 */
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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * 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 jsr166 1.22 * replace fails when the given key is not present
262 dl 1.7 */
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 jsr166 1.22 * replace succeeds if the key is already present
271 dl 1.7 */
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     * replace value fails when the given key not mapped to expected value
280     */
281     public void testReplaceValue() {
282     ConcurrentHashMap map = map5();
283     assertEquals("A", map.get(one));
284 jsr166 1.17 assertFalse(map.replace(one, "Z", "Z"));
285 dl 1.7 assertEquals("A", map.get(one));
286     }
287    
288     /**
289     * replace value succeeds when the given key mapped to expected value
290     */
291     public void testReplaceValue2() {
292     ConcurrentHashMap map = map5();
293     assertEquals("A", map.get(one));
294 jsr166 1.17 assertTrue(map.replace(one, "A", "Z"));
295 dl 1.7 assertEquals("Z", map.get(one));
296     }
297    
298     /**
299 jsr166 1.22 * remove removes the correct key-value pair from the map
300 dl 1.1 */
301 dl 1.5 public void testRemove() {
302 dl 1.1 ConcurrentHashMap map = map5();
303 jsr166 1.17 map.remove(five);
304     assertEquals(4, map.size());
305     assertFalse(map.containsKey(five));
306 dl 1.1 }
307    
308 dl 1.5 /**
309     * remove(key,value) removes only if pair present
310     */
311     public void testRemove2() {
312 dl 1.1 ConcurrentHashMap map = map5();
313 jsr166 1.17 map.remove(five, "E");
314     assertEquals(4, map.size());
315     assertFalse(map.containsKey(five));
316     map.remove(four, "A");
317     assertEquals(4, map.size());
318     assertTrue(map.containsKey(four));
319 dl 1.1 }
320    
321     /**
322 jsr166 1.22 * size returns the correct values
323 dl 1.1 */
324 dl 1.5 public void testSize() {
325 dl 1.1 ConcurrentHashMap map = map5();
326     ConcurrentHashMap empty = new ConcurrentHashMap();
327 jsr166 1.17 assertEquals(0, empty.size());
328     assertEquals(5, map.size());
329 dl 1.1 }
330    
331 dl 1.5 /**
332     * toString contains toString of elements
333     */
334     public void testToString() {
335 dl 1.1 ConcurrentHashMap map = map5();
336     String s = map.toString();
337     for (int i = 1; i <= 5; ++i) {
338 jsr166 1.24 assertTrue(s.contains(String.valueOf(i)));
339 dl 1.1 }
340 jsr166 1.14 }
341 dl 1.1
342     // Exception tests
343 jsr166 1.14
344 dl 1.5 /**
345 jsr166 1.14 * Cannot create with negative capacity
346 dl 1.5 */
347     public void testConstructor1() {
348     try {
349 dl 1.1 new ConcurrentHashMap(-1,0,1);
350 dl 1.5 shouldThrow();
351 jsr166 1.19 } catch (IllegalArgumentException success) {}
352 dl 1.1 }
353    
354 dl 1.5 /**
355     * Cannot create with negative concurrency level
356     */
357     public void testConstructor2() {
358     try {
359 dl 1.1 new ConcurrentHashMap(1,0,-1);
360 dl 1.5 shouldThrow();
361 jsr166 1.19 } catch (IllegalArgumentException success) {}
362 dl 1.1 }
363    
364 dl 1.5 /**
365     * Cannot create with only negative capacity
366     */
367     public void testConstructor3() {
368     try {
369 dl 1.1 new ConcurrentHashMap(-1);
370 dl 1.5 shouldThrow();
371 jsr166 1.19 } catch (IllegalArgumentException success) {}
372 dl 1.1 }
373    
374 dl 1.5 /**
375     * get(null) throws NPE
376     */
377     public void testGet_NullPointerException() {
378     try {
379 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
380     c.get(null);
381 dl 1.5 shouldThrow();
382 jsr166 1.19 } catch (NullPointerException success) {}
383 dl 1.1 }
384    
385 dl 1.5 /**
386     * containsKey(null) throws NPE
387     */
388     public void testContainsKey_NullPointerException() {
389     try {
390 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
391     c.containsKey(null);
392 dl 1.5 shouldThrow();
393 jsr166 1.19 } catch (NullPointerException success) {}
394 dl 1.1 }
395    
396 dl 1.5 /**
397     * containsValue(null) throws NPE
398     */
399     public void testContainsValue_NullPointerException() {
400     try {
401 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
402     c.containsValue(null);
403 dl 1.5 shouldThrow();
404 jsr166 1.19 } catch (NullPointerException success) {}
405 dl 1.1 }
406    
407 dl 1.5 /**
408     * contains(null) throws NPE
409     */
410     public void testContains_NullPointerException() {
411     try {
412 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
413     c.contains(null);
414 dl 1.5 shouldThrow();
415 jsr166 1.19 } catch (NullPointerException success) {}
416 dl 1.1 }
417    
418 dl 1.5 /**
419     * put(null,x) throws NPE
420     */
421     public void testPut1_NullPointerException() {
422     try {
423 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
424     c.put(null, "whatever");
425 dl 1.5 shouldThrow();
426 jsr166 1.19 } catch (NullPointerException success) {}
427 dl 1.1 }
428    
429 dl 1.5 /**
430     * put(x, null) throws NPE
431     */
432     public void testPut2_NullPointerException() {
433     try {
434 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
435     c.put("whatever", null);
436 dl 1.5 shouldThrow();
437 jsr166 1.19 } catch (NullPointerException success) {}
438 dl 1.1 }
439    
440 dl 1.5 /**
441     * putIfAbsent(null, x) throws NPE
442     */
443     public void testPutIfAbsent1_NullPointerException() {
444     try {
445 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
446     c.putIfAbsent(null, "whatever");
447 dl 1.5 shouldThrow();
448 jsr166 1.19 } catch (NullPointerException success) {}
449 dl 1.1 }
450    
451 dl 1.5 /**
452 dl 1.7 * replace(null, x) throws NPE
453     */
454     public void testReplace_NullPointerException() {
455     try {
456     ConcurrentHashMap c = new ConcurrentHashMap(5);
457     c.replace(null, "whatever");
458     shouldThrow();
459 jsr166 1.19 } catch (NullPointerException success) {}
460 dl 1.7 }
461    
462     /**
463     * replace(null, x, y) throws NPE
464     */
465     public void testReplaceValue_NullPointerException() {
466     try {
467     ConcurrentHashMap c = new ConcurrentHashMap(5);
468     c.replace(null, one, "whatever");
469     shouldThrow();
470 jsr166 1.19 } catch (NullPointerException success) {}
471 dl 1.7 }
472    
473     /**
474 dl 1.5 * putIfAbsent(x, null) throws NPE
475     */
476     public void testPutIfAbsent2_NullPointerException() {
477     try {
478 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
479     c.putIfAbsent("whatever", null);
480 dl 1.7 shouldThrow();
481 jsr166 1.19 } catch (NullPointerException success) {}
482 dl 1.7 }
483    
484     /**
485     * replace(x, null) throws NPE
486     */
487     public void testReplace2_NullPointerException() {
488     try {
489     ConcurrentHashMap c = new ConcurrentHashMap(5);
490     c.replace("whatever", null);
491     shouldThrow();
492 jsr166 1.19 } catch (NullPointerException success) {}
493 dl 1.7 }
494    
495     /**
496     * replace(x, null, y) throws NPE
497     */
498     public void testReplaceValue2_NullPointerException() {
499     try {
500     ConcurrentHashMap c = new ConcurrentHashMap(5);
501     c.replace("whatever", null, "A");
502     shouldThrow();
503 jsr166 1.19 } catch (NullPointerException success) {}
504 dl 1.7 }
505    
506     /**
507     * replace(x, y, null) throws NPE
508     */
509     public void testReplaceValue3_NullPointerException() {
510     try {
511     ConcurrentHashMap c = new ConcurrentHashMap(5);
512     c.replace("whatever", one, null);
513 dl 1.5 shouldThrow();
514 jsr166 1.19 } catch (NullPointerException success) {}
515 dl 1.1 }
516    
517 dl 1.5 /**
518     * remove(null) throws NPE
519     */
520     public void testRemove1_NullPointerException() {
521     try {
522 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
523     c.put("sadsdf", "asdads");
524     c.remove(null);
525 dl 1.5 shouldThrow();
526 jsr166 1.19 } catch (NullPointerException success) {}
527 dl 1.1 }
528    
529 dl 1.5 /**
530     * remove(null, x) throws NPE
531     */
532     public void testRemove2_NullPointerException() {
533     try {
534 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
535     c.put("sadsdf", "asdads");
536     c.remove(null, "whatever");
537 dl 1.5 shouldThrow();
538 jsr166 1.19 } catch (NullPointerException success) {}
539 dl 1.1 }
540 dl 1.2
541 dl 1.5 /**
542 dl 1.12 * remove(x, null) returns false
543     */
544     public void testRemove3() {
545 jsr166 1.19 ConcurrentHashMap c = new ConcurrentHashMap(5);
546     c.put("sadsdf", "asdads");
547     assertFalse(c.remove("sadsdf", null));
548 dl 1.12 }
549    
550     /**
551 dl 1.5 * A deserialized map equals original
552     */
553 jsr166 1.18 public void testSerialization() throws Exception {
554 dl 1.2 ConcurrentHashMap q = map5();
555    
556 jsr166 1.18 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
557     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
558     out.writeObject(q);
559     out.close();
560    
561     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
562     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
563     ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
564     assertEquals(q.size(), r.size());
565     assertTrue(q.equals(r));
566     assertTrue(r.equals(q));
567 dl 1.2 }
568 dl 1.6
569     /**
570     * SetValue of an EntrySet entry sets value in the map.
571     */
572     public void testSetValueWriteThrough() {
573 jsr166 1.14 // Adapted from a bug report by Eric Zoerner
574 dl 1.6 ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
575     assertTrue(map.isEmpty());
576     for (int i = 0; i < 20; i++)
577     map.put(new Integer(i), new Integer(i));
578     assertFalse(map.isEmpty());
579     Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
580 jsr166 1.14
581 dl 1.6 // assert that entry1 is not 16
582     assertTrue("entry is 16, test not valid",
583     !entry1.getKey().equals(new Integer(16)));
584 jsr166 1.14
585     // remove 16 (a different key) from map
586 dl 1.6 // which just happens to cause entry1 to be cloned in map
587     map.remove(new Integer(16));
588     entry1.setValue("XYZ");
589     assertTrue(map.containsValue("XYZ")); // fails
590     }
591 jsr166 1.14
592 dl 1.1 }