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<Integer, String> map5() { |
47 |
< |
ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5); |
47 |
> |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5); |
48 |
|
assertTrue(map.isEmpty()); |
49 |
|
map.put(one, "A"); |
50 |
|
map.put(two, "B"); |
57 |
|
} |
58 |
|
|
59 |
|
/** Re-implement Integer.compare for old java versions */ |
60 |
< |
static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } |
60 |
> |
static int compare(int x, int y) { |
61 |
> |
return (x < y) ? -1 : (x > y) ? 1 : 0; |
62 |
> |
} |
63 |
|
|
64 |
|
// classes for testing Comparable fallbacks |
65 |
|
static class BI implements Comparable<BI> { |
127 |
|
* class are found. |
128 |
|
*/ |
129 |
|
public void testComparableFamily() { |
130 |
< |
ConcurrentHashMap<BI, Boolean> m = |
131 |
< |
new ConcurrentHashMap<BI, Boolean>(); |
132 |
< |
for (int i = 0; i < 1000; i++) { |
133 |
< |
assertTrue(m.put(new CI(i), true) == null); |
130 |
> |
int size = 500; // makes measured test run time -> 60ms |
131 |
> |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
132 |
> |
for (int i = 0; i < size; i++) { |
133 |
> |
assertNull(m.put(new CI(i), true)); |
134 |
|
} |
135 |
< |
for (int i = 0; i < 1000; i++) { |
135 |
> |
for (int i = 0; i < size; i++) { |
136 |
|
assertTrue(m.containsKey(new CI(i))); |
137 |
|
assertTrue(m.containsKey(new DI(i))); |
138 |
|
} |
143 |
|
* on Comparable can be inserted and found. |
144 |
|
*/ |
145 |
|
public void testGenericComparable() { |
146 |
< |
ConcurrentHashMap<Object, Boolean> m = |
147 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
148 |
< |
for (int i = 0; i < 1000; i++) { |
146 |
> |
int size = 120; // makes measured test run time -> 60ms |
147 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
148 |
> |
for (int i = 0; i < size; i++) { |
149 |
|
BI bi = new BI(i); |
150 |
|
BS bs = new BS(String.valueOf(i)); |
151 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
152 |
< |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
153 |
< |
assertTrue(m.putIfAbsent(bis, true) == null); |
151 |
> |
LexicographicList<BI> bis = new LexicographicList<>(bi); |
152 |
> |
LexicographicList<BS> bss = new LexicographicList<>(bs); |
153 |
> |
assertNull(m.putIfAbsent(bis, true)); |
154 |
|
assertTrue(m.containsKey(bis)); |
155 |
|
if (m.putIfAbsent(bss, true) == null) |
156 |
|
assertTrue(m.containsKey(bss)); |
157 |
|
assertTrue(m.containsKey(bis)); |
158 |
|
} |
159 |
< |
for (int i = 0; i < 1000; i++) { |
160 |
< |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
159 |
> |
for (int i = 0; i < size; i++) { |
160 |
> |
assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); |
161 |
|
} |
162 |
|
} |
163 |
|
|
167 |
|
* inserted and found. |
168 |
|
*/ |
169 |
|
public void testGenericComparable2() { |
170 |
< |
ConcurrentHashMap<Object, Boolean> m = |
171 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
172 |
< |
for (int i = 0; i < 1000; i++) { |
173 |
< |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
170 |
> |
int size = 500; // makes measured test run time -> 60ms |
171 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
172 |
> |
for (int i = 0; i < size; i++) { |
173 |
> |
m.put(Collections.singletonList(new BI(i)), true); |
174 |
|
} |
175 |
|
|
176 |
< |
for (int i = 0; i < 1000; i++) { |
177 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
176 |
> |
for (int i = 0; i < size; i++) { |
177 |
> |
LexicographicList<BI> bis = new LexicographicList<>(new BI(i)); |
178 |
|
assertTrue(m.containsKey(bis)); |
179 |
|
} |
180 |
|
} |
184 |
|
* can be inserted and found. |
185 |
|
*/ |
186 |
|
public void testMixedComparable() { |
187 |
< |
int size = 10000; |
188 |
< |
ConcurrentHashMap<Object, Object> map = |
189 |
< |
new ConcurrentHashMap<Object, Object>(); |
166 |
< |
Random rng = new Random(1370014958369218000L); |
187 |
> |
int size = 1200; // makes measured test run time -> 35ms |
188 |
> |
ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>(); |
189 |
> |
Random rng = new Random(); |
190 |
|
for (int i = 0; i < size; i++) { |
191 |
|
Object x; |
192 |
|
switch (rng.nextInt(4)) { |
297 |
|
assertEquals("A", (String)map.get(one)); |
298 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
299 |
|
assertNull(map.get("anything")); |
300 |
+ |
assertNull(empty.get("anything")); |
301 |
|
} |
302 |
|
|
303 |
|
/** |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
+ |
* 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 |
|
* keySet.toArray returns contains all keys |
358 |
|
*/ |
359 |
|
public void testKeySetToArray() { |
558 |
|
/** |
559 |
|
* Constructor (initialCapacity, loadFactor) throws |
560 |
|
* IllegalArgumentException if either argument is negative |
561 |
< |
*/ |
561 |
> |
*/ |
562 |
|
public void testConstructor2() { |
563 |
|
try { |
564 |
|
new ConcurrentHashMap(-1, .75f); |
565 |
|
shouldThrow(); |
566 |
< |
} catch (IllegalArgumentException e) {} |
566 |
> |
} catch (IllegalArgumentException success) {} |
567 |
|
|
568 |
|
try { |
569 |
|
new ConcurrentHashMap(16, -1); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (IllegalArgumentException e) {} |
571 |
> |
} catch (IllegalArgumentException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
579 |
|
try { |
580 |
|
new ConcurrentHashMap(-1, .75f, 1); |
581 |
|
shouldThrow(); |
582 |
< |
} catch (IllegalArgumentException e) {} |
582 |
> |
} catch (IllegalArgumentException success) {} |
583 |
|
|
584 |
|
try { |
585 |
|
new ConcurrentHashMap(16, -1, 1); |
586 |
|
shouldThrow(); |
587 |
< |
} catch (IllegalArgumentException e) {} |
587 |
> |
} catch (IllegalArgumentException success) {} |
588 |
|
|
589 |
|
try { |
590 |
|
new ConcurrentHashMap(16, .75f, -1); |
591 |
|
shouldThrow(); |
592 |
< |
} catch (IllegalArgumentException e) {} |
592 |
> |
} catch (IllegalArgumentException success) {} |
593 |
|
} |
594 |
|
|
595 |
|
/** |
600 |
|
try { |
601 |
|
new ConcurrentHashMap(null); |
602 |
|
shouldThrow(); |
603 |
< |
} catch (NullPointerException e) {} |
603 |
> |
} catch (NullPointerException success) {} |
604 |
|
} |
605 |
|
|
606 |
|
/** |
615 |
|
assertFalse(map2.equals(map1)); |
616 |
|
} |
617 |
|
|
579 |
– |
|
618 |
|
/** |
619 |
|
* get(null) throws NPE |
620 |
|
*/ |
621 |
|
public void testGet_NullPointerException() { |
622 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
623 |
|
try { |
585 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
624 |
|
c.get(null); |
625 |
|
shouldThrow(); |
626 |
|
} catch (NullPointerException success) {} |
630 |
|
* containsKey(null) throws NPE |
631 |
|
*/ |
632 |
|
public void testContainsKey_NullPointerException() { |
633 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
634 |
|
try { |
596 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
635 |
|
c.containsKey(null); |
636 |
|
shouldThrow(); |
637 |
|
} catch (NullPointerException success) {} |
641 |
|
* containsValue(null) throws NPE |
642 |
|
*/ |
643 |
|
public void testContainsValue_NullPointerException() { |
644 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
645 |
|
try { |
607 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
646 |
|
c.containsValue(null); |
647 |
|
shouldThrow(); |
648 |
|
} catch (NullPointerException success) {} |
652 |
|
* contains(null) throws NPE |
653 |
|
*/ |
654 |
|
public void testContains_NullPointerException() { |
655 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
656 |
|
try { |
618 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
657 |
|
c.contains(null); |
658 |
|
shouldThrow(); |
659 |
|
} catch (NullPointerException success) {} |
663 |
|
* put(null,x) throws NPE |
664 |
|
*/ |
665 |
|
public void testPut1_NullPointerException() { |
666 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
667 |
|
try { |
629 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
668 |
|
c.put(null, "whatever"); |
669 |
|
shouldThrow(); |
670 |
|
} catch (NullPointerException success) {} |
674 |
|
* put(x, null) throws NPE |
675 |
|
*/ |
676 |
|
public void testPut2_NullPointerException() { |
677 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
678 |
|
try { |
640 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
679 |
|
c.put("whatever", null); |
680 |
|
shouldThrow(); |
681 |
|
} catch (NullPointerException success) {} |
685 |
|
* putIfAbsent(null, x) throws NPE |
686 |
|
*/ |
687 |
|
public void testPutIfAbsent1_NullPointerException() { |
688 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
689 |
|
try { |
651 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
690 |
|
c.putIfAbsent(null, "whatever"); |
691 |
|
shouldThrow(); |
692 |
|
} catch (NullPointerException success) {} |
696 |
|
* replace(null, x) throws NPE |
697 |
|
*/ |
698 |
|
public void testReplace_NullPointerException() { |
699 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
700 |
|
try { |
662 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
701 |
|
c.replace(null, "whatever"); |
702 |
|
shouldThrow(); |
703 |
|
} catch (NullPointerException success) {} |
707 |
|
* replace(null, x, y) throws NPE |
708 |
|
*/ |
709 |
|
public void testReplaceValue_NullPointerException() { |
710 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
711 |
|
try { |
673 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
712 |
|
c.replace(null, one, "whatever"); |
713 |
|
shouldThrow(); |
714 |
|
} catch (NullPointerException success) {} |
718 |
|
* putIfAbsent(x, null) throws NPE |
719 |
|
*/ |
720 |
|
public void testPutIfAbsent2_NullPointerException() { |
721 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
722 |
|
try { |
684 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
723 |
|
c.putIfAbsent("whatever", null); |
724 |
|
shouldThrow(); |
725 |
|
} catch (NullPointerException success) {} |
729 |
|
* replace(x, null) throws NPE |
730 |
|
*/ |
731 |
|
public void testReplace2_NullPointerException() { |
732 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
733 |
|
try { |
695 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
734 |
|
c.replace("whatever", null); |
735 |
|
shouldThrow(); |
736 |
|
} catch (NullPointerException success) {} |
740 |
|
* replace(x, null, y) throws NPE |
741 |
|
*/ |
742 |
|
public void testReplaceValue2_NullPointerException() { |
743 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
744 |
|
try { |
706 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
745 |
|
c.replace("whatever", null, "A"); |
746 |
|
shouldThrow(); |
747 |
|
} catch (NullPointerException success) {} |
751 |
|
* replace(x, y, null) throws NPE |
752 |
|
*/ |
753 |
|
public void testReplaceValue3_NullPointerException() { |
754 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
755 |
|
try { |
717 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
756 |
|
c.replace("whatever", one, null); |
757 |
|
shouldThrow(); |
758 |
|
} catch (NullPointerException success) {} |
762 |
|
* remove(null) throws NPE |
763 |
|
*/ |
764 |
|
public void testRemove1_NullPointerException() { |
765 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
766 |
+ |
c.put("sadsdf", "asdads"); |
767 |
|
try { |
728 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
729 |
– |
c.put("sadsdf", "asdads"); |
768 |
|
c.remove(null); |
769 |
|
shouldThrow(); |
770 |
|
} catch (NullPointerException success) {} |
774 |
|
* remove(null, x) throws NPE |
775 |
|
*/ |
776 |
|
public void testRemove2_NullPointerException() { |
777 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
778 |
+ |
c.put("sadsdf", "asdads"); |
779 |
|
try { |
740 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
741 |
– |
c.put("sadsdf", "asdads"); |
780 |
|
c.remove(null, "whatever"); |
781 |
|
shouldThrow(); |
782 |
|
} catch (NullPointerException success) {} |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* A deserialized map equals original |
795 |
> |
* A deserialized/reserialized map equals original |
796 |
|
*/ |
797 |
|
public void testSerialization() throws Exception { |
798 |
|
Map x = map5(); |
826 |
|
} |
827 |
|
} |
828 |
|
|
829 |
+ |
/** |
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 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
835 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
836 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
837 |
+ |
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 |
+ |
|
846 |
|
} |