ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.23
Committed: Fri Feb 27 19:47:57 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.22: +19 -27 lines
Log Message:
sync test improvements from CopyOnWriteArraySetTest

File Contents

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