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.10 by dl, Wed Jun 2 23:49:34 2004 UTC vs.
Revision 1.20 by jsr166, Tue Dec 1 09:48:13 2009 UTC

# Line 2 | Line 2
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.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 12 | Line 12 | import java.util.concurrent.*;
12   import java.util.Enumeration;
13   import java.io.*;
14  
15 < public class ConcurrentHashMapTest extends JSR166TestCase{
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);
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);
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");
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;
36 >        return map;
37      }
38  
39      /**
# Line 41 | Line 41 | public class ConcurrentHashMapTest exten
41       */
42      public void testClear() {
43          ConcurrentHashMap map = map5();
44 <        map.clear();
45 <        assertEquals(map.size(), 0);
44 >        map.clear();
45 >        assertEquals(map.size(), 0);
46      }
47  
48      /**
# Line 53 | Line 53 | public class ConcurrentHashMapTest exten
53          ConcurrentHashMap map2 = map5();
54          assertEquals(map1, map2);
55          assertEquals(map2, map1);
56 <        map1.clear();
56 >        map1.clear();
57          assertFalse(map1.equals(map2));
58          assertFalse(map2.equals(map1));
59      }
# Line 63 | Line 63 | public class ConcurrentHashMapTest exten
63       */
64      public void testContains() {
65          ConcurrentHashMap map = map5();
66 <        assertTrue(map.contains("A"));
66 >        assertTrue(map.contains("A"));
67          assertFalse(map.contains("Z"));
68      }
69 <    
69 >
70      /**
71       *  containsKey returns true for contained key
72       */
73      public void testContainsKey() {
74          ConcurrentHashMap map = map5();
75 <        assertTrue(map.containsKey(one));
75 >        assertTrue(map.containsKey(one));
76          assertFalse(map.containsKey(zero));
77      }
78  
# Line 81 | 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 91 | Line 91 | public class ConcurrentHashMapTest exten
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);
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      /**
# Line 106 | Line 106 | public class ConcurrentHashMapTest exten
106       */
107      public void testGet() {
108          ConcurrentHashMap map = map5();
109 <        assertEquals("A", (String)map.get(one));
109 >        assertEquals("A", (String)map.get(one));
110          ConcurrentHashMap empty = new ConcurrentHashMap();
111          assertNull(map.get("anything"));
112      }
# Line 117 | Line 117 | public class ConcurrentHashMapTest exten
117      public void testIsEmpty() {
118          ConcurrentHashMap empty = new ConcurrentHashMap();
119          ConcurrentHashMap map = map5();
120 <        assertTrue(empty.isEmpty());
120 >        assertTrue(empty.isEmpty());
121          assertFalse(map.isEmpty());
122      }
123  
# Line 126 | Line 126 | public class ConcurrentHashMapTest exten
126       */
127      public void testKeys() {
128          ConcurrentHashMap map = map5();
129 <        Enumeration e = map.keys();
130 <        int count = 0;
131 <        while(e.hasMoreElements()){
132 <            count++;
133 <            e.nextElement();
134 <        }
135 <        assertEquals(5, count);
129 >        Enumeration e = map.keys();
130 >        int count = 0;
131 >        while (e.hasMoreElements()) {
132 >            count++;
133 >            e.nextElement();
134 >        }
135 >        assertEquals(5, count);
136      }
137  
138      /**
# Line 140 | Line 140 | public class ConcurrentHashMapTest exten
140       */
141      public void testKeySet() {
142          ConcurrentHashMap map = map5();
143 <        Set s = map.keySet();
144 <        assertEquals(5, s.size());
145 <        assertTrue(s.contains(one));
146 <        assertTrue(s.contains(two));
147 <        assertTrue(s.contains(three));
148 <        assertTrue(s.contains(four));
149 <        assertTrue(s.contains(five));
143 >        Set s = map.keySet();
144 >        assertEquals(5, s.size());
145 >        assertTrue(s.contains(one));
146 >        assertTrue(s.contains(two));
147 >        assertTrue(s.contains(three));
148 >        assertTrue(s.contains(four));
149 >        assertTrue(s.contains(five));
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      /**
# Line 154 | Line 197 | public class ConcurrentHashMapTest exten
197       */
198      public void testValues() {
199          ConcurrentHashMap map = map5();
200 <        Collection s = map.values();
201 <        assertEquals(5, s.size());
202 <        assertTrue(s.contains("A"));
203 <        assertTrue(s.contains("B"));
204 <        assertTrue(s.contains("C"));
205 <        assertTrue(s.contains("D"));
206 <        assertTrue(s.contains("E"));
200 >        Collection s = map.values();
201 >        assertEquals(5, s.size());
202 >        assertTrue(s.contains("A"));
203 >        assertTrue(s.contains("B"));
204 >        assertTrue(s.contains("C"));
205 >        assertTrue(s.contains("D"));
206 >        assertTrue(s.contains("E"));
207      }
208  
209      /**
# Line 168 | Line 211 | public class ConcurrentHashMapTest exten
211       */
212      public void testEntrySet() {
213          ConcurrentHashMap map = map5();
214 <        Set s = map.entrySet();
215 <        assertEquals(5, s.size());
214 >        Set s = map.entrySet();
215 >        assertEquals(5, s.size());
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 188 | Line 231 | public class ConcurrentHashMapTest exten
231      public void testPutAll() {
232          ConcurrentHashMap empty = new ConcurrentHashMap();
233          ConcurrentHashMap map = map5();
234 <        empty.putAll(map);
235 <        assertEquals(5, empty.size());
236 <        assertTrue(empty.containsKey(one));
237 <        assertTrue(empty.containsKey(two));
238 <        assertTrue(empty.containsKey(three));
239 <        assertTrue(empty.containsKey(four));
240 <        assertTrue(empty.containsKey(five));
234 >        empty.putAll(map);
235 >        assertEquals(5, empty.size());
236 >        assertTrue(empty.containsKey(one));
237 >        assertTrue(empty.containsKey(two));
238 >        assertTrue(empty.containsKey(three));
239 >        assertTrue(empty.containsKey(four));
240 >        assertTrue(empty.containsKey(five));
241      }
242  
243      /**
# Line 202 | Line 245 | public class ConcurrentHashMapTest exten
245       */
246      public void testPutIfAbsent() {
247          ConcurrentHashMap map = map5();
248 <        map.putIfAbsent(six, "Z");
248 >        map.putIfAbsent(six, "Z");
249          assertTrue(map.containsKey(six));
250      }
251  
# Line 219 | Line 262 | public class ConcurrentHashMapTest exten
262       */
263      public void testReplace() {
264          ConcurrentHashMap map = map5();
265 <        assertNull(map.replace(six, "Z"));
265 >        assertNull(map.replace(six, "Z"));
266          assertFalse(map.containsKey(six));
267      }
268  
# Line 239 | Line 282 | public class ConcurrentHashMapTest exten
282      public void testReplaceValue() {
283          ConcurrentHashMap map = map5();
284          assertEquals("A", map.get(one));
285 <        assertFalse(map.replace(one, "Z", "Z"));
285 >        assertFalse(map.replace(one, "Z", "Z"));
286          assertEquals("A", map.get(one));
287      }
288  
# Line 249 | Line 292 | public class ConcurrentHashMapTest exten
292      public void testReplaceValue2() {
293          ConcurrentHashMap map = map5();
294          assertEquals("A", map.get(one));
295 <        assertTrue(map.replace(one, "A", "Z"));
295 >        assertTrue(map.replace(one, "A", "Z"));
296          assertEquals("Z", map.get(one));
297      }
298  
# Line 259 | Line 302 | public class ConcurrentHashMapTest exten
302       */
303      public void testRemove() {
304          ConcurrentHashMap map = map5();
305 <        map.remove(five);
306 <        assertEquals(4, map.size());
307 <        assertFalse(map.containsKey(five));
305 >        map.remove(five);
306 >        assertEquals(4, map.size());
307 >        assertFalse(map.containsKey(five));
308      }
309  
310      /**
# Line 269 | Line 312 | public class ConcurrentHashMapTest exten
312       */
313      public void testRemove2() {
314          ConcurrentHashMap map = map5();
315 <        map.remove(five, "E");
316 <        assertEquals(4, map.size());
317 <        assertFalse(map.containsKey(five));
318 <        map.remove(four, "A");
319 <        assertEquals(4, map.size());
320 <        assertTrue(map.containsKey(four));
278 <
315 >        map.remove(five, "E");
316 >        assertEquals(4, map.size());
317 >        assertFalse(map.containsKey(five));
318 >        map.remove(four, "A");
319 >        assertEquals(4, map.size());
320 >        assertTrue(map.containsKey(four));
321      }
322  
323      /**
# Line 284 | Line 326 | public class ConcurrentHashMapTest exten
326      public void testSize() {
327          ConcurrentHashMap map = map5();
328          ConcurrentHashMap empty = new ConcurrentHashMap();
329 <        assertEquals(0, empty.size());
330 <        assertEquals(5, map.size());
329 >        assertEquals(0, empty.size());
330 >        assertEquals(5, map.size());
331      }
332  
333      /**
# Line 297 | Line 339 | public class ConcurrentHashMapTest exten
339          for (int i = 1; i <= 5; ++i) {
340              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
341          }
342 <    }        
342 >    }
343  
344      // Exception tests
345 <    
345 >
346      /**
347 <     * Cannot create with negative capacity
347 >     * Cannot create with negative capacity
348       */
349      public void testConstructor1() {
350          try {
351              new ConcurrentHashMap(-1,0,1);
352              shouldThrow();
353 <        } catch(IllegalArgumentException e){}
353 >        } catch (IllegalArgumentException success) {}
354      }
355  
356      /**
# Line 318 | Line 360 | public class ConcurrentHashMapTest exten
360          try {
361              new ConcurrentHashMap(1,0,-1);
362              shouldThrow();
363 <        } catch(IllegalArgumentException e){}
363 >        } catch (IllegalArgumentException success) {}
364      }
365  
366      /**
# Line 328 | Line 370 | public class ConcurrentHashMapTest exten
370          try {
371              new ConcurrentHashMap(-1);
372              shouldThrow();
373 <        } catch(IllegalArgumentException e){}
373 >        } catch (IllegalArgumentException success) {}
374      }
375  
376      /**
# Line 339 | Line 381 | public class ConcurrentHashMapTest exten
381              ConcurrentHashMap c = new ConcurrentHashMap(5);
382              c.get(null);
383              shouldThrow();
384 <        } catch(NullPointerException e){}
384 >        } catch (NullPointerException success) {}
385      }
386  
387      /**
# Line 350 | Line 392 | public class ConcurrentHashMapTest exten
392              ConcurrentHashMap c = new ConcurrentHashMap(5);
393              c.containsKey(null);
394              shouldThrow();
395 <        } catch(NullPointerException e){}
395 >        } catch (NullPointerException success) {}
396      }
397  
398      /**
# Line 361 | Line 403 | public class ConcurrentHashMapTest exten
403              ConcurrentHashMap c = new ConcurrentHashMap(5);
404              c.containsValue(null);
405              shouldThrow();
406 <        } catch(NullPointerException e){}
406 >        } catch (NullPointerException success) {}
407      }
408  
409      /**
# Line 372 | Line 414 | public class ConcurrentHashMapTest exten
414              ConcurrentHashMap c = new ConcurrentHashMap(5);
415              c.contains(null);
416              shouldThrow();
417 <        } catch(NullPointerException e){}
417 >        } catch (NullPointerException success) {}
418      }
419  
420      /**
# Line 383 | Line 425 | public class ConcurrentHashMapTest exten
425              ConcurrentHashMap c = new ConcurrentHashMap(5);
426              c.put(null, "whatever");
427              shouldThrow();
428 <        } catch(NullPointerException e){}
428 >        } catch (NullPointerException success) {}
429      }
430  
431      /**
# Line 394 | Line 436 | public class ConcurrentHashMapTest exten
436              ConcurrentHashMap c = new ConcurrentHashMap(5);
437              c.put("whatever", null);
438              shouldThrow();
439 <        } catch(NullPointerException e){}
439 >        } catch (NullPointerException success) {}
440      }
441  
442      /**
# Line 405 | Line 447 | public class ConcurrentHashMapTest exten
447              ConcurrentHashMap c = new ConcurrentHashMap(5);
448              c.putIfAbsent(null, "whatever");
449              shouldThrow();
450 <        } catch(NullPointerException e){}
450 >        } catch (NullPointerException success) {}
451      }
452  
453      /**
# Line 416 | Line 458 | public class ConcurrentHashMapTest exten
458              ConcurrentHashMap c = new ConcurrentHashMap(5);
459              c.replace(null, "whatever");
460              shouldThrow();
461 <        } catch(NullPointerException e){}
461 >        } catch (NullPointerException success) {}
462      }
463  
464      /**
# Line 427 | Line 469 | public class ConcurrentHashMapTest exten
469              ConcurrentHashMap c = new ConcurrentHashMap(5);
470              c.replace(null, one, "whatever");
471              shouldThrow();
472 <        } catch(NullPointerException e){}
472 >        } catch (NullPointerException success) {}
473      }
474  
475      /**
# Line 438 | Line 480 | public class ConcurrentHashMapTest exten
480              ConcurrentHashMap c = new ConcurrentHashMap(5);
481              c.putIfAbsent("whatever", null);
482              shouldThrow();
483 <        } catch(NullPointerException e){}
483 >        } catch (NullPointerException success) {}
484      }
485  
486  
# Line 450 | Line 492 | public class ConcurrentHashMapTest exten
492              ConcurrentHashMap c = new ConcurrentHashMap(5);
493              c.replace("whatever", null);
494              shouldThrow();
495 <        } catch(NullPointerException e){}
495 >        } catch (NullPointerException success) {}
496      }
497  
498      /**
# Line 461 | Line 503 | public class ConcurrentHashMapTest exten
503              ConcurrentHashMap c = new ConcurrentHashMap(5);
504              c.replace("whatever", null, "A");
505              shouldThrow();
506 <        } catch(NullPointerException e){}
506 >        } catch (NullPointerException success) {}
507      }
508  
509      /**
# Line 472 | Line 514 | public class ConcurrentHashMapTest exten
514              ConcurrentHashMap c = new ConcurrentHashMap(5);
515              c.replace("whatever", one, null);
516              shouldThrow();
517 <        } catch(NullPointerException e){}
517 >        } catch (NullPointerException success) {}
518      }
519  
520  
# Line 485 | Line 527 | public class ConcurrentHashMapTest exten
527              c.put("sadsdf", "asdads");
528              c.remove(null);
529              shouldThrow();
530 <        } catch(NullPointerException e){}
530 >        } catch (NullPointerException success) {}
531      }
532  
533      /**
# Line 497 | Line 539 | public class ConcurrentHashMapTest exten
539              c.put("sadsdf", "asdads");
540              c.remove(null, "whatever");
541              shouldThrow();
542 <        } catch(NullPointerException e){}
542 >        } catch (NullPointerException success) {}
543 >    }
544 >
545 >    /**
546 >     * remove(x, null) returns false
547 >     */
548 >    public void testRemove3() {
549 >        ConcurrentHashMap c = new ConcurrentHashMap(5);
550 >        c.put("sadsdf", "asdads");
551 >        assertFalse(c.remove("sadsdf", null));
552      }
553  
554      /**
555       * A deserialized map equals original
556       */
557 <    public void testSerialization() {
557 >    public void testSerialization() throws Exception {
558          ConcurrentHashMap q = map5();
559  
560 <        try {
561 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
562 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
563 <            out.writeObject(q);
564 <            out.close();
565 <
566 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
567 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
568 <            ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
569 <            assertEquals(q.size(), r.size());
570 <            assertTrue(q.equals(r));
520 <            assertTrue(r.equals(q));
521 <        } catch(Exception e){
522 <            e.printStackTrace();
523 <            unexpectedException();
524 <        }
560 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
561 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
562 >        out.writeObject(q);
563 >        out.close();
564 >
565 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
566 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
567 >        ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
568 >        assertEquals(q.size(), r.size());
569 >        assertTrue(q.equals(r));
570 >        assertTrue(r.equals(q));
571      }
572  
573  
# Line 529 | Line 575 | public class ConcurrentHashMapTest exten
575       * SetValue of an EntrySet entry sets value in the map.
576       */
577      public void testSetValueWriteThrough() {
578 <        // Adapted from a bug report by Eric Zoerner
578 >        // Adapted from a bug report by Eric Zoerner
579          ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
580          assertTrue(map.isEmpty());
581          for (int i = 0; i < 20; i++)
582              map.put(new Integer(i), new Integer(i));
583          assertFalse(map.isEmpty());
584          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
585 <        
585 >
586          // assert that entry1 is not 16
587          assertTrue("entry is 16, test not valid",
588                     !entry1.getKey().equals(new Integer(16)));
589 <        
590 <        // remove 16 (a different key) from map
589 >
590 >        // remove 16 (a different key) from map
591          // which just happens to cause entry1 to be cloned in map
592          map.remove(new Integer(16));
593          entry1.setValue("XYZ");
594          assertTrue(map.containsValue("XYZ")); // fails
595      }
596 <    
596 >
597   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines