ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.22
Committed: Sat Jan 17 22:55:06 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +11 -5 lines
Log Message:
add more tests of exhausted iterators

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