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.15 by jsr166, Thu Aug 8 18:37:05 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.toString(), y.toString());
543 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
544 +        assertEquals(x, y);
545 +        assertEquals(y, x);
546 +    }
547 +
548 +    static final int SIZE = 10000;
549 +    static ConcurrentHashMap<Long, Long> longMap;
550 +
551 +    static ConcurrentHashMap<Long, Long> longMap() {
552 +        if (longMap == null) {
553 +            longMap = new ConcurrentHashMap<Long, Long>(SIZE);
554 +            for (int i = 0; i < SIZE; ++i)
555 +                longMap.put(Long.valueOf(i), Long.valueOf(2 *i));
556 +        }
557 +        return longMap;
558 +    }
559 +
560 +    // explicit function class to avoid type inference problems
561 +    static class AddKeys implements BiFunction<Map.Entry<Long,Long>, Map.Entry<Long,Long>, Map.Entry<Long,Long>> {
562 +        public Map.Entry<Long,Long> apply(Map.Entry<Long,Long> x, Map.Entry<Long,Long> y) {
563 +            return new AbstractMap.SimpleEntry<Long,Long>
564 +             (Long.valueOf(x.getKey().longValue() + y.getKey().longValue()),
565 +              Long.valueOf(1L));
566 +        }
567 +    }
568 +
569 +    /**
570 +     * forEachKeySequentially traverses all keys
571 +     */
572 +    public void testForEachKeySequentially() {
573 +        LongAdder adder = new LongAdder();
574 +        ConcurrentHashMap<Long, Long> m = longMap();
575 +        m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
576 +        assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
577 +    }
578 +
579 +    /**
580 +     * forEachValueSequentially traverses all values
581 +     */
582 +    public void testForEachValueSequentially() {
583 +        LongAdder adder = new LongAdder();
584 +        ConcurrentHashMap<Long, Long> m = longMap();
585 +        m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
586 +        assertEquals(adder.sum(), SIZE * (SIZE - 1));
587 +    }
588 +
589 +    /**
590 +     * forEachSequentially traverses all mappings
591 +     */
592 +    public void testForEachSequentially() {
593 +        LongAdder adder = new LongAdder();
594 +        ConcurrentHashMap<Long, Long> m = longMap();
595 +        m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
596 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
597 +    }
598 +
599 +    /**
600 +     * forEachEntrySequentially traverses all entries
601 +     */
602 +    public void testForEachEntrySequentially() {
603 +        LongAdder adder = new LongAdder();
604 +        ConcurrentHashMap<Long, Long> m = longMap();
605 +        m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
606 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
607 +    }
608 +
609 +    /**
610 +     * forEachKeyInParallel traverses all keys
611 +     */
612 +    public void testForEachKeyInParallel() {
613 +        LongAdder adder = new LongAdder();
614 +        ConcurrentHashMap<Long, Long> m = longMap();
615 +        m.forEachKey(1L, (Long x) -> adder.add(x.longValue()));
616 +        assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
617 +    }
618 +
619 +    /**
620 +     * forEachValueInParallel traverses all values
621 +     */
622 +    public void testForEachValueInParallel() {
623 +        LongAdder adder = new LongAdder();
624 +        ConcurrentHashMap<Long, Long> m = longMap();
625 +        m.forEachValue(1L, (Long x) -> adder.add(x.longValue()));
626 +        assertEquals(adder.sum(), SIZE * (SIZE - 1));
627 +    }
628 +
629 +    /**
630 +     * forEachInParallel traverses all mappings
631 +     */
632 +    public void testForEachInParallel() {
633 +        LongAdder adder = new LongAdder();
634 +        ConcurrentHashMap<Long, Long> m = longMap();
635 +        m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
636 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
637 +    }
638 +
639 +    /**
640 +     * forEachEntryInParallel traverses all entries
641 +     */
642 +    public void testForEachEntryInParallel() {
643 +        LongAdder adder = new LongAdder();
644 +        ConcurrentHashMap<Long, Long> m = longMap();
645 +        m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
646 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
647 +    }
648 +
649 +    /**
650 +     * Mapped forEachKeySequentially traverses the given
651 +     * transformations of all keys
652 +     */
653 +    public void testMappedForEachKeySequentially() {
654 +        LongAdder adder = new LongAdder();
655 +        ConcurrentHashMap<Long, Long> m = longMap();
656 +        m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
657 +                                 (Long x) -> adder.add(x.longValue()));
658 +        assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
659 +    }
660 +
661 +    /**
662 +     * Mapped forEachValueSequentially traverses the given
663 +     * transformations of all values
664 +     */
665 +    public void testMappedForEachValueSequentially() {
666 +        LongAdder adder = new LongAdder();
667 +        ConcurrentHashMap<Long, Long> m = longMap();
668 +        m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
669 +                                   (Long x) -> adder.add(x.longValue()));
670 +        assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
671 +    }
672 +
673 +    /**
674 +     * Mapped forEachSequentially traverses the given
675 +     * transformations of all mappings
676 +     */
677 +    public void testMappedForEachSequentially() {
678 +        LongAdder adder = new LongAdder();
679 +        ConcurrentHashMap<Long, Long> m = longMap();
680 +        m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
681 +                              (Long x) -> adder.add(x.longValue()));
682 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
683 +    }
684 +
685 +    /**
686 +     * Mapped forEachEntrySequentially traverses the given
687 +     * transformations of all entries
688 +     */
689 +    public void testMappedForEachEntrySequentially() {
690 +        LongAdder adder = new LongAdder();
691 +        ConcurrentHashMap<Long, Long> m = longMap();
692 +        m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
693 +                                   (Long x) -> adder.add(x.longValue()));
694 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
695 +    }
696 +
697 +    /**
698 +     * Mapped forEachKeyInParallel traverses the given
699 +     * transformations of all keys
700 +     */
701 +    public void testMappedForEachKeyInParallel() {
702 +        LongAdder adder = new LongAdder();
703 +        ConcurrentHashMap<Long, Long> m = longMap();
704 +        m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
705 +                               (Long x) -> adder.add(x.longValue()));
706 +        assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
707 +    }
708 +
709 +    /**
710 +     * Mapped forEachValueInParallel traverses the given
711 +     * transformations of all values
712 +     */
713 +    public void testMappedForEachValueInParallel() {
714 +        LongAdder adder = new LongAdder();
715 +        ConcurrentHashMap<Long, Long> m = longMap();
716 +        m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
717 +                                 (Long x) -> adder.add(x.longValue()));
718 +        assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
719 +    }
720 +
721 +    /**
722 +     * Mapped forEachInParallel traverses the given
723 +     * transformations of all mappings
724 +     */
725 +    public void testMappedForEachInParallel() {
726 +        LongAdder adder = new LongAdder();
727 +        ConcurrentHashMap<Long, Long> m = longMap();
728 +        m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
729 +                            (Long x) -> adder.add(x.longValue()));
730 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
731 +    }
732 +
733 +    /**
734 +     * Mapped forEachEntryInParallel traverses the given
735 +     * transformations of all entries
736 +     */
737 +    public void testMappedForEachEntryInParallel() {
738 +        LongAdder adder = new LongAdder();
739 +        ConcurrentHashMap<Long, Long> m = longMap();
740 +        m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
741 +                                 (Long x) -> adder.add(x.longValue()));
742 +        assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
743 +    }
744 +
745 +    /**
746 +     * reduceKeysSequentially accumulates across all keys,
747 +     */
748 +    public void testReduceKeysSequentially() {
749 +        ConcurrentHashMap<Long, Long> m = longMap();
750 +        Long r;
751 +        r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
752 +        assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
753 +    }
754 +
755 +    /**
756 +     * reduceValuesSequentially accumulates across all values
757 +     */
758 +    public void testReduceValuesSequentially() {
759 +        ConcurrentHashMap<Long, Long> m = longMap();
760 +        Long r;
761 +        r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
762 +        assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
763 +    }
764  
765 +    /**
766 +     * reduceEntriesSequentially accumulates across all entries
767 +     */
768 +    public void testReduceEntriesSequentially() {
769 +        ConcurrentHashMap<Long, Long> m = longMap();
770 +        Map.Entry<Long,Long> r;
771 +        r = m.reduceEntries(Long.MAX_VALUE, new AddKeys());
772 +        assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
773 +    }
774 +
775 +    /**
776 +     * reduceKeysInParallel accumulates across all keys
777 +     */
778 +    public void testReduceKeysInParallel() {
779 +        ConcurrentHashMap<Long, Long> m = longMap();
780 +        Long r;
781 +        r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
782 +        assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
783 +    }
784 +
785 +    /**
786 +     * reduceValuesInParallel accumulates across all values
787 +     */
788 +    public void testReduceValuesInParallel() {
789 +        ConcurrentHashMap<Long, Long> m = longMap();
790 +        Long r;
791 +        r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
792 +        assertEquals((long)r, (long)SIZE * (SIZE - 1));
793 +    }
794 +
795 +    /**
796 +     * reduceEntriesInParallel accumulate across all entries
797 +     */
798 +    public void testReduceEntriesInParallel() {
799 +        ConcurrentHashMap<Long, Long> m = longMap();
800 +        Map.Entry<Long,Long> r;
801 +        r = m.reduceEntries(1L, new AddKeys());
802 +        assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
803 +    }
804 +
805 +    /**
806 +     * Mapped reduceKeysSequentially accumulates mapped keys
807 +     */
808 +    public void testMapReduceKeysSequentially() {
809 +        ConcurrentHashMap<Long, Long> m = longMap();
810 +        Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
811 +                                     (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
812 +        assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
813 +    }
814 +
815 +    /**
816 +     * Mapped reduceValuesSequentially accumulates mapped values
817 +     */
818 +    public void testMapReduceValuesSequentially() {
819 +        ConcurrentHashMap<Long, Long> m = longMap();
820 +        Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
821 +                                       (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
822 +        assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
823 +    }
824 +
825 +    /**
826 +     * reduceSequentially accumulates across all transformed mappings
827 +     */
828 +    public void testMappedReduceSequentially() {
829 +        ConcurrentHashMap<Long, Long> m = longMap();
830 +        Long r = m.reduce(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
831 +                                 (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
832 +
833 +        assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
834 +    }
835 +
836 +    /**
837 +     * Mapped reduceKeysInParallel, accumulates mapped keys
838 +     */
839 +    public void testMapReduceKeysInParallel() {
840 +        ConcurrentHashMap<Long, Long> m = longMap();
841 +        Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
842 +                                   (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
843 +        assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
844 +    }
845 +
846 +    /**
847 +     * Mapped reduceValuesInParallel accumulates mapped values
848 +     */
849 +    public void testMapReduceValuesInParallel() {
850 +        ConcurrentHashMap<Long, Long> m = longMap();
851 +        Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
852 +                                     (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
853 +        assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
854 +    }
855 +
856 +    /**
857 +     * reduceInParallel accumulate across all transformed mappings
858 +     */
859 +    public void testMappedReduceInParallel() {
860 +        ConcurrentHashMap<Long, Long> m = longMap();
861 +        Long r;
862 +        r = m.reduce(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
863 +                               (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
864 +        assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
865 +    }
866 +
867 +    /**
868 +     * reduceKeysToLongSequentially accumulates mapped keys
869 +     */
870 +    public void testReduceKeysToLongSequentially() {
871 +        ConcurrentHashMap<Long, Long> m = longMap();
872 +        long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
873 +        assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
874 +    }
875 +
876 +    /**
877 +     * reduceKeysToIntSequentially accumulates mapped keys
878 +     */
879 +    public void testReduceKeysToIntSequentially() {
880 +        ConcurrentHashMap<Long, Long> m = longMap();
881 +        int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
882 +        assertEquals(ir, SIZE * (SIZE - 1) / 2);
883 +    }
884 +
885 +    /**
886 +     * reduceKeysToDoubleSequentially accumulates mapped keys
887 +     */
888 +    public void testReduceKeysToDoubleSequentially() {
889 +        ConcurrentHashMap<Long, Long> m = longMap();
890 +        double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
891 +        assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
892 +    }
893 +
894 +    /**
895 +     * reduceValuesToLongSequentially accumulates mapped values
896 +     */
897 +    public void testReduceValuesToLongSequentially() {
898 +        ConcurrentHashMap<Long, Long> m = longMap();
899 +        long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
900 +        assertEquals(lr, (long)SIZE * (SIZE - 1));
901 +    }
902 +
903 +    /**
904 +     * reduceValuesToIntSequentially accumulates mapped values
905 +     */
906 +    public void testReduceValuesToIntSequentially() {
907 +        ConcurrentHashMap<Long, Long> m = longMap();
908 +        int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
909 +        assertEquals(ir, SIZE * (SIZE - 1));
910 +    }
911 +
912 +    /**
913 +     * reduceValuesToDoubleSequentially accumulates mapped values
914 +     */
915 +    public void testReduceValuesToDoubleSequentially() {
916 +        ConcurrentHashMap<Long, Long> m = longMap();
917 +        double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
918 +        assertEquals(dr, (double)SIZE * (SIZE - 1));
919 +    }
920 +
921 +    /**
922 +     * reduceKeysToLongInParallel accumulates mapped keys
923 +     */
924 +    public void testReduceKeysToLongInParallel() {
925 +        ConcurrentHashMap<Long, Long> m = longMap();
926 +        long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
927 +        assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
928 +    }
929 +
930 +    /**
931 +     * reduceKeysToIntInParallel accumulates mapped keys
932 +     */
933 +    public void testReduceKeysToIntInParallel() {
934 +        ConcurrentHashMap<Long, Long> m = longMap();
935 +        int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
936 +        assertEquals(ir, SIZE * (SIZE - 1) / 2);
937 +    }
938 +
939 +    /**
940 +     * reduceKeysToDoubleInParallel accumulates mapped values
941 +     */
942 +    public void testReduceKeysToDoubleInParallel() {
943 +        ConcurrentHashMap<Long, Long> m = longMap();
944 +        double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
945 +        assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
946 +    }
947 +
948 +    /**
949 +     * reduceValuesToLongInParallel accumulates mapped values
950 +     */
951 +    public void testReduceValuesToLongInParallel() {
952 +        ConcurrentHashMap<Long, Long> m = longMap();
953 +        long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
954 +        assertEquals(lr, (long)SIZE * (SIZE - 1));
955 +    }
956 +
957 +    /**
958 +     * reduceValuesToIntInParallel accumulates mapped values
959 +     */
960 +    public void testReduceValuesToIntInParallel() {
961 +        ConcurrentHashMap<Long, Long> m = longMap();
962 +        int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
963 +        assertEquals(ir, SIZE * (SIZE - 1));
964 +    }
965 +
966 +    /**
967 +     * reduceValuesToDoubleInParallel accumulates mapped values
968 +     */
969 +    public void testReduceValuesToDoubleInParallel() {
970 +        ConcurrentHashMap<Long, Long> m = longMap();
971 +        double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
972 +        assertEquals(dr, (double)SIZE * (SIZE - 1));
973 +    }
974 +
975 +    /**
976 +     * searchKeysSequentially returns a non-null result of search
977 +     * function, or null if none
978 +     */
979 +    public void testSearchKeysSequentially() {
980 +        ConcurrentHashMap<Long, Long> m = longMap();
981 +        Long r;
982 +        r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
983 +        assertEquals((long)r, (long)(SIZE/2));
984 +        r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
985 +        assertNull(r);
986 +    }
987 +
988 +    /**
989 +     * searchValuesSequentially returns a non-null result of search
990 +     * function, or null if none
991 +     */
992 +    public void testSearchValuesSequentially() {
993 +        ConcurrentHashMap<Long, Long> m = longMap();
994 +        Long r;
995 +        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null);
996 +        assertEquals((long)r, (long)(SIZE/2));
997 +        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
998 +        assertNull(r);
999 +    }
1000 +
1001 +    /**
1002 +     * searchSequentially returns a non-null result of search
1003 +     * function, or null if none
1004 +     */
1005 +    public void testSearchSequentially() {
1006 +        ConcurrentHashMap<Long, Long> m = longMap();
1007 +        Long r;
1008 +        r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1009 +        assertEquals((long)r, (long)(SIZE/2));
1010 +        r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1011 +        assertNull(r);
1012 +    }
1013 +
1014 +    /**
1015 +     * searchEntriesSequentially returns a non-null result of search
1016 +     * function, or null if none
1017 +     */
1018 +    public void testSearchEntriesSequentially() {
1019 +        ConcurrentHashMap<Long, Long> m = longMap();
1020 +        Long r;
1021 +        r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1022 +        assertEquals((long)r, (long)(SIZE/2));
1023 +        r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1024 +        assertNull(r);
1025 +    }
1026 +
1027 +    /**
1028 +     * searchKeysInParallel returns a non-null result of search
1029 +     * function, or null if none
1030 +     */
1031 +    public void testSearchKeysInParallel() {
1032 +        ConcurrentHashMap<Long, Long> m = longMap();
1033 +        Long r;
1034 +        r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1035 +        assertEquals((long)r, (long)(SIZE/2));
1036 +        r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null);
1037 +        assertNull(r);
1038 +    }
1039 +
1040 +    /**
1041 +     * searchValuesInParallel returns a non-null result of search
1042 +     * function, or null if none
1043 +     */
1044 +    public void testSearchValuesInParallel() {
1045 +        ConcurrentHashMap<Long, Long> m = longMap();
1046 +        Long r;
1047 +        r = m.searchValues(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
1048 +        assertEquals((long)r, (long)(SIZE/2));
1049 +        r = m.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null);
1050 +        assertNull(r);
1051 +    }
1052 +
1053 +    /**
1054 +     * searchInParallel returns a non-null result of search function,
1055 +     * or null if none
1056 +     */
1057 +    public void testSearchInParallel() {
1058 +        ConcurrentHashMap<Long, Long> m = longMap();
1059 +        Long r;
1060 +        r = m.search(1L, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
1061 +        assertEquals((long)r, (long)(SIZE/2));
1062 +        r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null);
1063 +        assertNull(r);
1064 +    }
1065 +
1066 +    /**
1067 +     * searchEntriesInParallel returns a non-null result of search
1068 +     * function, or null if none
1069 +     */
1070 +    public void testSearchEntriesInParallel() {
1071 +        ConcurrentHashMap<Long, Long> m = longMap();
1072 +        Long r;
1073 +        r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
1074 +        assertEquals((long)r, (long)(SIZE/2));
1075 +        r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
1076 +        assertNull(r);
1077 +    }
1078  
1079   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines