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 |
|
|
20 |
+ |
import junit.framework.Test; |
21 |
+ |
import junit.framework.TestSuite; |
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); |
45 |
|
} |
46 |
|
|
47 |
|
/** Re-implement Integer.compare for old java versions */ |
48 |
< |
static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } |
48 |
> |
static int compare(int x, int y) { |
49 |
> |
return (x < y) ? -1 : (x > y) ? 1 : 0; |
50 |
> |
} |
51 |
|
|
52 |
|
// classes for testing Comparable fallbacks |
53 |
|
static class BI implements Comparable<BI> { |
115 |
|
* class are found. |
116 |
|
*/ |
117 |
|
public void testComparableFamily() { |
118 |
+ |
int size = 500; // makes measured test run time -> 60ms |
119 |
|
ConcurrentHashMap<BI, Boolean> m = |
120 |
|
new ConcurrentHashMap<BI, Boolean>(); |
121 |
< |
for (int i = 0; i < 1000; i++) { |
121 |
> |
for (int i = 0; i < size; i++) { |
122 |
|
assertTrue(m.put(new CI(i), true) == null); |
123 |
|
} |
124 |
< |
for (int i = 0; i < 1000; i++) { |
124 |
> |
for (int i = 0; i < size; i++) { |
125 |
|
assertTrue(m.containsKey(new CI(i))); |
126 |
|
assertTrue(m.containsKey(new DI(i))); |
127 |
|
} |
132 |
|
* on Comparable can be inserted and found. |
133 |
|
*/ |
134 |
|
public void testGenericComparable() { |
135 |
+ |
int size = 120; // makes measured test run time -> 60ms |
136 |
|
ConcurrentHashMap<Object, Boolean> m = |
137 |
|
new ConcurrentHashMap<Object, Boolean>(); |
138 |
< |
for (int i = 0; i < 1000; i++) { |
138 |
> |
for (int i = 0; i < size; i++) { |
139 |
|
BI bi = new BI(i); |
140 |
|
BS bs = new BS(String.valueOf(i)); |
141 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
146 |
|
assertTrue(m.containsKey(bss)); |
147 |
|
assertTrue(m.containsKey(bis)); |
148 |
|
} |
149 |
< |
for (int i = 0; i < 1000; i++) { |
150 |
< |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
149 |
> |
for (int i = 0; i < size; i++) { |
150 |
> |
assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); |
151 |
|
} |
152 |
|
} |
153 |
|
|
157 |
|
* inserted and found. |
158 |
|
*/ |
159 |
|
public void testGenericComparable2() { |
160 |
+ |
int size = 500; // makes measured test run time -> 60ms |
161 |
|
ConcurrentHashMap<Object, Boolean> m = |
162 |
|
new ConcurrentHashMap<Object, Boolean>(); |
163 |
< |
for (int i = 0; i < 1000; i++) { |
164 |
< |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
163 |
> |
for (int i = 0; i < size; i++) { |
164 |
> |
m.put(Collections.singletonList(new BI(i)), true); |
165 |
|
} |
166 |
|
|
167 |
< |
for (int i = 0; i < 1000; i++) { |
167 |
> |
for (int i = 0; i < size; i++) { |
168 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
169 |
|
assertTrue(m.containsKey(bis)); |
170 |
|
} |
175 |
|
* can be inserted and found. |
176 |
|
*/ |
177 |
|
public void testMixedComparable() { |
178 |
< |
int size = 10000; |
178 |
> |
int size = 1200; // makes measured test run time -> 35ms |
179 |
|
ConcurrentHashMap<Object, Object> map = |
180 |
|
new ConcurrentHashMap<Object, Object>(); |
181 |
< |
Random rng = new Random(1370014958369218000L); |
181 |
> |
Random rng = new Random(); |
182 |
|
for (int i = 0; i < size; i++) { |
183 |
|
Object x; |
184 |
|
switch (rng.nextInt(4)) { |
289 |
|
assertEquals("A", (String)map.get(one)); |
290 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
291 |
|
assertNull(map.get("anything")); |
292 |
+ |
assertNull(empty.get("anything")); |
293 |
|
} |
294 |
|
|
295 |
|
/** |
535 |
|
/** |
536 |
|
* Constructor (initialCapacity, loadFactor) throws |
537 |
|
* IllegalArgumentException if either argument is negative |
538 |
< |
*/ |
538 |
> |
*/ |
539 |
|
public void testConstructor2() { |
540 |
|
try { |
541 |
|
new ConcurrentHashMap(-1, .75f); |
542 |
|
shouldThrow(); |
543 |
< |
} catch (IllegalArgumentException e) {} |
543 |
> |
} catch (IllegalArgumentException success) {} |
544 |
|
|
545 |
|
try { |
546 |
|
new ConcurrentHashMap(16, -1); |
547 |
|
shouldThrow(); |
548 |
< |
} catch (IllegalArgumentException e) {} |
548 |
> |
} catch (IllegalArgumentException success) {} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
556 |
|
try { |
557 |
|
new ConcurrentHashMap(-1, .75f, 1); |
558 |
|
shouldThrow(); |
559 |
< |
} catch (IllegalArgumentException e) {} |
559 |
> |
} catch (IllegalArgumentException success) {} |
560 |
|
|
561 |
|
try { |
562 |
|
new ConcurrentHashMap(16, -1, 1); |
563 |
|
shouldThrow(); |
564 |
< |
} catch (IllegalArgumentException e) {} |
564 |
> |
} catch (IllegalArgumentException success) {} |
565 |
|
|
566 |
|
try { |
567 |
|
new ConcurrentHashMap(16, .75f, -1); |
568 |
|
shouldThrow(); |
569 |
< |
} catch (IllegalArgumentException e) {} |
569 |
> |
} catch (IllegalArgumentException success) {} |
570 |
|
} |
571 |
|
|
572 |
|
/** |
577 |
|
try { |
578 |
|
new ConcurrentHashMap(null); |
579 |
|
shouldThrow(); |
580 |
< |
} catch (NullPointerException e) {} |
580 |
> |
} catch (NullPointerException success) {} |
581 |
|
} |
582 |
|
|
583 |
|
/** |
592 |
|
assertFalse(map2.equals(map1)); |
593 |
|
} |
594 |
|
|
579 |
– |
|
595 |
|
/** |
596 |
|
* get(null) throws NPE |
597 |
|
*/ |
598 |
|
public void testGet_NullPointerException() { |
599 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
600 |
|
try { |
585 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
601 |
|
c.get(null); |
602 |
|
shouldThrow(); |
603 |
|
} catch (NullPointerException success) {} |
607 |
|
* containsKey(null) throws NPE |
608 |
|
*/ |
609 |
|
public void testContainsKey_NullPointerException() { |
610 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
611 |
|
try { |
596 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
612 |
|
c.containsKey(null); |
613 |
|
shouldThrow(); |
614 |
|
} catch (NullPointerException success) {} |
618 |
|
* containsValue(null) throws NPE |
619 |
|
*/ |
620 |
|
public void testContainsValue_NullPointerException() { |
621 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
622 |
|
try { |
607 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
623 |
|
c.containsValue(null); |
624 |
|
shouldThrow(); |
625 |
|
} catch (NullPointerException success) {} |
629 |
|
* contains(null) throws NPE |
630 |
|
*/ |
631 |
|
public void testContains_NullPointerException() { |
632 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
633 |
|
try { |
618 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
634 |
|
c.contains(null); |
635 |
|
shouldThrow(); |
636 |
|
} catch (NullPointerException success) {} |
640 |
|
* put(null,x) throws NPE |
641 |
|
*/ |
642 |
|
public void testPut1_NullPointerException() { |
643 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
644 |
|
try { |
629 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
645 |
|
c.put(null, "whatever"); |
646 |
|
shouldThrow(); |
647 |
|
} catch (NullPointerException success) {} |
651 |
|
* put(x, null) throws NPE |
652 |
|
*/ |
653 |
|
public void testPut2_NullPointerException() { |
654 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
655 |
|
try { |
640 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
656 |
|
c.put("whatever", null); |
657 |
|
shouldThrow(); |
658 |
|
} catch (NullPointerException success) {} |
662 |
|
* putIfAbsent(null, x) throws NPE |
663 |
|
*/ |
664 |
|
public void testPutIfAbsent1_NullPointerException() { |
665 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
666 |
|
try { |
651 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
667 |
|
c.putIfAbsent(null, "whatever"); |
668 |
|
shouldThrow(); |
669 |
|
} catch (NullPointerException success) {} |
673 |
|
* replace(null, x) throws NPE |
674 |
|
*/ |
675 |
|
public void testReplace_NullPointerException() { |
676 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
677 |
|
try { |
662 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
678 |
|
c.replace(null, "whatever"); |
679 |
|
shouldThrow(); |
680 |
|
} catch (NullPointerException success) {} |
684 |
|
* replace(null, x, y) throws NPE |
685 |
|
*/ |
686 |
|
public void testReplaceValue_NullPointerException() { |
687 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
688 |
|
try { |
673 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
689 |
|
c.replace(null, one, "whatever"); |
690 |
|
shouldThrow(); |
691 |
|
} catch (NullPointerException success) {} |
695 |
|
* putIfAbsent(x, null) throws NPE |
696 |
|
*/ |
697 |
|
public void testPutIfAbsent2_NullPointerException() { |
698 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
699 |
|
try { |
684 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
700 |
|
c.putIfAbsent("whatever", null); |
701 |
|
shouldThrow(); |
702 |
|
} catch (NullPointerException success) {} |
706 |
|
* replace(x, null) throws NPE |
707 |
|
*/ |
708 |
|
public void testReplace2_NullPointerException() { |
709 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
710 |
|
try { |
695 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
711 |
|
c.replace("whatever", null); |
712 |
|
shouldThrow(); |
713 |
|
} catch (NullPointerException success) {} |
717 |
|
* replace(x, null, y) throws NPE |
718 |
|
*/ |
719 |
|
public void testReplaceValue2_NullPointerException() { |
720 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
721 |
|
try { |
706 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
722 |
|
c.replace("whatever", null, "A"); |
723 |
|
shouldThrow(); |
724 |
|
} catch (NullPointerException success) {} |
728 |
|
* replace(x, y, null) throws NPE |
729 |
|
*/ |
730 |
|
public void testReplaceValue3_NullPointerException() { |
731 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
732 |
|
try { |
717 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
733 |
|
c.replace("whatever", one, null); |
734 |
|
shouldThrow(); |
735 |
|
} catch (NullPointerException success) {} |
739 |
|
* remove(null) throws NPE |
740 |
|
*/ |
741 |
|
public void testRemove1_NullPointerException() { |
742 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
743 |
+ |
c.put("sadsdf", "asdads"); |
744 |
|
try { |
728 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
729 |
– |
c.put("sadsdf", "asdads"); |
745 |
|
c.remove(null); |
746 |
|
shouldThrow(); |
747 |
|
} catch (NullPointerException success) {} |
751 |
|
* remove(null, x) throws NPE |
752 |
|
*/ |
753 |
|
public void testRemove2_NullPointerException() { |
754 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
755 |
+ |
c.put("sadsdf", "asdads"); |
756 |
|
try { |
740 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
741 |
– |
c.put("sadsdf", "asdads"); |
757 |
|
c.remove(null, "whatever"); |
758 |
|
shouldThrow(); |
759 |
|
} catch (NullPointerException success) {} |
803 |
|
} |
804 |
|
} |
805 |
|
|
806 |
+ |
/** |
807 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
808 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
809 |
+ |
*/ |
810 |
+ |
public void testRemoveAll_performance() { |
811 |
+ |
int mapSize = expensiveTests ? 1_000_000 : 100; |
812 |
+ |
int iterations = expensiveTests ? 500 : 2; |
813 |
+ |
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
814 |
+ |
for (int i = 0; i < mapSize; i++) |
815 |
+ |
map.put(i, i); |
816 |
+ |
Set<Integer> keySet = map.keySet(); |
817 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
818 |
+ |
for (int i = 0; i < iterations; i++) |
819 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
820 |
+ |
assertEquals(mapSize, map.size()); |
821 |
+ |
} |
822 |
|
} |