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 boolean isConcurrent() { return true; } |
32 |
> |
public boolean permitsNullKeys() { return false; } |
33 |
> |
public boolean permitsNullValues() { return false; } |
34 |
> |
public boolean supportsSetValue() { return true; } |
35 |
> |
} |
36 |
> |
return newTestSuite( |
37 |
> |
ConcurrentHashMapTest.class, |
38 |
> |
MapTest.testSuite(new Implementation())); |
39 |
|
} |
40 |
|
|
41 |
|
/** |
42 |
|
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
43 |
|
*/ |
44 |
|
private static ConcurrentHashMap<Integer, String> map5() { |
45 |
< |
ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5); |
45 |
> |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5); |
46 |
|
assertTrue(map.isEmpty()); |
47 |
|
map.put(one, "A"); |
48 |
|
map.put(two, "B"); |
54 |
|
return map; |
55 |
|
} |
56 |
|
|
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 |
– |
|
57 |
|
// classes for testing Comparable fallbacks |
58 |
|
static class BI implements Comparable<BI> { |
59 |
|
private final int value; |
60 |
|
BI(int value) { this.value = value; } |
61 |
|
public int compareTo(BI other) { |
62 |
< |
return compare(value, other.value); |
62 |
> |
return Integer.compare(value, other.value); |
63 |
|
} |
64 |
|
public boolean equals(Object x) { |
65 |
|
return (x instanceof BI) && ((BI)x).value == value; |
93 |
|
break; |
94 |
|
} |
95 |
|
if (r == 0) |
96 |
< |
r = compare(size(), other.size()); |
96 |
> |
r = Integer.compare(size(), other.size()); |
97 |
|
return r; |
98 |
|
} |
99 |
|
private static final long serialVersionUID = 0; |
121 |
|
*/ |
122 |
|
public void testComparableFamily() { |
123 |
|
int size = 500; // makes measured test run time -> 60ms |
124 |
< |
ConcurrentHashMap<BI, Boolean> m = |
108 |
< |
new ConcurrentHashMap<BI, Boolean>(); |
124 |
> |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
125 |
|
for (int i = 0; i < size; i++) { |
126 |
< |
assertTrue(m.put(new CI(i), true) == null); |
126 |
> |
assertNull(m.put(new CI(i), true)); |
127 |
|
} |
128 |
|
for (int i = 0; i < size; i++) { |
129 |
|
assertTrue(m.containsKey(new CI(i))); |
137 |
|
*/ |
138 |
|
public void testGenericComparable() { |
139 |
|
int size = 120; // makes measured test run time -> 60ms |
140 |
< |
ConcurrentHashMap<Object, Boolean> m = |
125 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
140 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
141 |
|
for (int i = 0; i < size; i++) { |
142 |
|
BI bi = new BI(i); |
143 |
|
BS bs = new BS(String.valueOf(i)); |
144 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
145 |
< |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
146 |
< |
assertTrue(m.putIfAbsent(bis, true) == null); |
144 |
> |
LexicographicList<BI> bis = new LexicographicList<>(bi); |
145 |
> |
LexicographicList<BS> bss = new LexicographicList<>(bs); |
146 |
> |
assertNull(m.putIfAbsent(bis, true)); |
147 |
|
assertTrue(m.containsKey(bis)); |
148 |
|
if (m.putIfAbsent(bss, true) == null) |
149 |
|
assertTrue(m.containsKey(bss)); |
161 |
|
*/ |
162 |
|
public void testGenericComparable2() { |
163 |
|
int size = 500; // makes measured test run time -> 60ms |
164 |
< |
ConcurrentHashMap<Object, Boolean> m = |
150 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
164 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
165 |
|
for (int i = 0; i < size; i++) { |
166 |
|
m.put(Collections.singletonList(new BI(i)), true); |
167 |
|
} |
168 |
|
|
169 |
|
for (int i = 0; i < size; i++) { |
170 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
170 |
> |
LexicographicList<BI> bis = new LexicographicList<>(new BI(i)); |
171 |
|
assertTrue(m.containsKey(bis)); |
172 |
|
} |
173 |
|
} |
178 |
|
*/ |
179 |
|
public void testMixedComparable() { |
180 |
|
int size = 1200; // makes measured test run time -> 35ms |
181 |
< |
ConcurrentHashMap<Object, Object> map = |
168 |
< |
new ConcurrentHashMap<Object, Object>(); |
181 |
> |
ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>(); |
182 |
|
Random rng = new Random(); |
183 |
|
for (int i = 0; i < size; i++) { |
184 |
|
Object x; |
290 |
|
assertEquals("A", (String)map.get(one)); |
291 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
292 |
|
assertNull(map.get("anything")); |
293 |
+ |
assertNull(empty.get("anything")); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
+ |
* Test keySet().removeAll on empty map |
336 |
+ |
*/ |
337 |
+ |
public void testKeySet_empty_removeAll() { |
338 |
+ |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); |
339 |
+ |
Set<Integer> set = map.keySet(); |
340 |
+ |
set.removeAll(Collections.emptyList()); |
341 |
+ |
assertTrue(map.isEmpty()); |
342 |
+ |
assertTrue(set.isEmpty()); |
343 |
+ |
// following is test for JDK-8163353 |
344 |
+ |
set.removeAll(Collections.emptySet()); |
345 |
+ |
assertTrue(map.isEmpty()); |
346 |
+ |
assertTrue(set.isEmpty()); |
347 |
+ |
} |
348 |
+ |
|
349 |
+ |
/** |
350 |
|
* keySet.toArray returns contains all keys |
351 |
|
*/ |
352 |
|
public void testKeySetToArray() { |
551 |
|
/** |
552 |
|
* Constructor (initialCapacity, loadFactor) throws |
553 |
|
* IllegalArgumentException if either argument is negative |
554 |
< |
*/ |
554 |
> |
*/ |
555 |
|
public void testConstructor2() { |
556 |
|
try { |
557 |
|
new ConcurrentHashMap(-1, .75f); |
558 |
|
shouldThrow(); |
559 |
< |
} catch (IllegalArgumentException e) {} |
559 |
> |
} catch (IllegalArgumentException success) {} |
560 |
|
|
561 |
|
try { |
562 |
|
new ConcurrentHashMap(16, -1); |
563 |
|
shouldThrow(); |
564 |
< |
} catch (IllegalArgumentException e) {} |
564 |
> |
} catch (IllegalArgumentException success) {} |
565 |
|
} |
566 |
|
|
567 |
|
/** |
572 |
|
try { |
573 |
|
new ConcurrentHashMap(-1, .75f, 1); |
574 |
|
shouldThrow(); |
575 |
< |
} catch (IllegalArgumentException e) {} |
575 |
> |
} catch (IllegalArgumentException success) {} |
576 |
|
|
577 |
|
try { |
578 |
|
new ConcurrentHashMap(16, -1, 1); |
579 |
|
shouldThrow(); |
580 |
< |
} catch (IllegalArgumentException e) {} |
580 |
> |
} catch (IllegalArgumentException success) {} |
581 |
|
|
582 |
|
try { |
583 |
|
new ConcurrentHashMap(16, .75f, -1); |
584 |
|
shouldThrow(); |
585 |
< |
} catch (IllegalArgumentException e) {} |
585 |
> |
} catch (IllegalArgumentException success) {} |
586 |
|
} |
587 |
|
|
588 |
|
/** |
593 |
|
try { |
594 |
|
new ConcurrentHashMap(null); |
595 |
|
shouldThrow(); |
596 |
< |
} catch (NullPointerException e) {} |
596 |
> |
} catch (NullPointerException success) {} |
597 |
|
} |
598 |
|
|
599 |
|
/** |
608 |
|
assertFalse(map2.equals(map1)); |
609 |
|
} |
610 |
|
|
582 |
– |
|
611 |
|
/** |
612 |
|
* get(null) throws NPE |
613 |
|
*/ |
614 |
|
public void testGet_NullPointerException() { |
615 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
616 |
|
try { |
588 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
617 |
|
c.get(null); |
618 |
|
shouldThrow(); |
619 |
|
} catch (NullPointerException success) {} |
623 |
|
* containsKey(null) throws NPE |
624 |
|
*/ |
625 |
|
public void testContainsKey_NullPointerException() { |
626 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
627 |
|
try { |
599 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
628 |
|
c.containsKey(null); |
629 |
|
shouldThrow(); |
630 |
|
} catch (NullPointerException success) {} |
634 |
|
* containsValue(null) throws NPE |
635 |
|
*/ |
636 |
|
public void testContainsValue_NullPointerException() { |
637 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
638 |
|
try { |
610 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
639 |
|
c.containsValue(null); |
640 |
|
shouldThrow(); |
641 |
|
} catch (NullPointerException success) {} |
645 |
|
* contains(null) throws NPE |
646 |
|
*/ |
647 |
|
public void testContains_NullPointerException() { |
648 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
649 |
|
try { |
621 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
650 |
|
c.contains(null); |
651 |
|
shouldThrow(); |
652 |
|
} catch (NullPointerException success) {} |
656 |
|
* put(null,x) throws NPE |
657 |
|
*/ |
658 |
|
public void testPut1_NullPointerException() { |
659 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
660 |
|
try { |
632 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
661 |
|
c.put(null, "whatever"); |
662 |
|
shouldThrow(); |
663 |
|
} catch (NullPointerException success) {} |
667 |
|
* put(x, null) throws NPE |
668 |
|
*/ |
669 |
|
public void testPut2_NullPointerException() { |
670 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
671 |
|
try { |
643 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
672 |
|
c.put("whatever", null); |
673 |
|
shouldThrow(); |
674 |
|
} catch (NullPointerException success) {} |
678 |
|
* putIfAbsent(null, x) throws NPE |
679 |
|
*/ |
680 |
|
public void testPutIfAbsent1_NullPointerException() { |
681 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
682 |
|
try { |
654 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
683 |
|
c.putIfAbsent(null, "whatever"); |
684 |
|
shouldThrow(); |
685 |
|
} catch (NullPointerException success) {} |
689 |
|
* replace(null, x) throws NPE |
690 |
|
*/ |
691 |
|
public void testReplace_NullPointerException() { |
692 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
693 |
|
try { |
665 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
694 |
|
c.replace(null, "whatever"); |
695 |
|
shouldThrow(); |
696 |
|
} catch (NullPointerException success) {} |
700 |
|
* replace(null, x, y) throws NPE |
701 |
|
*/ |
702 |
|
public void testReplaceValue_NullPointerException() { |
703 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
704 |
|
try { |
676 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
705 |
|
c.replace(null, one, "whatever"); |
706 |
|
shouldThrow(); |
707 |
|
} catch (NullPointerException success) {} |
711 |
|
* putIfAbsent(x, null) throws NPE |
712 |
|
*/ |
713 |
|
public void testPutIfAbsent2_NullPointerException() { |
714 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
715 |
|
try { |
687 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
716 |
|
c.putIfAbsent("whatever", null); |
717 |
|
shouldThrow(); |
718 |
|
} catch (NullPointerException success) {} |
722 |
|
* replace(x, null) throws NPE |
723 |
|
*/ |
724 |
|
public void testReplace2_NullPointerException() { |
725 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
726 |
|
try { |
698 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
727 |
|
c.replace("whatever", null); |
728 |
|
shouldThrow(); |
729 |
|
} catch (NullPointerException success) {} |
733 |
|
* replace(x, null, y) throws NPE |
734 |
|
*/ |
735 |
|
public void testReplaceValue2_NullPointerException() { |
736 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
737 |
|
try { |
709 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
738 |
|
c.replace("whatever", null, "A"); |
739 |
|
shouldThrow(); |
740 |
|
} catch (NullPointerException success) {} |
744 |
|
* replace(x, y, null) throws NPE |
745 |
|
*/ |
746 |
|
public void testReplaceValue3_NullPointerException() { |
747 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
748 |
|
try { |
720 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
749 |
|
c.replace("whatever", one, null); |
750 |
|
shouldThrow(); |
751 |
|
} catch (NullPointerException success) {} |
755 |
|
* remove(null) throws NPE |
756 |
|
*/ |
757 |
|
public void testRemove1_NullPointerException() { |
758 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
759 |
+ |
c.put("sadsdf", "asdads"); |
760 |
|
try { |
731 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
732 |
– |
c.put("sadsdf", "asdads"); |
761 |
|
c.remove(null); |
762 |
|
shouldThrow(); |
763 |
|
} catch (NullPointerException success) {} |
767 |
|
* remove(null, x) throws NPE |
768 |
|
*/ |
769 |
|
public void testRemove2_NullPointerException() { |
770 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
771 |
+ |
c.put("sadsdf", "asdads"); |
772 |
|
try { |
743 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
744 |
– |
c.put("sadsdf", "asdads"); |
773 |
|
c.remove(null, "whatever"); |
774 |
|
shouldThrow(); |
775 |
|
} catch (NullPointerException success) {} |
785 |
|
} |
786 |
|
|
787 |
|
/** |
788 |
< |
* A deserialized map equals original |
788 |
> |
* A deserialized/reserialized map equals original |
789 |
|
*/ |
790 |
|
public void testSerialization() throws Exception { |
791 |
|
Map x = map5(); |
819 |
|
} |
820 |
|
} |
821 |
|
|
822 |
+ |
/** |
823 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
824 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
825 |
+ |
*/ |
826 |
+ |
public void testRemoveAll_performance() { |
827 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
828 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
829 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
830 |
+ |
for (int i = 0; i < mapSize; i++) |
831 |
+ |
map.put(i, i); |
832 |
+ |
Set<Integer> keySet = map.keySet(); |
833 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
834 |
+ |
for (int i = 0; i < iterations; i++) |
835 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
836 |
+ |
assertEquals(mapSize, map.size()); |
837 |
+ |
} |
838 |
+ |
|
839 |
+ |
public void testReentrantComputeIfAbsent() { |
840 |
+ |
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(16); |
841 |
+ |
try { |
842 |
+ |
for (int i = 0; i < 100; i++) { // force a resize |
843 |
+ |
map.computeIfAbsent(i, key -> findValue(map, key)); |
844 |
+ |
} |
845 |
+ |
fail("recursive computeIfAbsent should throw IllegalStateException"); |
846 |
+ |
} catch (IllegalStateException success) {} |
847 |
+ |
} |
848 |
+ |
|
849 |
+ |
private Integer findValue(ConcurrentHashMap<Integer, Integer> map, |
850 |
+ |
Integer key) { |
851 |
+ |
return (key % 5 == 0) ? key : |
852 |
+ |
map.computeIfAbsent(key + 1, k -> findValue(map, k)); |
853 |
+ |
} |
854 |
+ |
|
855 |
|
} |