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.7 by dl, Wed Dec 3 21:08:24 2003 UTC

# Line 19 | Line 19 | public class ConcurrentHashMapTest exten
19          return new TestSuite(ConcurrentHashMapTest.class);
20      }
21  
22 +    /**
23 +     * Create a map from Integers 1-5 to Strings "A"-"E".
24 +     */
25      private static ConcurrentHashMap map5() {  
26          ConcurrentHashMap map = new ConcurrentHashMap(5);
27          assertTrue(map.isEmpty());
# Line 33 | Line 36 | public class ConcurrentHashMapTest exten
36      }
37  
38      /**
39 <     *   clear  removes all key-element pairs from the map
39 >     *  clear removes all pairs
40       */
41 <    public void testClear(){
41 >    public void testClear() {
42          ConcurrentHashMap map = map5();
43          map.clear();
44          assertEquals(map.size(), 0);
45      }
46  
47      /**
48 <     *   contains gives the appropriate value
48 >     *  Maps with same contents are equal
49 >     */
50 >    public void testEquals() {
51 >        ConcurrentHashMap map1 = map5();
52 >        ConcurrentHashMap map2 = map5();
53 >        assertEquals(map1, map2);
54 >        assertEquals(map2, map1);
55 >        map1.clear();
56 >        assertFalse(map1.equals(map2));
57 >        assertFalse(map2.equals(map1));
58 >    }
59 >
60 >    /**
61 >     *  contains returns true for contained value
62       */
63 <    public void testContains(){
63 >    public void testContains() {
64          ConcurrentHashMap map = map5();
65          assertTrue(map.contains("A"));
66          assertFalse(map.contains("Z"));
67      }
68      
69      /**
70 <     *   containsKey gives the appropriate value
70 >     *  containsKey returns true for contained key
71       */
72 <    public void testContainsKey(){
72 >    public void testContainsKey() {
73          ConcurrentHashMap map = map5();
74          assertTrue(map.containsKey(one));
75 <        assertFalse(map.containsKey(new Integer(100)));
75 >        assertFalse(map.containsKey(zero));
76      }
77  
78      /**
79 <     *  Identical to normal contains
79 >     *  containsValue returns true for held values
80       */
81 <    public void testContainsValue(){
81 >    public void testContainsValue() {
82          ConcurrentHashMap map = map5();
83          assertTrue(map.contains("A"));
84          assertFalse(map.contains("Z"));
85      }
86  
87      /**
88 <     *  tes to verify enumeration returns an enumeration containing the correct elements
88 >     *   enumeration returns an enumeration containing the correct
89 >     *   elements
90       */
91 <    public void testEnumeration(){
91 >    public void testEnumeration() {
92          ConcurrentHashMap map = map5();
93          Enumeration e = map.elements();
94          int count = 0;
# Line 83 | Line 100 | public class ConcurrentHashMapTest exten
100      }
101  
102      /**
103 <     *   get returns the correct element at the given index
103 >     *   Clone creates an equal map
104       */
105 <    public void testGet(){
105 >    public void testClone() {
106          ConcurrentHashMap map = map5();
107 <        assertEquals("A", (String)map.get(one));
107 >        ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone());
108 >        assertEquals(map, m2);
109      }
110  
111      /**
112 <     *   get on a nonexistant key returns null
112 >     *  get returns the correct element at the given key,
113 >     *  or null if not present
114       */
115 <    public void testGet2(){
115 >    public void testGet() {
116 >        ConcurrentHashMap map = map5();
117 >        assertEquals("A", (String)map.get(one));
118          ConcurrentHashMap empty = new ConcurrentHashMap();
119 <        assertNull(empty.get("anything"));
119 >        assertNull(map.get("anything"));
120      }
121  
122      /**
123 <     *  Simple test to verify isEmpty returns the correct value
123 >     *  isEmpty is true of empty map and false for non-empty
124       */
125 <    public void testIsEmpty(){
125 >    public void testIsEmpty() {
126          ConcurrentHashMap empty = new ConcurrentHashMap();
127          ConcurrentHashMap map = map5();
128          assertTrue(empty.isEmpty());
# Line 111 | Line 132 | public class ConcurrentHashMapTest exten
132      /**
133       *   keys returns an enumeration containing all the keys from the map
134       */
135 <    public void testKeys(){
135 >    public void testKeys() {
136          ConcurrentHashMap map = map5();
137          Enumeration e = map.keys();
138          int count = 0;
# Line 125 | Line 146 | public class ConcurrentHashMapTest exten
146      /**
147       *   keySet returns a Set containing all the keys
148       */
149 <    public void testKeySet(){
149 >    public void testKeySet() {
150          ConcurrentHashMap map = map5();
151          Set s = map.keySet();
152          assertEquals(5, s.size());
# Line 136 | Line 157 | public class ConcurrentHashMapTest exten
157          assertTrue(s.contains(five));
158      }
159  
160 <    public void testValues(){
160 >    /**
161 >     * values collection contains all values
162 >     */
163 >    public void testValues() {
164          ConcurrentHashMap map = map5();
165          Collection s = map.values();
166          assertEquals(5, s.size());
# Line 147 | Line 171 | public class ConcurrentHashMapTest exten
171          assertTrue(s.contains("E"));
172      }
173  
174 <    public void testEntrySet(){
174 >    /**
175 >     * entrySet contains all pairs
176 >     */
177 >    public void testEntrySet() {
178          ConcurrentHashMap map = map5();
179          Set s = map.entrySet();
180          assertEquals(5, s.size());
# Line 166 | Line 193 | public class ConcurrentHashMapTest exten
193      /**
194       *   putAll  adds all key-value pairs from the given map
195       */
196 <    public void testPutAll(){
196 >    public void testPutAll() {
197          ConcurrentHashMap empty = new ConcurrentHashMap();
198          ConcurrentHashMap map = map5();
199          empty.putAll(map);
# Line 181 | Line 208 | public class ConcurrentHashMapTest exten
208      /**
209       *   putIfAbsent works when the given key is not present
210       */
211 <    public void testPutIfAbsent(){
211 >    public void testPutIfAbsent() {
212          ConcurrentHashMap map = map5();
213 <        map.putIfAbsent(new Integer(6), "Z");
214 <        assertTrue(map.containsKey(new Integer(6)));
213 >        map.putIfAbsent(six, "Z");
214 >        assertTrue(map.containsKey(six));
215      }
216  
217      /**
218       *   putIfAbsent does not add the pair if the key is already present
219       */
220 <    public void testPutIfAbsent2(){
220 >    public void testPutIfAbsent2() {
221          ConcurrentHashMap map = map5();
222          assertEquals("A", map.putIfAbsent(one, "Z"));
223      }
224  
225      /**
226 +     *   replace fails when the given key is not present
227 +     */
228 +    public void testReplace() {
229 +        ConcurrentHashMap map = map5();
230 +        assertFalse(map.replace(six, "Z"));
231 +        assertFalse(map.containsKey(six));
232 +    }
233 +
234 +    /**
235 +     *   replace succeeds if the key is already present
236 +     */
237 +    public void testReplace2() {
238 +        ConcurrentHashMap map = map5();
239 +        assertTrue(map.replace(one, "Z"));
240 +        assertEquals("Z", map.get(one));
241 +    }
242 +
243 +
244 +    /**
245 +     * replace value fails when the given key not mapped to expected value
246 +     */
247 +    public void testReplaceValue() {
248 +        ConcurrentHashMap map = map5();
249 +        assertEquals("A", map.get(one));
250 +        assertFalse(map.replace(one, "Z", "Z"));
251 +        assertEquals("A", map.get(one));
252 +    }
253 +
254 +    /**
255 +     * replace value succeeds when the given key mapped to expected value
256 +     */
257 +    public void testReplaceValue2() {
258 +        ConcurrentHashMap map = map5();
259 +        assertEquals("A", map.get(one));
260 +        assertTrue(map.replace(one, "A", "Z"));
261 +        assertEquals("Z", map.get(one));
262 +    }
263 +
264 +
265 +    /**
266       *   remove removes the correct key-value pair from the map
267       */
268 <    public void testRemove(){
268 >    public void testRemove() {
269          ConcurrentHashMap map = map5();
270          map.remove(five);
271          assertEquals(4, map.size());
272          assertFalse(map.containsKey(five));
273      }
274  
275 <    public void testRemove2(){
275 >    /**
276 >     * remove(key,value) removes only if pair present
277 >     */
278 >    public void testRemove2() {
279          ConcurrentHashMap map = map5();
280          map.remove(five, "E");
281          assertEquals(4, map.size());
# Line 219 | Line 289 | public class ConcurrentHashMapTest exten
289      /**
290       *   size returns the correct values
291       */
292 <    public void testSize(){
292 >    public void testSize() {
293          ConcurrentHashMap map = map5();
294          ConcurrentHashMap empty = new ConcurrentHashMap();
295          assertEquals(0, empty.size());
296          assertEquals(5, map.size());
297      }
298  
299 <    public void testToString(){
299 >    /**
300 >     * toString contains toString of elements
301 >     */
302 >    public void testToString() {
303          ConcurrentHashMap map = map5();
304          String s = map.toString();
305          for (int i = 1; i <= 5; ++i) {
# Line 236 | Line 309 | public class ConcurrentHashMapTest exten
309  
310      // Exception tests
311      
312 <    public void testConstructor1(){
313 <        try{
312 >    /**
313 >     * Cannot create with negative capacity
314 >     */
315 >    public void testConstructor1() {
316 >        try {
317              new ConcurrentHashMap(-1,0,1);
318 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
319 <        }catch(IllegalArgumentException e){}
318 >            shouldThrow();
319 >        } catch(IllegalArgumentException e){}
320      }
321  
322 <    public void testConstructor2(){
323 <        try{
322 >    /**
323 >     * Cannot create with negative concurrency level
324 >     */
325 >    public void testConstructor2() {
326 >        try {
327              new ConcurrentHashMap(1,0,-1);
328 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
329 <        }catch(IllegalArgumentException e){}
328 >            shouldThrow();
329 >        } catch(IllegalArgumentException e){}
330      }
331  
332 <    public void testConstructor3(){
333 <        try{
332 >    /**
333 >     * Cannot create with only negative capacity
334 >     */
335 >    public void testConstructor3() {
336 >        try {
337              new ConcurrentHashMap(-1);
338 <            fail("ConcurrentHashMap(int) should throw Illegal Argument Exception");
339 <        }catch(IllegalArgumentException e){}
338 >            shouldThrow();
339 >        } catch(IllegalArgumentException e){}
340      }
341  
342 <    public void testGet_NullPointerException(){
343 <        try{
342 >    /**
343 >     * get(null) throws NPE
344 >     */
345 >    public void testGet_NullPointerException() {
346 >        try {
347              ConcurrentHashMap c = new ConcurrentHashMap(5);
348              c.get(null);
349 <            fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception");
350 <        }catch(NullPointerException e){}
349 >            shouldThrow();
350 >        } catch(NullPointerException e){}
351      }
352  
353 <    public void testContainsKey_NullPointerException(){
354 <        try{
353 >    /**
354 >     * containsKey(null) throws NPE
355 >     */
356 >    public void testContainsKey_NullPointerException() {
357 >        try {
358              ConcurrentHashMap c = new ConcurrentHashMap(5);
359              c.containsKey(null);
360 <            fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception");
361 <        }catch(NullPointerException e){}
360 >            shouldThrow();
361 >        } catch(NullPointerException e){}
362      }
363  
364 <    public void testContainsValue_NullPointerException(){
365 <        try{
364 >    /**
365 >     * containsValue(null) throws NPE
366 >     */
367 >    public void testContainsValue_NullPointerException() {
368 >        try {
369              ConcurrentHashMap c = new ConcurrentHashMap(5);
370              c.containsValue(null);
371 <            fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception");
372 <        }catch(NullPointerException e){}
371 >            shouldThrow();
372 >        } catch(NullPointerException e){}
373      }
374  
375 <    public void testContains_NullPointerException(){
376 <        try{
375 >    /**
376 >     * contains(null) throws NPE
377 >     */
378 >    public void testContains_NullPointerException() {
379 >        try {
380              ConcurrentHashMap c = new ConcurrentHashMap(5);
381              c.contains(null);
382 <            fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception");
383 <        }catch(NullPointerException e){}
382 >            shouldThrow();
383 >        } catch(NullPointerException e){}
384      }
385  
386 <    public void testPut1_NullPointerException(){
387 <        try{
386 >    /**
387 >     * put(null,x) throws NPE
388 >     */
389 >    public void testPut1_NullPointerException() {
390 >        try {
391              ConcurrentHashMap c = new ConcurrentHashMap(5);
392              c.put(null, "whatever");
393 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
394 <        }catch(NullPointerException e){}
393 >            shouldThrow();
394 >        } catch(NullPointerException e){}
395      }
396  
397 <    public void testPut2_NullPointerException(){
398 <        try{
397 >    /**
398 >     * put(x, null) throws NPE
399 >     */
400 >    public void testPut2_NullPointerException() {
401 >        try {
402              ConcurrentHashMap c = new ConcurrentHashMap(5);
403              c.put("whatever", null);
404 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
405 <        }catch(NullPointerException e){}
404 >            shouldThrow();
405 >        } catch(NullPointerException e){}
406      }
407  
408 <    public void testPutIfAbsent1_NullPointerException(){
409 <        try{
408 >    /**
409 >     * putIfAbsent(null, x) throws NPE
410 >     */
411 >    public void testPutIfAbsent1_NullPointerException() {
412 >        try {
413              ConcurrentHashMap c = new ConcurrentHashMap(5);
414              c.putIfAbsent(null, "whatever");
415 <            fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
416 <        }catch(NullPointerException e){}
415 >            shouldThrow();
416 >        } catch(NullPointerException e){}
417      }
418  
419 <    public void testPutIfAbsent2_NullPointerException(){
420 <        try{
419 >    /**
420 >     * replace(null, x) throws NPE
421 >     */
422 >    public void testReplace_NullPointerException() {
423 >        try {
424 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
425 >            c.replace(null, "whatever");
426 >            shouldThrow();
427 >        } catch(NullPointerException e){}
428 >    }
429 >
430 >    /**
431 >     * replace(null, x, y) throws NPE
432 >     */
433 >    public void testReplaceValue_NullPointerException() {
434 >        try {
435 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
436 >            c.replace(null, one, "whatever");
437 >            shouldThrow();
438 >        } catch(NullPointerException e){}
439 >    }
440 >
441 >    /**
442 >     * putIfAbsent(x, null) throws NPE
443 >     */
444 >    public void testPutIfAbsent2_NullPointerException() {
445 >        try {
446              ConcurrentHashMap c = new ConcurrentHashMap(5);
447              c.putIfAbsent("whatever", null);
448 <            fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
449 <        }catch(NullPointerException e){}
448 >            shouldThrow();
449 >        } catch(NullPointerException e){}
450 >    }
451 >
452 >
453 >    /**
454 >     * replace(x, null) throws NPE
455 >     */
456 >    public void testReplace2_NullPointerException() {
457 >        try {
458 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
459 >            c.replace("whatever", null);
460 >            shouldThrow();
461 >        } catch(NullPointerException e){}
462      }
463  
464 +    /**
465 +     * replace(x, null, y) throws NPE
466 +     */
467 +    public void testReplaceValue2_NullPointerException() {
468 +        try {
469 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
470 +            c.replace("whatever", null, "A");
471 +            shouldThrow();
472 +        } catch(NullPointerException e){}
473 +    }
474  
475 <    public void testRemove1_NullPointerException(){
476 <        try{
475 >    /**
476 >     * replace(x, y, null) throws NPE
477 >     */
478 >    public void testReplaceValue3_NullPointerException() {
479 >        try {
480 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
481 >            c.replace("whatever", one, null);
482 >            shouldThrow();
483 >        } catch(NullPointerException e){}
484 >    }
485 >
486 >
487 >    /**
488 >     * remove(null) throws NPE
489 >     */
490 >    public void testRemove1_NullPointerException() {
491 >        try {
492              ConcurrentHashMap c = new ConcurrentHashMap(5);
493              c.put("sadsdf", "asdads");
494              c.remove(null);
495 <            fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione");
496 <        }catch(NullPointerException e){}
495 >            shouldThrow();
496 >        } catch(NullPointerException e){}
497      }
498  
499 <    public void testRemove2_NullPointerException(){
500 <        try{
499 >    /**
500 >     * remove(null, x) throws NPE
501 >     */
502 >    public void testRemove2_NullPointerException() {
503 >        try {
504              ConcurrentHashMap c = new ConcurrentHashMap(5);
505              c.put("sadsdf", "asdads");
506              c.remove(null, "whatever");
507 <            fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione");
508 <        }catch(NullPointerException e){}
507 >            shouldThrow();
508 >        } catch(NullPointerException e){}
509      }
510  
511 +    /**
512 +     * A deserialized map equals original
513 +     */
514      public void testSerialization() {
515          ConcurrentHashMap q = map5();
516  
# Line 357 | Line 528 | public class ConcurrentHashMapTest exten
528              assertTrue(r.equals(q));
529          } catch(Exception e){
530              e.printStackTrace();
531 <            fail("unexpected exception");
531 >            unexpectedException();
532          }
533      }
534  
535 +
536 +    /**
537 +     * SetValue of an EntrySet entry sets value in the map.
538 +     */
539 +    public void testSetValueWriteThrough() {
540 +        // Adapted from a bug report by Eric Zoerner
541 +        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
542 +        assertTrue(map.isEmpty());
543 +        for (int i = 0; i < 20; i++)
544 +            map.put(new Integer(i), new Integer(i));
545 +        assertFalse(map.isEmpty());
546 +        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
547 +        
548 +        // assert that entry1 is not 16
549 +        assertTrue("entry is 16, test not valid",
550 +                   !entry1.getKey().equals(new Integer(16)));
551 +        
552 +        // remove 16 (a different key) from map
553 +        // which just happens to cause entry1 to be cloned in map
554 +        map.remove(new Integer(16));
555 +        entry1.setValue("XYZ");
556 +        assertTrue(map.containsValue("XYZ")); // fails
557 +    }
558      
559   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines