1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
2 |
> |
* 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 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
|
* Pat Fisher, Mike Judd. |
8 |
|
*/ |
9 |
|
|
10 |
< |
import junit.framework.*; |
11 |
< |
import java.util.*; |
10 |
> |
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 |
|
import java.util.concurrent.ConcurrentHashMap; |
20 |
|
|
21 |
+ |
import junit.framework.Test; |
22 |
+ |
|
23 |
|
public class ConcurrentHashMapTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
< |
return new TestSuite(ConcurrentHashMapTest.class); |
28 |
> |
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 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
|
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
45 |
|
*/ |
46 |
< |
private static ConcurrentHashMap map5() { |
47 |
< |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
46 |
> |
private static ConcurrentHashMap<Integer, String> map5() { |
47 |
> |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5); |
48 |
|
assertTrue(map.isEmpty()); |
49 |
|
map.put(one, "A"); |
50 |
|
map.put(two, "B"); |
56 |
|
return map; |
57 |
|
} |
58 |
|
|
37 |
– |
/** Re-implement Integer.compare for old java versions */ |
38 |
– |
static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } |
39 |
– |
|
59 |
|
// classes for testing Comparable fallbacks |
60 |
|
static class BI implements Comparable<BI> { |
61 |
|
private final int value; |
62 |
|
BI(int value) { this.value = value; } |
63 |
|
public int compareTo(BI other) { |
64 |
< |
return compare(value, other.value); |
64 |
> |
return Integer.compare(value, other.value); |
65 |
|
} |
66 |
|
public boolean equals(Object x) { |
67 |
|
return (x instanceof BI) && ((BI)x).value == value; |
95 |
|
break; |
96 |
|
} |
97 |
|
if (r == 0) |
98 |
< |
r = compare(size(), other.size()); |
98 |
> |
r = Integer.compare(size(), other.size()); |
99 |
|
return r; |
100 |
|
} |
101 |
|
private static final long serialVersionUID = 0; |
122 |
|
* class are found. |
123 |
|
*/ |
124 |
|
public void testComparableFamily() { |
125 |
< |
ConcurrentHashMap<BI, Boolean> m = |
126 |
< |
new ConcurrentHashMap<BI, Boolean>(); |
127 |
< |
for (int i = 0; i < 1000; i++) { |
128 |
< |
assertTrue(m.put(new CI(i), true) == null); |
125 |
> |
int size = 500; // makes measured test run time -> 60ms |
126 |
> |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
127 |
> |
for (int i = 0; i < size; i++) { |
128 |
> |
assertNull(m.put(new CI(i), true)); |
129 |
|
} |
130 |
< |
for (int i = 0; i < 1000; i++) { |
130 |
> |
for (int i = 0; i < size; i++) { |
131 |
|
assertTrue(m.containsKey(new CI(i))); |
132 |
|
assertTrue(m.containsKey(new DI(i))); |
133 |
|
} |
138 |
|
* on Comparable can be inserted and found. |
139 |
|
*/ |
140 |
|
public void testGenericComparable() { |
141 |
< |
ConcurrentHashMap<Object, Boolean> m = |
142 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
143 |
< |
for (int i = 0; i < 1000; i++) { |
141 |
> |
int size = 120; // makes measured test run time -> 60ms |
142 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
143 |
> |
for (int i = 0; i < size; i++) { |
144 |
|
BI bi = new BI(i); |
145 |
|
BS bs = new BS(String.valueOf(i)); |
146 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
147 |
< |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
148 |
< |
assertTrue(m.putIfAbsent(bis, true) == null); |
146 |
> |
LexicographicList<BI> bis = new LexicographicList<>(bi); |
147 |
> |
LexicographicList<BS> bss = new LexicographicList<>(bs); |
148 |
> |
assertNull(m.putIfAbsent(bis, true)); |
149 |
|
assertTrue(m.containsKey(bis)); |
150 |
|
if (m.putIfAbsent(bss, true) == null) |
151 |
|
assertTrue(m.containsKey(bss)); |
152 |
|
assertTrue(m.containsKey(bis)); |
153 |
|
} |
154 |
< |
for (int i = 0; i < 1000; i++) { |
155 |
< |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
154 |
> |
for (int i = 0; i < size; i++) { |
155 |
> |
assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); |
156 |
|
} |
157 |
|
} |
158 |
|
|
162 |
|
* inserted and found. |
163 |
|
*/ |
164 |
|
public void testGenericComparable2() { |
165 |
< |
ConcurrentHashMap<Object, Boolean> m = |
166 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
167 |
< |
for (int i = 0; i < 1000; i++) { |
168 |
< |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
165 |
> |
int size = 500; // makes measured test run time -> 60ms |
166 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
167 |
> |
for (int i = 0; i < size; i++) { |
168 |
> |
m.put(Collections.singletonList(new BI(i)), true); |
169 |
|
} |
170 |
|
|
171 |
< |
for (int i = 0; i < 1000; i++) { |
172 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
171 |
> |
for (int i = 0; i < size; i++) { |
172 |
> |
LexicographicList<BI> bis = new LexicographicList<>(new BI(i)); |
173 |
|
assertTrue(m.containsKey(bis)); |
174 |
|
} |
175 |
|
} |
179 |
|
* can be inserted and found. |
180 |
|
*/ |
181 |
|
public void testMixedComparable() { |
182 |
< |
int size = 10000; |
183 |
< |
ConcurrentHashMap<Object, Object> map = |
184 |
< |
new ConcurrentHashMap<Object, Object>(); |
166 |
< |
Random rng = new Random(1370014958369218000L); |
182 |
> |
int size = 1200; // makes measured test run time -> 35ms |
183 |
> |
ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>(); |
184 |
> |
Random rng = new Random(); |
185 |
|
for (int i = 0; i < size; i++) { |
186 |
|
Object x; |
187 |
|
switch (rng.nextInt(4)) { |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
+ |
* hashCode() equals sum of each key.hashCode ^ value.hashCode |
235 |
+ |
*/ |
236 |
+ |
public void testHashCode() { |
237 |
+ |
ConcurrentHashMap<Integer,String> map = map5(); |
238 |
+ |
int sum = 0; |
239 |
+ |
for (Map.Entry<Integer,String> e : map.entrySet()) |
240 |
+ |
sum += e.getKey().hashCode() ^ e.getValue().hashCode(); |
241 |
+ |
assertEquals(sum, map.hashCode()); |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
/** |
245 |
|
* contains returns true for contained value |
246 |
|
*/ |
247 |
|
public void testContains() { |
292 |
|
assertEquals("A", (String)map.get(one)); |
293 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
294 |
|
assertNull(map.get("anything")); |
295 |
+ |
assertNull(empty.get("anything")); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
+ |
* Test keySet().removeAll on empty map |
338 |
+ |
*/ |
339 |
+ |
public void testKeySet_empty_removeAll() { |
340 |
+ |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); |
341 |
+ |
Set<Integer> set = map.keySet(); |
342 |
+ |
set.removeAll(Collections.emptyList()); |
343 |
+ |
assertTrue(map.isEmpty()); |
344 |
+ |
assertTrue(set.isEmpty()); |
345 |
+ |
// following is test for JDK-8163353 |
346 |
+ |
set.removeAll(Collections.emptySet()); |
347 |
+ |
assertTrue(map.isEmpty()); |
348 |
+ |
assertTrue(set.isEmpty()); |
349 |
+ |
} |
350 |
+ |
|
351 |
+ |
/** |
352 |
|
* keySet.toArray returns contains all keys |
353 |
|
*/ |
354 |
|
public void testKeySetToArray() { |
541 |
|
// Exception tests |
542 |
|
|
543 |
|
/** |
544 |
< |
* Cannot create with negative capacity |
544 |
> |
* Cannot create with only negative capacity |
545 |
|
*/ |
546 |
|
public void testConstructor1() { |
547 |
|
try { |
548 |
< |
new ConcurrentHashMap(-1,0,1); |
548 |
> |
new ConcurrentHashMap(-1); |
549 |
|
shouldThrow(); |
550 |
|
} catch (IllegalArgumentException success) {} |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
< |
* Cannot create with negative concurrency level |
554 |
> |
* Constructor (initialCapacity, loadFactor) throws |
555 |
> |
* IllegalArgumentException if either argument is negative |
556 |
|
*/ |
557 |
|
public void testConstructor2() { |
558 |
|
try { |
559 |
< |
new ConcurrentHashMap(1,0,-1); |
559 |
> |
new ConcurrentHashMap(-1, .75f); |
560 |
> |
shouldThrow(); |
561 |
> |
} catch (IllegalArgumentException success) {} |
562 |
> |
|
563 |
> |
try { |
564 |
> |
new ConcurrentHashMap(16, -1); |
565 |
|
shouldThrow(); |
566 |
|
} catch (IllegalArgumentException success) {} |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
< |
* Cannot create with only negative capacity |
570 |
> |
* Constructor (initialCapacity, loadFactor, concurrencyLevel) |
571 |
> |
* throws IllegalArgumentException if any argument is negative |
572 |
|
*/ |
573 |
|
public void testConstructor3() { |
574 |
|
try { |
575 |
< |
new ConcurrentHashMap(-1); |
575 |
> |
new ConcurrentHashMap(-1, .75f, 1); |
576 |
> |
shouldThrow(); |
577 |
> |
} catch (IllegalArgumentException success) {} |
578 |
> |
|
579 |
> |
try { |
580 |
> |
new ConcurrentHashMap(16, -1, 1); |
581 |
> |
shouldThrow(); |
582 |
> |
} catch (IllegalArgumentException success) {} |
583 |
> |
|
584 |
> |
try { |
585 |
> |
new ConcurrentHashMap(16, .75f, -1); |
586 |
|
shouldThrow(); |
587 |
|
} catch (IllegalArgumentException success) {} |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
+ |
* ConcurrentHashMap(map) throws NullPointerException if the given |
592 |
+ |
* map is null |
593 |
+ |
*/ |
594 |
+ |
public void testConstructor4() { |
595 |
+ |
try { |
596 |
+ |
new ConcurrentHashMap(null); |
597 |
+ |
shouldThrow(); |
598 |
+ |
} catch (NullPointerException success) {} |
599 |
+ |
} |
600 |
+ |
|
601 |
+ |
/** |
602 |
+ |
* ConcurrentHashMap(map) creates a new map with the same mappings |
603 |
+ |
* as the given map |
604 |
+ |
*/ |
605 |
+ |
public void testConstructor5() { |
606 |
+ |
ConcurrentHashMap map1 = map5(); |
607 |
+ |
ConcurrentHashMap map2 = new ConcurrentHashMap(map5()); |
608 |
+ |
assertTrue(map2.equals(map1)); |
609 |
+ |
map2.put(one, "F"); |
610 |
+ |
assertFalse(map2.equals(map1)); |
611 |
+ |
} |
612 |
+ |
|
613 |
+ |
/** |
614 |
|
* get(null) throws NPE |
615 |
|
*/ |
616 |
|
public void testGet_NullPointerException() { |
617 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
618 |
|
try { |
533 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
619 |
|
c.get(null); |
620 |
|
shouldThrow(); |
621 |
|
} catch (NullPointerException success) {} |
625 |
|
* containsKey(null) throws NPE |
626 |
|
*/ |
627 |
|
public void testContainsKey_NullPointerException() { |
628 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
629 |
|
try { |
544 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
630 |
|
c.containsKey(null); |
631 |
|
shouldThrow(); |
632 |
|
} catch (NullPointerException success) {} |
636 |
|
* containsValue(null) throws NPE |
637 |
|
*/ |
638 |
|
public void testContainsValue_NullPointerException() { |
639 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
640 |
|
try { |
555 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
641 |
|
c.containsValue(null); |
642 |
|
shouldThrow(); |
643 |
|
} catch (NullPointerException success) {} |
647 |
|
* contains(null) throws NPE |
648 |
|
*/ |
649 |
|
public void testContains_NullPointerException() { |
650 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
651 |
|
try { |
566 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
652 |
|
c.contains(null); |
653 |
|
shouldThrow(); |
654 |
|
} catch (NullPointerException success) {} |
658 |
|
* put(null,x) throws NPE |
659 |
|
*/ |
660 |
|
public void testPut1_NullPointerException() { |
661 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
662 |
|
try { |
577 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
663 |
|
c.put(null, "whatever"); |
664 |
|
shouldThrow(); |
665 |
|
} catch (NullPointerException success) {} |
669 |
|
* put(x, null) throws NPE |
670 |
|
*/ |
671 |
|
public void testPut2_NullPointerException() { |
672 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
673 |
|
try { |
588 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
674 |
|
c.put("whatever", null); |
675 |
|
shouldThrow(); |
676 |
|
} catch (NullPointerException success) {} |
680 |
|
* putIfAbsent(null, x) throws NPE |
681 |
|
*/ |
682 |
|
public void testPutIfAbsent1_NullPointerException() { |
683 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
684 |
|
try { |
599 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
685 |
|
c.putIfAbsent(null, "whatever"); |
686 |
|
shouldThrow(); |
687 |
|
} catch (NullPointerException success) {} |
691 |
|
* replace(null, x) throws NPE |
692 |
|
*/ |
693 |
|
public void testReplace_NullPointerException() { |
694 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
695 |
|
try { |
610 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
696 |
|
c.replace(null, "whatever"); |
697 |
|
shouldThrow(); |
698 |
|
} catch (NullPointerException success) {} |
702 |
|
* replace(null, x, y) throws NPE |
703 |
|
*/ |
704 |
|
public void testReplaceValue_NullPointerException() { |
705 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
706 |
|
try { |
621 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
707 |
|
c.replace(null, one, "whatever"); |
708 |
|
shouldThrow(); |
709 |
|
} catch (NullPointerException success) {} |
713 |
|
* putIfAbsent(x, null) throws NPE |
714 |
|
*/ |
715 |
|
public void testPutIfAbsent2_NullPointerException() { |
716 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
717 |
|
try { |
632 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
718 |
|
c.putIfAbsent("whatever", null); |
719 |
|
shouldThrow(); |
720 |
|
} catch (NullPointerException success) {} |
724 |
|
* replace(x, null) throws NPE |
725 |
|
*/ |
726 |
|
public void testReplace2_NullPointerException() { |
727 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
728 |
|
try { |
643 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
729 |
|
c.replace("whatever", null); |
730 |
|
shouldThrow(); |
731 |
|
} catch (NullPointerException success) {} |
735 |
|
* replace(x, null, y) throws NPE |
736 |
|
*/ |
737 |
|
public void testReplaceValue2_NullPointerException() { |
738 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
739 |
|
try { |
654 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
740 |
|
c.replace("whatever", null, "A"); |
741 |
|
shouldThrow(); |
742 |
|
} catch (NullPointerException success) {} |
746 |
|
* replace(x, y, null) throws NPE |
747 |
|
*/ |
748 |
|
public void testReplaceValue3_NullPointerException() { |
749 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
750 |
|
try { |
665 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
751 |
|
c.replace("whatever", one, null); |
752 |
|
shouldThrow(); |
753 |
|
} catch (NullPointerException success) {} |
757 |
|
* remove(null) throws NPE |
758 |
|
*/ |
759 |
|
public void testRemove1_NullPointerException() { |
760 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
761 |
+ |
c.put("sadsdf", "asdads"); |
762 |
|
try { |
676 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
677 |
– |
c.put("sadsdf", "asdads"); |
763 |
|
c.remove(null); |
764 |
|
shouldThrow(); |
765 |
|
} catch (NullPointerException success) {} |
769 |
|
* remove(null, x) throws NPE |
770 |
|
*/ |
771 |
|
public void testRemove2_NullPointerException() { |
772 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
773 |
+ |
c.put("sadsdf", "asdads"); |
774 |
|
try { |
688 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
689 |
– |
c.put("sadsdf", "asdads"); |
775 |
|
c.remove(null, "whatever"); |
776 |
|
shouldThrow(); |
777 |
|
} catch (NullPointerException success) {} |
787 |
|
} |
788 |
|
|
789 |
|
/** |
790 |
< |
* A deserialized map equals original |
790 |
> |
* A deserialized/reserialized map equals original |
791 |
|
*/ |
792 |
|
public void testSerialization() throws Exception { |
793 |
|
Map x = map5(); |
821 |
|
} |
822 |
|
} |
823 |
|
|
824 |
+ |
/** |
825 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
826 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
827 |
+ |
*/ |
828 |
+ |
public void testRemoveAll_performance() { |
829 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
830 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
831 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
832 |
+ |
for (int i = 0; i < mapSize; i++) |
833 |
+ |
map.put(i, i); |
834 |
+ |
Set<Integer> keySet = map.keySet(); |
835 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
836 |
+ |
for (int i = 0; i < iterations; i++) |
837 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
838 |
+ |
assertEquals(mapSize, map.size()); |
839 |
+ |
} |
840 |
+ |
|
841 |
+ |
public void testReentrantComputeIfAbsent() { |
842 |
+ |
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(16); |
843 |
+ |
try { |
844 |
+ |
for (int i = 0; i < 100; i++) { // force a resize |
845 |
+ |
map.computeIfAbsent(i, key -> findValue(map, key)); |
846 |
+ |
} |
847 |
+ |
fail("recursive computeIfAbsent"); |
848 |
+ |
} catch (IllegalStateException ex) { |
849 |
+ |
} |
850 |
+ |
} |
851 |
+ |
|
852 |
+ |
private Integer findValue(ConcurrentHashMap<Integer, Integer> map, |
853 |
+ |
Integer key) { |
854 |
+ |
return (key % 5 == 0) ? key : |
855 |
+ |
map.computeIfAbsent(key + 1, k -> findValue(map, k)); |
856 |
+ |
} |
857 |
+ |
|
858 |
|
} |