ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.49
Committed: Sun Jul 17 19:02:07 2016 UTC (7 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +32 -3 lines
Log Message:
add testcomputeIfAbsent_performance for JDK-8161372

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.9 * 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 jsr166 1.23 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.14 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.40 import java.util.ArrayList;
10     import java.util.Arrays;
11     import java.util.Collection;
12     import java.util.Collections;
13     import java.util.Enumeration;
14     import java.util.Iterator;
15     import java.util.Map;
16     import java.util.Random;
17     import java.util.Set;
18 jsr166 1.25 import java.util.concurrent.ConcurrentHashMap;
19 jsr166 1.49 import java.util.concurrent.ExecutorService;
20     import java.util.concurrent.Executors;
21 dl 1.1
22 jsr166 1.40 import junit.framework.Test;
23     import junit.framework.TestSuite;
24    
25 jsr166 1.16 public class ConcurrentHashMapTest extends JSR166TestCase {
26 dl 1.1 public static void main(String[] args) {
27 jsr166 1.45 main(suite(), args);
28 dl 1.1 }
29     public static Test suite() {
30 jsr166 1.17 return new TestSuite(ConcurrentHashMapTest.class);
31 dl 1.1 }
32    
33 dl 1.5 /**
34 jsr166 1.28 * Returns a new map from Integers 1-5 to Strings "A"-"E".
35 dl 1.5 */
36 dl 1.36 private static ConcurrentHashMap<Integer, String> map5() {
37     ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
38 dl 1.1 assertTrue(map.isEmpty());
39 jsr166 1.17 map.put(one, "A");
40     map.put(two, "B");
41     map.put(three, "C");
42     map.put(four, "D");
43     map.put(five, "E");
44 dl 1.1 assertFalse(map.isEmpty());
45     assertEquals(5, map.size());
46 jsr166 1.17 return map;
47 dl 1.1 }
48    
49 jsr166 1.33 /** Re-implement Integer.compare for old java versions */
50 jsr166 1.46 static int compare(int x, int y) {
51     return (x < y) ? -1 : (x > y) ? 1 : 0;
52     }
53 jsr166 1.33
54 jsr166 1.30 // classes for testing Comparable fallbacks
55     static class BI implements Comparable<BI> {
56     private final int value;
57     BI(int value) { this.value = value; }
58     public int compareTo(BI other) {
59 jsr166 1.33 return compare(value, other.value);
60 jsr166 1.30 }
61     public boolean equals(Object x) {
62     return (x instanceof BI) && ((BI)x).value == value;
63     }
64     public int hashCode() { return 42; }
65     }
66     static class CI extends BI { CI(int value) { super(value); } }
67     static class DI extends BI { DI(int value) { super(value); } }
68    
69     static class BS implements Comparable<BS> {
70     private final String value;
71     BS(String value) { this.value = value; }
72     public int compareTo(BS other) {
73     return value.compareTo(other.value);
74     }
75     public boolean equals(Object x) {
76     return (x instanceof BS) && value.equals(((BS)x).value);
77     }
78     public int hashCode() { return 42; }
79     }
80    
81     static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
82     implements Comparable<LexicographicList<E>> {
83     LexicographicList(Collection<E> c) { super(c); }
84     LexicographicList(E e) { super(Collections.singleton(e)); }
85     public int compareTo(LexicographicList<E> other) {
86     int common = Math.min(size(), other.size());
87     int r = 0;
88     for (int i = 0; i < common; i++) {
89     if ((r = get(i).compareTo(other.get(i))) != 0)
90     break;
91     }
92     if (r == 0)
93 jsr166 1.33 r = compare(size(), other.size());
94 jsr166 1.30 return r;
95     }
96     private static final long serialVersionUID = 0;
97     }
98    
99 dl 1.34 static class CollidingObject {
100     final String value;
101     CollidingObject(final String value) { this.value = value; }
102     public int hashCode() { return this.value.hashCode() & 1; }
103     public boolean equals(final Object obj) {
104     return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
105     }
106     }
107    
108     static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
109     ComparableCollidingObject(final String value) { super(value); }
110     public int compareTo(final ComparableCollidingObject o) {
111     return value.compareTo(o.value);
112     }
113     }
114    
115 jsr166 1.30 /**
116     * Inserted elements that are subclasses of the same Comparable
117     * class are found.
118     */
119     public void testComparableFamily() {
120 jsr166 1.39 int size = 500; // makes measured test run time -> 60ms
121 jsr166 1.32 ConcurrentHashMap<BI, Boolean> m =
122     new ConcurrentHashMap<BI, Boolean>();
123 jsr166 1.39 for (int i = 0; i < size; i++) {
124 jsr166 1.30 assertTrue(m.put(new CI(i), true) == null);
125     }
126 jsr166 1.39 for (int i = 0; i < size; i++) {
127 jsr166 1.30 assertTrue(m.containsKey(new CI(i)));
128     assertTrue(m.containsKey(new DI(i)));
129     }
130     }
131    
132     /**
133     * Elements of classes with erased generic type parameters based
134     * on Comparable can be inserted and found.
135     */
136     public void testGenericComparable() {
137 jsr166 1.39 int size = 120; // makes measured test run time -> 60ms
138 jsr166 1.32 ConcurrentHashMap<Object, Boolean> m =
139     new ConcurrentHashMap<Object, Boolean>();
140 jsr166 1.39 for (int i = 0; i < size; i++) {
141 jsr166 1.30 BI bi = new BI(i);
142     BS bs = new BS(String.valueOf(i));
143     LexicographicList<BI> bis = new LexicographicList<BI>(bi);
144     LexicographicList<BS> bss = new LexicographicList<BS>(bs);
145     assertTrue(m.putIfAbsent(bis, true) == null);
146     assertTrue(m.containsKey(bis));
147     if (m.putIfAbsent(bss, true) == null)
148     assertTrue(m.containsKey(bss));
149     assertTrue(m.containsKey(bis));
150     }
151 jsr166 1.39 for (int i = 0; i < size; i++) {
152     assertTrue(m.containsKey(Collections.singletonList(new BI(i))));
153 jsr166 1.30 }
154     }
155    
156     /**
157     * Elements of non-comparable classes equal to those of classes
158     * with erased generic type parameters based on Comparable can be
159     * inserted and found.
160     */
161     public void testGenericComparable2() {
162 jsr166 1.39 int size = 500; // makes measured test run time -> 60ms
163 jsr166 1.32 ConcurrentHashMap<Object, Boolean> m =
164     new ConcurrentHashMap<Object, Boolean>();
165 jsr166 1.39 for (int i = 0; i < size; i++) {
166     m.put(Collections.singletonList(new BI(i)), true);
167 jsr166 1.30 }
168    
169 jsr166 1.39 for (int i = 0; i < size; i++) {
170 jsr166 1.30 LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
171     assertTrue(m.containsKey(bis));
172     }
173     }
174    
175 dl 1.1 /**
176 dl 1.34 * Mixtures of instances of comparable and non-comparable classes
177     * can be inserted and found.
178     */
179     public void testMixedComparable() {
180 jsr166 1.38 int size = 1200; // makes measured test run time -> 35ms
181 jsr166 1.35 ConcurrentHashMap<Object, Object> map =
182 dl 1.34 new ConcurrentHashMap<Object, Object>();
183 jsr166 1.38 Random rng = new Random();
184 jsr166 1.35 for (int i = 0; i < size; i++) {
185 dl 1.34 Object x;
186 jsr166 1.35 switch (rng.nextInt(4)) {
187 dl 1.34 case 0:
188     x = new Object();
189     break;
190     case 1:
191     x = new CollidingObject(Integer.toString(i));
192     break;
193     default:
194     x = new ComparableCollidingObject(Integer.toString(i));
195     }
196     assertNull(map.put(x, x));
197     }
198     int count = 0;
199     for (Object k : map.keySet()) {
200     assertEquals(map.get(k), k);
201     ++count;
202     }
203     assertEquals(count, size);
204     assertEquals(map.size(), size);
205     for (Object k : map.keySet()) {
206     assertEquals(map.put(k, k), k);
207     }
208     }
209    
210     /**
211 jsr166 1.22 * clear removes all pairs
212 dl 1.1 */
213 dl 1.5 public void testClear() {
214 dl 1.1 ConcurrentHashMap map = map5();
215 jsr166 1.17 map.clear();
216 jsr166 1.26 assertEquals(0, map.size());
217 dl 1.1 }
218    
219     /**
220 jsr166 1.22 * Maps with same contents are equal
221 dl 1.5 */
222     public void testEquals() {
223     ConcurrentHashMap map1 = map5();
224     ConcurrentHashMap map2 = map5();
225     assertEquals(map1, map2);
226     assertEquals(map2, map1);
227 jsr166 1.17 map1.clear();
228 dl 1.5 assertFalse(map1.equals(map2));
229     assertFalse(map2.equals(map1));
230     }
231    
232 dl 1.36 /**
233 jsr166 1.37 * hashCode() equals sum of each key.hashCode ^ value.hashCode
234     */
235 dl 1.36 public void testHashCode() {
236     ConcurrentHashMap<Integer,String> map = map5();
237     int sum = 0;
238     for (Map.Entry<Integer,String> e : map.entrySet())
239     sum += e.getKey().hashCode() ^ e.getValue().hashCode();
240     assertEquals(sum, map.hashCode());
241     }
242    
243 dl 1.5 /**
244 jsr166 1.22 * contains returns true for contained value
245 dl 1.1 */
246 dl 1.5 public void testContains() {
247 dl 1.1 ConcurrentHashMap map = map5();
248 jsr166 1.17 assertTrue(map.contains("A"));
249 dl 1.1 assertFalse(map.contains("Z"));
250     }
251 jsr166 1.14
252 dl 1.1 /**
253 jsr166 1.22 * containsKey returns true for contained key
254 dl 1.1 */
255 dl 1.5 public void testContainsKey() {
256 dl 1.1 ConcurrentHashMap map = map5();
257 jsr166 1.17 assertTrue(map.containsKey(one));
258 dl 1.5 assertFalse(map.containsKey(zero));
259 dl 1.1 }
260    
261     /**
262 jsr166 1.22 * containsValue returns true for held values
263 dl 1.1 */
264 dl 1.5 public void testContainsValue() {
265 dl 1.1 ConcurrentHashMap map = map5();
266 jsr166 1.17 assertTrue(map.containsValue("A"));
267 dl 1.11 assertFalse(map.containsValue("Z"));
268 dl 1.1 }
269    
270     /**
271 jsr166 1.22 * enumeration returns an enumeration containing the correct
272     * elements
273 dl 1.1 */
274 dl 1.5 public void testEnumeration() {
275 dl 1.1 ConcurrentHashMap map = map5();
276 jsr166 1.17 Enumeration e = map.elements();
277     int count = 0;
278     while (e.hasMoreElements()) {
279     count++;
280     e.nextElement();
281     }
282     assertEquals(5, count);
283 dl 1.4 }
284    
285     /**
286 jsr166 1.22 * get returns the correct element at the given key,
287     * or null if not present
288 dl 1.1 */
289 dl 1.5 public void testGet() {
290 dl 1.1 ConcurrentHashMap map = map5();
291 jsr166 1.17 assertEquals("A", (String)map.get(one));
292 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
293 dl 1.5 assertNull(map.get("anything"));
294 jsr166 1.42 assertNull(empty.get("anything"));
295 dl 1.1 }
296    
297     /**
298 jsr166 1.22 * isEmpty is true of empty map and false for non-empty
299 dl 1.1 */
300 dl 1.5 public void testIsEmpty() {
301 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
302     ConcurrentHashMap map = map5();
303 jsr166 1.17 assertTrue(empty.isEmpty());
304 dl 1.1 assertFalse(map.isEmpty());
305     }
306    
307     /**
308 jsr166 1.22 * keys returns an enumeration containing all the keys from the map
309 dl 1.1 */
310 dl 1.5 public void testKeys() {
311 dl 1.1 ConcurrentHashMap map = map5();
312 jsr166 1.17 Enumeration e = map.keys();
313     int count = 0;
314     while (e.hasMoreElements()) {
315     count++;
316     e.nextElement();
317     }
318     assertEquals(5, count);
319 dl 1.1 }
320    
321     /**
322 jsr166 1.22 * keySet returns a Set containing all the keys
323 dl 1.1 */
324 dl 1.5 public void testKeySet() {
325 dl 1.1 ConcurrentHashMap map = map5();
326 jsr166 1.17 Set s = map.keySet();
327     assertEquals(5, s.size());
328     assertTrue(s.contains(one));
329     assertTrue(s.contains(two));
330     assertTrue(s.contains(three));
331     assertTrue(s.contains(four));
332     assertTrue(s.contains(five));
333 dl 1.1 }
334    
335 dl 1.5 /**
336 jsr166 1.22 * keySet.toArray returns contains all keys
337 dl 1.13 */
338     public void testKeySetToArray() {
339     ConcurrentHashMap map = map5();
340 jsr166 1.17 Set s = map.keySet();
341 dl 1.13 Object[] ar = s.toArray();
342     assertTrue(s.containsAll(Arrays.asList(ar)));
343 jsr166 1.17 assertEquals(5, ar.length);
344 dl 1.13 ar[0] = m10;
345     assertFalse(s.containsAll(Arrays.asList(ar)));
346     }
347    
348     /**
349 jsr166 1.22 * Values.toArray contains all values
350 dl 1.13 */
351     public void testValuesToArray() {
352     ConcurrentHashMap map = map5();
353 jsr166 1.17 Collection v = map.values();
354 dl 1.13 Object[] ar = v.toArray();
355     ArrayList s = new ArrayList(Arrays.asList(ar));
356 jsr166 1.17 assertEquals(5, ar.length);
357     assertTrue(s.contains("A"));
358     assertTrue(s.contains("B"));
359     assertTrue(s.contains("C"));
360     assertTrue(s.contains("D"));
361     assertTrue(s.contains("E"));
362 dl 1.13 }
363    
364     /**
365 jsr166 1.22 * entrySet.toArray contains all entries
366 dl 1.13 */
367     public void testEntrySetToArray() {
368     ConcurrentHashMap map = map5();
369 jsr166 1.17 Set s = map.entrySet();
370 dl 1.13 Object[] ar = s.toArray();
371     assertEquals(5, ar.length);
372     for (int i = 0; i < 5; ++i) {
373     assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
374     assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
375     }
376     }
377    
378     /**
379 dl 1.5 * values collection contains all values
380     */
381     public void testValues() {
382 dl 1.1 ConcurrentHashMap map = map5();
383 jsr166 1.17 Collection s = map.values();
384     assertEquals(5, s.size());
385     assertTrue(s.contains("A"));
386     assertTrue(s.contains("B"));
387     assertTrue(s.contains("C"));
388     assertTrue(s.contains("D"));
389     assertTrue(s.contains("E"));
390 dl 1.1 }
391    
392 dl 1.5 /**
393     * entrySet contains all pairs
394     */
395     public void testEntrySet() {
396 dl 1.1 ConcurrentHashMap map = map5();
397 jsr166 1.17 Set s = map.entrySet();
398     assertEquals(5, s.size());
399 dl 1.1 Iterator it = s.iterator();
400     while (it.hasNext()) {
401     Map.Entry e = (Map.Entry) it.next();
402 jsr166 1.14 assertTrue(
403 dl 1.1 (e.getKey().equals(one) && e.getValue().equals("A")) ||
404     (e.getKey().equals(two) && e.getValue().equals("B")) ||
405     (e.getKey().equals(three) && e.getValue().equals("C")) ||
406     (e.getKey().equals(four) && e.getValue().equals("D")) ||
407     (e.getKey().equals(five) && e.getValue().equals("E")));
408     }
409     }
410    
411     /**
412 jsr166 1.22 * putAll adds all key-value pairs from the given map
413 dl 1.1 */
414 dl 1.5 public void testPutAll() {
415 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
416     ConcurrentHashMap map = map5();
417 jsr166 1.17 empty.putAll(map);
418     assertEquals(5, empty.size());
419     assertTrue(empty.containsKey(one));
420     assertTrue(empty.containsKey(two));
421     assertTrue(empty.containsKey(three));
422     assertTrue(empty.containsKey(four));
423     assertTrue(empty.containsKey(five));
424 dl 1.1 }
425    
426     /**
427 jsr166 1.22 * putIfAbsent works when the given key is not present
428 dl 1.1 */
429 dl 1.5 public void testPutIfAbsent() {
430 dl 1.1 ConcurrentHashMap map = map5();
431 jsr166 1.17 map.putIfAbsent(six, "Z");
432 dl 1.7 assertTrue(map.containsKey(six));
433 dl 1.1 }
434    
435     /**
436 jsr166 1.22 * putIfAbsent does not add the pair if the key is already present
437 dl 1.1 */
438 dl 1.5 public void testPutIfAbsent2() {
439 dl 1.1 ConcurrentHashMap map = map5();
440     assertEquals("A", map.putIfAbsent(one, "Z"));
441     }
442    
443     /**
444 jsr166 1.22 * replace fails when the given key is not present
445 dl 1.7 */
446     public void testReplace() {
447     ConcurrentHashMap map = map5();
448 jsr166 1.17 assertNull(map.replace(six, "Z"));
449 dl 1.7 assertFalse(map.containsKey(six));
450     }
451    
452     /**
453 jsr166 1.22 * replace succeeds if the key is already present
454 dl 1.7 */
455     public void testReplace2() {
456     ConcurrentHashMap map = map5();
457 dl 1.8 assertNotNull(map.replace(one, "Z"));
458 dl 1.7 assertEquals("Z", map.get(one));
459     }
460    
461     /**
462     * replace value fails when the given key not mapped to expected value
463     */
464     public void testReplaceValue() {
465     ConcurrentHashMap map = map5();
466     assertEquals("A", map.get(one));
467 jsr166 1.17 assertFalse(map.replace(one, "Z", "Z"));
468 dl 1.7 assertEquals("A", map.get(one));
469     }
470    
471     /**
472     * replace value succeeds when the given key mapped to expected value
473     */
474     public void testReplaceValue2() {
475     ConcurrentHashMap map = map5();
476     assertEquals("A", map.get(one));
477 jsr166 1.17 assertTrue(map.replace(one, "A", "Z"));
478 dl 1.7 assertEquals("Z", map.get(one));
479     }
480    
481     /**
482 jsr166 1.22 * remove removes the correct key-value pair from the map
483 dl 1.1 */
484 dl 1.5 public void testRemove() {
485 dl 1.1 ConcurrentHashMap map = map5();
486 jsr166 1.17 map.remove(five);
487     assertEquals(4, map.size());
488     assertFalse(map.containsKey(five));
489 dl 1.1 }
490    
491 dl 1.5 /**
492     * remove(key,value) removes only if pair present
493     */
494     public void testRemove2() {
495 dl 1.1 ConcurrentHashMap map = map5();
496 jsr166 1.17 map.remove(five, "E");
497     assertEquals(4, map.size());
498     assertFalse(map.containsKey(five));
499     map.remove(four, "A");
500     assertEquals(4, map.size());
501     assertTrue(map.containsKey(four));
502 dl 1.1 }
503    
504     /**
505 jsr166 1.22 * size returns the correct values
506 dl 1.1 */
507 dl 1.5 public void testSize() {
508 dl 1.1 ConcurrentHashMap map = map5();
509     ConcurrentHashMap empty = new ConcurrentHashMap();
510 jsr166 1.17 assertEquals(0, empty.size());
511     assertEquals(5, map.size());
512 dl 1.1 }
513    
514 dl 1.5 /**
515     * toString contains toString of elements
516     */
517     public void testToString() {
518 dl 1.1 ConcurrentHashMap map = map5();
519     String s = map.toString();
520     for (int i = 1; i <= 5; ++i) {
521 jsr166 1.24 assertTrue(s.contains(String.valueOf(i)));
522 dl 1.1 }
523 jsr166 1.14 }
524 dl 1.1
525     // Exception tests
526 jsr166 1.14
527 dl 1.5 /**
528 dl 1.36 * Cannot create with only negative capacity
529 dl 1.5 */
530     public void testConstructor1() {
531     try {
532 dl 1.36 new ConcurrentHashMap(-1);
533 dl 1.5 shouldThrow();
534 jsr166 1.19 } catch (IllegalArgumentException success) {}
535 dl 1.1 }
536    
537 dl 1.5 /**
538 dl 1.36 * Constructor (initialCapacity, loadFactor) throws
539     * IllegalArgumentException if either argument is negative
540 jsr166 1.47 */
541 dl 1.5 public void testConstructor2() {
542     try {
543 dl 1.36 new ConcurrentHashMap(-1, .75f);
544 dl 1.5 shouldThrow();
545 jsr166 1.43 } catch (IllegalArgumentException success) {}
546 jsr166 1.37
547 dl 1.36 try {
548     new ConcurrentHashMap(16, -1);
549     shouldThrow();
550 jsr166 1.43 } catch (IllegalArgumentException success) {}
551 dl 1.1 }
552 jsr166 1.37
553     /**
554     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
555     * throws IllegalArgumentException if any argument is negative
556     */
557 dl 1.36 public void testConstructor3() {
558 jsr166 1.37 try {
559     new ConcurrentHashMap(-1, .75f, 1);
560     shouldThrow();
561 jsr166 1.43 } catch (IllegalArgumentException success) {}
562 jsr166 1.37
563     try {
564     new ConcurrentHashMap(16, -1, 1);
565     shouldThrow();
566 jsr166 1.43 } catch (IllegalArgumentException success) {}
567 jsr166 1.37
568     try {
569     new ConcurrentHashMap(16, .75f, -1);
570     shouldThrow();
571 jsr166 1.43 } catch (IllegalArgumentException success) {}
572 jsr166 1.37 }
573 dl 1.1
574 dl 1.5 /**
575 dl 1.36 * ConcurrentHashMap(map) throws NullPointerException if the given
576     * map is null
577 dl 1.5 */
578 dl 1.36 public void testConstructor4() {
579 dl 1.5 try {
580 dl 1.36 new ConcurrentHashMap(null);
581 dl 1.5 shouldThrow();
582 jsr166 1.43 } catch (NullPointerException success) {}
583 dl 1.36 }
584    
585     /**
586     * ConcurrentHashMap(map) creates a new map with the same mappings
587     * as the given map
588     */
589     public void testConstructor5() {
590     ConcurrentHashMap map1 = map5();
591     ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
592     assertTrue(map2.equals(map1));
593     map2.put(one, "F");
594     assertFalse(map2.equals(map1));
595 dl 1.1 }
596    
597 dl 1.5 /**
598     * get(null) throws NPE
599     */
600     public void testGet_NullPointerException() {
601 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
602 dl 1.5 try {
603 dl 1.1 c.get(null);
604 dl 1.5 shouldThrow();
605 jsr166 1.19 } catch (NullPointerException success) {}
606 dl 1.1 }
607    
608 dl 1.5 /**
609     * containsKey(null) throws NPE
610     */
611     public void testContainsKey_NullPointerException() {
612 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
613 dl 1.5 try {
614 dl 1.1 c.containsKey(null);
615 dl 1.5 shouldThrow();
616 jsr166 1.19 } catch (NullPointerException success) {}
617 dl 1.1 }
618    
619 dl 1.5 /**
620     * containsValue(null) throws NPE
621     */
622     public void testContainsValue_NullPointerException() {
623 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
624 dl 1.5 try {
625 dl 1.1 c.containsValue(null);
626 dl 1.5 shouldThrow();
627 jsr166 1.19 } catch (NullPointerException success) {}
628 dl 1.1 }
629    
630 dl 1.5 /**
631     * contains(null) throws NPE
632     */
633     public void testContains_NullPointerException() {
634 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
635 dl 1.5 try {
636 dl 1.1 c.contains(null);
637 dl 1.5 shouldThrow();
638 jsr166 1.19 } catch (NullPointerException success) {}
639 dl 1.1 }
640    
641 dl 1.5 /**
642     * put(null,x) throws NPE
643     */
644     public void testPut1_NullPointerException() {
645 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
646 dl 1.5 try {
647 dl 1.1 c.put(null, "whatever");
648 dl 1.5 shouldThrow();
649 jsr166 1.19 } catch (NullPointerException success) {}
650 dl 1.1 }
651    
652 dl 1.5 /**
653     * put(x, null) throws NPE
654     */
655     public void testPut2_NullPointerException() {
656 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
657 dl 1.5 try {
658 dl 1.1 c.put("whatever", null);
659 dl 1.5 shouldThrow();
660 jsr166 1.19 } catch (NullPointerException success) {}
661 dl 1.1 }
662    
663 dl 1.5 /**
664     * putIfAbsent(null, x) throws NPE
665     */
666     public void testPutIfAbsent1_NullPointerException() {
667 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
668 dl 1.5 try {
669 dl 1.1 c.putIfAbsent(null, "whatever");
670 dl 1.5 shouldThrow();
671 jsr166 1.19 } catch (NullPointerException success) {}
672 dl 1.1 }
673    
674 dl 1.5 /**
675 dl 1.7 * replace(null, x) throws NPE
676     */
677     public void testReplace_NullPointerException() {
678 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
679 dl 1.7 try {
680     c.replace(null, "whatever");
681     shouldThrow();
682 jsr166 1.19 } catch (NullPointerException success) {}
683 dl 1.7 }
684    
685     /**
686     * replace(null, x, y) throws NPE
687     */
688     public void testReplaceValue_NullPointerException() {
689 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
690 dl 1.7 try {
691     c.replace(null, one, "whatever");
692     shouldThrow();
693 jsr166 1.19 } catch (NullPointerException success) {}
694 dl 1.7 }
695    
696     /**
697 dl 1.5 * putIfAbsent(x, null) throws NPE
698     */
699     public void testPutIfAbsent2_NullPointerException() {
700 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
701 dl 1.5 try {
702 dl 1.1 c.putIfAbsent("whatever", null);
703 dl 1.7 shouldThrow();
704 jsr166 1.19 } catch (NullPointerException success) {}
705 dl 1.7 }
706    
707     /**
708     * replace(x, null) throws NPE
709     */
710     public void testReplace2_NullPointerException() {
711 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
712 dl 1.7 try {
713     c.replace("whatever", null);
714     shouldThrow();
715 jsr166 1.19 } catch (NullPointerException success) {}
716 dl 1.7 }
717    
718     /**
719     * replace(x, null, y) throws NPE
720     */
721     public void testReplaceValue2_NullPointerException() {
722 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
723 dl 1.7 try {
724     c.replace("whatever", null, "A");
725     shouldThrow();
726 jsr166 1.19 } catch (NullPointerException success) {}
727 dl 1.7 }
728    
729     /**
730     * replace(x, y, null) throws NPE
731     */
732     public void testReplaceValue3_NullPointerException() {
733 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
734 dl 1.7 try {
735     c.replace("whatever", one, null);
736 dl 1.5 shouldThrow();
737 jsr166 1.19 } catch (NullPointerException success) {}
738 dl 1.1 }
739    
740 dl 1.5 /**
741     * remove(null) throws NPE
742     */
743     public void testRemove1_NullPointerException() {
744 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
745     c.put("sadsdf", "asdads");
746 dl 1.5 try {
747 dl 1.1 c.remove(null);
748 dl 1.5 shouldThrow();
749 jsr166 1.19 } catch (NullPointerException success) {}
750 dl 1.1 }
751    
752 dl 1.5 /**
753     * remove(null, x) throws NPE
754     */
755     public void testRemove2_NullPointerException() {
756 jsr166 1.44 ConcurrentHashMap c = new ConcurrentHashMap(5);
757     c.put("sadsdf", "asdads");
758 dl 1.5 try {
759 dl 1.1 c.remove(null, "whatever");
760 dl 1.5 shouldThrow();
761 jsr166 1.19 } catch (NullPointerException success) {}
762 dl 1.1 }
763 dl 1.2
764 dl 1.5 /**
765 dl 1.12 * remove(x, null) returns false
766     */
767     public void testRemove3() {
768 jsr166 1.19 ConcurrentHashMap c = new ConcurrentHashMap(5);
769     c.put("sadsdf", "asdads");
770     assertFalse(c.remove("sadsdf", null));
771 dl 1.12 }
772    
773     /**
774 dl 1.5 * A deserialized map equals original
775     */
776 jsr166 1.18 public void testSerialization() throws Exception {
777 jsr166 1.25 Map x = map5();
778     Map y = serialClone(x);
779 dl 1.2
780 jsr166 1.31 assertNotSame(x, y);
781 jsr166 1.25 assertEquals(x.size(), y.size());
782     assertEquals(x, y);
783     assertEquals(y, x);
784 dl 1.2 }
785 dl 1.6
786     /**
787     * SetValue of an EntrySet entry sets value in the map.
788     */
789     public void testSetValueWriteThrough() {
790 jsr166 1.14 // Adapted from a bug report by Eric Zoerner
791 dl 1.6 ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
792     assertTrue(map.isEmpty());
793     for (int i = 0; i < 20; i++)
794     map.put(new Integer(i), new Integer(i));
795     assertFalse(map.isEmpty());
796     Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
797 dl 1.29 // Unless it happens to be first (in which case remainder of
798     // test is skipped), remove a possibly-colliding key from map
799     // which, under some implementations, may cause entry1 to be
800     // cloned in map
801     if (!entry1.getKey().equals(new Integer(16))) {
802     map.remove(new Integer(16));
803     entry1.setValue("XYZ");
804     assertTrue(map.containsValue("XYZ")); // fails if write-through broken
805     }
806 dl 1.6 }
807 jsr166 1.14
808 jsr166 1.48 /**
809     * Tests performance of removeAll when the other collection is much smaller.
810     * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck
811     */
812     public void testRemoveAll_performance() {
813 jsr166 1.49 final int mapSize = expensiveTests ? 1_000_000 : 100;
814     final int iterations = expensiveTests ? 500 : 2;
815     final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
816 jsr166 1.48 for (int i = 0; i < mapSize; i++)
817     map.put(i, i);
818     Set<Integer> keySet = map.keySet();
819     Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
820     for (int i = 0; i < iterations; i++)
821     assertFalse(keySet.removeAll(removeMe));
822     assertEquals(mapSize, map.size());
823     }
824 jsr166 1.49
825     /**
826     * Tests performance of computeIfAbsent when the element is present.
827     * See JDK-8161372
828     * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck
829     */
830     public void testcomputeIfAbsent_performance() {
831     final int mapSize = 20;
832     final int iterations = expensiveTests ? (1 << 23) : mapSize * 2;
833     final int threads = expensiveTests ? 10 : 2;
834     final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
835     for (int i = 0; i < mapSize; i++)
836     map.put(i, i);
837     final ExecutorService pool = Executors.newFixedThreadPool(2);
838     try (PoolCleaner cleaner = cleaner(pool)) {
839     Runnable r = new CheckedRunnable() {
840     public void realRun() {
841     int result = 0;
842     for (int i = 0; i < iterations; i++)
843     result += map.computeIfAbsent(i % mapSize, (k) -> k + k);
844     if (result == -42) throw new Error();
845     }};
846     for (int i = 0; i < threads; i++)
847     pool.execute(r);
848     }
849     }
850    
851 dl 1.1 }