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.21 by jsr166, Thu Jan 15 18:34:19 2015 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 265 | Line 181 | public class ConcurrentHashMap8Test exte
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       */
199      public void testNewKeySet() {
# 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       * keySet.addAll adds each element from the given collection
228       */
229      public void testAddAll() {
# Line 318 | Line 269 | public class ConcurrentHashMap8Test exte
269      }
270  
271      /**
272 +     * keySet.add throws UnsupportedOperationException if no default
273 +     * mapped value
274 +     */
275 +    public void testAdd4() {
276 +        Set full = map5().keySet();
277 +        try {
278 +            full.add(three);
279 +            shouldThrow();
280 +        } catch (UnsupportedOperationException e){}
281 +    }
282 +
283 +    /**
284 +     * keySet.add throws NullPointerException if the specified key is
285 +     * null
286 +     */
287 +    public void testAdd5() {
288 +        Set full = populatedSet(3);
289 +        try {
290 +            full.add(null);
291 +            shouldThrow();
292 +        } catch (NullPointerException e){}
293 +    }
294 +
295 +    /**
296 +     * KeySetView.getMappedValue returns the map's mapped value
297 +     */
298 +    public void testGetMappedValue() {
299 +        ConcurrentHashMap map = map5();
300 +        assertNull(map.keySet().getMappedValue());
301 +        try {
302 +            map.keySet(null);
303 +            shouldThrow();
304 +        } catch (NullPointerException e) {}
305 +        ConcurrentHashMap.KeySetView set = map.keySet(one);
306 +        set.add(one);
307 +        set.add(six);
308 +        set.add(seven);
309 +        assertTrue(set.getMappedValue() == one);
310 +        assertTrue(map.get(one) != one);
311 +        assertTrue(map.get(six) == one);
312 +        assertTrue(map.get(seven) == one);
313 +    }
314 +
315 +    void checkSpliteratorCharacteristics(Spliterator<?> sp,
316 +                                         int requiredCharacteristics) {
317 +        assertEquals(requiredCharacteristics,
318 +                     requiredCharacteristics & sp.characteristics());
319 +    }
320 +
321 +    /**
322 +     * KeySetView.spliterator returns spliterator over the elements in this set
323 +     */
324 +    public void testKeySetSpliterator() {
325 +        LongAdder adder = new LongAdder();
326 +        ConcurrentHashMap map = map5();
327 +        Set set = map.keySet();
328 +        Spliterator<Integer> sp = set.spliterator();
329 +        checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
330 +        assertEquals(sp.estimateSize(), map.size());
331 +        Spliterator<Integer> sp2 = sp.trySplit();
332 +        sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
333 +        long v = adder.sumThenReset();
334 +        sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
335 +        long v2 = adder.sum();
336 +        assertEquals(v + v2, 15);
337 +    }
338 +
339 +    /**
340       * keyset.clear removes all elements from the set
341       */
342      public void testClear() {
# Line 530 | Line 549 | public class ConcurrentHashMap8Test exte
549          Set x = populatedSet(size);
550          Set y = serialClone(x);
551  
552 <        assertTrue(x != y);
552 >        assertNotSame(x, y);
553          assertEquals(x.size(), y.size());
535        assertEquals(x.toString(), y.toString());
536        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
554          assertEquals(x, y);
555          assertEquals(y, x);
556      }
557  
541
558      static final int SIZE = 10000;
559      static ConcurrentHashMap<Long, Long> longMap;
560  
# Line 566 | Line 582 | public class ConcurrentHashMap8Test exte
582      public void testForEachKeySequentially() {
583          LongAdder adder = new LongAdder();
584          ConcurrentHashMap<Long, Long> m = longMap();
585 <        m.forEachKeySequentially((Long x) -> adder.add(x.longValue()));
585 >        m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
586          assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
587      }
588  
# Line 576 | Line 592 | public class ConcurrentHashMap8Test exte
592      public void testForEachValueSequentially() {
593          LongAdder adder = new LongAdder();
594          ConcurrentHashMap<Long, Long> m = longMap();
595 <        m.forEachValueSequentially((Long x) -> adder.add(x.longValue()));
595 >        m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
596          assertEquals(adder.sum(), SIZE * (SIZE - 1));
597      }
598  
# Line 586 | Line 602 | public class ConcurrentHashMap8Test exte
602      public void testForEachSequentially() {
603          LongAdder adder = new LongAdder();
604          ConcurrentHashMap<Long, Long> m = longMap();
605 <        m.forEachSequentially((Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
605 >        m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
606          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
607      }
608  
# Line 596 | Line 612 | public class ConcurrentHashMap8Test exte
612      public void testForEachEntrySequentially() {
613          LongAdder adder = new LongAdder();
614          ConcurrentHashMap<Long, Long> m = longMap();
615 <        m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
615 >        m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
616          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
617      }
618  
# Line 606 | Line 622 | public class ConcurrentHashMap8Test exte
622      public void testForEachKeyInParallel() {
623          LongAdder adder = new LongAdder();
624          ConcurrentHashMap<Long, Long> m = longMap();
625 <        m.forEachKeyInParallel((Long x) -> adder.add(x.longValue()));
625 >        m.forEachKey(1L, (Long x) -> adder.add(x.longValue()));
626          assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
627      }
628  
# Line 616 | Line 632 | public class ConcurrentHashMap8Test exte
632      public void testForEachValueInParallel() {
633          LongAdder adder = new LongAdder();
634          ConcurrentHashMap<Long, Long> m = longMap();
635 <        m.forEachValueInParallel((Long x) -> adder.add(x.longValue()));
635 >        m.forEachValue(1L, (Long x) -> adder.add(x.longValue()));
636          assertEquals(adder.sum(), SIZE * (SIZE - 1));
637      }
638  
# Line 626 | Line 642 | public class ConcurrentHashMap8Test exte
642      public void testForEachInParallel() {
643          LongAdder adder = new LongAdder();
644          ConcurrentHashMap<Long, Long> m = longMap();
645 <        m.forEachInParallel((Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
645 >        m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
646          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
647      }
648  
# Line 636 | Line 652 | public class ConcurrentHashMap8Test exte
652      public void testForEachEntryInParallel() {
653          LongAdder adder = new LongAdder();
654          ConcurrentHashMap<Long, Long> m = longMap();
655 <        m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
655 >        m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
656          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
657      }
658  
# Line 647 | Line 663 | public class ConcurrentHashMap8Test exte
663      public void testMappedForEachKeySequentially() {
664          LongAdder adder = new LongAdder();
665          ConcurrentHashMap<Long, Long> m = longMap();
666 <        m.forEachKeySequentially((Long x) -> Long.valueOf(4 * x.longValue()),
666 >        m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
667                                   (Long x) -> adder.add(x.longValue()));
668          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
669      }
# Line 659 | Line 675 | public class ConcurrentHashMap8Test exte
675      public void testMappedForEachValueSequentially() {
676          LongAdder adder = new LongAdder();
677          ConcurrentHashMap<Long, Long> m = longMap();
678 <        m.forEachValueSequentially((Long x) -> Long.valueOf(4 * x.longValue()),
678 >        m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
679                                     (Long x) -> adder.add(x.longValue()));
680          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
681      }
# Line 671 | Line 687 | public class ConcurrentHashMap8Test exte
687      public void testMappedForEachSequentially() {
688          LongAdder adder = new LongAdder();
689          ConcurrentHashMap<Long, Long> m = longMap();
690 <        m.forEachSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
690 >        m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
691                                (Long x) -> adder.add(x.longValue()));
692          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
693      }
# Line 683 | Line 699 | public class ConcurrentHashMap8Test exte
699      public void testMappedForEachEntrySequentially() {
700          LongAdder adder = new LongAdder();
701          ConcurrentHashMap<Long, Long> m = longMap();
702 <        m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
702 >        m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
703                                     (Long x) -> adder.add(x.longValue()));
704          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
705      }
# Line 695 | Line 711 | public class ConcurrentHashMap8Test exte
711      public void testMappedForEachKeyInParallel() {
712          LongAdder adder = new LongAdder();
713          ConcurrentHashMap<Long, Long> m = longMap();
714 <        m.forEachKeyInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
714 >        m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
715                                 (Long x) -> adder.add(x.longValue()));
716          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
717      }
# Line 707 | Line 723 | public class ConcurrentHashMap8Test exte
723      public void testMappedForEachValueInParallel() {
724          LongAdder adder = new LongAdder();
725          ConcurrentHashMap<Long, Long> m = longMap();
726 <        m.forEachValueInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
726 >        m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
727                                   (Long x) -> adder.add(x.longValue()));
728          assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
729      }
# Line 719 | Line 735 | public class ConcurrentHashMap8Test exte
735      public void testMappedForEachInParallel() {
736          LongAdder adder = new LongAdder();
737          ConcurrentHashMap<Long, Long> m = longMap();
738 <        m.forEachInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
738 >        m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
739                              (Long x) -> adder.add(x.longValue()));
740          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
741      }
# Line 731 | Line 747 | public class ConcurrentHashMap8Test exte
747      public void testMappedForEachEntryInParallel() {
748          LongAdder adder = new LongAdder();
749          ConcurrentHashMap<Long, Long> m = longMap();
750 <        m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
750 >        m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
751                                   (Long x) -> adder.add(x.longValue()));
752          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
753      }
754  
739
755      /**
756       * reduceKeysSequentially accumulates across all keys,
757       */
758      public void testReduceKeysSequentially() {
759          ConcurrentHashMap<Long, Long> m = longMap();
760          Long r;
761 <        r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
761 >        r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
762          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
763      }
764  
# Line 753 | Line 768 | public class ConcurrentHashMap8Test exte
768      public void testReduceValuesSequentially() {
769          ConcurrentHashMap<Long, Long> m = longMap();
770          Long r;
771 <        r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
771 >        r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
772          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
773      }
774  
760
775      /**
776       * reduceEntriesSequentially accumulates across all entries
777       */
778      public void testReduceEntriesSequentially() {
779          ConcurrentHashMap<Long, Long> m = longMap();
780          Map.Entry<Long,Long> r;
781 <        r = m.reduceEntriesSequentially(new AddKeys());
781 >        r = m.reduceEntries(Long.MAX_VALUE, new AddKeys());
782          assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
783      }
784  
# Line 774 | Line 788 | public class ConcurrentHashMap8Test exte
788      public void testReduceKeysInParallel() {
789          ConcurrentHashMap<Long, Long> m = longMap();
790          Long r;
791 <        r = m.reduceKeysInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
791 >        r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
792          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
793      }
794  
# Line 784 | Line 798 | public class ConcurrentHashMap8Test exte
798      public void testReduceValuesInParallel() {
799          ConcurrentHashMap<Long, Long> m = longMap();
800          Long r;
801 <        r = m.reduceValuesInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
801 >        r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
802          assertEquals((long)r, (long)SIZE * (SIZE - 1));
803      }
804  
# Line 794 | Line 808 | public class ConcurrentHashMap8Test exte
808      public void testReduceEntriesInParallel() {
809          ConcurrentHashMap<Long, Long> m = longMap();
810          Map.Entry<Long,Long> r;
811 <        r = m.reduceEntriesInParallel(new AddKeys());
811 >        r = m.reduceEntries(1L, new AddKeys());
812          assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
813      }
814  
# Line 803 | Line 817 | public class ConcurrentHashMap8Test exte
817       */
818      public void testMapReduceKeysSequentially() {
819          ConcurrentHashMap<Long, Long> m = longMap();
820 <        Long r = m.reduceKeysSequentially((Long x) -> Long.valueOf(4 * x.longValue()),
820 >        Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
821                                       (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
822          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
823      }
# Line 813 | Line 827 | public class ConcurrentHashMap8Test exte
827       */
828      public void testMapReduceValuesSequentially() {
829          ConcurrentHashMap<Long, Long> m = longMap();
830 <        Long r = m.reduceValuesSequentially((Long x) -> Long.valueOf(4 * x.longValue()),
830 >        Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
831                                         (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
832          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
833      }
# Line 823 | Line 837 | public class ConcurrentHashMap8Test exte
837       */
838      public void testMappedReduceSequentially() {
839          ConcurrentHashMap<Long, Long> m = longMap();
840 <        Long r = m.reduceSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
840 >        Long r = m.reduce(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
841                                   (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
842  
843          assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
# Line 834 | Line 848 | public class ConcurrentHashMap8Test exte
848       */
849      public void testMapReduceKeysInParallel() {
850          ConcurrentHashMap<Long, Long> m = longMap();
851 <        Long r = m.reduceKeysInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
851 >        Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
852                                     (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
853          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
854      }
# Line 844 | Line 858 | public class ConcurrentHashMap8Test exte
858       */
859      public void testMapReduceValuesInParallel() {
860          ConcurrentHashMap<Long, Long> m = longMap();
861 <        Long r = m.reduceValuesInParallel((Long x) -> Long.valueOf(4 * x.longValue()),
861 >        Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
862                                       (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
863          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
864      }
# Line 855 | Line 869 | public class ConcurrentHashMap8Test exte
869      public void testMappedReduceInParallel() {
870          ConcurrentHashMap<Long, Long> m = longMap();
871          Long r;
872 <        r = m.reduceInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
872 >        r = m.reduce(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
873                                 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
874          assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
875      }
876  
863
877      /**
878       * reduceKeysToLongSequentially accumulates mapped keys
879       */
880      public void testReduceKeysToLongSequentially() {
881          ConcurrentHashMap<Long, Long> m = longMap();
882 <        long lr = m.reduceKeysToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum);
882 >        long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
883          assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
884      }
885  
# Line 875 | Line 888 | public class ConcurrentHashMap8Test exte
888       */
889      public void testReduceKeysToIntSequentially() {
890          ConcurrentHashMap<Long, Long> m = longMap();
891 <        int ir = m.reduceKeysToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum);
891 >        int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
892          assertEquals(ir, SIZE * (SIZE - 1) / 2);
893      }
894  
# Line 884 | Line 897 | public class ConcurrentHashMap8Test exte
897       */
898      public void testReduceKeysToDoubleSequentially() {
899          ConcurrentHashMap<Long, Long> m = longMap();
900 <        double dr = m.reduceKeysToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum);
900 >        double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
901          assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
902      }
903  
# Line 893 | Line 906 | public class ConcurrentHashMap8Test exte
906       */
907      public void testReduceValuesToLongSequentially() {
908          ConcurrentHashMap<Long, Long> m = longMap();
909 <        long lr = m.reduceValuesToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum);
909 >        long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
910          assertEquals(lr, (long)SIZE * (SIZE - 1));
911      }
912  
# Line 902 | Line 915 | public class ConcurrentHashMap8Test exte
915       */
916      public void testReduceValuesToIntSequentially() {
917          ConcurrentHashMap<Long, Long> m = longMap();
918 <        int ir = m.reduceValuesToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum);
918 >        int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
919          assertEquals(ir, SIZE * (SIZE - 1));
920      }
921  
# Line 911 | Line 924 | public class ConcurrentHashMap8Test exte
924       */
925      public void testReduceValuesToDoubleSequentially() {
926          ConcurrentHashMap<Long, Long> m = longMap();
927 <        double dr = m.reduceValuesToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum);
927 >        double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
928          assertEquals(dr, (double)SIZE * (SIZE - 1));
929      }
930  
# Line 920 | Line 933 | public class ConcurrentHashMap8Test exte
933       */
934      public void testReduceKeysToLongInParallel() {
935          ConcurrentHashMap<Long, Long> m = longMap();
936 <        long lr = m.reduceKeysToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum);
936 >        long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
937          assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
938      }
939  
# Line 929 | Line 942 | public class ConcurrentHashMap8Test exte
942       */
943      public void testReduceKeysToIntInParallel() {
944          ConcurrentHashMap<Long, Long> m = longMap();
945 <        int ir = m.reduceKeysToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum);
945 >        int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
946          assertEquals(ir, SIZE * (SIZE - 1) / 2);
947      }
948  
# Line 938 | Line 951 | public class ConcurrentHashMap8Test exte
951       */
952      public void testReduceKeysToDoubleInParallel() {
953          ConcurrentHashMap<Long, Long> m = longMap();
954 <        double dr = m.reduceKeysToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum);
954 >        double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
955          assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
956      }
957  
# Line 947 | Line 960 | public class ConcurrentHashMap8Test exte
960       */
961      public void testReduceValuesToLongInParallel() {
962          ConcurrentHashMap<Long, Long> m = longMap();
963 <        long lr = m.reduceValuesToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum);
963 >        long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
964          assertEquals(lr, (long)SIZE * (SIZE - 1));
965      }
966  
# Line 956 | Line 969 | public class ConcurrentHashMap8Test exte
969       */
970      public void testReduceValuesToIntInParallel() {
971          ConcurrentHashMap<Long, Long> m = longMap();
972 <        int ir = m.reduceValuesToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum);
972 >        int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
973          assertEquals(ir, SIZE * (SIZE - 1));
974      }
975  
# Line 965 | Line 978 | public class ConcurrentHashMap8Test exte
978       */
979      public void testReduceValuesToDoubleInParallel() {
980          ConcurrentHashMap<Long, Long> m = longMap();
981 <        double dr = m.reduceValuesToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum);
981 >        double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
982          assertEquals(dr, (double)SIZE * (SIZE - 1));
983      }
984  
# Line 976 | Line 989 | public class ConcurrentHashMap8Test exte
989      public void testSearchKeysSequentially() {
990          ConcurrentHashMap<Long, Long> m = longMap();
991          Long r;
992 <        r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
992 >        r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
993          assertEquals((long)r, (long)(SIZE/2));
994 <        r = m.searchKeysSequentially((Long x) -> x.longValue() < 0L ? x : null);
994 >        r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
995          assertNull(r);
996      }
997  
# Line 989 | Line 1002 | public class ConcurrentHashMap8Test exte
1002      public void testSearchValuesSequentially() {
1003          ConcurrentHashMap<Long, Long> m = longMap();
1004          Long r;
1005 <        r = m.searchValuesSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null);
1005 >        r = m.searchValues(Long.MAX_VALUE,
1006 >            (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,
1009 >            (Long x) -> (x.longValue() < 0L) ? x : null);
1010          assertNull(r);
1011      }
1012  
# Line 1002 | Line 1017 | public class ConcurrentHashMap8Test exte
1017      public void testSearchSequentially() {
1018          ConcurrentHashMap<Long, Long> m = longMap();
1019          Long r;
1020 <        r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1020 >        r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1021          assertEquals((long)r, (long)(SIZE/2));
1022 <        r = m.searchSequentially((Long x, Long y) -> x.longValue() < 0L ? x : null);
1022 >        r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1023          assertNull(r);
1024      }
1025  
# Line 1015 | Line 1030 | public class ConcurrentHashMap8Test exte
1030      public void testSearchEntriesSequentially() {
1031          ConcurrentHashMap<Long, Long> m = longMap();
1032          Long r;
1033 <        r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1033 >        r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1034          assertEquals((long)r, (long)(SIZE/2));
1035 <        r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1035 >        r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1036          assertNull(r);
1037      }
1038  
# Line 1028 | Line 1043 | public class ConcurrentHashMap8Test exte
1043      public void testSearchKeysInParallel() {
1044          ConcurrentHashMap<Long, Long> m = longMap();
1045          Long r;
1046 <        r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1046 >        r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1047          assertEquals((long)r, (long)(SIZE/2));
1048 <        r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L ? x : null);
1048 >        r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null);
1049          assertNull(r);
1050      }
1051  
# Line 1041 | Line 1056 | public class ConcurrentHashMap8Test exte
1056      public void testSearchValuesInParallel() {
1057          ConcurrentHashMap<Long, Long> m = longMap();
1058          Long r;
1059 <        r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1059 >        r = m.searchValues(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1060          assertEquals((long)r, (long)(SIZE/2));
1061 <        r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L ? x : null);
1061 >        r = m.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null);
1062          assertNull(r);
1063      }
1064  
# Line 1054 | Line 1069 | public class ConcurrentHashMap8Test exte
1069      public void testSearchInParallel() {
1070          ConcurrentHashMap<Long, Long> m = longMap();
1071          Long r;
1072 <        r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1072 >        r = m.search(1L, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1073          assertEquals((long)r, (long)(SIZE/2));
1074 <        r = m.searchInParallel((Long x, Long y) -> x.longValue() < 0L ? x : null);
1074 >        r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1075          assertNull(r);
1076      }
1077  
# Line 1067 | Line 1082 | public class ConcurrentHashMap8Test exte
1082      public void testSearchEntriesInParallel() {
1083          ConcurrentHashMap<Long, Long> m = longMap();
1084          Long r;
1085 <        r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1085 >        r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1086          assertEquals((long)r, (long)(SIZE/2));
1087 <        r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1087 >        r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1088          assertNull(r);
1089      }
1090  
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    }
1091   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines