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.6 by dl, Fri Nov 28 12:38:08 2003 UTC vs.
Revision 1.14 by jsr166, Mon Nov 2 20:28:31 2009 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 13 | Line 14 | import java.io.*;
14  
15   public class ConcurrentHashMapTest extends JSR166TestCase{
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20          return new TestSuite(ConcurrentHashMapTest.class);
# Line 22 | Line 23 | public class ConcurrentHashMapTest exten
23      /**
24       * Create a map from Integers 1-5 to Strings "A"-"E".
25       */
26 <    private static ConcurrentHashMap map5() {  
26 >    private static ConcurrentHashMap map5() {
27          ConcurrentHashMap map = new ConcurrentHashMap(5);
28          assertTrue(map.isEmpty());
29          map.put(one, "A");
# Line 65 | Line 66 | public class ConcurrentHashMapTest exten
66          assertTrue(map.contains("A"));
67          assertFalse(map.contains("Z"));
68      }
69 <    
69 >
70      /**
71       *  containsKey returns true for contained key
72       */
# Line 80 | Line 81 | public class ConcurrentHashMapTest exten
81       */
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      /**
# Line 100 | Line 101 | public class ConcurrentHashMapTest exten
101      }
102  
103      /**
103     *   Clone creates an equal map
104     */
105    public void testClone() {
106        ConcurrentHashMap map = map5();
107        ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone());
108        assertEquals(map, m2);
109    }
110
111    /**
104       *  get returns the correct element at the given key,
105       *  or null if not present
106       */
# Line 158 | Line 150 | public class ConcurrentHashMapTest exten
150      }
151  
152      /**
153 +     *  keySet.toArray returns contains all keys
154 +     */
155 +    public void testKeySetToArray() {
156 +        ConcurrentHashMap map = map5();
157 +        Set s = map.keySet();
158 +        Object[] ar = s.toArray();
159 +        assertTrue(s.containsAll(Arrays.asList(ar)));
160 +        assertEquals(5, ar.length);
161 +        ar[0] = m10;
162 +        assertFalse(s.containsAll(Arrays.asList(ar)));
163 +    }
164 +
165 +    /**
166 +     *  Values.toArray contains all values
167 +     */
168 +    public void testValuesToArray() {
169 +        ConcurrentHashMap map = map5();
170 +        Collection v = map.values();
171 +        Object[] ar = v.toArray();
172 +        ArrayList s = new ArrayList(Arrays.asList(ar));
173 +        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 +    }
180 +
181 +    /**
182 +     *  entrySet.toArray contains all entries
183 +     */
184 +    public void testEntrySetToArray() {
185 +        ConcurrentHashMap map = map5();
186 +        Set s = map.entrySet();
187 +        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       * values collection contains all values
197       */
198      public void testValues() {
# Line 181 | Line 216 | public class ConcurrentHashMapTest exten
216          Iterator it = s.iterator();
217          while (it.hasNext()) {
218              Map.Entry e = (Map.Entry) it.next();
219 <            assertTrue(
219 >            assertTrue(
220                         (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")) ||
# Line 210 | Line 245 | public class ConcurrentHashMapTest exten
245       */
246      public void testPutIfAbsent() {
247          ConcurrentHashMap map = map5();
248 <        map.putIfAbsent(new Integer(6), "Z");
249 <        assertTrue(map.containsKey(new Integer(6)));
248 >        map.putIfAbsent(six, "Z");
249 >        assertTrue(map.containsKey(six));
250      }
251  
252      /**
# Line 223 | Line 258 | public class ConcurrentHashMapTest exten
258      }
259  
260      /**
261 +     *   replace fails when the given key is not present
262 +     */
263 +    public void testReplace() {
264 +        ConcurrentHashMap map = map5();
265 +        assertNull(map.replace(six, "Z"));
266 +        assertFalse(map.containsKey(six));
267 +    }
268 +
269 +    /**
270 +     *   replace succeeds if the key is already present
271 +     */
272 +    public void testReplace2() {
273 +        ConcurrentHashMap map = map5();
274 +        assertNotNull(map.replace(one, "Z"));
275 +        assertEquals("Z", map.get(one));
276 +    }
277 +
278 +
279 +    /**
280 +     * replace value fails when the given key not mapped to expected value
281 +     */
282 +    public void testReplaceValue() {
283 +        ConcurrentHashMap map = map5();
284 +        assertEquals("A", map.get(one));
285 +        assertFalse(map.replace(one, "Z", "Z"));
286 +        assertEquals("A", map.get(one));
287 +    }
288 +
289 +    /**
290 +     * replace value succeeds when the given key mapped to expected value
291 +     */
292 +    public void testReplaceValue2() {
293 +        ConcurrentHashMap map = map5();
294 +        assertEquals("A", map.get(one));
295 +        assertTrue(map.replace(one, "A", "Z"));
296 +        assertEquals("Z", map.get(one));
297 +    }
298 +
299 +
300 +    /**
301       *   remove removes the correct key-value pair from the map
302       */
303      public void testRemove() {
# Line 265 | Line 340 | public class ConcurrentHashMapTest exten
340          for (int i = 1; i <= 5; ++i) {
341              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
342          }
343 <    }        
343 >    }
344  
345      // Exception tests
346 <    
346 >
347      /**
348 <     * Cannot create with negative capacity
348 >     * Cannot create with negative capacity
349       */
350      public void testConstructor1() {
351          try {
# Line 377 | Line 452 | public class ConcurrentHashMapTest exten
452      }
453  
454      /**
455 +     * replace(null, x) throws NPE
456 +     */
457 +    public void testReplace_NullPointerException() {
458 +        try {
459 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
460 +            c.replace(null, "whatever");
461 +            shouldThrow();
462 +        } catch(NullPointerException e){}
463 +    }
464 +
465 +    /**
466 +     * replace(null, x, y) throws NPE
467 +     */
468 +    public void testReplaceValue_NullPointerException() {
469 +        try {
470 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
471 +            c.replace(null, one, "whatever");
472 +            shouldThrow();
473 +        } catch(NullPointerException e){}
474 +    }
475 +
476 +    /**
477       * putIfAbsent(x, null) throws NPE
478       */
479      public void testPutIfAbsent2_NullPointerException() {
# Line 389 | Line 486 | public class ConcurrentHashMapTest exten
486  
487  
488      /**
489 +     * replace(x, null) throws NPE
490 +     */
491 +    public void testReplace2_NullPointerException() {
492 +        try {
493 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
494 +            c.replace("whatever", null);
495 +            shouldThrow();
496 +        } catch(NullPointerException e){}
497 +    }
498 +
499 +    /**
500 +     * replace(x, null, y) throws NPE
501 +     */
502 +    public void testReplaceValue2_NullPointerException() {
503 +        try {
504 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
505 +            c.replace("whatever", null, "A");
506 +            shouldThrow();
507 +        } catch(NullPointerException e){}
508 +    }
509 +
510 +    /**
511 +     * replace(x, y, null) throws NPE
512 +     */
513 +    public void testReplaceValue3_NullPointerException() {
514 +        try {
515 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
516 +            c.replace("whatever", one, null);
517 +            shouldThrow();
518 +        } catch(NullPointerException e){}
519 +    }
520 +
521 +
522 +    /**
523       * remove(null) throws NPE
524       */
525      public void testRemove1_NullPointerException() {
# Line 413 | Line 544 | public class ConcurrentHashMapTest exten
544      }
545  
546      /**
547 +     * remove(x, null) returns false
548 +     */
549 +    public void testRemove3() {
550 +        try {
551 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
552 +            c.put("sadsdf", "asdads");
553 +            assertFalse(c.remove("sadsdf", null));
554 +        } catch(NullPointerException e){
555 +            fail();
556 +        }
557 +    }
558 +
559 +    /**
560       * A deserialized map equals original
561       */
562      public void testSerialization() {
# Line 441 | Line 585 | public class ConcurrentHashMapTest exten
585       * SetValue of an EntrySet entry sets value in the map.
586       */
587      public void testSetValueWriteThrough() {
588 <        // Adapted from a bug report by Eric Zoerner
588 >        // Adapted from a bug report by Eric Zoerner
589          ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
590          assertTrue(map.isEmpty());
591          for (int i = 0; i < 20; i++)
592              map.put(new Integer(i), new Integer(i));
593          assertFalse(map.isEmpty());
594          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
595 <        
595 >
596          // assert that entry1 is not 16
597          assertTrue("entry is 16, test not valid",
598                     !entry1.getKey().equals(new Integer(16)));
599 <        
600 <        // remove 16 (a different key) from map
599 >
600 >        // remove 16 (a different key) from map
601          // which just happens to cause entry1 to be cloned in map
602          map.remove(new Integer(16));
603          entry1.setValue("XYZ");
604          assertTrue(map.containsValue("XYZ")); // fails
605      }
606 <    
606 >
607   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines