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. |
19 |
|
import java.util.concurrent.ConcurrentHashMap; |
20 |
|
|
21 |
|
import junit.framework.Test; |
21 |
– |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class ConcurrentHashMapTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
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> { |
131 |
|
ConcurrentHashMap<BI, Boolean> m = |
132 |
|
new ConcurrentHashMap<BI, Boolean>(); |
133 |
|
for (int i = 0; i < size; i++) { |
134 |
< |
assertTrue(m.put(new CI(i), true) == null); |
134 |
> |
assertNull(m.put(new CI(i), true)); |
135 |
|
} |
136 |
|
for (int i = 0; i < size; i++) { |
137 |
|
assertTrue(m.containsKey(new CI(i))); |
152 |
|
BS bs = new BS(String.valueOf(i)); |
153 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
154 |
|
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
155 |
< |
assertTrue(m.putIfAbsent(bis, true) == null); |
155 |
> |
assertNull(m.putIfAbsent(bis, true)); |
156 |
|
assertTrue(m.containsKey(bis)); |
157 |
|
if (m.putIfAbsent(bss, true) == null) |
158 |
|
assertTrue(m.containsKey(bss)); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
+ |
* Test keySet().removeAll on empty map |
347 |
+ |
*/ |
348 |
+ |
public void testKeySet_empty_removeAll() { |
349 |
+ |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); |
350 |
+ |
Set<Integer> set = map.keySet(); |
351 |
+ |
set.removeAll(Collections.emptyList()); |
352 |
+ |
assertTrue(map.isEmpty()); |
353 |
+ |
assertTrue(set.isEmpty()); |
354 |
+ |
// following is test for JDK-8163353 |
355 |
+ |
set.removeAll(Collections.emptySet()); |
356 |
+ |
assertTrue(map.isEmpty()); |
357 |
+ |
assertTrue(set.isEmpty()); |
358 |
+ |
} |
359 |
+ |
|
360 |
+ |
/** |
361 |
|
* keySet.toArray returns contains all keys |
362 |
|
*/ |
363 |
|
public void testKeySetToArray() { |
562 |
|
/** |
563 |
|
* Constructor (initialCapacity, loadFactor) throws |
564 |
|
* IllegalArgumentException if either argument is negative |
565 |
< |
*/ |
565 |
> |
*/ |
566 |
|
public void testConstructor2() { |
567 |
|
try { |
568 |
|
new ConcurrentHashMap(-1, .75f); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
< |
* A deserialized map equals original |
799 |
> |
* A deserialized/reserialized map equals original |
800 |
|
*/ |
801 |
|
public void testSerialization() throws Exception { |
802 |
|
Map x = map5(); |
830 |
|
} |
831 |
|
} |
832 |
|
|
833 |
+ |
/** |
834 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
835 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
836 |
+ |
*/ |
837 |
+ |
public void testRemoveAll_performance() { |
838 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
839 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
840 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
841 |
+ |
for (int i = 0; i < mapSize; i++) |
842 |
+ |
map.put(i, i); |
843 |
+ |
Set<Integer> keySet = map.keySet(); |
844 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
845 |
+ |
for (int i = 0; i < iterations; i++) |
846 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
847 |
+ |
assertEquals(mapSize, map.size()); |
848 |
+ |
} |
849 |
+ |
|
850 |
|
} |