ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.7
Committed: Thu Apr 11 19:54:13 2013 UTC (11 years ago) by jsr166
Branch: MAIN
Changes since 1.6: +16 -16 lines
Log Message:
convert to javadoc comments

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