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

# 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/licenses/publicdomain
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.*;
12 import java.util.Enumeration;
13 import java.io.*;
14
15 public class ConcurrentHashMapTest extends JSR166TestCase{
16 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 /**
24 * Create a map from Integers 1-5 to Strings "A"-"E".
25 */
26 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 * clear removes all pairs
41 */
42 public void testClear() {
43 ConcurrentHashMap map = map5();
44 map.clear();
45 assertEquals(map.size(), 0);
46 }
47
48 /**
49 * 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 */
64 public void testContains() {
65 ConcurrentHashMap map = map5();
66 assertTrue(map.contains("A"));
67 assertFalse(map.contains("Z"));
68 }
69
70 /**
71 * containsKey returns true for contained key
72 */
73 public void testContainsKey() {
74 ConcurrentHashMap map = map5();
75 assertTrue(map.containsKey(one));
76 assertFalse(map.containsKey(zero));
77 }
78
79 /**
80 * containsValue returns true for held values
81 */
82 public void testContainsValue() {
83 ConcurrentHashMap map = map5();
84 assertTrue(map.contains("A"));
85 assertFalse(map.contains("Z"));
86 }
87
88 /**
89 * enumeration returns an enumeration containing the correct
90 * elements
91 */
92 public void testEnumeration() {
93 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 }
102
103 /**
104 * Clone creates an equal map
105 */
106 public void testClone() {
107 ConcurrentHashMap map = map5();
108 ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone());
109 assertEquals(map, m2);
110 }
111
112 /**
113 * get returns the correct element at the given key,
114 * or null if not present
115 */
116 public void testGet() {
117 ConcurrentHashMap map = map5();
118 assertEquals("A", (String)map.get(one));
119 ConcurrentHashMap empty = new ConcurrentHashMap();
120 assertNull(map.get("anything"));
121 }
122
123 /**
124 * isEmpty is true of empty map and false for non-empty
125 */
126 public void testIsEmpty() {
127 ConcurrentHashMap empty = new ConcurrentHashMap();
128 ConcurrentHashMap map = map5();
129 assertTrue(empty.isEmpty());
130 assertFalse(map.isEmpty());
131 }
132
133 /**
134 * keys returns an enumeration containing all the keys from the map
135 */
136 public void testKeys() {
137 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 * keySet returns a Set containing all the keys
149 */
150 public void testKeySet() {
151 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 /**
162 * values collection contains all values
163 */
164 public void testValues() {
165 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 /**
176 * entrySet contains all pairs
177 */
178 public void testEntrySet() {
179 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 * putAll adds all key-value pairs from the given map
196 */
197 public void testPutAll() {
198 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 * putIfAbsent works when the given key is not present
211 */
212 public void testPutIfAbsent() {
213 ConcurrentHashMap map = map5();
214 map.putIfAbsent(six, "Z");
215 assertTrue(map.containsKey(six));
216 }
217
218 /**
219 * putIfAbsent does not add the pair if the key is already present
220 */
221 public void testPutIfAbsent2() {
222 ConcurrentHashMap map = map5();
223 assertEquals("A", map.putIfAbsent(one, "Z"));
224 }
225
226 /**
227 * replace fails when the given key is not present
228 */
229 public void testReplace() {
230 ConcurrentHashMap map = map5();
231 assertNull(map.replace(six, "Z"));
232 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 assertNotNull(map.replace(one, "Z"));
241 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 * remove removes the correct key-value pair from the map
268 */
269 public void testRemove() {
270 ConcurrentHashMap map = map5();
271 map.remove(five);
272 assertEquals(4, map.size());
273 assertFalse(map.containsKey(five));
274 }
275
276 /**
277 * remove(key,value) removes only if pair present
278 */
279 public void testRemove2() {
280 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 * size returns the correct values
292 */
293 public void testSize() {
294 ConcurrentHashMap map = map5();
295 ConcurrentHashMap empty = new ConcurrentHashMap();
296 assertEquals(0, empty.size());
297 assertEquals(5, map.size());
298 }
299
300 /**
301 * toString contains toString of elements
302 */
303 public void testToString() {
304 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 /**
314 * Cannot create with negative capacity
315 */
316 public void testConstructor1() {
317 try {
318 new ConcurrentHashMap(-1,0,1);
319 shouldThrow();
320 } catch(IllegalArgumentException e){}
321 }
322
323 /**
324 * Cannot create with negative concurrency level
325 */
326 public void testConstructor2() {
327 try {
328 new ConcurrentHashMap(1,0,-1);
329 shouldThrow();
330 } catch(IllegalArgumentException e){}
331 }
332
333 /**
334 * Cannot create with only negative capacity
335 */
336 public void testConstructor3() {
337 try {
338 new ConcurrentHashMap(-1);
339 shouldThrow();
340 } catch(IllegalArgumentException e){}
341 }
342
343 /**
344 * get(null) throws NPE
345 */
346 public void testGet_NullPointerException() {
347 try {
348 ConcurrentHashMap c = new ConcurrentHashMap(5);
349 c.get(null);
350 shouldThrow();
351 } catch(NullPointerException e){}
352 }
353
354 /**
355 * containsKey(null) throws NPE
356 */
357 public void testContainsKey_NullPointerException() {
358 try {
359 ConcurrentHashMap c = new ConcurrentHashMap(5);
360 c.containsKey(null);
361 shouldThrow();
362 } catch(NullPointerException e){}
363 }
364
365 /**
366 * containsValue(null) throws NPE
367 */
368 public void testContainsValue_NullPointerException() {
369 try {
370 ConcurrentHashMap c = new ConcurrentHashMap(5);
371 c.containsValue(null);
372 shouldThrow();
373 } catch(NullPointerException e){}
374 }
375
376 /**
377 * contains(null) throws NPE
378 */
379 public void testContains_NullPointerException() {
380 try {
381 ConcurrentHashMap c = new ConcurrentHashMap(5);
382 c.contains(null);
383 shouldThrow();
384 } catch(NullPointerException e){}
385 }
386
387 /**
388 * put(null,x) throws NPE
389 */
390 public void testPut1_NullPointerException() {
391 try {
392 ConcurrentHashMap c = new ConcurrentHashMap(5);
393 c.put(null, "whatever");
394 shouldThrow();
395 } catch(NullPointerException e){}
396 }
397
398 /**
399 * put(x, null) throws NPE
400 */
401 public void testPut2_NullPointerException() {
402 try {
403 ConcurrentHashMap c = new ConcurrentHashMap(5);
404 c.put("whatever", null);
405 shouldThrow();
406 } catch(NullPointerException e){}
407 }
408
409 /**
410 * putIfAbsent(null, x) throws NPE
411 */
412 public void testPutIfAbsent1_NullPointerException() {
413 try {
414 ConcurrentHashMap c = new ConcurrentHashMap(5);
415 c.putIfAbsent(null, "whatever");
416 shouldThrow();
417 } catch(NullPointerException e){}
418 }
419
420 /**
421 * 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 * putIfAbsent(x, null) throws NPE
444 */
445 public void testPutIfAbsent2_NullPointerException() {
446 try {
447 ConcurrentHashMap c = new ConcurrentHashMap(5);
448 c.putIfAbsent("whatever", null);
449 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 shouldThrow();
484 } catch(NullPointerException e){}
485 }
486
487
488 /**
489 * remove(null) throws NPE
490 */
491 public void testRemove1_NullPointerException() {
492 try {
493 ConcurrentHashMap c = new ConcurrentHashMap(5);
494 c.put("sadsdf", "asdads");
495 c.remove(null);
496 shouldThrow();
497 } catch(NullPointerException e){}
498 }
499
500 /**
501 * remove(null, x) throws NPE
502 */
503 public void testRemove2_NullPointerException() {
504 try {
505 ConcurrentHashMap c = new ConcurrentHashMap(5);
506 c.put("sadsdf", "asdads");
507 c.remove(null, "whatever");
508 shouldThrow();
509 } catch(NullPointerException e){}
510 }
511
512 /**
513 * A deserialized map equals original
514 */
515 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 unexpectedException();
533 }
534 }
535
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
560 }