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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines