ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.57
Committed: Sun Jan 7 22:59:18 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.56: +7 -11 lines
Log Message:
use <>

File Contents

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