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; |
19 |
|
|
20 |
|
import junit.framework.Test; |
21 |
|
import junit.framework.TestSuite; |
32 |
|
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
33 |
|
*/ |
34 |
|
private static ConcurrentHashMap<Integer, String> map5() { |
35 |
< |
ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5); |
35 |
> |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5); |
36 |
|
assertTrue(map.isEmpty()); |
37 |
|
map.put(one, "A"); |
38 |
|
map.put(two, "B"); |
119 |
|
ConcurrentHashMap<BI, Boolean> m = |
120 |
|
new ConcurrentHashMap<BI, Boolean>(); |
121 |
|
for (int i = 0; i < size; i++) { |
122 |
< |
assertTrue(m.put(new CI(i), true) == null); |
122 |
> |
assertNull(m.put(new CI(i), true)); |
123 |
|
} |
124 |
|
for (int i = 0; i < size; i++) { |
125 |
|
assertTrue(m.containsKey(new CI(i))); |
140 |
|
BS bs = new BS(String.valueOf(i)); |
141 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
142 |
|
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
143 |
< |
assertTrue(m.putIfAbsent(bis, true) == null); |
143 |
> |
assertNull(m.putIfAbsent(bis, true)); |
144 |
|
assertTrue(m.containsKey(bis)); |
145 |
|
if (m.putIfAbsent(bss, true) == null) |
146 |
|
assertTrue(m.containsKey(bss)); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
+ |
* Test keySet().removeAll on empty map |
335 |
+ |
*/ |
336 |
+ |
public void testKeySet_empty_removeAll() { |
337 |
+ |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); |
338 |
+ |
Set<Integer> set = map.keySet(); |
339 |
+ |
set.removeAll(Collections.emptyList()); |
340 |
+ |
assertTrue(map.isEmpty()); |
341 |
+ |
assertTrue(set.isEmpty()); |
342 |
+ |
// following is test for JDK-8163353 |
343 |
+ |
set.removeAll(Collections.emptySet()); |
344 |
+ |
assertTrue(map.isEmpty()); |
345 |
+ |
assertTrue(set.isEmpty()); |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
/** |
349 |
|
* keySet.toArray returns contains all keys |
350 |
|
*/ |
351 |
|
public void testKeySetToArray() { |
835 |
|
assertEquals(mapSize, map.size()); |
836 |
|
} |
837 |
|
|
825 |
– |
/** |
826 |
– |
* Tests performance of computeIfAbsent when the element is present. |
827 |
– |
* See JDK-8161372 |
828 |
– |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck |
829 |
– |
*/ |
830 |
– |
public void testcomputeIfAbsent_performance() { |
831 |
– |
final int mapSize = 20; |
832 |
– |
final int iterations = expensiveTests ? (1 << 23) : mapSize * 2; |
833 |
– |
final int threads = expensiveTests ? 10 : 2; |
834 |
– |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
835 |
– |
for (int i = 0; i < mapSize; i++) |
836 |
– |
map.put(i, i); |
837 |
– |
final ExecutorService pool = Executors.newFixedThreadPool(2); |
838 |
– |
try (PoolCleaner cleaner = cleaner(pool)) { |
839 |
– |
Runnable r = new CheckedRunnable() { |
840 |
– |
public void realRun() { |
841 |
– |
int result = 0; |
842 |
– |
for (int i = 0; i < iterations; i++) |
843 |
– |
result += map.computeIfAbsent(i % mapSize, (k) -> k + k); |
844 |
– |
if (result == -42) throw new Error(); |
845 |
– |
}}; |
846 |
– |
for (int i = 0; i < threads; i++) |
847 |
– |
pool.execute(r); |
848 |
– |
} |
849 |
– |
} |
850 |
– |
|
838 |
|
} |