ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.19
Committed: Wed Dec 31 21:35:01 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +2 -2 lines
Log Message:
tidy testReplaceAll

File Contents

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