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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines