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.12 by dl, Fri May 20 14:31:06 2005 UTC vs.
Revision 1.28 by jsr166, Tue Feb 21 02:04:17 2012 UTC

# Line 1 | Line 1
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.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
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.*;
11 > import java.util.concurrent.ConcurrentHashMap;
12  
13 < public class ConcurrentHashMapTest extends JSR166TestCase{
13 > public class ConcurrentHashMapTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());  
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18 <        return new TestSuite(ConcurrentHashMapTest.class);
18 >        return new TestSuite(ConcurrentHashMapTest.class);
19      }
20  
21      /**
22 <     * Create a map from Integers 1-5 to Strings "A"-"E".
22 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
23       */
24 <    private static ConcurrentHashMap map5() {  
25 <        ConcurrentHashMap map = new ConcurrentHashMap(5);
24 >    private static ConcurrentHashMap map5() {
25 >        ConcurrentHashMap map = new ConcurrentHashMap(5);
26          assertTrue(map.isEmpty());
27 <        map.put(one, "A");
28 <        map.put(two, "B");
29 <        map.put(three, "C");
30 <        map.put(four, "D");
31 <        map.put(five, "E");
27 >        map.put(one, "A");
28 >        map.put(two, "B");
29 >        map.put(three, "C");
30 >        map.put(four, "D");
31 >        map.put(five, "E");
32          assertFalse(map.isEmpty());
33          assertEquals(5, map.size());
34 <        return map;
34 >        return map;
35      }
36  
37      /**
38 <     *  clear removes all pairs
38 >     * clear removes all pairs
39       */
40      public void testClear() {
41          ConcurrentHashMap map = map5();
42 <        map.clear();
43 <        assertEquals(map.size(), 0);
42 >        map.clear();
43 >        assertEquals(0, map.size());
44      }
45  
46      /**
47 <     *  Maps with same contents are equal
47 >     * Maps with same contents are equal
48       */
49      public void testEquals() {
50          ConcurrentHashMap map1 = map5();
51          ConcurrentHashMap map2 = map5();
52          assertEquals(map1, map2);
53          assertEquals(map2, map1);
54 <        map1.clear();
54 >        map1.clear();
55          assertFalse(map1.equals(map2));
56          assertFalse(map2.equals(map1));
57      }
58  
59      /**
60 <     *  contains returns true for contained value
60 >     * contains returns true for contained value
61       */
62      public void testContains() {
63          ConcurrentHashMap map = map5();
64 <        assertTrue(map.contains("A"));
64 >        assertTrue(map.contains("A"));
65          assertFalse(map.contains("Z"));
66      }
67 <    
67 >
68      /**
69 <     *  containsKey returns true for contained key
69 >     * containsKey returns true for contained key
70       */
71      public void testContainsKey() {
72          ConcurrentHashMap map = map5();
73 <        assertTrue(map.containsKey(one));
73 >        assertTrue(map.containsKey(one));
74          assertFalse(map.containsKey(zero));
75      }
76  
77      /**
78 <     *  containsValue returns true for held values
78 >     * containsValue returns true for held values
79       */
80      public void testContainsValue() {
81          ConcurrentHashMap map = map5();
82 <        assertTrue(map.containsValue("A"));
82 >        assertTrue(map.containsValue("A"));
83          assertFalse(map.containsValue("Z"));
84      }
85  
86      /**
87 <     *   enumeration returns an enumeration containing the correct
88 <     *   elements
87 >     * enumeration returns an enumeration containing the correct
88 >     * elements
89       */
90      public void testEnumeration() {
91          ConcurrentHashMap map = map5();
92 <        Enumeration e = map.elements();
93 <        int count = 0;
94 <        while(e.hasMoreElements()){
95 <            count++;
96 <            e.nextElement();
97 <        }
98 <        assertEquals(5, count);
92 >        Enumeration e = map.elements();
93 >        int count = 0;
94 >        while (e.hasMoreElements()) {
95 >            count++;
96 >            e.nextElement();
97 >        }
98 >        assertEquals(5, count);
99      }
100  
101      /**
102 <     *  get returns the correct element at the given key,
103 <     *  or null if not present
102 >     * get returns the correct element at the given key,
103 >     * or null if not present
104       */
105      public void testGet() {
106          ConcurrentHashMap map = map5();
107 <        assertEquals("A", (String)map.get(one));
107 >        assertEquals("A", (String)map.get(one));
108          ConcurrentHashMap empty = new ConcurrentHashMap();
109          assertNull(map.get("anything"));
110      }
111  
112      /**
113 <     *  isEmpty is true of empty map and false for non-empty
113 >     * isEmpty is true of empty map and false for non-empty
114       */
115      public void testIsEmpty() {
116          ConcurrentHashMap empty = new ConcurrentHashMap();
117          ConcurrentHashMap map = map5();
118 <        assertTrue(empty.isEmpty());
118 >        assertTrue(empty.isEmpty());
119          assertFalse(map.isEmpty());
120      }
121  
122      /**
123 <     *   keys returns an enumeration containing all the keys from the map
123 >     * keys returns an enumeration containing all the keys from the map
124       */
125      public void testKeys() {
126          ConcurrentHashMap map = map5();
127 <        Enumeration e = map.keys();
128 <        int count = 0;
129 <        while(e.hasMoreElements()){
130 <            count++;
131 <            e.nextElement();
132 <        }
133 <        assertEquals(5, count);
127 >        Enumeration e = map.keys();
128 >        int count = 0;
129 >        while (e.hasMoreElements()) {
130 >            count++;
131 >            e.nextElement();
132 >        }
133 >        assertEquals(5, count);
134      }
135  
136      /**
137 <     *   keySet returns a Set containing all the keys
137 >     * keySet returns a Set containing all the keys
138       */
139      public void testKeySet() {
140          ConcurrentHashMap map = map5();
141 <        Set s = map.keySet();
142 <        assertEquals(5, s.size());
143 <        assertTrue(s.contains(one));
144 <        assertTrue(s.contains(two));
145 <        assertTrue(s.contains(three));
146 <        assertTrue(s.contains(four));
147 <        assertTrue(s.contains(five));
141 >        Set s = map.keySet();
142 >        assertEquals(5, s.size());
143 >        assertTrue(s.contains(one));
144 >        assertTrue(s.contains(two));
145 >        assertTrue(s.contains(three));
146 >        assertTrue(s.contains(four));
147 >        assertTrue(s.contains(five));
148 >    }
149 >
150 >    /**
151 >     * keySet.toArray returns contains all keys
152 >     */
153 >    public void testKeySetToArray() {
154 >        ConcurrentHashMap map = map5();
155 >        Set s = map.keySet();
156 >        Object[] ar = s.toArray();
157 >        assertTrue(s.containsAll(Arrays.asList(ar)));
158 >        assertEquals(5, ar.length);
159 >        ar[0] = m10;
160 >        assertFalse(s.containsAll(Arrays.asList(ar)));
161 >    }
162 >
163 >    /**
164 >     * Values.toArray contains all values
165 >     */
166 >    public void testValuesToArray() {
167 >        ConcurrentHashMap map = map5();
168 >        Collection v = map.values();
169 >        Object[] ar = v.toArray();
170 >        ArrayList s = new ArrayList(Arrays.asList(ar));
171 >        assertEquals(5, ar.length);
172 >        assertTrue(s.contains("A"));
173 >        assertTrue(s.contains("B"));
174 >        assertTrue(s.contains("C"));
175 >        assertTrue(s.contains("D"));
176 >        assertTrue(s.contains("E"));
177 >    }
178 >
179 >    /**
180 >     * entrySet.toArray contains all entries
181 >     */
182 >    public void testEntrySetToArray() {
183 >        ConcurrentHashMap map = map5();
184 >        Set s = map.entrySet();
185 >        Object[] ar = s.toArray();
186 >        assertEquals(5, ar.length);
187 >        for (int i = 0; i < 5; ++i) {
188 >            assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
189 >            assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
190 >        }
191      }
192  
193      /**
# Line 154 | Line 195 | public class ConcurrentHashMapTest exten
195       */
196      public void testValues() {
197          ConcurrentHashMap map = map5();
198 <        Collection s = map.values();
199 <        assertEquals(5, s.size());
200 <        assertTrue(s.contains("A"));
201 <        assertTrue(s.contains("B"));
202 <        assertTrue(s.contains("C"));
203 <        assertTrue(s.contains("D"));
204 <        assertTrue(s.contains("E"));
198 >        Collection s = map.values();
199 >        assertEquals(5, s.size());
200 >        assertTrue(s.contains("A"));
201 >        assertTrue(s.contains("B"));
202 >        assertTrue(s.contains("C"));
203 >        assertTrue(s.contains("D"));
204 >        assertTrue(s.contains("E"));
205      }
206  
207      /**
# Line 168 | Line 209 | public class ConcurrentHashMapTest exten
209       */
210      public void testEntrySet() {
211          ConcurrentHashMap map = map5();
212 <        Set s = map.entrySet();
213 <        assertEquals(5, s.size());
212 >        Set s = map.entrySet();
213 >        assertEquals(5, s.size());
214          Iterator it = s.iterator();
215          while (it.hasNext()) {
216              Map.Entry e = (Map.Entry) it.next();
217 <            assertTrue(
217 >            assertTrue(
218                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
219                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
220                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 183 | Line 224 | public class ConcurrentHashMapTest exten
224      }
225  
226      /**
227 <     *   putAll  adds all key-value pairs from the given map
227 >     * putAll adds all key-value pairs from the given map
228       */
229      public void testPutAll() {
230          ConcurrentHashMap empty = new ConcurrentHashMap();
231          ConcurrentHashMap map = map5();
232 <        empty.putAll(map);
233 <        assertEquals(5, empty.size());
234 <        assertTrue(empty.containsKey(one));
235 <        assertTrue(empty.containsKey(two));
236 <        assertTrue(empty.containsKey(three));
237 <        assertTrue(empty.containsKey(four));
238 <        assertTrue(empty.containsKey(five));
232 >        empty.putAll(map);
233 >        assertEquals(5, empty.size());
234 >        assertTrue(empty.containsKey(one));
235 >        assertTrue(empty.containsKey(two));
236 >        assertTrue(empty.containsKey(three));
237 >        assertTrue(empty.containsKey(four));
238 >        assertTrue(empty.containsKey(five));
239      }
240  
241      /**
242 <     *   putIfAbsent works when the given key is not present
242 >     * putIfAbsent works when the given key is not present
243       */
244      public void testPutIfAbsent() {
245          ConcurrentHashMap map = map5();
246 <        map.putIfAbsent(six, "Z");
246 >        map.putIfAbsent(six, "Z");
247          assertTrue(map.containsKey(six));
248      }
249  
250      /**
251 <     *   putIfAbsent does not add the pair if the key is already present
251 >     * putIfAbsent does not add the pair if the key is already present
252       */
253      public void testPutIfAbsent2() {
254          ConcurrentHashMap map = map5();
# Line 215 | Line 256 | public class ConcurrentHashMapTest exten
256      }
257  
258      /**
259 <     *   replace fails when the given key is not present
259 >     * replace fails when the given key is not present
260       */
261      public void testReplace() {
262          ConcurrentHashMap map = map5();
263 <        assertNull(map.replace(six, "Z"));
263 >        assertNull(map.replace(six, "Z"));
264          assertFalse(map.containsKey(six));
265      }
266  
267      /**
268 <     *   replace succeeds if the key is already present
268 >     * replace succeeds if the key is already present
269       */
270      public void testReplace2() {
271          ConcurrentHashMap map = map5();
# Line 232 | Line 273 | public class ConcurrentHashMapTest exten
273          assertEquals("Z", map.get(one));
274      }
275  
235
276      /**
277       * replace value fails when the given key not mapped to expected value
278       */
279      public void testReplaceValue() {
280          ConcurrentHashMap map = map5();
281          assertEquals("A", map.get(one));
282 <        assertFalse(map.replace(one, "Z", "Z"));
282 >        assertFalse(map.replace(one, "Z", "Z"));
283          assertEquals("A", map.get(one));
284      }
285  
# Line 249 | Line 289 | public class ConcurrentHashMapTest exten
289      public void testReplaceValue2() {
290          ConcurrentHashMap map = map5();
291          assertEquals("A", map.get(one));
292 <        assertTrue(map.replace(one, "A", "Z"));
292 >        assertTrue(map.replace(one, "A", "Z"));
293          assertEquals("Z", map.get(one));
294      }
295  
256
296      /**
297 <     *   remove removes the correct key-value pair from the map
297 >     * remove removes the correct key-value pair from the map
298       */
299      public void testRemove() {
300          ConcurrentHashMap map = map5();
301 <        map.remove(five);
302 <        assertEquals(4, map.size());
303 <        assertFalse(map.containsKey(five));
301 >        map.remove(five);
302 >        assertEquals(4, map.size());
303 >        assertFalse(map.containsKey(five));
304      }
305  
306      /**
# Line 269 | Line 308 | public class ConcurrentHashMapTest exten
308       */
309      public void testRemove2() {
310          ConcurrentHashMap map = map5();
311 <        map.remove(five, "E");
312 <        assertEquals(4, map.size());
313 <        assertFalse(map.containsKey(five));
314 <        map.remove(four, "A");
315 <        assertEquals(4, map.size());
316 <        assertTrue(map.containsKey(four));
278 <
311 >        map.remove(five, "E");
312 >        assertEquals(4, map.size());
313 >        assertFalse(map.containsKey(five));
314 >        map.remove(four, "A");
315 >        assertEquals(4, map.size());
316 >        assertTrue(map.containsKey(four));
317      }
318  
319      /**
320 <     *   size returns the correct values
320 >     * size returns the correct values
321       */
322      public void testSize() {
323          ConcurrentHashMap map = map5();
324          ConcurrentHashMap empty = new ConcurrentHashMap();
325 <        assertEquals(0, empty.size());
326 <        assertEquals(5, map.size());
325 >        assertEquals(0, empty.size());
326 >        assertEquals(5, map.size());
327      }
328  
329      /**
# Line 295 | Line 333 | public class ConcurrentHashMapTest exten
333          ConcurrentHashMap map = map5();
334          String s = map.toString();
335          for (int i = 1; i <= 5; ++i) {
336 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
336 >            assertTrue(s.contains(String.valueOf(i)));
337          }
338 <    }        
338 >    }
339  
340      // Exception tests
341 <    
341 >
342      /**
343 <     * Cannot create with negative capacity
343 >     * Cannot create with negative capacity
344       */
345      public void testConstructor1() {
346          try {
347              new ConcurrentHashMap(-1,0,1);
348              shouldThrow();
349 <        } catch(IllegalArgumentException e){}
349 >        } catch (IllegalArgumentException success) {}
350      }
351  
352      /**
# Line 318 | Line 356 | public class ConcurrentHashMapTest exten
356          try {
357              new ConcurrentHashMap(1,0,-1);
358              shouldThrow();
359 <        } catch(IllegalArgumentException e){}
359 >        } catch (IllegalArgumentException success) {}
360      }
361  
362      /**
# Line 328 | Line 366 | public class ConcurrentHashMapTest exten
366          try {
367              new ConcurrentHashMap(-1);
368              shouldThrow();
369 <        } catch(IllegalArgumentException e){}
369 >        } catch (IllegalArgumentException success) {}
370      }
371  
372      /**
# Line 339 | Line 377 | public class ConcurrentHashMapTest exten
377              ConcurrentHashMap c = new ConcurrentHashMap(5);
378              c.get(null);
379              shouldThrow();
380 <        } catch(NullPointerException e){}
380 >        } catch (NullPointerException success) {}
381      }
382  
383      /**
# Line 350 | Line 388 | public class ConcurrentHashMapTest exten
388              ConcurrentHashMap c = new ConcurrentHashMap(5);
389              c.containsKey(null);
390              shouldThrow();
391 <        } catch(NullPointerException e){}
391 >        } catch (NullPointerException success) {}
392      }
393  
394      /**
# Line 361 | Line 399 | public class ConcurrentHashMapTest exten
399              ConcurrentHashMap c = new ConcurrentHashMap(5);
400              c.containsValue(null);
401              shouldThrow();
402 <        } catch(NullPointerException e){}
402 >        } catch (NullPointerException success) {}
403      }
404  
405      /**
# Line 372 | Line 410 | public class ConcurrentHashMapTest exten
410              ConcurrentHashMap c = new ConcurrentHashMap(5);
411              c.contains(null);
412              shouldThrow();
413 <        } catch(NullPointerException e){}
413 >        } catch (NullPointerException success) {}
414      }
415  
416      /**
# Line 383 | Line 421 | public class ConcurrentHashMapTest exten
421              ConcurrentHashMap c = new ConcurrentHashMap(5);
422              c.put(null, "whatever");
423              shouldThrow();
424 <        } catch(NullPointerException e){}
424 >        } catch (NullPointerException success) {}
425      }
426  
427      /**
# Line 394 | Line 432 | public class ConcurrentHashMapTest exten
432              ConcurrentHashMap c = new ConcurrentHashMap(5);
433              c.put("whatever", null);
434              shouldThrow();
435 <        } catch(NullPointerException e){}
435 >        } catch (NullPointerException success) {}
436      }
437  
438      /**
# Line 405 | Line 443 | public class ConcurrentHashMapTest exten
443              ConcurrentHashMap c = new ConcurrentHashMap(5);
444              c.putIfAbsent(null, "whatever");
445              shouldThrow();
446 <        } catch(NullPointerException e){}
446 >        } catch (NullPointerException success) {}
447      }
448  
449      /**
# Line 416 | Line 454 | public class ConcurrentHashMapTest exten
454              ConcurrentHashMap c = new ConcurrentHashMap(5);
455              c.replace(null, "whatever");
456              shouldThrow();
457 <        } catch(NullPointerException e){}
457 >        } catch (NullPointerException success) {}
458      }
459  
460      /**
# Line 427 | Line 465 | public class ConcurrentHashMapTest exten
465              ConcurrentHashMap c = new ConcurrentHashMap(5);
466              c.replace(null, one, "whatever");
467              shouldThrow();
468 <        } catch(NullPointerException e){}
468 >        } catch (NullPointerException success) {}
469      }
470  
471      /**
# Line 438 | Line 476 | public class ConcurrentHashMapTest exten
476              ConcurrentHashMap c = new ConcurrentHashMap(5);
477              c.putIfAbsent("whatever", null);
478              shouldThrow();
479 <        } catch(NullPointerException e){}
479 >        } catch (NullPointerException success) {}
480      }
481  
444
482      /**
483       * replace(x, null) throws NPE
484       */
# Line 450 | Line 487 | public class ConcurrentHashMapTest exten
487              ConcurrentHashMap c = new ConcurrentHashMap(5);
488              c.replace("whatever", null);
489              shouldThrow();
490 <        } catch(NullPointerException e){}
490 >        } catch (NullPointerException success) {}
491      }
492  
493      /**
# Line 461 | Line 498 | public class ConcurrentHashMapTest exten
498              ConcurrentHashMap c = new ConcurrentHashMap(5);
499              c.replace("whatever", null, "A");
500              shouldThrow();
501 <        } catch(NullPointerException e){}
501 >        } catch (NullPointerException success) {}
502      }
503  
504      /**
# Line 472 | Line 509 | public class ConcurrentHashMapTest exten
509              ConcurrentHashMap c = new ConcurrentHashMap(5);
510              c.replace("whatever", one, null);
511              shouldThrow();
512 <        } catch(NullPointerException e){}
512 >        } catch (NullPointerException success) {}
513      }
514  
478
515      /**
516       * remove(null) throws NPE
517       */
# Line 485 | Line 521 | public class ConcurrentHashMapTest exten
521              c.put("sadsdf", "asdads");
522              c.remove(null);
523              shouldThrow();
524 <        } catch(NullPointerException e){}
524 >        } catch (NullPointerException success) {}
525      }
526  
527      /**
# Line 497 | Line 533 | public class ConcurrentHashMapTest exten
533              c.put("sadsdf", "asdads");
534              c.remove(null, "whatever");
535              shouldThrow();
536 <        } catch(NullPointerException e){}
536 >        } catch (NullPointerException success) {}
537      }
538  
539      /**
540       * remove(x, null) returns false
541       */
542      public void testRemove3() {
543 <        try {
544 <            ConcurrentHashMap c = new ConcurrentHashMap(5);
545 <            c.put("sadsdf", "asdads");
510 <            assertFalse(c.remove("sadsdf", null));
511 <        } catch(NullPointerException e){
512 <            fail();
513 <        }
543 >        ConcurrentHashMap c = new ConcurrentHashMap(5);
544 >        c.put("sadsdf", "asdads");
545 >        assertFalse(c.remove("sadsdf", null));
546      }
547  
548      /**
549       * A deserialized map equals original
550       */
551 <    public void testSerialization() {
552 <        ConcurrentHashMap q = map5();
553 <
554 <        try {
555 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
556 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
557 <            out.writeObject(q);
558 <            out.close();
527 <
528 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
529 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
530 <            ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
531 <            assertEquals(q.size(), r.size());
532 <            assertTrue(q.equals(r));
533 <            assertTrue(r.equals(q));
534 <        } catch(Exception e){
535 <            e.printStackTrace();
536 <            unexpectedException();
537 <        }
551 >    public void testSerialization() throws Exception {
552 >        Map x = map5();
553 >        Map y = serialClone(x);
554 >
555 >        assertTrue(x != y);
556 >        assertEquals(x.size(), y.size());
557 >        assertEquals(x, y);
558 >        assertEquals(y, x);
559      }
560  
540
561      /**
562       * SetValue of an EntrySet entry sets value in the map.
563       */
564      public void testSetValueWriteThrough() {
565 <        // Adapted from a bug report by Eric Zoerner
565 >        // Adapted from a bug report by Eric Zoerner
566          ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
567          assertTrue(map.isEmpty());
568          for (int i = 0; i < 20; i++)
569              map.put(new Integer(i), new Integer(i));
570          assertFalse(map.isEmpty());
571          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
572 <        
572 >
573          // assert that entry1 is not 16
574          assertTrue("entry is 16, test not valid",
575                     !entry1.getKey().equals(new Integer(16)));
576 <        
577 <        // remove 16 (a different key) from map
576 >
577 >        // remove 16 (a different key) from map
578          // which just happens to cause entry1 to be cloned in map
579          map.remove(new Integer(16));
580          entry1.setValue("XYZ");
581          assertTrue(map.containsValue("XYZ")); // fails
582      }
583 <    
583 >
584   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines