1 |
dl |
1.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 |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
|
|
import java.util.*; |
9 |
dl |
1.2 |
import java.util.concurrent.atomic.LongAdder; |
10 |
dl |
1.1 |
import java.util.concurrent.ConcurrentHashMap; |
11 |
|
|
|
12 |
|
|
public class ConcurrentHashMap8Test extends JSR166TestCase { |
13 |
|
|
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run(suite()); |
15 |
|
|
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(ConcurrentHashMap8Test.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
22 |
|
|
*/ |
23 |
|
|
private static ConcurrentHashMap map5() { |
24 |
|
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
25 |
|
|
assertTrue(map.isEmpty()); |
26 |
|
|
map.put(one, "A"); |
27 |
|
|
map.put(two, "B"); |
28 |
|
|
map.put(three, "C"); |
29 |
|
|
map.put(four, "D"); |
30 |
|
|
map.put(five, "E"); |
31 |
|
|
assertFalse(map.isEmpty()); |
32 |
|
|
assertEquals(5, map.size()); |
33 |
|
|
return map; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* computeIfAbsent adds when the given key is not present |
38 |
|
|
*/ |
39 |
|
|
public void testComputeIfAbsent() { |
40 |
|
|
ConcurrentHashMap map = map5(); |
41 |
|
|
map.computeIfAbsent(six, (x) -> "Z"); |
42 |
|
|
assertTrue(map.containsKey(six)); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* computeIfAbsent does not replace if the key is already present |
47 |
|
|
*/ |
48 |
|
|
public void testComputeIfAbsent2() { |
49 |
|
|
ConcurrentHashMap map = map5(); |
50 |
|
|
assertEquals("A", map.computeIfAbsent(one, (x) -> "Z")); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
/** |
54 |
|
|
* computeIfAbsent does not add if function returns null |
55 |
|
|
*/ |
56 |
|
|
public void testComputeIfAbsent3() { |
57 |
|
|
ConcurrentHashMap map = map5(); |
58 |
|
|
map.computeIfAbsent(six, (x) -> null); |
59 |
|
|
assertFalse(map.containsKey(six)); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* computeIfPresent does not replace if the key is already present |
64 |
|
|
*/ |
65 |
|
|
public void testComputeIfPresent() { |
66 |
|
|
ConcurrentHashMap map = map5(); |
67 |
|
|
map.computeIfPresent(six, (x, y) -> "Z"); |
68 |
|
|
assertFalse(map.containsKey(six)); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* computeIfPresent adds when the given key is not present |
73 |
|
|
*/ |
74 |
|
|
public void testComputeIfPresent2() { |
75 |
|
|
ConcurrentHashMap map = map5(); |
76 |
|
|
assertEquals("Z", map.computeIfPresent(one, (x, y) -> "Z")); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/** |
80 |
|
|
* compute does not replace if the function returns null |
81 |
|
|
*/ |
82 |
|
|
public void testCompute() { |
83 |
|
|
ConcurrentHashMap map = map5(); |
84 |
|
|
map.compute(six, (x, y) -> null); |
85 |
|
|
assertFalse(map.containsKey(six)); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* compute adds when the given key is not present |
90 |
|
|
*/ |
91 |
|
|
public void testCompute2() { |
92 |
|
|
ConcurrentHashMap map = map5(); |
93 |
|
|
assertEquals("Z", map.compute(six, (x, y) -> "Z")); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* compute replaces when the given key is present |
98 |
|
|
*/ |
99 |
|
|
public void testCompute3() { |
100 |
|
|
ConcurrentHashMap map = map5(); |
101 |
|
|
assertEquals("Z", map.compute(one, (x, y) -> "Z")); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* compute removes when the given key is present and function returns null |
106 |
|
|
*/ |
107 |
|
|
public void testCompute4() { |
108 |
|
|
ConcurrentHashMap map = map5(); |
109 |
|
|
map.compute(one, (x, y) -> null); |
110 |
|
|
assertFalse(map.containsKey(one)); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* merge adds when the given key is not present |
115 |
|
|
*/ |
116 |
|
|
public void testMerge1() { |
117 |
|
|
ConcurrentHashMap map = map5(); |
118 |
|
|
assertEquals("Y", map.merge(six, "Y", (x, y) -> "Z")); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/** |
122 |
|
|
* merge replaces when the given key is present |
123 |
|
|
*/ |
124 |
|
|
public void testMerge2() { |
125 |
|
|
ConcurrentHashMap map = map5(); |
126 |
|
|
assertEquals("Z", map.merge(one, "Y", (x, y) -> "Z")); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
|
|
* merge removes when the given key is present and function returns null |
131 |
|
|
*/ |
132 |
|
|
public void testMerge3() { |
133 |
|
|
ConcurrentHashMap map = map5(); |
134 |
|
|
map.merge(one, "Y", (x, y) -> null); |
135 |
|
|
assertFalse(map.containsKey(one)); |
136 |
|
|
} |
137 |
|
|
|
138 |
dl |
1.2 |
static final int SIZE = 10000; |
139 |
|
|
static ConcurrentHashMap<Long, Long> longMap; |
140 |
|
|
|
141 |
|
|
static ConcurrentHashMap<Long, Long> longMap() { |
142 |
|
|
if (longMap == null) { |
143 |
|
|
longMap = new ConcurrentHashMap<Long, Long>(SIZE); |
144 |
|
|
for (int i = 0; i < SIZE; ++i) |
145 |
|
|
longMap.put(Long.valueOf(i), Long.valueOf(2 *i)); |
146 |
|
|
} |
147 |
|
|
return longMap; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* forEachKeySequentially, forEachValueSequentially, |
152 |
|
|
* forEachEntrySequentially, forEachSequentially, |
153 |
|
|
* forEachKeyInParallel, forEachValueInParallel, |
154 |
|
|
* forEachEntryInParallel, forEachInParallel traverse all keys, |
155 |
|
|
* values, entries, or mappings accordingly |
156 |
|
|
*/ |
157 |
|
|
public void testForEach() { |
158 |
|
|
LongAdder adder = new LongAdder(); |
159 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
160 |
|
|
m.forEachKeySequentially((Long x) -> adder.add(x.longValue())); |
161 |
|
|
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
162 |
|
|
adder.reset(); |
163 |
|
|
m.forEachValueSequentially((Long x) -> adder.add(x.longValue())); |
164 |
|
|
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
165 |
|
|
adder.reset(); |
166 |
|
|
m.forEachSequentially((Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
167 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
168 |
|
|
adder.reset(); |
169 |
|
|
m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
170 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
171 |
|
|
adder.reset(); |
172 |
|
|
m.forEachKeyInParallel((Long x) -> adder.add(x.longValue())); |
173 |
|
|
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
174 |
|
|
adder.reset(); |
175 |
|
|
m.forEachValueInParallel((Long x) -> adder.add(x.longValue())); |
176 |
|
|
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
177 |
|
|
adder.reset(); |
178 |
|
|
m.forEachInParallel((Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
179 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
180 |
|
|
adder.reset(); |
181 |
|
|
m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
182 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
|
|
* Mapped forEachKeySequentially, forEachValueSequentially, |
187 |
|
|
* forEachEntrySequentially, forEachSequentially, |
188 |
|
|
* forEachKeyInParallel, forEachValueInParallel, |
189 |
|
|
* forEachEntryInParallel, forEachInParallel traverse the given |
190 |
|
|
* transformations of all keys, values, entries, or mappings |
191 |
|
|
* accordingly |
192 |
|
|
*/ |
193 |
|
|
public void testMappedForEach() { |
194 |
|
|
LongAdder adder = new LongAdder(); |
195 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
196 |
|
|
m.forEachKeySequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
197 |
|
|
(Long x) -> adder.add(x.longValue())); |
198 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
199 |
|
|
adder.reset(); |
200 |
|
|
m.forEachValueSequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
201 |
|
|
(Long x) -> adder.add(x.longValue())); |
202 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
203 |
|
|
adder.reset(); |
204 |
|
|
m.forEachSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
205 |
|
|
(Long x) -> adder.add(x.longValue())); |
206 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
207 |
|
|
adder.reset(); |
208 |
|
|
m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
209 |
|
|
(Long x) -> adder.add(x.longValue())); |
210 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
211 |
|
|
adder.reset(); |
212 |
|
|
m.forEachKeyInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
213 |
|
|
(Long x) -> adder.add(x.longValue())); |
214 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
215 |
|
|
adder.reset(); |
216 |
|
|
m.forEachValueInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
217 |
|
|
(Long x) -> adder.add(x.longValue())); |
218 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
219 |
|
|
adder.reset(); |
220 |
|
|
m.forEachInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
221 |
|
|
(Long x) -> adder.add(x.longValue())); |
222 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
223 |
|
|
adder.reset(); |
224 |
|
|
m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
225 |
|
|
(Long x) -> adder.add(x.longValue())); |
226 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
/** |
230 |
|
|
* reduceKeysSequentially, reduceValuesSequentially, |
231 |
|
|
* reduceSequentially, reduceEntriesSequentially, |
232 |
|
|
* reduceKeysInParallel, reduceValuesInParallel, reduceInParallel, |
233 |
|
|
* and reduceEntriesInParallel, accumulate across all keys, |
234 |
|
|
* values, entries, or mappings accordingly |
235 |
|
|
*/ |
236 |
|
|
public void testReduce() { |
237 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
238 |
|
|
Long r; |
239 |
|
|
r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
240 |
|
|
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
241 |
|
|
r = m.reduceValuesSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
242 |
|
|
assertEquals((long)r, (long)SIZE * (SIZE - 1)); |
243 |
|
|
r = m.reduceSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
244 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
245 |
|
|
|
246 |
|
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
247 |
|
|
r = m.reduceEntriesSequentially((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
248 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
249 |
|
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
250 |
|
|
|
251 |
|
|
r = m.reduceKeysInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
252 |
|
|
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
253 |
|
|
r = m.reduceValuesInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
254 |
|
|
assertEquals((long)r, (long)SIZE * (SIZE - 1)); |
255 |
|
|
r = m.reduceInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
256 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
257 |
|
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
258 |
|
|
r = m.reduceEntriesInParallel((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
259 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
260 |
|
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
/* |
264 |
|
|
* Mapped reduceKeysSequentially, reduceKeysToIntSequentially, |
265 |
|
|
* reduceKeysToLongSequentially, reduceKeysToDoubleSequentially, |
266 |
|
|
* reduceValuesSequentially, reduceValuesToLongSequentially, |
267 |
|
|
* reduceValuesToDoubleSequentially, reduceKeysInParallel, |
268 |
|
|
* reduceKeysToLongInParallel, reduceKeysToIntInParallel, |
269 |
|
|
* reduceKeysToDoubleInParallel, reduceValuesInParallel, |
270 |
|
|
* reduceValuesToLongInParallel, reduceValuesToIntInParallel, |
271 |
|
|
* reduceValuesToDoubleInParallel accumulate mapped keys, values, |
272 |
|
|
* entries, or mappings accordingly |
273 |
|
|
*/ |
274 |
|
|
public void testMapReduce() { |
275 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
276 |
|
|
Long r; long lr; int ir; double dr; |
277 |
|
|
r = m.reduceKeysSequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
278 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
279 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
280 |
|
|
lr = m.reduceKeysToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum); |
281 |
|
|
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
282 |
|
|
|
283 |
|
|
ir = m.reduceKeysToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); |
284 |
|
|
assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); |
285 |
|
|
dr = m.reduceKeysToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum); |
286 |
|
|
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
287 |
|
|
|
288 |
|
|
r = m.reduceValuesSequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
289 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
290 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
291 |
|
|
lr = m.reduceValuesToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum); |
292 |
|
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
293 |
|
|
ir = m.reduceValuesToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); |
294 |
|
|
assertEquals(ir, (int)SIZE * (SIZE - 1)); |
295 |
|
|
|
296 |
|
|
dr = m.reduceValuesToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum); |
297 |
|
|
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
298 |
|
|
|
299 |
|
|
r = m.reduceKeysInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
300 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
301 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
302 |
|
|
lr = m.reduceKeysToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum); |
303 |
|
|
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
304 |
|
|
ir = m.reduceKeysToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); |
305 |
|
|
assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); |
306 |
|
|
dr = m.reduceKeysToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum); |
307 |
|
|
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
308 |
|
|
|
309 |
|
|
r = m.reduceValuesInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
310 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
311 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
312 |
|
|
lr = m.reduceValuesToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum); |
313 |
|
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
314 |
|
|
ir = m.reduceValuesToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); |
315 |
|
|
assertEquals(ir, (int)SIZE * (SIZE - 1)); |
316 |
|
|
dr = m.reduceValuesToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum); |
317 |
|
|
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
318 |
|
|
} |
319 |
|
|
|
320 |
|
|
/** |
321 |
|
|
* searchKeysSequentially, searchValuesSequentially, |
322 |
|
|
* searchSequentially, searchEntriesSequentially, |
323 |
|
|
* searchKeysInParallel, searchValuesInParallel, searchInParallel, |
324 |
|
|
* searchEntriesInParallel all return a non-null result of search |
325 |
|
|
* function, or null if none, across keys, values, entries, or |
326 |
|
|
* mappings accordingly |
327 |
|
|
*/ |
328 |
|
|
public void testSearch() { |
329 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
330 |
|
|
Long r; |
331 |
|
|
r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
332 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
333 |
|
|
r = m.searchValuesSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
334 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
335 |
|
|
r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null); |
336 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
337 |
|
|
r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null); |
338 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
339 |
|
|
|
340 |
|
|
r = m.searchKeysSequentially((Long x) -> x.longValue() < 0L? x : null); |
341 |
|
|
assertNull(r); |
342 |
|
|
r = m.searchValuesSequentially((Long x) -> x.longValue() < 0L? x : null); |
343 |
|
|
assertNull(r); |
344 |
|
|
r = m.searchSequentially((Long x, Long y) -> x.longValue() < 0L? x : null); |
345 |
|
|
assertNull(r); |
346 |
|
|
r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L? e.getKey() : null); |
347 |
|
|
assertNull(r); |
348 |
|
|
|
349 |
|
|
r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
350 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
351 |
|
|
r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
352 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
353 |
|
|
r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null); |
354 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
355 |
|
|
r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null); |
356 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
357 |
|
|
|
358 |
|
|
r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L? x : null); |
359 |
|
|
assertNull(r); |
360 |
|
|
r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L? x : null); |
361 |
|
|
assertNull(r); |
362 |
|
|
r = m.searchInParallel((Long x, Long y) -> x.longValue() < 0L? x : null); |
363 |
|
|
assertNull(r); |
364 |
|
|
r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L? e.getKey() : null); |
365 |
|
|
assertNull(r); |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
/** |
369 |
|
|
* Invoking task versions of bulk methods has same effect as |
370 |
|
|
* parallel methods |
371 |
|
|
*/ |
372 |
|
|
public void testForkJoinTasks() { |
373 |
|
|
LongAdder adder = new LongAdder(); |
374 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
375 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEachKey |
376 |
|
|
(m, (Long x) -> adder.add(x.longValue())).invoke(); |
377 |
|
|
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
378 |
|
|
adder.reset(); |
379 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEachValue |
380 |
|
|
(m, (Long x) -> adder.add(x.longValue())).invoke(); |
381 |
|
|
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
382 |
|
|
adder.reset(); |
383 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEach |
384 |
|
|
(m, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())).invoke(); |
385 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
386 |
|
|
adder.reset(); |
387 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEachEntry |
388 |
|
|
(m, |
389 |
|
|
(Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())).invoke(); |
390 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
391 |
|
|
adder.reset(); |
392 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEachKey |
393 |
|
|
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
394 |
|
|
(Long x) -> adder.add(x.longValue())).invoke(); |
395 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
396 |
|
|
adder.reset(); |
397 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEachValue |
398 |
|
|
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
399 |
|
|
(Long x) -> adder.add(x.longValue())).invoke(); |
400 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
401 |
|
|
adder.reset(); |
402 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEach |
403 |
|
|
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
404 |
|
|
(Long x) -> adder.add(x.longValue())).invoke(); |
405 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
406 |
|
|
adder.reset(); |
407 |
|
|
ConcurrentHashMap.ForkJoinTasks.forEachEntry |
408 |
|
|
(m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
409 |
|
|
(Long x) -> adder.add(x.longValue())).invoke(); |
410 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
411 |
|
|
adder.reset(); |
412 |
|
|
|
413 |
|
|
Long r; long lr; int ir; double dr; |
414 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.reduceKeys |
415 |
|
|
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
416 |
|
|
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
417 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.reduceValues |
418 |
|
|
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
419 |
|
|
assertEquals((long)r, (long)SIZE * (SIZE - 1)); |
420 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.reduce |
421 |
|
|
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
422 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
423 |
|
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
424 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.reduceEntries |
425 |
|
|
(m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
426 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
427 |
|
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
428 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.reduceKeys |
429 |
|
|
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
430 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
431 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
432 |
|
|
lr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToLong |
433 |
|
|
(m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); |
434 |
|
|
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
435 |
|
|
ir = ConcurrentHashMap.ForkJoinTasks.reduceKeysToInt |
436 |
|
|
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
437 |
|
|
assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); |
438 |
|
|
dr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToDouble |
439 |
|
|
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
440 |
|
|
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
441 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.reduceValues |
442 |
|
|
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
443 |
|
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
444 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
445 |
|
|
lr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToLong |
446 |
|
|
(m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); |
447 |
|
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
448 |
|
|
ir = ConcurrentHashMap.ForkJoinTasks.reduceValuesToInt |
449 |
|
|
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
450 |
|
|
assertEquals(ir, (int)SIZE * (SIZE - 1)); |
451 |
|
|
dr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToDouble |
452 |
|
|
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
453 |
|
|
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
454 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.searchKeys |
455 |
|
|
(m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
456 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
457 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.searchValues |
458 |
|
|
(m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
459 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
460 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.search |
461 |
|
|
(m, (Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
462 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
463 |
|
|
r = ConcurrentHashMap.ForkJoinTasks.searchEntries |
464 |
|
|
(m, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null).invoke(); |
465 |
|
|
assertEquals((long)r, (long)(SIZE/2)); |
466 |
|
|
} |
467 |
dl |
1.1 |
} |