ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentHashMapTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.12 by dl, Fri May 20 14:31:06 2005 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.*;
# Line 19 | Line 20 | public class ConcurrentHashMapTest exten
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());
# Line 33 | Line 37 | public class ConcurrentHashMapTest exten
37      }
38  
39      /**
40 <     *   clear  removes all key-element pairs from the map
40 >     *  clear removes all pairs
41       */
42 <    public void testClear(){
42 >    public void testClear() {
43          ConcurrentHashMap map = map5();
44          map.clear();
45          assertEquals(map.size(), 0);
46      }
47  
48      /**
49 <     *   contains gives the appropriate value
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(){
64 >    public void testContains() {
65          ConcurrentHashMap map = map5();
66          assertTrue(map.contains("A"));
67          assertFalse(map.contains("Z"));
68      }
69      
70      /**
71 <     *   containsKey gives the appropriate value
71 >     *  containsKey returns true for contained key
72       */
73 <    public void testContainsKey(){
73 >    public void testContainsKey() {
74          ConcurrentHashMap map = map5();
75          assertTrue(map.containsKey(one));
76 <        assertFalse(map.containsKey(new Integer(100)));
76 >        assertFalse(map.containsKey(zero));
77      }
78  
79      /**
80 <     *  Identical to normal contains
80 >     *  containsValue returns true for held values
81       */
82 <    public void testContainsValue(){
82 >    public void testContainsValue() {
83          ConcurrentHashMap map = map5();
84 <        assertTrue(map.contains("A"));
85 <        assertFalse(map.contains("Z"));
84 >        assertTrue(map.containsValue("A"));
85 >        assertFalse(map.containsValue("Z"));
86      }
87  
88      /**
89 <     *  tes to verify enumeration returns an enumeration containing the correct elements
89 >     *   enumeration returns an enumeration containing the correct
90 >     *   elements
91       */
92 <    public void testEnumeration(){
92 >    public void testEnumeration() {
93          ConcurrentHashMap map = map5();
94          Enumeration e = map.elements();
95          int count = 0;
# Line 83 | Line 101 | public class ConcurrentHashMapTest exten
101      }
102  
103      /**
104 <     *   get returns the correct element at the given index
104 >     *  get returns the correct element at the given key,
105 >     *  or null if not present
106       */
107 <    public void testGet(){
107 >    public void testGet() {
108          ConcurrentHashMap map = map5();
109          assertEquals("A", (String)map.get(one));
91    }
92
93    /**
94     *   get on a nonexistant key returns null
95     */
96    public void testGet2(){
110          ConcurrentHashMap empty = new ConcurrentHashMap();
111 <        assertNull(empty.get("anything"));
111 >        assertNull(map.get("anything"));
112      }
113  
114      /**
115 <     *  Simple test to verify isEmpty returns the correct value
115 >     *  isEmpty is true of empty map and false for non-empty
116       */
117 <    public void testIsEmpty(){
117 >    public void testIsEmpty() {
118          ConcurrentHashMap empty = new ConcurrentHashMap();
119          ConcurrentHashMap map = map5();
120          assertTrue(empty.isEmpty());
# Line 111 | Line 124 | public class ConcurrentHashMapTest exten
124      /**
125       *   keys returns an enumeration containing all the keys from the map
126       */
127 <    public void testKeys(){
127 >    public void testKeys() {
128          ConcurrentHashMap map = map5();
129          Enumeration e = map.keys();
130          int count = 0;
# Line 125 | Line 138 | public class ConcurrentHashMapTest exten
138      /**
139       *   keySet returns a Set containing all the keys
140       */
141 <    public void testKeySet(){
141 >    public void testKeySet() {
142          ConcurrentHashMap map = map5();
143          Set s = map.keySet();
144          assertEquals(5, s.size());
# Line 136 | Line 149 | public class ConcurrentHashMapTest exten
149          assertTrue(s.contains(five));
150      }
151  
152 <    public void testValues(){
152 >    /**
153 >     * values collection contains all values
154 >     */
155 >    public void testValues() {
156          ConcurrentHashMap map = map5();
157          Collection s = map.values();
158          assertEquals(5, s.size());
# Line 147 | Line 163 | public class ConcurrentHashMapTest exten
163          assertTrue(s.contains("E"));
164      }
165  
166 <    public void testEntrySet(){
166 >    /**
167 >     * entrySet contains all pairs
168 >     */
169 >    public void testEntrySet() {
170          ConcurrentHashMap map = map5();
171          Set s = map.entrySet();
172          assertEquals(5, s.size());
# Line 166 | Line 185 | public class ConcurrentHashMapTest exten
185      /**
186       *   putAll  adds all key-value pairs from the given map
187       */
188 <    public void testPutAll(){
188 >    public void testPutAll() {
189          ConcurrentHashMap empty = new ConcurrentHashMap();
190          ConcurrentHashMap map = map5();
191          empty.putAll(map);
# Line 181 | Line 200 | public class ConcurrentHashMapTest exten
200      /**
201       *   putIfAbsent works when the given key is not present
202       */
203 <    public void testPutIfAbsent(){
203 >    public void testPutIfAbsent() {
204          ConcurrentHashMap map = map5();
205 <        map.putIfAbsent(new Integer(6), "Z");
206 <        assertTrue(map.containsKey(new Integer(6)));
205 >        map.putIfAbsent(six, "Z");
206 >        assertTrue(map.containsKey(six));
207      }
208  
209      /**
210       *   putIfAbsent does not add the pair if the key is already present
211       */
212 <    public void testPutIfAbsent2(){
212 >    public void testPutIfAbsent2() {
213          ConcurrentHashMap map = map5();
214          assertEquals("A", map.putIfAbsent(one, "Z"));
215      }
216  
217      /**
218 +     *   replace fails when the given key is not present
219 +     */
220 +    public void testReplace() {
221 +        ConcurrentHashMap map = map5();
222 +        assertNull(map.replace(six, "Z"));
223 +        assertFalse(map.containsKey(six));
224 +    }
225 +
226 +    /**
227 +     *   replace succeeds if the key is already present
228 +     */
229 +    public void testReplace2() {
230 +        ConcurrentHashMap map = map5();
231 +        assertNotNull(map.replace(one, "Z"));
232 +        assertEquals("Z", map.get(one));
233 +    }
234 +
235 +
236 +    /**
237 +     * replace value fails when the given key not mapped to expected value
238 +     */
239 +    public void testReplaceValue() {
240 +        ConcurrentHashMap map = map5();
241 +        assertEquals("A", map.get(one));
242 +        assertFalse(map.replace(one, "Z", "Z"));
243 +        assertEquals("A", map.get(one));
244 +    }
245 +
246 +    /**
247 +     * replace value succeeds when the given key mapped to expected value
248 +     */
249 +    public void testReplaceValue2() {
250 +        ConcurrentHashMap map = map5();
251 +        assertEquals("A", map.get(one));
252 +        assertTrue(map.replace(one, "A", "Z"));
253 +        assertEquals("Z", map.get(one));
254 +    }
255 +
256 +
257 +    /**
258       *   remove removes the correct key-value pair from the map
259       */
260 <    public void testRemove(){
260 >    public void testRemove() {
261          ConcurrentHashMap map = map5();
262          map.remove(five);
263          assertEquals(4, map.size());
264          assertFalse(map.containsKey(five));
265      }
266  
267 <    public void testRemove2(){
267 >    /**
268 >     * remove(key,value) removes only if pair present
269 >     */
270 >    public void testRemove2() {
271          ConcurrentHashMap map = map5();
272          map.remove(five, "E");
273          assertEquals(4, map.size());
# Line 219 | Line 281 | public class ConcurrentHashMapTest exten
281      /**
282       *   size returns the correct values
283       */
284 <    public void testSize(){
284 >    public void testSize() {
285          ConcurrentHashMap map = map5();
286          ConcurrentHashMap empty = new ConcurrentHashMap();
287          assertEquals(0, empty.size());
288          assertEquals(5, map.size());
289      }
290  
291 <    public void testToString(){
291 >    /**
292 >     * toString contains toString of elements
293 >     */
294 >    public void testToString() {
295          ConcurrentHashMap map = map5();
296          String s = map.toString();
297          for (int i = 1; i <= 5; ++i) {
# Line 236 | Line 301 | public class ConcurrentHashMapTest exten
301  
302      // Exception tests
303      
304 <    public void testConstructor1(){
305 <        try{
304 >    /**
305 >     * Cannot create with negative capacity
306 >     */
307 >    public void testConstructor1() {
308 >        try {
309              new ConcurrentHashMap(-1,0,1);
310 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
311 <        }catch(IllegalArgumentException e){}
310 >            shouldThrow();
311 >        } catch(IllegalArgumentException e){}
312      }
313  
314 <    public void testConstructor2(){
315 <        try{
314 >    /**
315 >     * Cannot create with negative concurrency level
316 >     */
317 >    public void testConstructor2() {
318 >        try {
319              new ConcurrentHashMap(1,0,-1);
320 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
321 <        }catch(IllegalArgumentException e){}
320 >            shouldThrow();
321 >        } catch(IllegalArgumentException e){}
322      }
323  
324 <    public void testConstructor3(){
325 <        try{
324 >    /**
325 >     * Cannot create with only negative capacity
326 >     */
327 >    public void testConstructor3() {
328 >        try {
329              new ConcurrentHashMap(-1);
330 <            fail("ConcurrentHashMap(int) should throw Illegal Argument Exception");
331 <        }catch(IllegalArgumentException e){}
330 >            shouldThrow();
331 >        } catch(IllegalArgumentException e){}
332      }
333  
334 <    public void testGet_NullPointerException(){
335 <        try{
334 >    /**
335 >     * get(null) throws NPE
336 >     */
337 >    public void testGet_NullPointerException() {
338 >        try {
339              ConcurrentHashMap c = new ConcurrentHashMap(5);
340              c.get(null);
341 <            fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception");
342 <        }catch(NullPointerException e){}
341 >            shouldThrow();
342 >        } catch(NullPointerException e){}
343      }
344  
345 <    public void testContainsKey_NullPointerException(){
346 <        try{
345 >    /**
346 >     * containsKey(null) throws NPE
347 >     */
348 >    public void testContainsKey_NullPointerException() {
349 >        try {
350              ConcurrentHashMap c = new ConcurrentHashMap(5);
351              c.containsKey(null);
352 <            fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception");
353 <        }catch(NullPointerException e){}
352 >            shouldThrow();
353 >        } catch(NullPointerException e){}
354      }
355  
356 <    public void testContainsValue_NullPointerException(){
357 <        try{
356 >    /**
357 >     * containsValue(null) throws NPE
358 >     */
359 >    public void testContainsValue_NullPointerException() {
360 >        try {
361              ConcurrentHashMap c = new ConcurrentHashMap(5);
362              c.containsValue(null);
363 <            fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception");
364 <        }catch(NullPointerException e){}
363 >            shouldThrow();
364 >        } catch(NullPointerException e){}
365      }
366  
367 <    public void testContains_NullPointerException(){
368 <        try{
367 >    /**
368 >     * contains(null) throws NPE
369 >     */
370 >    public void testContains_NullPointerException() {
371 >        try {
372              ConcurrentHashMap c = new ConcurrentHashMap(5);
373              c.contains(null);
374 <            fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception");
375 <        }catch(NullPointerException e){}
374 >            shouldThrow();
375 >        } catch(NullPointerException e){}
376      }
377  
378 <    public void testPut1_NullPointerException(){
379 <        try{
378 >    /**
379 >     * put(null,x) throws NPE
380 >     */
381 >    public void testPut1_NullPointerException() {
382 >        try {
383              ConcurrentHashMap c = new ConcurrentHashMap(5);
384              c.put(null, "whatever");
385 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
386 <        }catch(NullPointerException e){}
385 >            shouldThrow();
386 >        } catch(NullPointerException e){}
387      }
388  
389 <    public void testPut2_NullPointerException(){
390 <        try{
389 >    /**
390 >     * put(x, null) throws NPE
391 >     */
392 >    public void testPut2_NullPointerException() {
393 >        try {
394              ConcurrentHashMap c = new ConcurrentHashMap(5);
395              c.put("whatever", null);
396 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
397 <        }catch(NullPointerException e){}
396 >            shouldThrow();
397 >        } catch(NullPointerException e){}
398      }
399  
400 <    public void testPutIfAbsent1_NullPointerException(){
401 <        try{
400 >    /**
401 >     * putIfAbsent(null, x) throws NPE
402 >     */
403 >    public void testPutIfAbsent1_NullPointerException() {
404 >        try {
405              ConcurrentHashMap c = new ConcurrentHashMap(5);
406              c.putIfAbsent(null, "whatever");
407 <            fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
408 <        }catch(NullPointerException e){}
407 >            shouldThrow();
408 >        } catch(NullPointerException e){}
409 >    }
410 >
411 >    /**
412 >     * replace(null, x) throws NPE
413 >     */
414 >    public void testReplace_NullPointerException() {
415 >        try {
416 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
417 >            c.replace(null, "whatever");
418 >            shouldThrow();
419 >        } catch(NullPointerException e){}
420 >    }
421 >
422 >    /**
423 >     * replace(null, x, y) throws NPE
424 >     */
425 >    public void testReplaceValue_NullPointerException() {
426 >        try {
427 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
428 >            c.replace(null, one, "whatever");
429 >            shouldThrow();
430 >        } catch(NullPointerException e){}
431      }
432  
433 <    public void testPutIfAbsent2_NullPointerException(){
434 <        try{
433 >    /**
434 >     * putIfAbsent(x, null) throws NPE
435 >     */
436 >    public void testPutIfAbsent2_NullPointerException() {
437 >        try {
438              ConcurrentHashMap c = new ConcurrentHashMap(5);
439              c.putIfAbsent("whatever", null);
440 <            fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
441 <        }catch(NullPointerException e){}
440 >            shouldThrow();
441 >        } catch(NullPointerException e){}
442 >    }
443 >
444 >
445 >    /**
446 >     * replace(x, null) throws NPE
447 >     */
448 >    public void testReplace2_NullPointerException() {
449 >        try {
450 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
451 >            c.replace("whatever", null);
452 >            shouldThrow();
453 >        } catch(NullPointerException e){}
454 >    }
455 >
456 >    /**
457 >     * replace(x, null, y) throws NPE
458 >     */
459 >    public void testReplaceValue2_NullPointerException() {
460 >        try {
461 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
462 >            c.replace("whatever", null, "A");
463 >            shouldThrow();
464 >        } catch(NullPointerException e){}
465 >    }
466 >
467 >    /**
468 >     * replace(x, y, null) throws NPE
469 >     */
470 >    public void testReplaceValue3_NullPointerException() {
471 >        try {
472 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
473 >            c.replace("whatever", one, null);
474 >            shouldThrow();
475 >        } catch(NullPointerException e){}
476      }
477  
478  
479 <    public void testRemove1_NullPointerException(){
480 <        try{
479 >    /**
480 >     * remove(null) throws NPE
481 >     */
482 >    public void testRemove1_NullPointerException() {
483 >        try {
484              ConcurrentHashMap c = new ConcurrentHashMap(5);
485              c.put("sadsdf", "asdads");
486              c.remove(null);
487 <            fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione");
488 <        }catch(NullPointerException e){}
487 >            shouldThrow();
488 >        } catch(NullPointerException e){}
489      }
490  
491 <    public void testRemove2_NullPointerException(){
492 <        try{
491 >    /**
492 >     * remove(null, x) throws NPE
493 >     */
494 >    public void testRemove2_NullPointerException() {
495 >        try {
496              ConcurrentHashMap c = new ConcurrentHashMap(5);
497              c.put("sadsdf", "asdads");
498              c.remove(null, "whatever");
499 <            fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione");
500 <        }catch(NullPointerException e){}
499 >            shouldThrow();
500 >        } catch(NullPointerException e){}
501      }
502  
503 +    /**
504 +     * remove(x, null) returns false
505 +     */
506 +    public void testRemove3() {
507 +        try {
508 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
509 +            c.put("sadsdf", "asdads");
510 +            assertFalse(c.remove("sadsdf", null));
511 +        } catch(NullPointerException e){
512 +            fail();
513 +        }
514 +    }
515 +
516 +    /**
517 +     * A deserialized map equals original
518 +     */
519      public void testSerialization() {
520          ConcurrentHashMap q = map5();
521  
# Line 357 | Line 533 | public class ConcurrentHashMapTest exten
533              assertTrue(r.equals(q));
534          } catch(Exception e){
535              e.printStackTrace();
536 <            fail("unexpected exception");
536 >            unexpectedException();
537          }
538      }
539  
540 +
541 +    /**
542 +     * SetValue of an EntrySet entry sets value in the map.
543 +     */
544 +    public void testSetValueWriteThrough() {
545 +        // Adapted from a bug report by Eric Zoerner
546 +        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
547 +        assertTrue(map.isEmpty());
548 +        for (int i = 0; i < 20; i++)
549 +            map.put(new Integer(i), new Integer(i));
550 +        assertFalse(map.isEmpty());
551 +        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
552 +        
553 +        // assert that entry1 is not 16
554 +        assertTrue("entry is 16, test not valid",
555 +                   !entry1.getKey().equals(new Integer(16)));
556 +        
557 +        // remove 16 (a different key) from map
558 +        // which just happens to cause entry1 to be cloned in map
559 +        map.remove(new Integer(16));
560 +        entry1.setValue("XYZ");
561 +        assertTrue(map.containsValue("XYZ")); // fails
562 +    }
563      
564   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines