6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
9 |
> |
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 |
|
import java.util.concurrent.ConcurrentHashMap; |
19 |
+ |
import java.util.concurrent.ExecutorService; |
20 |
+ |
import java.util.concurrent.Executors; |
21 |
+ |
|
22 |
+ |
import junit.framework.Test; |
23 |
+ |
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
public class ConcurrentHashMapTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
34 |
|
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
35 |
|
*/ |
36 |
|
private static ConcurrentHashMap<Integer, String> map5() { |
37 |
< |
ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5); |
37 |
> |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5); |
38 |
|
assertTrue(map.isEmpty()); |
39 |
|
map.put(one, "A"); |
40 |
|
map.put(two, "B"); |
47 |
|
} |
48 |
|
|
49 |
|
/** Re-implement Integer.compare for old java versions */ |
50 |
< |
static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } |
50 |
> |
static int compare(int x, int y) { |
51 |
> |
return (x < y) ? -1 : (x > y) ? 1 : 0; |
52 |
> |
} |
53 |
|
|
54 |
|
// classes for testing Comparable fallbacks |
55 |
|
static class BI implements Comparable<BI> { |
117 |
|
* class are found. |
118 |
|
*/ |
119 |
|
public void testComparableFamily() { |
120 |
+ |
int size = 500; // makes measured test run time -> 60ms |
121 |
|
ConcurrentHashMap<BI, Boolean> m = |
122 |
|
new ConcurrentHashMap<BI, Boolean>(); |
123 |
< |
for (int i = 0; i < 1000; i++) { |
123 |
> |
for (int i = 0; i < size; i++) { |
124 |
|
assertTrue(m.put(new CI(i), true) == null); |
125 |
|
} |
126 |
< |
for (int i = 0; i < 1000; i++) { |
126 |
> |
for (int i = 0; i < size; i++) { |
127 |
|
assertTrue(m.containsKey(new CI(i))); |
128 |
|
assertTrue(m.containsKey(new DI(i))); |
129 |
|
} |
134 |
|
* on Comparable can be inserted and found. |
135 |
|
*/ |
136 |
|
public void testGenericComparable() { |
137 |
+ |
int size = 120; // makes measured test run time -> 60ms |
138 |
|
ConcurrentHashMap<Object, Boolean> m = |
139 |
|
new ConcurrentHashMap<Object, Boolean>(); |
140 |
< |
for (int i = 0; i < 1000; i++) { |
140 |
> |
for (int i = 0; i < size; i++) { |
141 |
|
BI bi = new BI(i); |
142 |
|
BS bs = new BS(String.valueOf(i)); |
143 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
148 |
|
assertTrue(m.containsKey(bss)); |
149 |
|
assertTrue(m.containsKey(bis)); |
150 |
|
} |
151 |
< |
for (int i = 0; i < 1000; i++) { |
152 |
< |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
151 |
> |
for (int i = 0; i < size; i++) { |
152 |
> |
assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); |
153 |
|
} |
154 |
|
} |
155 |
|
|
159 |
|
* inserted and found. |
160 |
|
*/ |
161 |
|
public void testGenericComparable2() { |
162 |
+ |
int size = 500; // makes measured test run time -> 60ms |
163 |
|
ConcurrentHashMap<Object, Boolean> m = |
164 |
|
new ConcurrentHashMap<Object, Boolean>(); |
165 |
< |
for (int i = 0; i < 1000; i++) { |
166 |
< |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
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 < 1000; i++) { |
169 |
> |
for (int i = 0; i < size; i++) { |
170 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
171 |
|
assertTrue(m.containsKey(bis)); |
172 |
|
} |
177 |
|
* can be inserted and found. |
178 |
|
*/ |
179 |
|
public void testMixedComparable() { |
180 |
< |
int size = 10000; |
180 |
> |
int size = 1200; // makes measured test run time -> 35ms |
181 |
|
ConcurrentHashMap<Object, Object> map = |
182 |
|
new ConcurrentHashMap<Object, Object>(); |
183 |
< |
Random rng = new Random(1370014958369218000L); |
183 |
> |
Random rng = new Random(); |
184 |
|
for (int i = 0; i < size; i++) { |
185 |
|
Object x; |
186 |
|
switch (rng.nextInt(4)) { |
229 |
|
assertFalse(map2.equals(map1)); |
230 |
|
} |
231 |
|
|
215 |
– |
|
232 |
|
/** |
233 |
< |
* hashCode() equals sum of each key.hashCode ^ value.hashCode |
234 |
< |
*/ |
233 |
> |
* hashCode() equals sum of each key.hashCode ^ value.hashCode |
234 |
> |
*/ |
235 |
|
public void testHashCode() { |
236 |
|
ConcurrentHashMap<Integer,String> map = map5(); |
237 |
|
int sum = 0; |
291 |
|
assertEquals("A", (String)map.get(one)); |
292 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
293 |
|
assertNull(map.get("anything")); |
294 |
+ |
assertNull(empty.get("anything")); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
333 |
|
} |
334 |
|
|
335 |
|
/** |
336 |
+ |
* Test keySet().removeAll on empty map |
337 |
+ |
*/ |
338 |
+ |
public void testKeySet_empty_removeAll() { |
339 |
+ |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); |
340 |
+ |
Set<Integer> set = map.keySet(); |
341 |
+ |
set.removeAll(Collections.emptyList()); |
342 |
+ |
assertTrue(map.isEmpty()); |
343 |
+ |
assertTrue(set.isEmpty()); |
344 |
+ |
// following is test for JDK-8163353 |
345 |
+ |
set.removeAll(Collections.emptySet()); |
346 |
+ |
assertTrue(map.isEmpty()); |
347 |
+ |
assertTrue(set.isEmpty()); |
348 |
+ |
} |
349 |
+ |
|
350 |
+ |
/** |
351 |
|
* keySet.toArray returns contains all keys |
352 |
|
*/ |
353 |
|
public void testKeySetToArray() { |
552 |
|
/** |
553 |
|
* Constructor (initialCapacity, loadFactor) throws |
554 |
|
* IllegalArgumentException if either argument is negative |
555 |
< |
*/ |
555 |
> |
*/ |
556 |
|
public void testConstructor2() { |
557 |
|
try { |
558 |
|
new ConcurrentHashMap(-1, .75f); |
559 |
|
shouldThrow(); |
560 |
< |
} catch (IllegalArgumentException e) {} |
561 |
< |
|
560 |
> |
} catch (IllegalArgumentException success) {} |
561 |
> |
|
562 |
|
try { |
563 |
|
new ConcurrentHashMap(16, -1); |
564 |
|
shouldThrow(); |
565 |
< |
} catch (IllegalArgumentException e) {} |
565 |
> |
} catch (IllegalArgumentException success) {} |
566 |
|
} |
567 |
< |
|
568 |
< |
/** |
569 |
< |
* Constructor (initialCapacity, loadFactor, concurrencyLevel) |
570 |
< |
* throws IllegalArgumentException if any argument is negative |
571 |
< |
*/ |
567 |
> |
|
568 |
> |
/** |
569 |
> |
* Constructor (initialCapacity, loadFactor, concurrencyLevel) |
570 |
> |
* throws IllegalArgumentException if any argument is negative |
571 |
> |
*/ |
572 |
|
public void testConstructor3() { |
573 |
< |
try { |
574 |
< |
new ConcurrentHashMap(-1, .75f, 1); |
575 |
< |
shouldThrow(); |
576 |
< |
} catch (IllegalArgumentException e) {} |
577 |
< |
|
578 |
< |
try { |
579 |
< |
new ConcurrentHashMap(16, -1, 1); |
580 |
< |
shouldThrow(); |
581 |
< |
} catch (IllegalArgumentException e) {} |
582 |
< |
|
583 |
< |
try { |
584 |
< |
new ConcurrentHashMap(16, .75f, -1); |
585 |
< |
shouldThrow(); |
586 |
< |
} catch (IllegalArgumentException e) {} |
587 |
< |
} |
573 |
> |
try { |
574 |
> |
new ConcurrentHashMap(-1, .75f, 1); |
575 |
> |
shouldThrow(); |
576 |
> |
} catch (IllegalArgumentException success) {} |
577 |
> |
|
578 |
> |
try { |
579 |
> |
new ConcurrentHashMap(16, -1, 1); |
580 |
> |
shouldThrow(); |
581 |
> |
} catch (IllegalArgumentException success) {} |
582 |
> |
|
583 |
> |
try { |
584 |
> |
new ConcurrentHashMap(16, .75f, -1); |
585 |
> |
shouldThrow(); |
586 |
> |
} catch (IllegalArgumentException success) {} |
587 |
> |
} |
588 |
|
|
589 |
|
/** |
590 |
|
* ConcurrentHashMap(map) throws NullPointerException if the given |
594 |
|
try { |
595 |
|
new ConcurrentHashMap(null); |
596 |
|
shouldThrow(); |
597 |
< |
} catch (NullPointerException e) {} |
597 |
> |
} catch (NullPointerException success) {} |
598 |
|
} |
599 |
|
|
600 |
|
/** |
609 |
|
assertFalse(map2.equals(map1)); |
610 |
|
} |
611 |
|
|
580 |
– |
|
612 |
|
/** |
613 |
|
* get(null) throws NPE |
614 |
|
*/ |
615 |
|
public void testGet_NullPointerException() { |
616 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
617 |
|
try { |
586 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
618 |
|
c.get(null); |
619 |
|
shouldThrow(); |
620 |
|
} catch (NullPointerException success) {} |
624 |
|
* containsKey(null) throws NPE |
625 |
|
*/ |
626 |
|
public void testContainsKey_NullPointerException() { |
627 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
628 |
|
try { |
597 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
629 |
|
c.containsKey(null); |
630 |
|
shouldThrow(); |
631 |
|
} catch (NullPointerException success) {} |
635 |
|
* containsValue(null) throws NPE |
636 |
|
*/ |
637 |
|
public void testContainsValue_NullPointerException() { |
638 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
639 |
|
try { |
608 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
640 |
|
c.containsValue(null); |
641 |
|
shouldThrow(); |
642 |
|
} catch (NullPointerException success) {} |
646 |
|
* contains(null) throws NPE |
647 |
|
*/ |
648 |
|
public void testContains_NullPointerException() { |
649 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
650 |
|
try { |
619 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
651 |
|
c.contains(null); |
652 |
|
shouldThrow(); |
653 |
|
} catch (NullPointerException success) {} |
657 |
|
* put(null,x) throws NPE |
658 |
|
*/ |
659 |
|
public void testPut1_NullPointerException() { |
660 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
661 |
|
try { |
630 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
662 |
|
c.put(null, "whatever"); |
663 |
|
shouldThrow(); |
664 |
|
} catch (NullPointerException success) {} |
668 |
|
* put(x, null) throws NPE |
669 |
|
*/ |
670 |
|
public void testPut2_NullPointerException() { |
671 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
672 |
|
try { |
641 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
673 |
|
c.put("whatever", null); |
674 |
|
shouldThrow(); |
675 |
|
} catch (NullPointerException success) {} |
679 |
|
* putIfAbsent(null, x) throws NPE |
680 |
|
*/ |
681 |
|
public void testPutIfAbsent1_NullPointerException() { |
682 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
683 |
|
try { |
652 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
684 |
|
c.putIfAbsent(null, "whatever"); |
685 |
|
shouldThrow(); |
686 |
|
} catch (NullPointerException success) {} |
690 |
|
* replace(null, x) throws NPE |
691 |
|
*/ |
692 |
|
public void testReplace_NullPointerException() { |
693 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
694 |
|
try { |
663 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
695 |
|
c.replace(null, "whatever"); |
696 |
|
shouldThrow(); |
697 |
|
} catch (NullPointerException success) {} |
701 |
|
* replace(null, x, y) throws NPE |
702 |
|
*/ |
703 |
|
public void testReplaceValue_NullPointerException() { |
704 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
705 |
|
try { |
674 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
706 |
|
c.replace(null, one, "whatever"); |
707 |
|
shouldThrow(); |
708 |
|
} catch (NullPointerException success) {} |
712 |
|
* putIfAbsent(x, null) throws NPE |
713 |
|
*/ |
714 |
|
public void testPutIfAbsent2_NullPointerException() { |
715 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
716 |
|
try { |
685 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
717 |
|
c.putIfAbsent("whatever", null); |
718 |
|
shouldThrow(); |
719 |
|
} catch (NullPointerException success) {} |
723 |
|
* replace(x, null) throws NPE |
724 |
|
*/ |
725 |
|
public void testReplace2_NullPointerException() { |
726 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
727 |
|
try { |
696 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
728 |
|
c.replace("whatever", null); |
729 |
|
shouldThrow(); |
730 |
|
} catch (NullPointerException success) {} |
734 |
|
* replace(x, null, y) throws NPE |
735 |
|
*/ |
736 |
|
public void testReplaceValue2_NullPointerException() { |
737 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
738 |
|
try { |
707 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
739 |
|
c.replace("whatever", null, "A"); |
740 |
|
shouldThrow(); |
741 |
|
} catch (NullPointerException success) {} |
745 |
|
* replace(x, y, null) throws NPE |
746 |
|
*/ |
747 |
|
public void testReplaceValue3_NullPointerException() { |
748 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
749 |
|
try { |
718 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
750 |
|
c.replace("whatever", one, null); |
751 |
|
shouldThrow(); |
752 |
|
} catch (NullPointerException success) {} |
756 |
|
* remove(null) throws NPE |
757 |
|
*/ |
758 |
|
public void testRemove1_NullPointerException() { |
759 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
760 |
+ |
c.put("sadsdf", "asdads"); |
761 |
|
try { |
729 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
730 |
– |
c.put("sadsdf", "asdads"); |
762 |
|
c.remove(null); |
763 |
|
shouldThrow(); |
764 |
|
} catch (NullPointerException success) {} |
768 |
|
* remove(null, x) throws NPE |
769 |
|
*/ |
770 |
|
public void testRemove2_NullPointerException() { |
771 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
772 |
+ |
c.put("sadsdf", "asdads"); |
773 |
|
try { |
741 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
742 |
– |
c.put("sadsdf", "asdads"); |
774 |
|
c.remove(null, "whatever"); |
775 |
|
shouldThrow(); |
776 |
|
} catch (NullPointerException success) {} |
820 |
|
} |
821 |
|
} |
822 |
|
|
823 |
+ |
/** |
824 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
825 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
826 |
+ |
*/ |
827 |
+ |
public void testRemoveAll_performance() { |
828 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
829 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
830 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
831 |
+ |
for (int i = 0; i < mapSize; i++) |
832 |
+ |
map.put(i, i); |
833 |
+ |
Set<Integer> keySet = map.keySet(); |
834 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
835 |
+ |
for (int i = 0; i < iterations; i++) |
836 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
837 |
+ |
assertEquals(mapSize, map.size()); |
838 |
+ |
} |
839 |
+ |
|
840 |
|
} |