ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.6
Committed: Fri Nov 28 12:38:08 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.5: +24 -0 lines
Log Message:
Test EntrySet.entry.setValue

File Contents

# User Rev Content
1 dl 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.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11     import java.util.Enumeration;
12 dl 1.2 import java.io.*;
13 dl 1.1
14 dl 1.3 public class ConcurrentHashMapTest extends JSR166TestCase{
15 dl 1.1 public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(ConcurrentHashMapTest.class);
20     }
21    
22 dl 1.5 /**
23     * Create a map from Integers 1-5 to Strings "A"-"E".
24     */
25 dl 1.1 private static ConcurrentHashMap map5() {
26     ConcurrentHashMap map = new ConcurrentHashMap(5);
27     assertTrue(map.isEmpty());
28     map.put(one, "A");
29     map.put(two, "B");
30     map.put(three, "C");
31     map.put(four, "D");
32     map.put(five, "E");
33     assertFalse(map.isEmpty());
34     assertEquals(5, map.size());
35     return map;
36     }
37    
38     /**
39 dl 1.5 * clear removes all pairs
40 dl 1.1 */
41 dl 1.5 public void testClear() {
42 dl 1.1 ConcurrentHashMap map = map5();
43     map.clear();
44     assertEquals(map.size(), 0);
45     }
46    
47     /**
48 dl 1.5 * 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 dl 1.1 */
63 dl 1.5 public void testContains() {
64 dl 1.1 ConcurrentHashMap map = map5();
65     assertTrue(map.contains("A"));
66     assertFalse(map.contains("Z"));
67     }
68    
69     /**
70 dl 1.5 * containsKey returns true for contained key
71 dl 1.1 */
72 dl 1.5 public void testContainsKey() {
73 dl 1.1 ConcurrentHashMap map = map5();
74     assertTrue(map.containsKey(one));
75 dl 1.5 assertFalse(map.containsKey(zero));
76 dl 1.1 }
77    
78     /**
79 dl 1.5 * containsValue returns true for held values
80 dl 1.1 */
81 dl 1.5 public void testContainsValue() {
82 dl 1.1 ConcurrentHashMap map = map5();
83     assertTrue(map.contains("A"));
84     assertFalse(map.contains("Z"));
85     }
86    
87     /**
88 dl 1.4 * enumeration returns an enumeration containing the correct
89     * elements
90 dl 1.1 */
91 dl 1.5 public void testEnumeration() {
92 dl 1.1 ConcurrentHashMap map = map5();
93     Enumeration e = map.elements();
94     int count = 0;
95     while(e.hasMoreElements()){
96     count++;
97     e.nextElement();
98     }
99     assertEquals(5, count);
100 dl 1.4 }
101    
102     /**
103     * Clone creates an equal map
104     */
105 dl 1.5 public void testClone() {
106 dl 1.4 ConcurrentHashMap map = map5();
107     ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone());
108     assertEquals(map, m2);
109 dl 1.1 }
110    
111     /**
112 dl 1.5 * get returns the correct element at the given key,
113     * or null if not present
114 dl 1.1 */
115 dl 1.5 public void testGet() {
116 dl 1.1 ConcurrentHashMap map = map5();
117     assertEquals("A", (String)map.get(one));
118     ConcurrentHashMap empty = new ConcurrentHashMap();
119 dl 1.5 assertNull(map.get("anything"));
120 dl 1.1 }
121    
122     /**
123 dl 1.5 * isEmpty is true of empty map and false for non-empty
124 dl 1.1 */
125 dl 1.5 public void testIsEmpty() {
126 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
127     ConcurrentHashMap map = map5();
128     assertTrue(empty.isEmpty());
129     assertFalse(map.isEmpty());
130     }
131    
132     /**
133 dl 1.3 * keys returns an enumeration containing all the keys from the map
134 dl 1.1 */
135 dl 1.5 public void testKeys() {
136 dl 1.1 ConcurrentHashMap map = map5();
137     Enumeration e = map.keys();
138     int count = 0;
139     while(e.hasMoreElements()){
140     count++;
141     e.nextElement();
142     }
143     assertEquals(5, count);
144     }
145    
146     /**
147 dl 1.3 * keySet returns a Set containing all the keys
148 dl 1.1 */
149 dl 1.5 public void testKeySet() {
150 dl 1.1 ConcurrentHashMap map = map5();
151     Set s = map.keySet();
152     assertEquals(5, s.size());
153     assertTrue(s.contains(one));
154     assertTrue(s.contains(two));
155     assertTrue(s.contains(three));
156     assertTrue(s.contains(four));
157     assertTrue(s.contains(five));
158     }
159    
160 dl 1.5 /**
161     * values collection contains all values
162     */
163     public void testValues() {
164 dl 1.1 ConcurrentHashMap map = map5();
165     Collection s = map.values();
166     assertEquals(5, s.size());
167     assertTrue(s.contains("A"));
168     assertTrue(s.contains("B"));
169     assertTrue(s.contains("C"));
170     assertTrue(s.contains("D"));
171     assertTrue(s.contains("E"));
172     }
173    
174 dl 1.5 /**
175     * entrySet contains all pairs
176     */
177     public void testEntrySet() {
178 dl 1.1 ConcurrentHashMap map = map5();
179     Set s = map.entrySet();
180     assertEquals(5, s.size());
181     Iterator it = s.iterator();
182     while (it.hasNext()) {
183     Map.Entry e = (Map.Entry) it.next();
184     assertTrue(
185     (e.getKey().equals(one) && e.getValue().equals("A")) ||
186     (e.getKey().equals(two) && e.getValue().equals("B")) ||
187     (e.getKey().equals(three) && e.getValue().equals("C")) ||
188     (e.getKey().equals(four) && e.getValue().equals("D")) ||
189     (e.getKey().equals(five) && e.getValue().equals("E")));
190     }
191     }
192    
193     /**
194 dl 1.3 * putAll adds all key-value pairs from the given map
195 dl 1.1 */
196 dl 1.5 public void testPutAll() {
197 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
198     ConcurrentHashMap map = map5();
199     empty.putAll(map);
200     assertEquals(5, empty.size());
201     assertTrue(empty.containsKey(one));
202     assertTrue(empty.containsKey(two));
203     assertTrue(empty.containsKey(three));
204     assertTrue(empty.containsKey(four));
205     assertTrue(empty.containsKey(five));
206     }
207    
208     /**
209 dl 1.3 * putIfAbsent works when the given key is not present
210 dl 1.1 */
211 dl 1.5 public void testPutIfAbsent() {
212 dl 1.1 ConcurrentHashMap map = map5();
213     map.putIfAbsent(new Integer(6), "Z");
214     assertTrue(map.containsKey(new Integer(6)));
215     }
216    
217     /**
218 dl 1.3 * putIfAbsent does not add the pair if the key is already present
219 dl 1.1 */
220 dl 1.5 public void testPutIfAbsent2() {
221 dl 1.1 ConcurrentHashMap map = map5();
222     assertEquals("A", map.putIfAbsent(one, "Z"));
223     }
224    
225     /**
226 dl 1.3 * remove removes the correct key-value pair from the map
227 dl 1.1 */
228 dl 1.5 public void testRemove() {
229 dl 1.1 ConcurrentHashMap map = map5();
230     map.remove(five);
231     assertEquals(4, map.size());
232     assertFalse(map.containsKey(five));
233     }
234    
235 dl 1.5 /**
236     * remove(key,value) removes only if pair present
237     */
238     public void testRemove2() {
239 dl 1.1 ConcurrentHashMap map = map5();
240     map.remove(five, "E");
241     assertEquals(4, map.size());
242     assertFalse(map.containsKey(five));
243     map.remove(four, "A");
244     assertEquals(4, map.size());
245     assertTrue(map.containsKey(four));
246    
247     }
248    
249     /**
250 dl 1.3 * size returns the correct values
251 dl 1.1 */
252 dl 1.5 public void testSize() {
253 dl 1.1 ConcurrentHashMap map = map5();
254     ConcurrentHashMap empty = new ConcurrentHashMap();
255     assertEquals(0, empty.size());
256     assertEquals(5, map.size());
257     }
258    
259 dl 1.5 /**
260     * toString contains toString of elements
261     */
262     public void testToString() {
263 dl 1.1 ConcurrentHashMap map = map5();
264     String s = map.toString();
265     for (int i = 1; i <= 5; ++i) {
266     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
267     }
268     }
269    
270     // Exception tests
271    
272 dl 1.5 /**
273     * Cannot create with negative capacity
274     */
275     public void testConstructor1() {
276     try {
277 dl 1.1 new ConcurrentHashMap(-1,0,1);
278 dl 1.5 shouldThrow();
279     } catch(IllegalArgumentException e){}
280 dl 1.1 }
281    
282 dl 1.5 /**
283     * Cannot create with negative concurrency level
284     */
285     public void testConstructor2() {
286     try {
287 dl 1.1 new ConcurrentHashMap(1,0,-1);
288 dl 1.5 shouldThrow();
289     } catch(IllegalArgumentException e){}
290 dl 1.1 }
291    
292 dl 1.5 /**
293     * Cannot create with only negative capacity
294     */
295     public void testConstructor3() {
296     try {
297 dl 1.1 new ConcurrentHashMap(-1);
298 dl 1.5 shouldThrow();
299     } catch(IllegalArgumentException e){}
300 dl 1.1 }
301    
302 dl 1.5 /**
303     * get(null) throws NPE
304     */
305     public void testGet_NullPointerException() {
306     try {
307 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
308     c.get(null);
309 dl 1.5 shouldThrow();
310     } catch(NullPointerException e){}
311 dl 1.1 }
312    
313 dl 1.5 /**
314     * containsKey(null) throws NPE
315     */
316     public void testContainsKey_NullPointerException() {
317     try {
318 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
319     c.containsKey(null);
320 dl 1.5 shouldThrow();
321     } catch(NullPointerException e){}
322 dl 1.1 }
323    
324 dl 1.5 /**
325     * containsValue(null) throws NPE
326     */
327     public void testContainsValue_NullPointerException() {
328     try {
329 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
330     c.containsValue(null);
331 dl 1.5 shouldThrow();
332     } catch(NullPointerException e){}
333 dl 1.1 }
334    
335 dl 1.5 /**
336     * contains(null) throws NPE
337     */
338     public void testContains_NullPointerException() {
339     try {
340 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
341     c.contains(null);
342 dl 1.5 shouldThrow();
343     } catch(NullPointerException e){}
344 dl 1.1 }
345    
346 dl 1.5 /**
347     * put(null,x) throws NPE
348     */
349     public void testPut1_NullPointerException() {
350     try {
351 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
352     c.put(null, "whatever");
353 dl 1.5 shouldThrow();
354     } catch(NullPointerException e){}
355 dl 1.1 }
356    
357 dl 1.5 /**
358     * put(x, null) throws NPE
359     */
360     public void testPut2_NullPointerException() {
361     try {
362 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
363     c.put("whatever", null);
364 dl 1.5 shouldThrow();
365     } catch(NullPointerException e){}
366 dl 1.1 }
367    
368 dl 1.5 /**
369     * putIfAbsent(null, x) throws NPE
370     */
371     public void testPutIfAbsent1_NullPointerException() {
372     try {
373 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
374     c.putIfAbsent(null, "whatever");
375 dl 1.5 shouldThrow();
376     } catch(NullPointerException e){}
377 dl 1.1 }
378    
379 dl 1.5 /**
380     * putIfAbsent(x, null) throws NPE
381     */
382     public void testPutIfAbsent2_NullPointerException() {
383     try {
384 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
385     c.putIfAbsent("whatever", null);
386 dl 1.5 shouldThrow();
387     } catch(NullPointerException e){}
388 dl 1.1 }
389    
390    
391 dl 1.5 /**
392     * remove(null) throws NPE
393     */
394     public void testRemove1_NullPointerException() {
395     try {
396 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
397     c.put("sadsdf", "asdads");
398     c.remove(null);
399 dl 1.5 shouldThrow();
400     } catch(NullPointerException e){}
401 dl 1.1 }
402    
403 dl 1.5 /**
404     * remove(null, x) throws NPE
405     */
406     public void testRemove2_NullPointerException() {
407     try {
408 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
409     c.put("sadsdf", "asdads");
410     c.remove(null, "whatever");
411 dl 1.5 shouldThrow();
412     } catch(NullPointerException e){}
413 dl 1.1 }
414 dl 1.2
415 dl 1.5 /**
416     * A deserialized map equals original
417     */
418 dl 1.2 public void testSerialization() {
419     ConcurrentHashMap q = map5();
420    
421     try {
422     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
423     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
424     out.writeObject(q);
425     out.close();
426    
427     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
428     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
429     ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
430     assertEquals(q.size(), r.size());
431     assertTrue(q.equals(r));
432     assertTrue(r.equals(q));
433     } catch(Exception e){
434     e.printStackTrace();
435 dl 1.5 unexpectedException();
436 dl 1.2 }
437     }
438 dl 1.6
439    
440     /**
441     * SetValue of an EntrySet entry sets value in the map.
442     */
443     public void testSetValueWriteThrough() {
444     // Adapted from a bug report by Eric Zoerner
445     ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
446     assertTrue(map.isEmpty());
447     for (int i = 0; i < 20; i++)
448     map.put(new Integer(i), new Integer(i));
449     assertFalse(map.isEmpty());
450     Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
451    
452     // assert that entry1 is not 16
453     assertTrue("entry is 16, test not valid",
454     !entry1.getKey().equals(new Integer(16)));
455    
456     // remove 16 (a different key) from map
457     // which just happens to cause entry1 to be cloned in map
458     map.remove(new Integer(16));
459     entry1.setValue("XYZ");
460     assertTrue(map.containsValue("XYZ")); // fails
461     }
462 dl 1.1
463     }