ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.11
Committed: Sun Jul 14 16:35:48 2013 UTC (10 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +0 -4 lines
Log Message:
whitespace

File Contents

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