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.1 by dl, Thu Mar 21 19:06:54 2013 UTC vs.
Revision 1.12 by dl, Sun Jul 21 22:24:18 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines