ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.17
Committed: Sun Dec 1 12:19:59 2013 UTC (10 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.16: +0 -2 lines
Log Message:
Remove iteration-order-dependent serialization check

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, y);
543     assertEquals(y, x);
544     }
545    
546 dl 1.2 static final int SIZE = 10000;
547     static ConcurrentHashMap<Long, Long> longMap;
548 jsr166 1.4
549 dl 1.2 static ConcurrentHashMap<Long, Long> longMap() {
550     if (longMap == null) {
551     longMap = new ConcurrentHashMap<Long, Long>(SIZE);
552     for (int i = 0; i < SIZE; ++i)
553     longMap.put(Long.valueOf(i), Long.valueOf(2 *i));
554     }
555     return longMap;
556     }
557    
558 dl 1.3 // explicit function class to avoid type inference problems
559     static class AddKeys implements BiFunction<Map.Entry<Long,Long>, Map.Entry<Long,Long>, Map.Entry<Long,Long>> {
560     public Map.Entry<Long,Long> apply(Map.Entry<Long,Long> x, Map.Entry<Long,Long> y) {
561     return new AbstractMap.SimpleEntry<Long,Long>
562 jsr166 1.4 (Long.valueOf(x.getKey().longValue() + y.getKey().longValue()),
563 dl 1.3 Long.valueOf(1L));
564     }
565     }
566    
567 dl 1.2 /**
568 dl 1.3 * forEachKeySequentially traverses all keys
569 dl 1.2 */
570 dl 1.3 public void testForEachKeySequentially() {
571 dl 1.2 LongAdder adder = new LongAdder();
572     ConcurrentHashMap<Long, Long> m = longMap();
573 dl 1.9 m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
574 dl 1.2 assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
575 dl 1.3 }
576    
577     /**
578     * forEachValueSequentially traverses all values
579     */
580     public void testForEachValueSequentially() {
581     LongAdder adder = new LongAdder();
582     ConcurrentHashMap<Long, Long> m = longMap();
583 dl 1.9 m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
584 dl 1.2 assertEquals(adder.sum(), SIZE * (SIZE - 1));
585 dl 1.3 }
586    
587     /**
588     * forEachSequentially traverses all mappings
589     */
590     public void testForEachSequentially() {
591     LongAdder adder = new LongAdder();
592     ConcurrentHashMap<Long, Long> m = longMap();
593 dl 1.9 m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
594 dl 1.2 assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
595 dl 1.3 }
596    
597     /**
598     * forEachEntrySequentially traverses all entries
599     */
600     public void testForEachEntrySequentially() {
601     LongAdder adder = new LongAdder();
602     ConcurrentHashMap<Long, Long> m = longMap();
603 dl 1.9 m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
604 dl 1.2 assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
605 dl 1.3 }
606    
607     /**
608     * forEachKeyInParallel traverses all keys
609     */
610     public void testForEachKeyInParallel() {
611     LongAdder adder = new LongAdder();
612     ConcurrentHashMap<Long, Long> m = longMap();
613 dl 1.9 m.forEachKey(1L, (Long x) -> adder.add(x.longValue()));
614 dl 1.2 assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
615 dl 1.3 }
616    
617     /**
618     * forEachValueInParallel traverses all values
619     */
620     public void testForEachValueInParallel() {
621     LongAdder adder = new LongAdder();
622     ConcurrentHashMap<Long, Long> m = longMap();
623 dl 1.9 m.forEachValue(1L, (Long x) -> adder.add(x.longValue()));
624 dl 1.2 assertEquals(adder.sum(), SIZE * (SIZE - 1));
625 dl 1.3 }
626    
627     /**
628     * forEachInParallel traverses all mappings
629     */
630     public void testForEachInParallel() {
631     LongAdder adder = new LongAdder();
632     ConcurrentHashMap<Long, Long> m = longMap();
633 dl 1.9 m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
634 dl 1.2 assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
635 dl 1.3 }
636    
637     /**
638     * forEachEntryInParallel traverses all entries
639     */
640     public void testForEachEntryInParallel() {
641     LongAdder adder = new LongAdder();
642     ConcurrentHashMap<Long, Long> m = longMap();
643 dl 1.9 m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
644 dl 1.2 assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
645     }
646    
647     /**
648 dl 1.3 * Mapped forEachKeySequentially traverses the given
649     * transformations of all keys
650 dl 1.2 */
651 dl 1.3 public void testMappedForEachKeySequentially() {
652 dl 1.2 LongAdder adder = new LongAdder();
653     ConcurrentHashMap<Long, Long> m = longMap();
654 dl 1.9 m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
655 dl 1.2 (Long x) -> adder.add(x.longValue()));
656     assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
657 dl 1.3 }
658    
659     /**
660     * Mapped forEachValueSequentially traverses the given
661     * transformations of all values
662     */
663     public void testMappedForEachValueSequentially() {
664     LongAdder adder = new LongAdder();
665     ConcurrentHashMap<Long, Long> m = longMap();
666 dl 1.9 m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
667 dl 1.2 (Long x) -> adder.add(x.longValue()));
668     assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
669 dl 1.3 }
670    
671     /**
672     * Mapped forEachSequentially traverses the given
673     * transformations of all mappings
674     */
675     public void testMappedForEachSequentially() {
676     LongAdder adder = new LongAdder();
677     ConcurrentHashMap<Long, Long> m = longMap();
678 dl 1.9 m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
679 dl 1.2 (Long x) -> adder.add(x.longValue()));
680     assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
681 dl 1.3 }
682    
683     /**
684     * Mapped forEachEntrySequentially traverses the given
685     * transformations of all entries
686     */
687     public void testMappedForEachEntrySequentially() {
688     LongAdder adder = new LongAdder();
689     ConcurrentHashMap<Long, Long> m = longMap();
690 dl 1.9 m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
691 dl 1.2 (Long x) -> adder.add(x.longValue()));
692     assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
693 dl 1.3 }
694    
695     /**
696     * Mapped forEachKeyInParallel traverses the given
697     * transformations of all keys
698     */
699     public void testMappedForEachKeyInParallel() {
700     LongAdder adder = new LongAdder();
701     ConcurrentHashMap<Long, Long> m = longMap();
702 dl 1.9 m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
703 dl 1.2 (Long x) -> adder.add(x.longValue()));
704     assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
705 dl 1.3 }
706    
707     /**
708     * Mapped forEachValueInParallel traverses the given
709     * transformations of all values
710     */
711     public void testMappedForEachValueInParallel() {
712     LongAdder adder = new LongAdder();
713     ConcurrentHashMap<Long, Long> m = longMap();
714 dl 1.9 m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
715 dl 1.2 (Long x) -> adder.add(x.longValue()));
716     assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
717 dl 1.3 }
718    
719     /**
720     * Mapped forEachInParallel traverses the given
721     * transformations of all mappings
722     */
723     public void testMappedForEachInParallel() {
724     LongAdder adder = new LongAdder();
725     ConcurrentHashMap<Long, Long> m = longMap();
726 dl 1.9 m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
727 dl 1.2 (Long x) -> adder.add(x.longValue()));
728     assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
729 dl 1.3 }
730    
731     /**
732     * Mapped forEachEntryInParallel traverses the given
733     * transformations of all entries
734     */
735     public void testMappedForEachEntryInParallel() {
736     LongAdder adder = new LongAdder();
737     ConcurrentHashMap<Long, Long> m = longMap();
738 dl 1.9 m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
739 dl 1.2 (Long x) -> adder.add(x.longValue()));
740     assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
741     }
742    
743 dl 1.3 /**
744     * reduceKeysSequentially accumulates across all keys,
745     */
746     public void testReduceKeysSequentially() {
747     ConcurrentHashMap<Long, Long> m = longMap();
748     Long r;
749 dl 1.9 r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
750 dl 1.3 assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
751     }
752    
753 dl 1.2 /**
754 dl 1.3 * reduceValuesSequentially accumulates across all values
755 dl 1.2 */
756 dl 1.3 public void testReduceValuesSequentially() {
757 dl 1.2 ConcurrentHashMap<Long, Long> m = longMap();
758     Long r;
759 dl 1.9 r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
760 dl 1.2 assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
761 dl 1.3 }
762    
763     /**
764     * reduceEntriesSequentially accumulates across all entries
765     */
766     public void testReduceEntriesSequentially() {
767     ConcurrentHashMap<Long, Long> m = longMap();
768     Map.Entry<Long,Long> r;
769 dl 1.9 r = m.reduceEntries(Long.MAX_VALUE, new AddKeys());
770 dl 1.3 assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
771     }
772    
773     /**
774     * reduceKeysInParallel accumulates across all keys
775     */
776     public void testReduceKeysInParallel() {
777     ConcurrentHashMap<Long, Long> m = longMap();
778     Long r;
779 dl 1.9 r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
780 dl 1.3 assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
781     }
782    
783     /**
784     * reduceValuesInParallel accumulates across all values
785     */
786     public void testReduceValuesInParallel() {
787     ConcurrentHashMap<Long, Long> m = longMap();
788     Long r;
789 dl 1.9 r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
790 dl 1.2 assertEquals((long)r, (long)SIZE * (SIZE - 1));
791 dl 1.3 }
792    
793     /**
794     * reduceEntriesInParallel accumulate across all entries
795     */
796     public void testReduceEntriesInParallel() {
797     ConcurrentHashMap<Long, Long> m = longMap();
798     Map.Entry<Long,Long> r;
799 dl 1.9 r = m.reduceEntries(1L, new AddKeys());
800 dl 1.3 assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
801     }
802    
803 jsr166 1.7 /**
804 dl 1.3 * Mapped reduceKeysSequentially accumulates mapped keys
805     */
806     public void testMapReduceKeysSequentially() {
807     ConcurrentHashMap<Long, Long> m = longMap();
808 dl 1.9 Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
809 dl 1.3 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
810     assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
811     }
812    
813 jsr166 1.7 /**
814 dl 1.3 * Mapped reduceValuesSequentially accumulates mapped values
815     */
816     public void testMapReduceValuesSequentially() {
817     ConcurrentHashMap<Long, Long> m = longMap();
818 dl 1.9 Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
819 dl 1.3 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
820     assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
821     }
822    
823     /**
824     * reduceSequentially accumulates across all transformed mappings
825     */
826     public void testMappedReduceSequentially() {
827     ConcurrentHashMap<Long, Long> m = longMap();
828 dl 1.9 Long r = m.reduce(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
829 dl 1.2 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
830 jsr166 1.4
831 dl 1.2 assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
832 dl 1.3 }
833    
834 jsr166 1.7 /**
835 dl 1.3 * Mapped reduceKeysInParallel, accumulates mapped keys
836     */
837     public void testMapReduceKeysInParallel() {
838     ConcurrentHashMap<Long, Long> m = longMap();
839 dl 1.9 Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
840 dl 1.3 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
841     assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
842     }
843    
844 jsr166 1.7 /**
845 dl 1.3 * Mapped reduceValuesInParallel accumulates mapped values
846     */
847     public void testMapReduceValuesInParallel() {
848     ConcurrentHashMap<Long, Long> m = longMap();
849 dl 1.9 Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
850 dl 1.3 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
851     assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
852     }
853 dl 1.2
854 dl 1.3 /**
855     * reduceInParallel accumulate across all transformed mappings
856     */
857     public void testMappedReduceInParallel() {
858     ConcurrentHashMap<Long, Long> m = longMap();
859     Long r;
860 dl 1.9 r = m.reduce(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
861 dl 1.2 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
862     assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
863     }
864    
865 jsr166 1.7 /**
866 dl 1.3 * reduceKeysToLongSequentially accumulates mapped keys
867 dl 1.2 */
868 dl 1.3 public void testReduceKeysToLongSequentially() {
869 dl 1.2 ConcurrentHashMap<Long, Long> m = longMap();
870 dl 1.9 long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
871 dl 1.2 assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
872 dl 1.3 }
873 dl 1.2
874 jsr166 1.7 /**
875 dl 1.3 * reduceKeysToIntSequentially accumulates mapped keys
876     */
877     public void testReduceKeysToIntSequentially() {
878     ConcurrentHashMap<Long, Long> m = longMap();
879 dl 1.9 int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
880 jsr166 1.5 assertEquals(ir, SIZE * (SIZE - 1) / 2);
881 dl 1.3 }
882    
883 jsr166 1.7 /**
884 dl 1.3 * reduceKeysToDoubleSequentially accumulates mapped keys
885     */
886     public void testReduceKeysToDoubleSequentially() {
887     ConcurrentHashMap<Long, Long> m = longMap();
888 dl 1.9 double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
889 dl 1.2 assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
890 dl 1.3 }
891 dl 1.2
892 jsr166 1.7 /**
893 dl 1.3 * reduceValuesToLongSequentially accumulates mapped values
894     */
895     public void testReduceValuesToLongSequentially() {
896     ConcurrentHashMap<Long, Long> m = longMap();
897 dl 1.9 long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
898 dl 1.2 assertEquals(lr, (long)SIZE * (SIZE - 1));
899 dl 1.3 }
900    
901 jsr166 1.7 /**
902 dl 1.3 * reduceValuesToIntSequentially accumulates mapped values
903     */
904     public void testReduceValuesToIntSequentially() {
905     ConcurrentHashMap<Long, Long> m = longMap();
906 dl 1.9 int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
907 jsr166 1.5 assertEquals(ir, SIZE * (SIZE - 1));
908 dl 1.3 }
909 dl 1.2
910 jsr166 1.7 /**
911 dl 1.3 * reduceValuesToDoubleSequentially accumulates mapped values
912     */
913     public void testReduceValuesToDoubleSequentially() {
914     ConcurrentHashMap<Long, Long> m = longMap();
915 dl 1.9 double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
916 dl 1.2 assertEquals(dr, (double)SIZE * (SIZE - 1));
917 dl 1.3 }
918 dl 1.2
919 jsr166 1.7 /**
920 dl 1.3 * reduceKeysToLongInParallel accumulates mapped keys
921     */
922     public void testReduceKeysToLongInParallel() {
923     ConcurrentHashMap<Long, Long> m = longMap();
924 dl 1.9 long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
925 dl 1.2 assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
926 dl 1.3 }
927    
928 jsr166 1.7 /**
929 dl 1.3 * reduceKeysToIntInParallel accumulates mapped keys
930     */
931     public void testReduceKeysToIntInParallel() {
932     ConcurrentHashMap<Long, Long> m = longMap();
933 dl 1.9 int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
934 jsr166 1.5 assertEquals(ir, SIZE * (SIZE - 1) / 2);
935 dl 1.3 }
936    
937 jsr166 1.7 /**
938 dl 1.3 * reduceKeysToDoubleInParallel accumulates mapped values
939     */
940     public void testReduceKeysToDoubleInParallel() {
941     ConcurrentHashMap<Long, Long> m = longMap();
942 dl 1.9 double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
943 dl 1.2 assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
944 dl 1.3 }
945 dl 1.2
946 jsr166 1.7 /**
947 dl 1.3 * reduceValuesToLongInParallel accumulates mapped values
948     */
949     public void testReduceValuesToLongInParallel() {
950     ConcurrentHashMap<Long, Long> m = longMap();
951 dl 1.9 long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
952 dl 1.2 assertEquals(lr, (long)SIZE * (SIZE - 1));
953 dl 1.3 }
954    
955 jsr166 1.7 /**
956 dl 1.3 * reduceValuesToIntInParallel accumulates mapped values
957     */
958     public void testReduceValuesToIntInParallel() {
959     ConcurrentHashMap<Long, Long> m = longMap();
960 dl 1.9 int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
961 jsr166 1.5 assertEquals(ir, SIZE * (SIZE - 1));
962 dl 1.3 }
963    
964 jsr166 1.7 /**
965 dl 1.3 * reduceValuesToDoubleInParallel accumulates mapped values
966     */
967     public void testReduceValuesToDoubleInParallel() {
968     ConcurrentHashMap<Long, Long> m = longMap();
969 dl 1.9 double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
970 dl 1.2 assertEquals(dr, (double)SIZE * (SIZE - 1));
971     }
972    
973     /**
974 dl 1.3 * searchKeysSequentially returns a non-null result of search
975     * function, or null if none
976 dl 1.2 */
977 dl 1.3 public void testSearchKeysSequentially() {
978 dl 1.2 ConcurrentHashMap<Long, Long> m = longMap();
979     Long r;
980 dl 1.9 r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
981 dl 1.2 assertEquals((long)r, (long)(SIZE/2));
982 dl 1.9 r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
983 dl 1.3 assertNull(r);
984     }
985    
986     /**
987     * searchValuesSequentially returns a non-null result of search
988     * function, or null if none
989     */
990     public void testSearchValuesSequentially() {
991     ConcurrentHashMap<Long, Long> m = longMap();
992     Long r;
993 dl 1.9 r = m.searchValues(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.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
996 dl 1.3 assertNull(r);
997     }
998    
999     /**
1000     * searchSequentially returns a non-null result of search
1001     * function, or null if none
1002     */
1003     public void testSearchSequentially() {
1004     ConcurrentHashMap<Long, Long> m = longMap();
1005     Long r;
1006 dl 1.9 r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1007 dl 1.2 assertEquals((long)r, (long)(SIZE/2));
1008 dl 1.9 r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1009 dl 1.3 assertNull(r);
1010     }
1011    
1012     /**
1013     * searchEntriesSequentially returns a non-null result of search
1014     * function, or null if none
1015     */
1016     public void testSearchEntriesSequentially() {
1017     ConcurrentHashMap<Long, Long> m = longMap();
1018     Long r;
1019 dl 1.9 r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1020 dl 1.2 assertEquals((long)r, (long)(SIZE/2));
1021 dl 1.9 r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1022 dl 1.2 assertNull(r);
1023 dl 1.3 }
1024 dl 1.2
1025 dl 1.3 /**
1026     * searchKeysInParallel returns a non-null result of search
1027     * function, or null if none
1028     */
1029     public void testSearchKeysInParallel() {
1030     ConcurrentHashMap<Long, Long> m = longMap();
1031     Long r;
1032 dl 1.9 r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1033 dl 1.2 assertEquals((long)r, (long)(SIZE/2));
1034 dl 1.9 r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null);
1035 dl 1.3 assertNull(r);
1036     }
1037    
1038     /**
1039     * searchValuesInParallel returns a non-null result of search
1040     * function, or null if none
1041     */
1042     public void testSearchValuesInParallel() {
1043     ConcurrentHashMap<Long, Long> m = longMap();
1044     Long r;
1045 dl 1.9 r = m.searchValues(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.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null);
1048 dl 1.3 assertNull(r);
1049     }
1050    
1051     /**
1052     * searchInParallel returns a non-null result of search function,
1053     * or null if none
1054     */
1055     public void testSearchInParallel() {
1056     ConcurrentHashMap<Long, Long> m = longMap();
1057     Long r;
1058 dl 1.9 r = m.search(1L, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1059 dl 1.2 assertEquals((long)r, (long)(SIZE/2));
1060 dl 1.9 r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1061 dl 1.3 assertNull(r);
1062     }
1063    
1064     /**
1065     * searchEntriesInParallel returns a non-null result of search
1066     * function, or null if none
1067     */
1068     public void testSearchEntriesInParallel() {
1069     ConcurrentHashMap<Long, Long> m = longMap();
1070     Long r;
1071 dl 1.9 r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1072 dl 1.2 assertEquals((long)r, (long)(SIZE/2));
1073 dl 1.9 r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1074 dl 1.2 assertNull(r);
1075     }
1076    
1077 dl 1.1 }