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.2 by dl, Fri Mar 22 00:24:35 2013 UTC vs.
Revision 1.3 by dl, Fri Mar 22 14:11:32 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines