ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.16
Committed: Thu Aug 8 19:39:48 2013 UTC (10 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +1 -1 lines
Log Message:
whitespace

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