ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.31
Committed: Wed Aug 24 22:22:39 2016 UTC (7 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +0 -1 lines
Log Message:
fix imports

File Contents

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