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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines