ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.53
Committed: Sat Mar 11 17:33:32 2017 UTC (7 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.52: +0 -2 lines
Log Message:
fix unused imports reported by errorprone [RemoveUnusedImports]

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