ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentHashMap8Test.java (file contents):
Revision 1.7 by jsr166, Thu Apr 11 19:54:13 2013 UTC vs.
Revision 1.18 by jsr166, Wed Dec 31 19:05:42 2014 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
8 < import java.util.*;
9 < import java.util.function.*;
10 < import java.util.concurrent.atomic.LongAdder;
7 > 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.Collections;
15 > import java.util.Iterator;
16 > import java.util.Map;
17 > import java.util.NoSuchElementException;
18 > import java.util.Set;
19 > import java.util.Spliterator;
20 > import java.util.Vector;
21   import java.util.concurrent.ConcurrentHashMap;
22 + import java.util.concurrent.atomic.LongAdder;
23 + import java.util.function.BiFunction;
24 +
25 + import junit.framework.Test;
26 + import junit.framework.TestSuite;
27  
28   public class ConcurrentHashMap8Test extends JSR166TestCase {
29      public static void main(String[] args) {
# Line 34 | Line 49 | public class ConcurrentHashMap8Test exte
49          return map;
50      }
51  
37    // classes for testing Comparable fallbacks
38    static class BI implements Comparable<BI> {
39        private final int value;
40        BI(int value) { this.value = value; }
41        public int compareTo(BI other) {
42            return Integer.compare(value, other.value);
43        }
44        public boolean equals(Object x) {
45            return (x instanceof BI) && ((BI)x).value == value;
46        }
47        public int hashCode() { return 42; }
48    }
49    static class CI extends BI { CI(int value) { super(value); } }
50    static class DI extends BI { DI(int value) { super(value); } }
51
52    static class BS implements Comparable<BS> {
53        private final String value;
54        BS(String value) { this.value = value; }
55        public int compareTo(BS other) {
56            return value.compareTo(other.value);
57        }
58        public boolean equals(Object x) {
59            return (x instanceof BS) && value.equals(((BS)x).value);
60        }
61        public int hashCode() { return 42; }
62    }
63
64    static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
65        implements Comparable<LexicographicList<E>> {
66        LexicographicList(Collection<E> c) { super(c); }
67        LexicographicList(E e) { super(Collections.singleton(e)); }
68        public int compareTo(LexicographicList<E> other) {
69            int common = Math.min(size(), other.size());
70            int r = 0;
71            for (int i = 0; i < common; i++) {
72                if ((r = get(i).compareTo(other.get(i))) != 0)
73                    break;
74            }
75            if (r == 0)
76                r = Integer.compare(size(), other.size());
77            return r;
78        }
79        private static final long serialVersionUID = 0;
80    }
81
82    /**
83     * Inserted elements that are subclasses of the same Comparable
84     * class are found.
85     */
86    public void testComparableFamily() {
87        ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
88        for (int i = 0; i < 1000; i++) {
89            assertTrue(m.put(new CI(i), true) == null);
90        }
91        for (int i = 0; i < 1000; i++) {
92            assertTrue(m.containsKey(new CI(i)));
93            assertTrue(m.containsKey(new DI(i)));
94        }
95    }
96
97    /**
98     * Elements of classes with erased generic type parameters based
99     * on Comparable can be inserted and found.
100     */
101    public void testGenericComparable() {
102        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
103        for (int i = 0; i < 1000; i++) {
104            BI bi = new BI(i);
105            BS bs = new BS(String.valueOf(i));
106            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
107            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
108            assertTrue(m.putIfAbsent(bis, true) == null);
109            assertTrue(m.containsKey(bis));
110            if (m.putIfAbsent(bss, true) == null)
111                assertTrue(m.containsKey(bss));
112            assertTrue(m.containsKey(bis));
113        }
114        for (int i = 0; i < 1000; i++) {
115            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
116        }
117    }
118
119    /**
120     * Elements of non-comparable classes equal to those of classes
121     * with erased generic type parameters based on Comparable can be
122     * inserted and found.
123     */
124    public void testGenericComparable2() {
125        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
126        for (int i = 0; i < 1000; i++) {
127            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
128        }
129
130        for (int i = 0; i < 1000; i++) {
131            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
132            assertTrue(m.containsKey(bis));
133        }
134    }
135
52      /**
53       * getOrDefault returns value if present, else default
54       */
# Line 152 | Line 68 | public class ConcurrentHashMap8Test exte
68      }
69  
70      /**
71 <     * computeIfAbsent does not replace  if the key is already present
71 >     * computeIfAbsent does not replace if the key is already present
72       */
73      public void testComputeIfAbsent2() {
74          ConcurrentHashMap map = map5();
# Line 169 | Line 85 | public class ConcurrentHashMap8Test exte
85      }
86  
87      /**
88 <     * computeIfPresent does not replace  if the key is already present
88 >     * computeIfPresent does not replace if the key is already present
89       */
90      public void testComputeIfPresent() {
91          ConcurrentHashMap map = map5();
# Line 186 | Line 102 | public class ConcurrentHashMap8Test exte
102      }
103  
104      /**
105 <     * compute does not replace  if the function returns null
105 >     * compute does not replace if the function returns null
106       */
107      public void testCompute() {
108          ConcurrentHashMap map = map5();
# Line 264 | Line 180 | public class ConcurrentHashMap8Test exte
180          return a;
181      }
182  
183 +    /*
184 +     * replaceAll replaces all matching values.
185 +     */
186 +    public void testReplaceAll() {
187 +        ConcurrentHashMap<Integer, String> map = map5();
188 +        map.replaceAll((x, y) -> {return x > 3 ? "Z" : y;});
189 +        assertEquals("A", map.get(one));
190 +        assertEquals("B", map.get(two));
191 +        assertEquals("C", map.get(three));
192 +        assertEquals("Z", map.get(four));
193 +        assertEquals("Z", map.get(five));
194 +    }
195 +
196      /**
197       * Default-constructed set is empty
198       */
# Line 273 | Line 202 | public class ConcurrentHashMap8Test exte
202      }
203  
204      /**
205 +     * keySet.add adds the key with the established value to the map;
206 +     * remove removes it.
207 +     */
208 +    public void testKeySetAddRemove() {
209 +        ConcurrentHashMap map = map5();
210 +        Set set1 = map.keySet();
211 +        Set set2 = map.keySet(true);
212 +        set2.add(six);
213 +        assertTrue(((ConcurrentHashMap.KeySetView)set2).getMap() == map);
214 +        assertTrue(((ConcurrentHashMap.KeySetView)set1).getMap() == map);
215 +        assertEquals(set2.size(), map.size());
216 +        assertEquals(set1.size(), map.size());
217 +        assertTrue((Boolean)map.get(six));
218 +        assertTrue(set1.contains(six));
219 +        assertTrue(set2.contains(six));
220 +        set2.remove(six);
221 +        assertNull(map.get(six));
222 +        assertFalse(set1.contains(six));
223 +        assertFalse(set2.contains(six));
224 +    }
225 +
226 +
227 +    /**
228       * keySet.addAll adds each element from the given collection
229       */
230      public void testAddAll() {
# Line 318 | Line 270 | public class ConcurrentHashMap8Test exte
270      }
271  
272      /**
273 +     * keySet.add throws UnsupportedOperationException if no default
274 +     * mapped value
275 +     */
276 +    public void testAdd4() {
277 +        Set full = map5().keySet();
278 +        try {
279 +            full.add(three);
280 +            shouldThrow();
281 +        } catch (UnsupportedOperationException e){}
282 +    }
283 +
284 +    /**
285 +     * keySet.add throws NullPointerException if the specified key is
286 +     * null
287 +     */
288 +    public void testAdd5() {
289 +        Set full = populatedSet(3);
290 +        try {
291 +            full.add(null);
292 +            shouldThrow();
293 +        } catch (NullPointerException e){}
294 +    }
295 +
296 +    /**
297 +     * KeySetView.getMappedValue returns the map's mapped value
298 +     */
299 +    public void testGetMappedValue() {
300 +        ConcurrentHashMap map = map5();
301 +        assertNull(map.keySet().getMappedValue());
302 +        try {
303 +            map.keySet(null);
304 +            shouldThrow();
305 +        } catch (NullPointerException e) {}
306 +        ConcurrentHashMap.KeySetView set = map.keySet(one);
307 +        set.add(one);
308 +        set.add(six);
309 +        set.add(seven);
310 +        assertTrue(set.getMappedValue() == one);
311 +        assertTrue(map.get(one) != one);
312 +        assertTrue(map.get(six) == one);
313 +        assertTrue(map.get(seven) == one);
314 +    }
315 +
316 +    void checkSpliteratorCharacteristics(Spliterator<?> sp,
317 +                                         int requiredCharacteristics) {
318 +        assertEquals(requiredCharacteristics,
319 +                     requiredCharacteristics & sp.characteristics());
320 +    }
321 +
322 +    /**
323 +     * KeySetView.spliterator returns spliterator over the elements in this set
324 +     */
325 +    public void testKeySetSpliterator() {
326 +        LongAdder adder = new LongAdder();
327 +        ConcurrentHashMap map = map5();
328 +        Set set = map.keySet();
329 +        Spliterator<Integer> sp = set.spliterator();
330 +        checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
331 +        assertEquals(sp.estimateSize(), map.size());
332 +        Spliterator<Integer> sp2 = sp.trySplit();
333 +        sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
334 +        long v = adder.sumThenReset();
335 +        sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
336 +        long v2 = adder.sum();
337 +        assertEquals(v + v2, 15);
338 +    }
339 +
340 +    /**
341       * keyset.clear removes all elements from the set
342       */
343      public void testClear() {
# Line 530 | Line 550 | public class ConcurrentHashMap8Test exte
550          Set x = populatedSet(size);
551          Set y = serialClone(x);
552  
553 <        assertTrue(x != y);
553 >        assertNotSame(x, y);
554          assertEquals(x.size(), y.size());
535        assertEquals(x.toString(), y.toString());
536        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
555          assertEquals(x, y);
556          assertEquals(y, x);
557      }
558  
541
559      static final int SIZE = 10000;
560      static ConcurrentHashMap<Long, Long> longMap;
561  
# Line 566 | Line 583 | public class ConcurrentHashMap8Test exte
583      public void testForEachKeySequentially() {
584          LongAdder adder = new LongAdder();
585          ConcurrentHashMap<Long, Long> m = longMap();
586 <        m.forEachKeySequentially((Long x) -> adder.add(x.longValue()));
586 >        m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
587          assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
588      }
589  
# Line 576 | Line 593 | public class ConcurrentHashMap8Test exte
593      public void testForEachValueSequentially() {
594          LongAdder adder = new LongAdder();
595          ConcurrentHashMap<Long, Long> m = longMap();
596 <        m.forEachValueSequentially((Long x) -> adder.add(x.longValue()));
596 >        m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
597          assertEquals(adder.sum(), SIZE * (SIZE - 1));
598      }
599  
# Line 586 | Line 603 | public class ConcurrentHashMap8Test exte
603      public void testForEachSequentially() {
604          LongAdder adder = new LongAdder();
605          ConcurrentHashMap<Long, Long> m = longMap();
606 <        m.forEachSequentially((Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
606 >        m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
607          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
608      }
609  
# Line 596 | Line 613 | public class ConcurrentHashMap8Test exte
613      public void testForEachEntrySequentially() {
614          LongAdder adder = new LongAdder();
615          ConcurrentHashMap<Long, Long> m = longMap();
616 <        m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
616 >        m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
617          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
618      }
619  
# Line 606 | Line 623 | public class ConcurrentHashMap8Test exte
623      public void testForEachKeyInParallel() {
624          LongAdder adder = new LongAdder();
625          ConcurrentHashMap<Long, Long> m = longMap();
626 <        m.forEachKeyInParallel((Long x) -> adder.add(x.longValue()));
626 >        m.forEachKey(1L, (Long x) -> adder.add(x.longValue()));
627          assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
628      }
629  
# Line 616 | Line 633 | public class ConcurrentHashMap8Test exte
633      public void testForEachValueInParallel() {
634          LongAdder adder = new LongAdder();
635          ConcurrentHashMap<Long, Long> m = longMap();
636 <        m.forEachValueInParallel((Long x) -> adder.add(x.longValue()));
636 >        m.forEachValue(1L, (Long x) -> adder.add(x.longValue()));
637          assertEquals(adder.sum(), SIZE * (SIZE - 1));
638      }
639  
# Line 626 | Line 643 | public class ConcurrentHashMap8Test exte
643      public void testForEachInParallel() {
644          LongAdder adder = new LongAdder();
645          ConcurrentHashMap<Long, Long> m = longMap();
646 <        m.forEachInParallel((Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
646 >        m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
647          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
648      }
649  
# Line 636 | Line 653 | public class ConcurrentHashMap8Test exte
653      public void testForEachEntryInParallel() {
654          LongAdder adder = new LongAdder();
655          ConcurrentHashMap<Long, Long> m = longMap();
656 <        m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
656 >        m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
657          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
658      }
659  
# Line 647 | Line 664 | public class ConcurrentHashMap8Test exte
664      public void testMappedForEachKeySequentially() {
665          LongAdder adder = new LongAdder();
666          ConcurrentHashMap<Long, Long> m = longMap();
667 <        m.forEachKeySequentially((Long x) -> Long.valueOf(4 * x.longValue()),
667 >        m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
668                                   (Long x) -> adder.add(x.longValue()));
669          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
670      }
# Line 659 | Line 676 | public class ConcurrentHashMap8Test exte
676      public void testMappedForEachValueSequentially() {
677          LongAdder adder = new LongAdder();
678          ConcurrentHashMap<Long, Long> m = longMap();
679 <        m.forEachValueSequentially((Long x) -> Long.valueOf(4 * x.longValue()),
679 >        m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
680                                     (Long x) -> adder.add(x.longValue()));
681          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
682      }
# Line 671 | Line 688 | public class ConcurrentHashMap8Test exte
688      public void testMappedForEachSequentially() {
689          LongAdder adder = new LongAdder();
690          ConcurrentHashMap<Long, Long> m = longMap();
691 <        m.forEachSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
691 >        m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
692                                (Long x) -> adder.add(x.longValue()));
693          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
694      }
# Line 683 | Line 700 | public class ConcurrentHashMap8Test exte
700      public void testMappedForEachEntrySequentially() {
701          LongAdder adder = new LongAdder();
702          ConcurrentHashMap<Long, Long> m = longMap();
703 <        m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
703 >        m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
704                                     (Long x) -> adder.add(x.longValue()));
705          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
706      }
# Line 695 | Line 712 | public class ConcurrentHashMap8Test exte
712      public void testMappedForEachKeyInParallel() {
713          LongAdder adder = new LongAdder();
714          ConcurrentHashMap<Long, Long> m = longMap();
715 <        m.forEachKeyInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
715 >        m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
716                                 (Long x) -> adder.add(x.longValue()));
717          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
718      }
# Line 707 | Line 724 | public class ConcurrentHashMap8Test exte
724      public void testMappedForEachValueInParallel() {
725          LongAdder adder = new LongAdder();
726          ConcurrentHashMap<Long, Long> m = longMap();
727 <        m.forEachValueInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
727 >        m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
728                                   (Long x) -> adder.add(x.longValue()));
729          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
730      }
# Line 719 | Line 736 | public class ConcurrentHashMap8Test exte
736      public void testMappedForEachInParallel() {
737          LongAdder adder = new LongAdder();
738          ConcurrentHashMap<Long, Long> m = longMap();
739 <        m.forEachInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
739 >        m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
740                              (Long x) -> adder.add(x.longValue()));
741          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
742      }
# Line 731 | Line 748 | public class ConcurrentHashMap8Test exte
748      public void testMappedForEachEntryInParallel() {
749          LongAdder adder = new LongAdder();
750          ConcurrentHashMap<Long, Long> m = longMap();
751 <        m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
751 >        m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
752                                   (Long x) -> adder.add(x.longValue()));
753          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
754      }
755  
739
756      /**
757       * reduceKeysSequentially accumulates across all keys,
758       */
759      public void testReduceKeysSequentially() {
760          ConcurrentHashMap<Long, Long> m = longMap();
761          Long r;
762 <        r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
762 >        r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
763          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
764      }
765  
# Line 753 | Line 769 | public class ConcurrentHashMap8Test exte
769      public void testReduceValuesSequentially() {
770          ConcurrentHashMap<Long, Long> m = longMap();
771          Long r;
772 <        r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
772 >        r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
773          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
774      }
775  
760
776      /**
777       * reduceEntriesSequentially accumulates across all entries
778       */
779      public void testReduceEntriesSequentially() {
780          ConcurrentHashMap<Long, Long> m = longMap();
781          Map.Entry<Long,Long> r;
782 <        r = m.reduceEntriesSequentially(new AddKeys());
782 >        r = m.reduceEntries(Long.MAX_VALUE, new AddKeys());
783          assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
784      }
785  
# Line 774 | Line 789 | public class ConcurrentHashMap8Test exte
789      public void testReduceKeysInParallel() {
790          ConcurrentHashMap<Long, Long> m = longMap();
791          Long r;
792 <        r = m.reduceKeysInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
792 >        r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
793          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
794      }
795  
# Line 784 | Line 799 | public class ConcurrentHashMap8Test exte
799      public void testReduceValuesInParallel() {
800          ConcurrentHashMap<Long, Long> m = longMap();
801          Long r;
802 <        r = m.reduceValuesInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
802 >        r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
803          assertEquals((long)r, (long)SIZE * (SIZE - 1));
804      }
805  
# Line 794 | Line 809 | public class ConcurrentHashMap8Test exte
809      public void testReduceEntriesInParallel() {
810          ConcurrentHashMap<Long, Long> m = longMap();
811          Map.Entry<Long,Long> r;
812 <        r = m.reduceEntriesInParallel(new AddKeys());
812 >        r = m.reduceEntries(1L, new AddKeys());
813          assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
814      }
815  
# Line 803 | Line 818 | public class ConcurrentHashMap8Test exte
818       */
819      public void testMapReduceKeysSequentially() {
820          ConcurrentHashMap<Long, Long> m = longMap();
821 <        Long r = m.reduceKeysSequentially((Long x) -> Long.valueOf(4 * x.longValue()),
821 >        Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
822                                       (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
823          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
824      }
# Line 813 | Line 828 | public class ConcurrentHashMap8Test exte
828       */
829      public void testMapReduceValuesSequentially() {
830          ConcurrentHashMap<Long, Long> m = longMap();
831 <        Long r = m.reduceValuesSequentially((Long x) -> Long.valueOf(4 * x.longValue()),
831 >        Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
832                                         (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
833          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
834      }
# Line 823 | Line 838 | public class ConcurrentHashMap8Test exte
838       */
839      public void testMappedReduceSequentially() {
840          ConcurrentHashMap<Long, Long> m = longMap();
841 <        Long r = m.reduceSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
841 >        Long r = m.reduce(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
842                                   (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
843  
844          assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
# Line 834 | Line 849 | public class ConcurrentHashMap8Test exte
849       */
850      public void testMapReduceKeysInParallel() {
851          ConcurrentHashMap<Long, Long> m = longMap();
852 <        Long r = m.reduceKeysInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
852 >        Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
853                                     (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
854          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
855      }
# Line 844 | Line 859 | public class ConcurrentHashMap8Test exte
859       */
860      public void testMapReduceValuesInParallel() {
861          ConcurrentHashMap<Long, Long> m = longMap();
862 <        Long r = m.reduceValuesInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
862 >        Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
863                                       (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
864          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
865      }
# Line 855 | Line 870 | public class ConcurrentHashMap8Test exte
870      public void testMappedReduceInParallel() {
871          ConcurrentHashMap<Long, Long> m = longMap();
872          Long r;
873 <        r = m.reduceInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
873 >        r = m.reduce(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
874                                 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
875          assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
876      }
877  
863
878      /**
879       * reduceKeysToLongSequentially accumulates mapped keys
880       */
881      public void testReduceKeysToLongSequentially() {
882          ConcurrentHashMap<Long, Long> m = longMap();
883 <        long lr = m.reduceKeysToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum);
883 >        long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
884          assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
885      }
886  
# Line 875 | Line 889 | public class ConcurrentHashMap8Test exte
889       */
890      public void testReduceKeysToIntSequentially() {
891          ConcurrentHashMap<Long, Long> m = longMap();
892 <        int ir = m.reduceKeysToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum);
892 >        int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
893          assertEquals(ir, SIZE * (SIZE - 1) / 2);
894      }
895  
# Line 884 | Line 898 | public class ConcurrentHashMap8Test exte
898       */
899      public void testReduceKeysToDoubleSequentially() {
900          ConcurrentHashMap<Long, Long> m = longMap();
901 <        double dr = m.reduceKeysToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum);
901 >        double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
902          assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
903      }
904  
# Line 893 | Line 907 | public class ConcurrentHashMap8Test exte
907       */
908      public void testReduceValuesToLongSequentially() {
909          ConcurrentHashMap<Long, Long> m = longMap();
910 <        long lr = m.reduceValuesToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum);
910 >        long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
911          assertEquals(lr, (long)SIZE * (SIZE - 1));
912      }
913  
# Line 902 | Line 916 | public class ConcurrentHashMap8Test exte
916       */
917      public void testReduceValuesToIntSequentially() {
918          ConcurrentHashMap<Long, Long> m = longMap();
919 <        int ir = m.reduceValuesToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum);
919 >        int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
920          assertEquals(ir, SIZE * (SIZE - 1));
921      }
922  
# Line 911 | Line 925 | public class ConcurrentHashMap8Test exte
925       */
926      public void testReduceValuesToDoubleSequentially() {
927          ConcurrentHashMap<Long, Long> m = longMap();
928 <        double dr = m.reduceValuesToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum);
928 >        double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
929          assertEquals(dr, (double)SIZE * (SIZE - 1));
930      }
931  
# Line 920 | Line 934 | public class ConcurrentHashMap8Test exte
934       */
935      public void testReduceKeysToLongInParallel() {
936          ConcurrentHashMap<Long, Long> m = longMap();
937 <        long lr = m.reduceKeysToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum);
937 >        long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
938          assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
939      }
940  
# Line 929 | Line 943 | public class ConcurrentHashMap8Test exte
943       */
944      public void testReduceKeysToIntInParallel() {
945          ConcurrentHashMap<Long, Long> m = longMap();
946 <        int ir = m.reduceKeysToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum);
946 >        int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
947          assertEquals(ir, SIZE * (SIZE - 1) / 2);
948      }
949  
# Line 938 | Line 952 | public class ConcurrentHashMap8Test exte
952       */
953      public void testReduceKeysToDoubleInParallel() {
954          ConcurrentHashMap<Long, Long> m = longMap();
955 <        double dr = m.reduceKeysToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum);
955 >        double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
956          assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
957      }
958  
# Line 947 | Line 961 | public class ConcurrentHashMap8Test exte
961       */
962      public void testReduceValuesToLongInParallel() {
963          ConcurrentHashMap<Long, Long> m = longMap();
964 <        long lr = m.reduceValuesToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum);
964 >        long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
965          assertEquals(lr, (long)SIZE * (SIZE - 1));
966      }
967  
# Line 956 | Line 970 | public class ConcurrentHashMap8Test exte
970       */
971      public void testReduceValuesToIntInParallel() {
972          ConcurrentHashMap<Long, Long> m = longMap();
973 <        int ir = m.reduceValuesToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum);
973 >        int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
974          assertEquals(ir, SIZE * (SIZE - 1));
975      }
976  
# Line 965 | Line 979 | public class ConcurrentHashMap8Test exte
979       */
980      public void testReduceValuesToDoubleInParallel() {
981          ConcurrentHashMap<Long, Long> m = longMap();
982 <        double dr = m.reduceValuesToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum);
982 >        double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
983          assertEquals(dr, (double)SIZE * (SIZE - 1));
984      }
985  
# Line 976 | Line 990 | public class ConcurrentHashMap8Test exte
990      public void testSearchKeysSequentially() {
991          ConcurrentHashMap<Long, Long> m = longMap();
992          Long r;
993 <        r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
993 >        r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
994          assertEquals((long)r, (long)(SIZE/2));
995 <        r = m.searchKeysSequentially((Long x) -> x.longValue() < 0L ? x : null);
995 >        r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
996          assertNull(r);
997      }
998  
# Line 989 | Line 1003 | public class ConcurrentHashMap8Test exte
1003      public void testSearchValuesSequentially() {
1004          ConcurrentHashMap<Long, Long> m = longMap();
1005          Long r;
1006 <        r = m.searchValuesSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null);
1006 >        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null);
1007          assertEquals((long)r, (long)(SIZE/2));
1008 <        r = m.searchValuesSequentially((Long x) -> x.longValue() < 0L ? x : null);
1008 >        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
1009          assertNull(r);
1010      }
1011  
# Line 1002 | Line 1016 | public class ConcurrentHashMap8Test exte
1016      public void testSearchSequentially() {
1017          ConcurrentHashMap<Long, Long> m = longMap();
1018          Long r;
1019 <        r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1019 >        r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1020          assertEquals((long)r, (long)(SIZE/2));
1021 <        r = m.searchSequentially((Long x, Long y) -> x.longValue() < 0L ? x : null);
1021 >        r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1022          assertNull(r);
1023      }
1024  
# Line 1015 | Line 1029 | public class ConcurrentHashMap8Test exte
1029      public void testSearchEntriesSequentially() {
1030          ConcurrentHashMap<Long, Long> m = longMap();
1031          Long r;
1032 <        r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1032 >        r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1033          assertEquals((long)r, (long)(SIZE/2));
1034 <        r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1034 >        r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1035          assertNull(r);
1036      }
1037  
# Line 1028 | Line 1042 | public class ConcurrentHashMap8Test exte
1042      public void testSearchKeysInParallel() {
1043          ConcurrentHashMap<Long, Long> m = longMap();
1044          Long r;
1045 <        r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1045 >        r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1046          assertEquals((long)r, (long)(SIZE/2));
1047 <        r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L ? x : null);
1047 >        r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null);
1048          assertNull(r);
1049      }
1050  
# Line 1041 | Line 1055 | public class ConcurrentHashMap8Test exte
1055      public void testSearchValuesInParallel() {
1056          ConcurrentHashMap<Long, Long> m = longMap();
1057          Long r;
1058 <        r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1058 >        r = m.searchValues(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1059          assertEquals((long)r, (long)(SIZE/2));
1060 <        r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L ? x : null);
1060 >        r = m.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null);
1061          assertNull(r);
1062      }
1063  
# Line 1054 | Line 1068 | public class ConcurrentHashMap8Test exte
1068      public void testSearchInParallel() {
1069          ConcurrentHashMap<Long, Long> m = longMap();
1070          Long r;
1071 <        r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1071 >        r = m.search(1L, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1072          assertEquals((long)r, (long)(SIZE/2));
1073 <        r = m.searchInParallel((Long x, Long y) -> x.longValue() < 0L ? x : null);
1073 >        r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1074          assertNull(r);
1075      }
1076  
# Line 1067 | Line 1081 | public class ConcurrentHashMap8Test exte
1081      public void testSearchEntriesInParallel() {
1082          ConcurrentHashMap<Long, Long> m = longMap();
1083          Long r;
1084 <        r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1084 >        r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1085          assertEquals((long)r, (long)(SIZE/2));
1086 <        r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1086 >        r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1087          assertNull(r);
1088      }
1089  
1076    /**
1077     * Invoking task versions of bulk methods has same effect as
1078     * parallel methods
1079     */
1080    public void testForkJoinTasks() {
1081        LongAdder adder = new LongAdder();
1082        ConcurrentHashMap<Long, Long> m = longMap();
1083        ConcurrentHashMap.ForkJoinTasks.forEachKey
1084            (m, (Long x) -> adder.add(x.longValue())).invoke();
1085        assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
1086        adder.reset();
1087        ConcurrentHashMap.ForkJoinTasks.forEachValue
1088            (m, (Long x) -> adder.add(x.longValue())).invoke();
1089        assertEquals(adder.sum(), SIZE * (SIZE - 1));
1090        adder.reset();
1091        ConcurrentHashMap.ForkJoinTasks.forEach
1092            (m, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())).invoke();
1093        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
1094        adder.reset();
1095        ConcurrentHashMap.ForkJoinTasks.forEachEntry
1096            (m,
1097             (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())).invoke();
1098        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
1099        adder.reset();
1100        ConcurrentHashMap.ForkJoinTasks.forEachKey
1101            (m, (Long x) -> Long.valueOf(4 * x.longValue()),
1102             (Long x) -> adder.add(x.longValue())).invoke();
1103        assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
1104        adder.reset();
1105        ConcurrentHashMap.ForkJoinTasks.forEachValue
1106            (m, (Long x) -> Long.valueOf(4 * x.longValue()),
1107             (Long x) -> adder.add(x.longValue())).invoke();
1108        assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
1109        adder.reset();
1110        ConcurrentHashMap.ForkJoinTasks.forEach
1111            (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
1112             (Long x) -> adder.add(x.longValue())).invoke();
1113        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
1114        adder.reset();
1115        ConcurrentHashMap.ForkJoinTasks.forEachEntry
1116            (m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
1117             (Long x) -> adder.add(x.longValue())).invoke();
1118        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
1119        adder.reset();
1120
1121        Long r; long lr; int ir; double dr;
1122        r = ConcurrentHashMap.ForkJoinTasks.reduceKeys
1123            (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke();
1124        assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
1125        r = ConcurrentHashMap.ForkJoinTasks.reduceValues
1126            (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke();
1127        assertEquals((long)r, (long)SIZE * (SIZE - 1));
1128        r = ConcurrentHashMap.ForkJoinTasks.reduce
1129            (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
1130             (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke();
1131        assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
1132        r = ConcurrentHashMap.ForkJoinTasks.reduceEntries
1133            (m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
1134             (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke();
1135        assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
1136        r = ConcurrentHashMap.ForkJoinTasks.reduceKeys
1137            (m, (Long x) -> Long.valueOf(4 * x.longValue()),
1138             (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke();
1139        assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
1140        lr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToLong
1141            (m, (Long x) -> x.longValue(), 0L, Long::sum).invoke();
1142        assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
1143        ir = ConcurrentHashMap.ForkJoinTasks.reduceKeysToInt
1144            (m, (Long x) -> x.intValue(), 0, Integer::sum).invoke();
1145        assertEquals(ir, SIZE * (SIZE - 1) / 2);
1146        dr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToDouble
1147            (m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke();
1148        assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
1149        r = ConcurrentHashMap.ForkJoinTasks.reduceValues
1150            (m, (Long x) -> Long.valueOf(4 * x.longValue()),
1151             (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke();
1152        assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
1153        lr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToLong
1154            (m, (Long x) -> x.longValue(), 0L, Long::sum).invoke();
1155        assertEquals(lr, (long)SIZE * (SIZE - 1));
1156        ir = ConcurrentHashMap.ForkJoinTasks.reduceValuesToInt
1157            (m, (Long x) -> x.intValue(), 0, Integer::sum).invoke();
1158        assertEquals(ir, SIZE * (SIZE - 1));
1159        dr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToDouble
1160            (m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke();
1161        assertEquals(dr, (double)SIZE * (SIZE - 1));
1162        r = ConcurrentHashMap.ForkJoinTasks.searchKeys
1163            (m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke();
1164        assertEquals((long)r, (long)(SIZE/2));
1165        r = ConcurrentHashMap.ForkJoinTasks.searchValues
1166            (m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke();
1167        assertEquals((long)r, (long)(SIZE/2));
1168        r = ConcurrentHashMap.ForkJoinTasks.search
1169            (m, (Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null).invoke();
1170        assertEquals((long)r, (long)(SIZE/2));
1171        r = ConcurrentHashMap.ForkJoinTasks.searchEntries
1172            (m, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null).invoke();
1173        assertEquals((long)r, (long)(SIZE/2));
1174    }
1090   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines