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 |
import java.util.function.*; |
10 |
import java.util.concurrent.atomic.LongAdder; |
11 |
import java.util.concurrent.ConcurrentHashMap; |
12 |
|
13 |
public class ConcurrentHashMap8Test extends JSR166TestCase { |
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run(suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(ConcurrentHashMap8Test.class); |
19 |
} |
20 |
|
21 |
/** |
22 |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
23 |
*/ |
24 |
private static ConcurrentHashMap map5() { |
25 |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
26 |
assertTrue(map.isEmpty()); |
27 |
map.put(one, "A"); |
28 |
map.put(two, "B"); |
29 |
map.put(three, "C"); |
30 |
map.put(four, "D"); |
31 |
map.put(five, "E"); |
32 |
assertFalse(map.isEmpty()); |
33 |
assertEquals(5, map.size()); |
34 |
return map; |
35 |
} |
36 |
|
37 |
/** |
38 |
* getOrDefault returns value if present, else default |
39 |
*/ |
40 |
public void testGetOrDefault() { |
41 |
ConcurrentHashMap map = map5(); |
42 |
assertEquals(map.getOrDefault(one, "Z"), "A"); |
43 |
assertEquals(map.getOrDefault(six, "Z"), "Z"); |
44 |
} |
45 |
|
46 |
/** |
47 |
* computeIfAbsent adds when the given key is not present |
48 |
*/ |
49 |
public void testComputeIfAbsent() { |
50 |
ConcurrentHashMap map = map5(); |
51 |
map.computeIfAbsent(six, (x) -> "Z"); |
52 |
assertTrue(map.containsKey(six)); |
53 |
} |
54 |
|
55 |
/** |
56 |
* computeIfAbsent does not replace if the key is already present |
57 |
*/ |
58 |
public void testComputeIfAbsent2() { |
59 |
ConcurrentHashMap map = map5(); |
60 |
assertEquals("A", map.computeIfAbsent(one, (x) -> "Z")); |
61 |
} |
62 |
|
63 |
/** |
64 |
* computeIfAbsent does not add if function returns null |
65 |
*/ |
66 |
public void testComputeIfAbsent3() { |
67 |
ConcurrentHashMap map = map5(); |
68 |
map.computeIfAbsent(six, (x) -> null); |
69 |
assertFalse(map.containsKey(six)); |
70 |
} |
71 |
|
72 |
/** |
73 |
* computeIfPresent does not replace if the key is already present |
74 |
*/ |
75 |
public void testComputeIfPresent() { |
76 |
ConcurrentHashMap map = map5(); |
77 |
map.computeIfPresent(six, (x, y) -> "Z"); |
78 |
assertFalse(map.containsKey(six)); |
79 |
} |
80 |
|
81 |
/** |
82 |
* computeIfPresent adds when the given key is not present |
83 |
*/ |
84 |
public void testComputeIfPresent2() { |
85 |
ConcurrentHashMap map = map5(); |
86 |
assertEquals("Z", map.computeIfPresent(one, (x, y) -> "Z")); |
87 |
} |
88 |
|
89 |
/** |
90 |
* compute does not replace if the function returns null |
91 |
*/ |
92 |
public void testCompute() { |
93 |
ConcurrentHashMap map = map5(); |
94 |
map.compute(six, (x, y) -> null); |
95 |
assertFalse(map.containsKey(six)); |
96 |
} |
97 |
|
98 |
/** |
99 |
* compute adds when the given key is not present |
100 |
*/ |
101 |
public void testCompute2() { |
102 |
ConcurrentHashMap map = map5(); |
103 |
assertEquals("Z", map.compute(six, (x, y) -> "Z")); |
104 |
} |
105 |
|
106 |
/** |
107 |
* compute replaces when the given key is present |
108 |
*/ |
109 |
public void testCompute3() { |
110 |
ConcurrentHashMap map = map5(); |
111 |
assertEquals("Z", map.compute(one, (x, y) -> "Z")); |
112 |
} |
113 |
|
114 |
/** |
115 |
* compute removes when the given key is present and function returns null |
116 |
*/ |
117 |
public void testCompute4() { |
118 |
ConcurrentHashMap map = map5(); |
119 |
map.compute(one, (x, y) -> null); |
120 |
assertFalse(map.containsKey(one)); |
121 |
} |
122 |
|
123 |
/** |
124 |
* merge adds when the given key is not present |
125 |
*/ |
126 |
public void testMerge1() { |
127 |
ConcurrentHashMap map = map5(); |
128 |
assertEquals("Y", map.merge(six, "Y", (x, y) -> "Z")); |
129 |
} |
130 |
|
131 |
/** |
132 |
* merge replaces when the given key is present |
133 |
*/ |
134 |
public void testMerge2() { |
135 |
ConcurrentHashMap map = map5(); |
136 |
assertEquals("Z", map.merge(one, "Y", (x, y) -> "Z")); |
137 |
} |
138 |
|
139 |
/** |
140 |
* merge removes when the given key is present and function returns null |
141 |
*/ |
142 |
public void testMerge3() { |
143 |
ConcurrentHashMap map = map5(); |
144 |
map.merge(one, "Y", (x, y) -> null); |
145 |
assertFalse(map.containsKey(one)); |
146 |
} |
147 |
|
148 |
static Set<Integer> populatedSet(int n) { |
149 |
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
150 |
assertTrue(a.isEmpty()); |
151 |
for (int i = 0; i < n; i++) |
152 |
a.add(i); |
153 |
assertFalse(a.isEmpty()); |
154 |
assertEquals(n, a.size()); |
155 |
return a; |
156 |
} |
157 |
|
158 |
static Set populatedSet(Integer[] elements) { |
159 |
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
160 |
assertTrue(a.isEmpty()); |
161 |
for (int i = 0; i < elements.length; i++) |
162 |
a.add(elements[i]); |
163 |
assertFalse(a.isEmpty()); |
164 |
assertEquals(elements.length, a.size()); |
165 |
return a; |
166 |
} |
167 |
|
168 |
/** |
169 |
* Default-constructed set is empty |
170 |
*/ |
171 |
public void testNewKeySet() { |
172 |
Set a = ConcurrentHashMap.newKeySet(); |
173 |
assertTrue(a.isEmpty()); |
174 |
} |
175 |
|
176 |
/** |
177 |
* keySet.addAll adds each element from the given collection |
178 |
*/ |
179 |
public void testAddAll() { |
180 |
Set full = populatedSet(3); |
181 |
Vector v = new Vector(); |
182 |
v.add(three); |
183 |
v.add(four); |
184 |
v.add(five); |
185 |
full.addAll(v); |
186 |
assertEquals(6, full.size()); |
187 |
} |
188 |
|
189 |
/** |
190 |
* keySet.addAll adds each element from the given collection that did not |
191 |
* already exist in the set |
192 |
*/ |
193 |
public void testAddAll2() { |
194 |
Set full = populatedSet(3); |
195 |
Vector v = new Vector(); |
196 |
v.add(three); |
197 |
v.add(four); |
198 |
v.add(one); // will not add this element |
199 |
full.addAll(v); |
200 |
assertEquals(5, full.size()); |
201 |
} |
202 |
|
203 |
/** |
204 |
* keySet.add will not add the element if it already exists in the set |
205 |
*/ |
206 |
public void testAdd2() { |
207 |
Set full = populatedSet(3); |
208 |
full.add(one); |
209 |
assertEquals(3, full.size()); |
210 |
} |
211 |
|
212 |
/** |
213 |
* keySet.add adds the element when it does not exist in the set |
214 |
*/ |
215 |
public void testAdd3() { |
216 |
Set full = populatedSet(3); |
217 |
full.add(three); |
218 |
assertTrue(full.contains(three)); |
219 |
} |
220 |
|
221 |
/** |
222 |
* keyset.clear removes all elements from the set |
223 |
*/ |
224 |
public void testClear() { |
225 |
Set full = populatedSet(3); |
226 |
full.clear(); |
227 |
assertEquals(0, full.size()); |
228 |
} |
229 |
|
230 |
/** |
231 |
* keyset.contains returns true for added elements |
232 |
*/ |
233 |
public void testContains() { |
234 |
Set full = populatedSet(3); |
235 |
assertTrue(full.contains(one)); |
236 |
assertFalse(full.contains(five)); |
237 |
} |
238 |
|
239 |
/** |
240 |
* KeySets with equal elements are equal |
241 |
*/ |
242 |
public void testEquals() { |
243 |
Set a = populatedSet(3); |
244 |
Set b = populatedSet(3); |
245 |
assertTrue(a.equals(b)); |
246 |
assertTrue(b.equals(a)); |
247 |
assertEquals(a.hashCode(), b.hashCode()); |
248 |
a.add(m1); |
249 |
assertFalse(a.equals(b)); |
250 |
assertFalse(b.equals(a)); |
251 |
b.add(m1); |
252 |
assertTrue(a.equals(b)); |
253 |
assertTrue(b.equals(a)); |
254 |
assertEquals(a.hashCode(), b.hashCode()); |
255 |
} |
256 |
|
257 |
/** |
258 |
* KeySet.containsAll returns true for collections with subset of elements |
259 |
*/ |
260 |
public void testContainsAll() { |
261 |
Set full = populatedSet(3); |
262 |
Vector v = new Vector(); |
263 |
v.add(one); |
264 |
v.add(two); |
265 |
assertTrue(full.containsAll(v)); |
266 |
v.add(six); |
267 |
assertFalse(full.containsAll(v)); |
268 |
} |
269 |
|
270 |
/** |
271 |
* KeySet.isEmpty is true when empty, else false |
272 |
*/ |
273 |
public void testIsEmpty() { |
274 |
Set empty = ConcurrentHashMap.newKeySet(); |
275 |
Set full = populatedSet(3); |
276 |
assertTrue(empty.isEmpty()); |
277 |
assertFalse(full.isEmpty()); |
278 |
} |
279 |
|
280 |
/** |
281 |
* KeySet.iterator() returns an iterator containing the elements of the |
282 |
* set |
283 |
*/ |
284 |
public void testIterator() { |
285 |
Collection empty = ConcurrentHashMap.newKeySet(); |
286 |
int size = 20; |
287 |
assertFalse(empty.iterator().hasNext()); |
288 |
try { |
289 |
empty.iterator().next(); |
290 |
shouldThrow(); |
291 |
} catch (NoSuchElementException success) {} |
292 |
|
293 |
Integer[] elements = new Integer[size]; |
294 |
for (int i = 0; i < size; i++) |
295 |
elements[i] = i; |
296 |
Collections.shuffle(Arrays.asList(elements)); |
297 |
Collection<Integer> full = populatedSet(elements); |
298 |
|
299 |
Iterator it = full.iterator(); |
300 |
for (int j = 0; j < size; j++) { |
301 |
assertTrue(it.hasNext()); |
302 |
it.next(); |
303 |
} |
304 |
assertFalse(it.hasNext()); |
305 |
try { |
306 |
it.next(); |
307 |
shouldThrow(); |
308 |
} catch (NoSuchElementException success) {} |
309 |
} |
310 |
|
311 |
/** |
312 |
* KeySet.iterator.remove removes current element |
313 |
*/ |
314 |
public void testIteratorRemove() { |
315 |
Set q = populatedSet(3); |
316 |
Iterator it = q.iterator(); |
317 |
Object removed = it.next(); |
318 |
it.remove(); |
319 |
|
320 |
it = q.iterator(); |
321 |
assertFalse(it.next().equals(removed)); |
322 |
assertFalse(it.next().equals(removed)); |
323 |
assertFalse(it.hasNext()); |
324 |
} |
325 |
|
326 |
/** |
327 |
* KeySet.toString holds toString of elements |
328 |
*/ |
329 |
public void testToString() { |
330 |
assertEquals("[]", ConcurrentHashMap.newKeySet().toString()); |
331 |
Set full = populatedSet(3); |
332 |
String s = full.toString(); |
333 |
for (int i = 0; i < 3; ++i) |
334 |
assertTrue(s.contains(String.valueOf(i))); |
335 |
} |
336 |
|
337 |
/** |
338 |
* KeySet.removeAll removes all elements from the given collection |
339 |
*/ |
340 |
public void testRemoveAll() { |
341 |
Set full = populatedSet(3); |
342 |
Vector v = new Vector(); |
343 |
v.add(one); |
344 |
v.add(two); |
345 |
full.removeAll(v); |
346 |
assertEquals(1, full.size()); |
347 |
} |
348 |
|
349 |
/** |
350 |
* KeySet.remove removes an element |
351 |
*/ |
352 |
public void testRemove() { |
353 |
Set full = populatedSet(3); |
354 |
full.remove(one); |
355 |
assertFalse(full.contains(one)); |
356 |
assertEquals(2, full.size()); |
357 |
} |
358 |
|
359 |
/** |
360 |
* keySet.size returns the number of elements |
361 |
*/ |
362 |
public void testSize() { |
363 |
Set empty = ConcurrentHashMap.newKeySet(); |
364 |
Set full = populatedSet(3); |
365 |
assertEquals(3, full.size()); |
366 |
assertEquals(0, empty.size()); |
367 |
} |
368 |
|
369 |
/** |
370 |
* KeySet.toArray() returns an Object array containing all elements from |
371 |
* the set |
372 |
*/ |
373 |
public void testToArray() { |
374 |
Object[] a = ConcurrentHashMap.newKeySet().toArray(); |
375 |
assertTrue(Arrays.equals(new Object[0], a)); |
376 |
assertSame(Object[].class, a.getClass()); |
377 |
int size = 20; |
378 |
Integer[] elements = new Integer[size]; |
379 |
for (int i = 0; i < size; i++) |
380 |
elements[i] = i; |
381 |
Collections.shuffle(Arrays.asList(elements)); |
382 |
Collection<Integer> full = populatedSet(elements); |
383 |
|
384 |
assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray()))); |
385 |
assertTrue(full.containsAll(Arrays.asList(full.toArray()))); |
386 |
assertSame(Object[].class, full.toArray().getClass()); |
387 |
} |
388 |
|
389 |
/** |
390 |
* toArray(Integer array) returns an Integer array containing all |
391 |
* elements from the set |
392 |
*/ |
393 |
public void testToArray2() { |
394 |
Collection empty = ConcurrentHashMap.newKeySet(); |
395 |
Integer[] a; |
396 |
int size = 20; |
397 |
|
398 |
a = new Integer[0]; |
399 |
assertSame(a, empty.toArray(a)); |
400 |
|
401 |
a = new Integer[size/2]; |
402 |
Arrays.fill(a, 42); |
403 |
assertSame(a, empty.toArray(a)); |
404 |
assertNull(a[0]); |
405 |
for (int i = 1; i < a.length; i++) |
406 |
assertEquals(42, (int) a[i]); |
407 |
|
408 |
Integer[] elements = new Integer[size]; |
409 |
for (int i = 0; i < size; i++) |
410 |
elements[i] = i; |
411 |
Collections.shuffle(Arrays.asList(elements)); |
412 |
Collection<Integer> full = populatedSet(elements); |
413 |
|
414 |
Arrays.fill(a, 42); |
415 |
assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray(a)))); |
416 |
for (int i = 0; i < a.length; i++) |
417 |
assertEquals(42, (int) a[i]); |
418 |
assertSame(Integer[].class, full.toArray(a).getClass()); |
419 |
|
420 |
a = new Integer[size]; |
421 |
Arrays.fill(a, 42); |
422 |
assertSame(a, full.toArray(a)); |
423 |
assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray(a)))); |
424 |
} |
425 |
|
426 |
/** |
427 |
* A deserialized serialized set is equal |
428 |
*/ |
429 |
public void testSerialization() throws Exception { |
430 |
int size = 20; |
431 |
Set x = populatedSet(size); |
432 |
Set y = serialClone(x); |
433 |
|
434 |
assertNotSame(x, y); |
435 |
assertEquals(x.size(), y.size()); |
436 |
assertEquals(x.toString(), y.toString()); |
437 |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
438 |
assertEquals(x, y); |
439 |
assertEquals(y, x); |
440 |
} |
441 |
|
442 |
static final int SIZE = 10000; |
443 |
static ConcurrentHashMap<Long, Long> longMap; |
444 |
|
445 |
static ConcurrentHashMap<Long, Long> longMap() { |
446 |
if (longMap == null) { |
447 |
longMap = new ConcurrentHashMap<Long, Long>(SIZE); |
448 |
for (int i = 0; i < SIZE; ++i) |
449 |
longMap.put(Long.valueOf(i), Long.valueOf(2 *i)); |
450 |
} |
451 |
return longMap; |
452 |
} |
453 |
|
454 |
// explicit function class to avoid type inference problems |
455 |
static class AddKeys implements BiFunction<Map.Entry<Long,Long>, Map.Entry<Long,Long>, Map.Entry<Long,Long>> { |
456 |
public Map.Entry<Long,Long> apply(Map.Entry<Long,Long> x, Map.Entry<Long,Long> y) { |
457 |
return new AbstractMap.SimpleEntry<Long,Long> |
458 |
(Long.valueOf(x.getKey().longValue() + y.getKey().longValue()), |
459 |
Long.valueOf(1L)); |
460 |
} |
461 |
} |
462 |
|
463 |
/** |
464 |
* forEachKeySequentially traverses all keys |
465 |
*/ |
466 |
public void testForEachKeySequentially() { |
467 |
LongAdder adder = new LongAdder(); |
468 |
ConcurrentHashMap<Long, Long> m = longMap(); |
469 |
m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); |
470 |
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
471 |
} |
472 |
|
473 |
/** |
474 |
* forEachValueSequentially traverses all values |
475 |
*/ |
476 |
public void testForEachValueSequentially() { |
477 |
LongAdder adder = new LongAdder(); |
478 |
ConcurrentHashMap<Long, Long> m = longMap(); |
479 |
m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); |
480 |
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
481 |
} |
482 |
|
483 |
/** |
484 |
* forEachSequentially traverses all mappings |
485 |
*/ |
486 |
public void testForEachSequentially() { |
487 |
LongAdder adder = new LongAdder(); |
488 |
ConcurrentHashMap<Long, Long> m = longMap(); |
489 |
m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
490 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
491 |
} |
492 |
|
493 |
/** |
494 |
* forEachEntrySequentially traverses all entries |
495 |
*/ |
496 |
public void testForEachEntrySequentially() { |
497 |
LongAdder adder = new LongAdder(); |
498 |
ConcurrentHashMap<Long, Long> m = longMap(); |
499 |
m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
500 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
501 |
} |
502 |
|
503 |
/** |
504 |
* forEachKeyInParallel traverses all keys |
505 |
*/ |
506 |
public void testForEachKeyInParallel() { |
507 |
LongAdder adder = new LongAdder(); |
508 |
ConcurrentHashMap<Long, Long> m = longMap(); |
509 |
m.forEachKey(1L, (Long x) -> adder.add(x.longValue())); |
510 |
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
511 |
} |
512 |
|
513 |
/** |
514 |
* forEachValueInParallel traverses all values |
515 |
*/ |
516 |
public void testForEachValueInParallel() { |
517 |
LongAdder adder = new LongAdder(); |
518 |
ConcurrentHashMap<Long, Long> m = longMap(); |
519 |
m.forEachValue(1L, (Long x) -> adder.add(x.longValue())); |
520 |
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
521 |
} |
522 |
|
523 |
/** |
524 |
* forEachInParallel traverses all mappings |
525 |
*/ |
526 |
public void testForEachInParallel() { |
527 |
LongAdder adder = new LongAdder(); |
528 |
ConcurrentHashMap<Long, Long> m = longMap(); |
529 |
m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
530 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
531 |
} |
532 |
|
533 |
/** |
534 |
* forEachEntryInParallel traverses all entries |
535 |
*/ |
536 |
public void testForEachEntryInParallel() { |
537 |
LongAdder adder = new LongAdder(); |
538 |
ConcurrentHashMap<Long, Long> m = longMap(); |
539 |
m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
540 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
541 |
} |
542 |
|
543 |
/** |
544 |
* Mapped forEachKeySequentially traverses the given |
545 |
* transformations of all keys |
546 |
*/ |
547 |
public void testMappedForEachKeySequentially() { |
548 |
LongAdder adder = new LongAdder(); |
549 |
ConcurrentHashMap<Long, Long> m = longMap(); |
550 |
m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
551 |
(Long x) -> adder.add(x.longValue())); |
552 |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
553 |
} |
554 |
|
555 |
/** |
556 |
* Mapped forEachValueSequentially traverses the given |
557 |
* transformations of all values |
558 |
*/ |
559 |
public void testMappedForEachValueSequentially() { |
560 |
LongAdder adder = new LongAdder(); |
561 |
ConcurrentHashMap<Long, Long> m = longMap(); |
562 |
m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
563 |
(Long x) -> adder.add(x.longValue())); |
564 |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
565 |
} |
566 |
|
567 |
/** |
568 |
* Mapped forEachSequentially traverses the given |
569 |
* transformations of all mappings |
570 |
*/ |
571 |
public void testMappedForEachSequentially() { |
572 |
LongAdder adder = new LongAdder(); |
573 |
ConcurrentHashMap<Long, Long> m = longMap(); |
574 |
m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
575 |
(Long x) -> adder.add(x.longValue())); |
576 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
577 |
} |
578 |
|
579 |
/** |
580 |
* Mapped forEachEntrySequentially traverses the given |
581 |
* transformations of all entries |
582 |
*/ |
583 |
public void testMappedForEachEntrySequentially() { |
584 |
LongAdder adder = new LongAdder(); |
585 |
ConcurrentHashMap<Long, Long> m = longMap(); |
586 |
m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
587 |
(Long x) -> adder.add(x.longValue())); |
588 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
589 |
} |
590 |
|
591 |
/** |
592 |
* Mapped forEachKeyInParallel traverses the given |
593 |
* transformations of all keys |
594 |
*/ |
595 |
public void testMappedForEachKeyInParallel() { |
596 |
LongAdder adder = new LongAdder(); |
597 |
ConcurrentHashMap<Long, Long> m = longMap(); |
598 |
m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
599 |
(Long x) -> adder.add(x.longValue())); |
600 |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
601 |
} |
602 |
|
603 |
/** |
604 |
* Mapped forEachValueInParallel traverses the given |
605 |
* transformations of all values |
606 |
*/ |
607 |
public void testMappedForEachValueInParallel() { |
608 |
LongAdder adder = new LongAdder(); |
609 |
ConcurrentHashMap<Long, Long> m = longMap(); |
610 |
m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
611 |
(Long x) -> adder.add(x.longValue())); |
612 |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
613 |
} |
614 |
|
615 |
/** |
616 |
* Mapped forEachInParallel traverses the given |
617 |
* transformations of all mappings |
618 |
*/ |
619 |
public void testMappedForEachInParallel() { |
620 |
LongAdder adder = new LongAdder(); |
621 |
ConcurrentHashMap<Long, Long> m = longMap(); |
622 |
m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
623 |
(Long x) -> adder.add(x.longValue())); |
624 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
625 |
} |
626 |
|
627 |
/** |
628 |
* Mapped forEachEntryInParallel traverses the given |
629 |
* transformations of all entries |
630 |
*/ |
631 |
public void testMappedForEachEntryInParallel() { |
632 |
LongAdder adder = new LongAdder(); |
633 |
ConcurrentHashMap<Long, Long> m = longMap(); |
634 |
m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
635 |
(Long x) -> adder.add(x.longValue())); |
636 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
637 |
} |
638 |
|
639 |
/** |
640 |
* reduceKeysSequentially accumulates across all keys, |
641 |
*/ |
642 |
public void testReduceKeysSequentially() { |
643 |
ConcurrentHashMap<Long, Long> m = longMap(); |
644 |
Long r; |
645 |
r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
646 |
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
647 |
} |
648 |
|
649 |
/** |
650 |
* reduceValuesSequentially accumulates across all values |
651 |
*/ |
652 |
public void testReduceValuesSequentially() { |
653 |
ConcurrentHashMap<Long, Long> m = longMap(); |
654 |
Long r; |
655 |
r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
656 |
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
657 |
} |
658 |
|
659 |
/** |
660 |
* reduceEntriesSequentially accumulates across all entries |
661 |
*/ |
662 |
public void testReduceEntriesSequentially() { |
663 |
ConcurrentHashMap<Long, Long> m = longMap(); |
664 |
Map.Entry<Long,Long> r; |
665 |
r = m.reduceEntries(Long.MAX_VALUE, new AddKeys()); |
666 |
assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); |
667 |
} |
668 |
|
669 |
/** |
670 |
* reduceKeysInParallel accumulates across all keys |
671 |
*/ |
672 |
public void testReduceKeysInParallel() { |
673 |
ConcurrentHashMap<Long, Long> m = longMap(); |
674 |
Long r; |
675 |
r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
676 |
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
677 |
} |
678 |
|
679 |
/** |
680 |
* reduceValuesInParallel accumulates across all values |
681 |
*/ |
682 |
public void testReduceValuesInParallel() { |
683 |
ConcurrentHashMap<Long, Long> m = longMap(); |
684 |
Long r; |
685 |
r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
686 |
assertEquals((long)r, (long)SIZE * (SIZE - 1)); |
687 |
} |
688 |
|
689 |
/** |
690 |
* reduceEntriesInParallel accumulate across all entries |
691 |
*/ |
692 |
public void testReduceEntriesInParallel() { |
693 |
ConcurrentHashMap<Long, Long> m = longMap(); |
694 |
Map.Entry<Long,Long> r; |
695 |
r = m.reduceEntries(1L, new AddKeys()); |
696 |
assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); |
697 |
} |
698 |
|
699 |
/** |
700 |
* Mapped reduceKeysSequentially accumulates mapped keys |
701 |
*/ |
702 |
public void testMapReduceKeysSequentially() { |
703 |
ConcurrentHashMap<Long, Long> m = longMap(); |
704 |
Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
705 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
706 |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
707 |
} |
708 |
|
709 |
/** |
710 |
* Mapped reduceValuesSequentially accumulates mapped values |
711 |
*/ |
712 |
public void testMapReduceValuesSequentially() { |
713 |
ConcurrentHashMap<Long, Long> m = longMap(); |
714 |
Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
715 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
716 |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
717 |
} |
718 |
|
719 |
/** |
720 |
* reduceSequentially accumulates across all transformed mappings |
721 |
*/ |
722 |
public void testMappedReduceSequentially() { |
723 |
ConcurrentHashMap<Long, Long> m = longMap(); |
724 |
Long r = m.reduce(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
725 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
726 |
|
727 |
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
728 |
} |
729 |
|
730 |
/** |
731 |
* Mapped reduceKeysInParallel, accumulates mapped keys |
732 |
*/ |
733 |
public void testMapReduceKeysInParallel() { |
734 |
ConcurrentHashMap<Long, Long> m = longMap(); |
735 |
Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
736 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
737 |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
738 |
} |
739 |
|
740 |
/** |
741 |
* Mapped reduceValuesInParallel accumulates mapped values |
742 |
*/ |
743 |
public void testMapReduceValuesInParallel() { |
744 |
ConcurrentHashMap<Long, Long> m = longMap(); |
745 |
Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
746 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
747 |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
748 |
} |
749 |
|
750 |
/** |
751 |
* reduceInParallel accumulate across all transformed mappings |
752 |
*/ |
753 |
public void testMappedReduceInParallel() { |
754 |
ConcurrentHashMap<Long, Long> m = longMap(); |
755 |
Long r; |
756 |
r = m.reduce(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
757 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
758 |
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
759 |
} |
760 |
|
761 |
/** |
762 |
* reduceKeysToLongSequentially accumulates mapped keys |
763 |
*/ |
764 |
public void testReduceKeysToLongSequentially() { |
765 |
ConcurrentHashMap<Long, Long> m = longMap(); |
766 |
long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum); |
767 |
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
768 |
} |
769 |
|
770 |
/** |
771 |
* reduceKeysToIntSequentially accumulates mapped keys |
772 |
*/ |
773 |
public void testReduceKeysToIntSequentially() { |
774 |
ConcurrentHashMap<Long, Long> m = longMap(); |
775 |
int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); |
776 |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
777 |
} |
778 |
|
779 |
/** |
780 |
* reduceKeysToDoubleSequentially accumulates mapped keys |
781 |
*/ |
782 |
public void testReduceKeysToDoubleSequentially() { |
783 |
ConcurrentHashMap<Long, Long> m = longMap(); |
784 |
double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
785 |
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
786 |
} |
787 |
|
788 |
/** |
789 |
* reduceValuesToLongSequentially accumulates mapped values |
790 |
*/ |
791 |
public void testReduceValuesToLongSequentially() { |
792 |
ConcurrentHashMap<Long, Long> m = longMap(); |
793 |
long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum); |
794 |
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
795 |
} |
796 |
|
797 |
/** |
798 |
* reduceValuesToIntSequentially accumulates mapped values |
799 |
*/ |
800 |
public void testReduceValuesToIntSequentially() { |
801 |
ConcurrentHashMap<Long, Long> m = longMap(); |
802 |
int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); |
803 |
assertEquals(ir, SIZE * (SIZE - 1)); |
804 |
} |
805 |
|
806 |
/** |
807 |
* reduceValuesToDoubleSequentially accumulates mapped values |
808 |
*/ |
809 |
public void testReduceValuesToDoubleSequentially() { |
810 |
ConcurrentHashMap<Long, Long> m = longMap(); |
811 |
double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
812 |
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
813 |
} |
814 |
|
815 |
/** |
816 |
* reduceKeysToLongInParallel accumulates mapped keys |
817 |
*/ |
818 |
public void testReduceKeysToLongInParallel() { |
819 |
ConcurrentHashMap<Long, Long> m = longMap(); |
820 |
long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); |
821 |
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
822 |
} |
823 |
|
824 |
/** |
825 |
* reduceKeysToIntInParallel accumulates mapped keys |
826 |
*/ |
827 |
public void testReduceKeysToIntInParallel() { |
828 |
ConcurrentHashMap<Long, Long> m = longMap(); |
829 |
int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); |
830 |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
831 |
} |
832 |
|
833 |
/** |
834 |
* reduceKeysToDoubleInParallel accumulates mapped values |
835 |
*/ |
836 |
public void testReduceKeysToDoubleInParallel() { |
837 |
ConcurrentHashMap<Long, Long> m = longMap(); |
838 |
double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
839 |
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
840 |
} |
841 |
|
842 |
/** |
843 |
* reduceValuesToLongInParallel accumulates mapped values |
844 |
*/ |
845 |
public void testReduceValuesToLongInParallel() { |
846 |
ConcurrentHashMap<Long, Long> m = longMap(); |
847 |
long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); |
848 |
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
849 |
} |
850 |
|
851 |
/** |
852 |
* reduceValuesToIntInParallel accumulates mapped values |
853 |
*/ |
854 |
public void testReduceValuesToIntInParallel() { |
855 |
ConcurrentHashMap<Long, Long> m = longMap(); |
856 |
int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); |
857 |
assertEquals(ir, SIZE * (SIZE - 1)); |
858 |
} |
859 |
|
860 |
/** |
861 |
* reduceValuesToDoubleInParallel accumulates mapped values |
862 |
*/ |
863 |
public void testReduceValuesToDoubleInParallel() { |
864 |
ConcurrentHashMap<Long, Long> m = longMap(); |
865 |
double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
866 |
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
867 |
} |
868 |
|
869 |
/** |
870 |
* searchKeysSequentially returns a non-null result of search |
871 |
* function, or null if none |
872 |
*/ |
873 |
public void testSearchKeysSequentially() { |
874 |
ConcurrentHashMap<Long, Long> m = longMap(); |
875 |
Long r; |
876 |
r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
877 |
assertEquals((long)r, (long)(SIZE/2)); |
878 |
r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); |
879 |
assertNull(r); |
880 |
} |
881 |
|
882 |
/** |
883 |
* searchValuesSequentially returns a non-null result of search |
884 |
* function, or null if none |
885 |
*/ |
886 |
public void testSearchValuesSequentially() { |
887 |
ConcurrentHashMap<Long, Long> m = longMap(); |
888 |
Long r; |
889 |
r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
890 |
assertEquals((long)r, (long)(SIZE/2)); |
891 |
r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); |
892 |
assertNull(r); |
893 |
} |
894 |
|
895 |
/** |
896 |
* searchSequentially returns a non-null result of search |
897 |
* function, or null if none |
898 |
*/ |
899 |
public void testSearchSequentially() { |
900 |
ConcurrentHashMap<Long, Long> m = longMap(); |
901 |
Long r; |
902 |
r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
903 |
assertEquals((long)r, (long)(SIZE/2)); |
904 |
r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null); |
905 |
assertNull(r); |
906 |
} |
907 |
|
908 |
/** |
909 |
* searchEntriesSequentially returns a non-null result of search |
910 |
* function, or null if none |
911 |
*/ |
912 |
public void testSearchEntriesSequentially() { |
913 |
ConcurrentHashMap<Long, Long> m = longMap(); |
914 |
Long r; |
915 |
r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
916 |
assertEquals((long)r, (long)(SIZE/2)); |
917 |
r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
918 |
assertNull(r); |
919 |
} |
920 |
|
921 |
/** |
922 |
* searchKeysInParallel returns a non-null result of search |
923 |
* function, or null if none |
924 |
*/ |
925 |
public void testSearchKeysInParallel() { |
926 |
ConcurrentHashMap<Long, Long> m = longMap(); |
927 |
Long r; |
928 |
r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
929 |
assertEquals((long)r, (long)(SIZE/2)); |
930 |
r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null); |
931 |
assertNull(r); |
932 |
} |
933 |
|
934 |
/** |
935 |
* searchValuesInParallel returns a non-null result of search |
936 |
* function, or null if none |
937 |
*/ |
938 |
public void testSearchValuesInParallel() { |
939 |
ConcurrentHashMap<Long, Long> m = longMap(); |
940 |
Long r; |
941 |
r = m.searchValues(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
942 |
assertEquals((long)r, (long)(SIZE/2)); |
943 |
r = m.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null); |
944 |
assertNull(r); |
945 |
} |
946 |
|
947 |
/** |
948 |
* searchInParallel returns a non-null result of search function, |
949 |
* or null if none |
950 |
*/ |
951 |
public void testSearchInParallel() { |
952 |
ConcurrentHashMap<Long, Long> m = longMap(); |
953 |
Long r; |
954 |
r = m.search(1L, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
955 |
assertEquals((long)r, (long)(SIZE/2)); |
956 |
r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null); |
957 |
assertNull(r); |
958 |
} |
959 |
|
960 |
/** |
961 |
* searchEntriesInParallel returns a non-null result of search |
962 |
* function, or null if none |
963 |
*/ |
964 |
public void testSearchEntriesInParallel() { |
965 |
ConcurrentHashMap<Long, Long> m = longMap(); |
966 |
Long r; |
967 |
r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
968 |
assertEquals((long)r, (long)(SIZE/2)); |
969 |
r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
970 |
assertNull(r); |
971 |
} |
972 |
|
973 |
} |