ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.14
Committed: Mon Jul 22 18:11:56 2013 UTC (10 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +3 -3 lines
Log Message:
whitespace

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