ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.12
Committed: Sun Jul 21 22:24:18 2013 UTC (10 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.11: +100 -0 lines
Log Message:
Adapt/incorporate JDK8 tests including suggestions by Eric Wang

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