11 |
|
import java.util.AbstractMap; |
12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
– |
import java.util.Collections; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.Map; |
16 |
|
import java.util.NoSuchElementException; |
17 |
|
import java.util.Set; |
18 |
|
import java.util.Spliterator; |
19 |
+ |
import java.util.concurrent.ExecutorService; |
20 |
+ |
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ConcurrentHashMap; |
22 |
|
import java.util.concurrent.atomic.LongAdder; |
23 |
|
import java.util.function.BiFunction; |
27 |
|
|
28 |
|
public class ConcurrentHashMap8Test extends JSR166TestCase { |
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run(suite()); |
30 |
> |
main(suite(), args); |
31 |
|
} |
32 |
|
public static Test suite() { |
33 |
|
return new TestSuite(ConcurrentHashMap8Test.class); |
63 |
|
*/ |
64 |
|
public void testComputeIfAbsent() { |
65 |
|
ConcurrentHashMap map = map5(); |
66 |
< |
map.computeIfAbsent(six, (x) -> "Z"); |
66 |
> |
map.computeIfAbsent(six, x -> "Z"); |
67 |
|
assertTrue(map.containsKey(six)); |
68 |
|
} |
69 |
|
|
72 |
|
*/ |
73 |
|
public void testComputeIfAbsent2() { |
74 |
|
ConcurrentHashMap map = map5(); |
75 |
< |
assertEquals("A", map.computeIfAbsent(one, (x) -> "Z")); |
75 |
> |
assertEquals("A", map.computeIfAbsent(one, x -> "Z")); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
80 |
|
*/ |
81 |
|
public void testComputeIfAbsent3() { |
82 |
|
ConcurrentHashMap map = map5(); |
83 |
< |
map.computeIfAbsent(six, (x) -> null); |
83 |
> |
map.computeIfAbsent(six, x -> null); |
84 |
|
assertFalse(map.containsKey(six)); |
85 |
|
} |
86 |
|
|
164 |
|
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
165 |
|
assertTrue(a.isEmpty()); |
166 |
|
for (int i = 0; i < n; i++) |
167 |
< |
a.add(i); |
167 |
> |
assertTrue(a.add(i)); |
168 |
|
assertEquals(n == 0, a.isEmpty()); |
169 |
|
assertEquals(n, a.size()); |
170 |
|
return a; |
174 |
|
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
175 |
|
assertTrue(a.isEmpty()); |
176 |
|
for (int i = 0; i < elements.length; i++) |
177 |
< |
a.add(elements[i]); |
177 |
> |
assertTrue(a.add(elements[i])); |
178 |
|
assertFalse(a.isEmpty()); |
179 |
|
assertEquals(elements.length, a.size()); |
180 |
|
return a; |
210 |
|
Set set1 = map.keySet(); |
211 |
|
Set set2 = map.keySet(true); |
212 |
|
set2.add(six); |
213 |
< |
assertTrue(((ConcurrentHashMap.KeySetView)set2).getMap() == map); |
214 |
< |
assertTrue(((ConcurrentHashMap.KeySetView)set1).getMap() == map); |
213 |
> |
assertSame(map, ((ConcurrentHashMap.KeySetView)set2).getMap()); |
214 |
> |
assertSame(map, ((ConcurrentHashMap.KeySetView)set1).getMap()); |
215 |
|
assertEquals(set2.size(), map.size()); |
216 |
|
assertEquals(set1.size(), map.size()); |
217 |
|
assertTrue((Boolean)map.get(six)); |
252 |
|
*/ |
253 |
|
public void testAdd2() { |
254 |
|
Set full = populatedSet(3); |
255 |
< |
full.add(one); |
255 |
> |
assertFalse(full.add(one)); |
256 |
|
assertEquals(3, full.size()); |
257 |
|
} |
258 |
|
|
261 |
|
*/ |
262 |
|
public void testAdd3() { |
263 |
|
Set full = populatedSet(3); |
264 |
< |
full.add(three); |
264 |
> |
assertTrue(full.add(three)); |
265 |
> |
assertTrue(full.contains(three)); |
266 |
> |
assertFalse(full.add(three)); |
267 |
|
assertTrue(full.contains(three)); |
268 |
|
} |
269 |
|
|
276 |
|
try { |
277 |
|
full.add(three); |
278 |
|
shouldThrow(); |
279 |
< |
} catch (UnsupportedOperationException e){} |
279 |
> |
} catch (UnsupportedOperationException success) {} |
280 |
|
} |
281 |
|
|
282 |
|
/** |
288 |
|
try { |
289 |
|
full.add(null); |
290 |
|
shouldThrow(); |
291 |
< |
} catch (NullPointerException e){} |
291 |
> |
} catch (NullPointerException success) {} |
292 |
|
} |
293 |
|
|
294 |
|
/** |
300 |
|
try { |
301 |
|
map.keySet(null); |
302 |
|
shouldThrow(); |
303 |
< |
} catch (NullPointerException e) {} |
303 |
> |
} catch (NullPointerException success) {} |
304 |
|
ConcurrentHashMap.KeySetView set = map.keySet(one); |
305 |
< |
set.add(one); |
306 |
< |
set.add(six); |
307 |
< |
set.add(seven); |
308 |
< |
assertTrue(set.getMappedValue() == one); |
309 |
< |
assertTrue(map.get(one) != one); |
310 |
< |
assertTrue(map.get(six) == one); |
311 |
< |
assertTrue(map.get(seven) == one); |
305 |
> |
assertFalse(set.add(one)); |
306 |
> |
assertTrue(set.add(six)); |
307 |
> |
assertTrue(set.add(seven)); |
308 |
> |
assertSame(one, set.getMappedValue()); |
309 |
> |
assertNotSame(one, map.get(one)); |
310 |
> |
assertSame(one, map.get(six)); |
311 |
> |
assertSame(one, map.get(seven)); |
312 |
|
} |
313 |
|
|
314 |
|
void checkSpliteratorCharacteristics(Spliterator<?> sp, |
407 |
|
Integer[] elements = new Integer[size]; |
408 |
|
for (int i = 0; i < size; i++) |
409 |
|
elements[i] = i; |
410 |
< |
Collections.shuffle(Arrays.asList(elements)); |
410 |
> |
shuffle(elements); |
411 |
|
Collection<Integer> full = populatedSet(elements); |
412 |
|
|
413 |
|
Iterator it = full.iterator(); |
497 |
|
Integer[] elements = new Integer[size]; |
498 |
|
for (int i = 0; i < size; i++) |
499 |
|
elements[i] = i; |
500 |
< |
Collections.shuffle(Arrays.asList(elements)); |
500 |
> |
shuffle(elements); |
501 |
|
Collection<Integer> full = populatedSet(elements); |
502 |
|
|
503 |
|
assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray()))); |
517 |
|
a = new Integer[0]; |
518 |
|
assertSame(a, empty.toArray(a)); |
519 |
|
|
520 |
< |
a = new Integer[size/2]; |
520 |
> |
a = new Integer[size / 2]; |
521 |
|
Arrays.fill(a, 42); |
522 |
|
assertSame(a, empty.toArray(a)); |
523 |
|
assertNull(a[0]); |
527 |
|
Integer[] elements = new Integer[size]; |
528 |
|
for (int i = 0; i < size; i++) |
529 |
|
elements[i] = i; |
530 |
< |
Collections.shuffle(Arrays.asList(elements)); |
530 |
> |
shuffle(elements); |
531 |
|
Collection<Integer> full = populatedSet(elements); |
532 |
|
|
533 |
|
Arrays.fill(a, 42); |
543 |
|
} |
544 |
|
|
545 |
|
/** |
546 |
< |
* A deserialized serialized set is equal |
546 |
> |
* A deserialized/reserialized set equals original |
547 |
|
*/ |
548 |
|
public void testSerialization() throws Exception { |
549 |
|
int size = 20; |
1089 |
|
assertNull(r); |
1090 |
|
} |
1091 |
|
|
1092 |
+ |
/** |
1093 |
+ |
* Tests performance of computeIfAbsent when the element is present. |
1094 |
+ |
* See JDK-8161372 |
1095 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck |
1096 |
+ |
*/ |
1097 |
+ |
public void testcomputeIfAbsent_performance() { |
1098 |
+ |
final int mapSize = 20; |
1099 |
+ |
final int iterations = expensiveTests ? (1 << 23) : mapSize * 2; |
1100 |
+ |
final int threads = expensiveTests ? 10 : 2; |
1101 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
1102 |
+ |
for (int i = 0; i < mapSize; i++) |
1103 |
+ |
map.put(i, i); |
1104 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(2); |
1105 |
+ |
try (PoolCleaner cleaner = cleaner(pool)) { |
1106 |
+ |
Runnable r = new CheckedRunnable() { |
1107 |
+ |
public void realRun() { |
1108 |
+ |
int result = 0; |
1109 |
+ |
for (int i = 0; i < iterations; i++) |
1110 |
+ |
result += map.computeIfAbsent(i % mapSize, k -> k + k); |
1111 |
+ |
if (result == -42) throw new Error(); |
1112 |
+ |
}}; |
1113 |
+ |
for (int i = 0; i < threads; i++) |
1114 |
+ |
pool.execute(r); |
1115 |
+ |
} |
1116 |
+ |
} |
1117 |
+ |
|
1118 |
|
} |