4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.function.*; |
10 |
< |
import java.util.concurrent.atomic.LongAdder; |
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) { |
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(); |
85 |
|
} |
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(); |
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(); |
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() { |
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() { |
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() { |
550 |
|
Set x = populatedSet(size); |
551 |
|
Set y = serialClone(x); |
552 |
|
|
553 |
< |
assertTrue(x != y); |
553 |
> |
assertNotSame(x, y); |
554 |
|
assertEquals(x.size(), y.size()); |
436 |
– |
assertEquals(x.toString(), y.toString()); |
437 |
– |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
555 |
|
assertEquals(x, y); |
556 |
|
assertEquals(y, x); |
557 |
|
} |
558 |
|
|
442 |
– |
|
559 |
|
static final int SIZE = 10000; |
560 |
|
static ConcurrentHashMap<Long, Long> longMap; |
561 |
|
|
583 |
|
public void testForEachKeySequentially() { |
584 |
|
LongAdder adder = new LongAdder(); |
585 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
586 |
< |
m.forEachKeySequentially((Long x) -> adder.add(x.longValue())); |
586 |
> |
m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); |
587 |
|
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
588 |
|
} |
589 |
|
|
593 |
|
public void testForEachValueSequentially() { |
594 |
|
LongAdder adder = new LongAdder(); |
595 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
596 |
< |
m.forEachValueSequentially((Long x) -> adder.add(x.longValue())); |
596 |
> |
m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); |
597 |
|
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
598 |
|
} |
599 |
|
|
603 |
|
public void testForEachSequentially() { |
604 |
|
LongAdder adder = new LongAdder(); |
605 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
606 |
< |
m.forEachSequentially((Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
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 |
|
|
613 |
|
public void testForEachEntrySequentially() { |
614 |
|
LongAdder adder = new LongAdder(); |
615 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
616 |
< |
m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
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 |
|
|
623 |
|
public void testForEachKeyInParallel() { |
624 |
|
LongAdder adder = new LongAdder(); |
625 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
626 |
< |
m.forEachKeyInParallel((Long x) -> adder.add(x.longValue())); |
626 |
> |
m.forEachKey(1L, (Long x) -> adder.add(x.longValue())); |
627 |
|
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
628 |
|
} |
629 |
|
|
633 |
|
public void testForEachValueInParallel() { |
634 |
|
LongAdder adder = new LongAdder(); |
635 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
636 |
< |
m.forEachValueInParallel((Long x) -> adder.add(x.longValue())); |
636 |
> |
m.forEachValue(1L, (Long x) -> adder.add(x.longValue())); |
637 |
|
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
638 |
|
} |
639 |
|
|
643 |
|
public void testForEachInParallel() { |
644 |
|
LongAdder adder = new LongAdder(); |
645 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
646 |
< |
m.forEachInParallel((Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
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 |
|
|
653 |
|
public void testForEachEntryInParallel() { |
654 |
|
LongAdder adder = new LongAdder(); |
655 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
656 |
< |
m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
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 |
|
|
664 |
|
public void testMappedForEachKeySequentially() { |
665 |
|
LongAdder adder = new LongAdder(); |
666 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
667 |
< |
m.forEachKeySequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
676 |
|
public void testMappedForEachValueSequentially() { |
677 |
|
LongAdder adder = new LongAdder(); |
678 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
679 |
< |
m.forEachValueSequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
688 |
|
public void testMappedForEachSequentially() { |
689 |
|
LongAdder adder = new LongAdder(); |
690 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
691 |
< |
m.forEachSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
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 |
|
} |
700 |
|
public void testMappedForEachEntrySequentially() { |
701 |
|
LongAdder adder = new LongAdder(); |
702 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
703 |
< |
m.forEachEntrySequentially((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
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 |
|
} |
712 |
|
public void testMappedForEachKeyInParallel() { |
713 |
|
LongAdder adder = new LongAdder(); |
714 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
715 |
< |
m.forEachKeyInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
724 |
|
public void testMappedForEachValueInParallel() { |
725 |
|
LongAdder adder = new LongAdder(); |
726 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
727 |
< |
m.forEachValueInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
736 |
|
public void testMappedForEachInParallel() { |
737 |
|
LongAdder adder = new LongAdder(); |
738 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
739 |
< |
m.forEachInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
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 |
|
} |
748 |
|
public void testMappedForEachEntryInParallel() { |
749 |
|
LongAdder adder = new LongAdder(); |
750 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
751 |
< |
m.forEachEntryInParallel((Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
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 |
|
|
640 |
– |
|
756 |
|
/** |
757 |
|
* reduceKeysSequentially accumulates across all keys, |
758 |
|
*/ |
759 |
|
public void testReduceKeysSequentially() { |
760 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
761 |
|
Long r; |
762 |
< |
r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
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 |
|
|
769 |
|
public void testReduceValuesSequentially() { |
770 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
771 |
|
Long r; |
772 |
< |
r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
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 |
|
|
661 |
– |
|
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.reduceEntriesSequentially(new AddKeys()); |
782 |
> |
r = m.reduceEntries(Long.MAX_VALUE, new AddKeys()); |
783 |
|
assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); |
784 |
|
} |
785 |
|
|
789 |
|
public void testReduceKeysInParallel() { |
790 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
791 |
|
Long r; |
792 |
< |
r = m.reduceKeysInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
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 |
|
|
799 |
|
public void testReduceValuesInParallel() { |
800 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
801 |
|
Long r; |
802 |
< |
r = m.reduceValuesInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
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 |
|
|
809 |
|
public void testReduceEntriesInParallel() { |
810 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
811 |
|
Map.Entry<Long,Long> r; |
812 |
< |
r = m.reduceEntriesInParallel(new AddKeys()); |
812 |
> |
r = m.reduceEntries(1L, new AddKeys()); |
813 |
|
assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); |
814 |
|
} |
815 |
|
|
818 |
|
*/ |
819 |
|
public void testMapReduceKeysSequentially() { |
820 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
821 |
< |
Long r = m.reduceKeysSequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
828 |
|
*/ |
829 |
|
public void testMapReduceValuesSequentially() { |
830 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
831 |
< |
Long r = m.reduceValuesSequentially((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
838 |
|
*/ |
839 |
|
public void testMappedReduceSequentially() { |
840 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
841 |
< |
Long r = m.reduceSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
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); |
849 |
|
*/ |
850 |
|
public void testMapReduceKeysInParallel() { |
851 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
852 |
< |
Long r = m.reduceKeysInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
859 |
|
*/ |
860 |
|
public void testMapReduceValuesInParallel() { |
861 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
862 |
< |
Long r = m.reduceValuesInParallel((Long x) -> Long.valueOf(4 * x.longValue()), |
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 |
|
} |
870 |
|
public void testMappedReduceInParallel() { |
871 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
872 |
|
Long r; |
873 |
< |
r = m.reduceInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
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 |
|
|
764 |
– |
|
878 |
|
/** |
879 |
|
* reduceKeysToLongSequentially accumulates mapped keys |
880 |
|
*/ |
881 |
|
public void testReduceKeysToLongSequentially() { |
882 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
883 |
< |
long lr = m.reduceKeysToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum); |
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 |
|
|
889 |
|
*/ |
890 |
|
public void testReduceKeysToIntSequentially() { |
891 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
892 |
< |
int ir = m.reduceKeysToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); |
892 |
> |
int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); |
893 |
|
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
894 |
|
} |
895 |
|
|
898 |
|
*/ |
899 |
|
public void testReduceKeysToDoubleSequentially() { |
900 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
901 |
< |
double dr = m.reduceKeysToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum); |
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 |
|
|
907 |
|
*/ |
908 |
|
public void testReduceValuesToLongSequentially() { |
909 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
910 |
< |
long lr = m.reduceValuesToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum); |
910 |
> |
long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum); |
911 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
912 |
|
} |
913 |
|
|
916 |
|
*/ |
917 |
|
public void testReduceValuesToIntSequentially() { |
918 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
919 |
< |
int ir = m.reduceValuesToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); |
919 |
> |
int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); |
920 |
|
assertEquals(ir, SIZE * (SIZE - 1)); |
921 |
|
} |
922 |
|
|
925 |
|
*/ |
926 |
|
public void testReduceValuesToDoubleSequentially() { |
927 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
928 |
< |
double dr = m.reduceValuesToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum); |
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 |
|
|
934 |
|
*/ |
935 |
|
public void testReduceKeysToLongInParallel() { |
936 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
937 |
< |
long lr = m.reduceKeysToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum); |
937 |
> |
long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); |
938 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
939 |
|
} |
940 |
|
|
943 |
|
*/ |
944 |
|
public void testReduceKeysToIntInParallel() { |
945 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
946 |
< |
int ir = m.reduceKeysToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); |
946 |
> |
int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); |
947 |
|
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
948 |
|
} |
949 |
|
|
952 |
|
*/ |
953 |
|
public void testReduceKeysToDoubleInParallel() { |
954 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
955 |
< |
double dr = m.reduceKeysToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum); |
955 |
> |
double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
956 |
|
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
957 |
|
} |
958 |
|
|
961 |
|
*/ |
962 |
|
public void testReduceValuesToLongInParallel() { |
963 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
964 |
< |
long lr = m.reduceValuesToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum); |
964 |
> |
long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); |
965 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
966 |
|
} |
967 |
|
|
970 |
|
*/ |
971 |
|
public void testReduceValuesToIntInParallel() { |
972 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
973 |
< |
int ir = m.reduceValuesToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); |
973 |
> |
int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); |
974 |
|
assertEquals(ir, SIZE * (SIZE - 1)); |
975 |
|
} |
976 |
|
|
979 |
|
*/ |
980 |
|
public void testReduceValuesToDoubleInParallel() { |
981 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
982 |
< |
double dr = m.reduceValuesToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum); |
982 |
> |
double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
983 |
|
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
984 |
|
} |
985 |
|
|
990 |
|
public void testSearchKeysSequentially() { |
991 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
992 |
|
Long r; |
993 |
< |
r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
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.searchKeysSequentially((Long x) -> x.longValue() < 0L ? x : null); |
995 |
> |
r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); |
996 |
|
assertNull(r); |
997 |
|
} |
998 |
|
|
1003 |
|
public void testSearchValuesSequentially() { |
1004 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1005 |
|
Long r; |
1006 |
< |
r = m.searchValuesSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
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.searchValuesSequentially((Long x) -> x.longValue() < 0L ? x : null); |
1009 |
> |
r = m.searchValues(Long.MAX_VALUE, |
1010 |
> |
(Long x) -> (x.longValue() < 0L) ? x : null); |
1011 |
|
assertNull(r); |
1012 |
|
} |
1013 |
|
|
1018 |
|
public void testSearchSequentially() { |
1019 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1020 |
|
Long r; |
1021 |
< |
r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
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.searchSequentially((Long x, Long y) -> x.longValue() < 0L ? x : null); |
1023 |
> |
r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null); |
1024 |
|
assertNull(r); |
1025 |
|
} |
1026 |
|
|
1031 |
|
public void testSearchEntriesSequentially() { |
1032 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1033 |
|
Long r; |
1034 |
< |
r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
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.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1036 |
> |
r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1037 |
|
assertNull(r); |
1038 |
|
} |
1039 |
|
|
1044 |
|
public void testSearchKeysInParallel() { |
1045 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1046 |
|
Long r; |
1047 |
< |
r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1047 |
> |
r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1048 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1049 |
< |
r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L ? x : null); |
1049 |
> |
r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null); |
1050 |
|
assertNull(r); |
1051 |
|
} |
1052 |
|
|
1057 |
|
public void testSearchValuesInParallel() { |
1058 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1059 |
|
Long r; |
1060 |
< |
r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1060 |
> |
r = m.searchValues(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1061 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1062 |
< |
r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L ? x : null); |
1062 |
> |
r = m.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null); |
1063 |
|
assertNull(r); |
1064 |
|
} |
1065 |
|
|
1070 |
|
public void testSearchInParallel() { |
1071 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1072 |
|
Long r; |
1073 |
< |
r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
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.searchInParallel((Long x, Long y) -> x.longValue() < 0L ? x : null); |
1075 |
> |
r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null); |
1076 |
|
assertNull(r); |
1077 |
|
} |
1078 |
|
|
1083 |
|
public void testSearchEntriesInParallel() { |
1084 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1085 |
|
Long r; |
1086 |
< |
r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
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.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1088 |
> |
r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1089 |
|
assertNull(r); |
1090 |
|
} |
1091 |
|
|
977 |
– |
/** |
978 |
– |
* Invoking task versions of bulk methods has same effect as |
979 |
– |
* parallel methods |
980 |
– |
*/ |
981 |
– |
public void testForkJoinTasks() { |
982 |
– |
LongAdder adder = new LongAdder(); |
983 |
– |
ConcurrentHashMap<Long, Long> m = longMap(); |
984 |
– |
ConcurrentHashMap.ForkJoinTasks.forEachKey |
985 |
– |
(m, (Long x) -> adder.add(x.longValue())).invoke(); |
986 |
– |
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
987 |
– |
adder.reset(); |
988 |
– |
ConcurrentHashMap.ForkJoinTasks.forEachValue |
989 |
– |
(m, (Long x) -> adder.add(x.longValue())).invoke(); |
990 |
– |
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
991 |
– |
adder.reset(); |
992 |
– |
ConcurrentHashMap.ForkJoinTasks.forEach |
993 |
– |
(m, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())).invoke(); |
994 |
– |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
995 |
– |
adder.reset(); |
996 |
– |
ConcurrentHashMap.ForkJoinTasks.forEachEntry |
997 |
– |
(m, |
998 |
– |
(Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())).invoke(); |
999 |
– |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1000 |
– |
adder.reset(); |
1001 |
– |
ConcurrentHashMap.ForkJoinTasks.forEachKey |
1002 |
– |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1003 |
– |
(Long x) -> adder.add(x.longValue())).invoke(); |
1004 |
– |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
1005 |
– |
adder.reset(); |
1006 |
– |
ConcurrentHashMap.ForkJoinTasks.forEachValue |
1007 |
– |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1008 |
– |
(Long x) -> adder.add(x.longValue())).invoke(); |
1009 |
– |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
1010 |
– |
adder.reset(); |
1011 |
– |
ConcurrentHashMap.ForkJoinTasks.forEach |
1012 |
– |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
1013 |
– |
(Long x) -> adder.add(x.longValue())).invoke(); |
1014 |
– |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1015 |
– |
adder.reset(); |
1016 |
– |
ConcurrentHashMap.ForkJoinTasks.forEachEntry |
1017 |
– |
(m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
1018 |
– |
(Long x) -> adder.add(x.longValue())).invoke(); |
1019 |
– |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1020 |
– |
adder.reset(); |
1021 |
– |
|
1022 |
– |
Long r; long lr; int ir; double dr; |
1023 |
– |
r = ConcurrentHashMap.ForkJoinTasks.reduceKeys |
1024 |
– |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1025 |
– |
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
1026 |
– |
r = ConcurrentHashMap.ForkJoinTasks.reduceValues |
1027 |
– |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1028 |
– |
assertEquals((long)r, (long)SIZE * (SIZE - 1)); |
1029 |
– |
r = ConcurrentHashMap.ForkJoinTasks.reduce |
1030 |
– |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
1031 |
– |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1032 |
– |
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
1033 |
– |
r = ConcurrentHashMap.ForkJoinTasks.reduceEntries |
1034 |
– |
(m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
1035 |
– |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1036 |
– |
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
1037 |
– |
r = ConcurrentHashMap.ForkJoinTasks.reduceKeys |
1038 |
– |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1039 |
– |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1040 |
– |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
1041 |
– |
lr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToLong |
1042 |
– |
(m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); |
1043 |
– |
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
1044 |
– |
ir = ConcurrentHashMap.ForkJoinTasks.reduceKeysToInt |
1045 |
– |
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
1046 |
– |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
1047 |
– |
dr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToDouble |
1048 |
– |
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
1049 |
– |
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
1050 |
– |
r = ConcurrentHashMap.ForkJoinTasks.reduceValues |
1051 |
– |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1052 |
– |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1053 |
– |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
1054 |
– |
lr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToLong |
1055 |
– |
(m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); |
1056 |
– |
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
1057 |
– |
ir = ConcurrentHashMap.ForkJoinTasks.reduceValuesToInt |
1058 |
– |
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
1059 |
– |
assertEquals(ir, SIZE * (SIZE - 1)); |
1060 |
– |
dr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToDouble |
1061 |
– |
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
1062 |
– |
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
1063 |
– |
r = ConcurrentHashMap.ForkJoinTasks.searchKeys |
1064 |
– |
(m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
1065 |
– |
assertEquals((long)r, (long)(SIZE/2)); |
1066 |
– |
r = ConcurrentHashMap.ForkJoinTasks.searchValues |
1067 |
– |
(m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
1068 |
– |
assertEquals((long)r, (long)(SIZE/2)); |
1069 |
– |
r = ConcurrentHashMap.ForkJoinTasks.search |
1070 |
– |
(m, (Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
1071 |
– |
assertEquals((long)r, (long)(SIZE/2)); |
1072 |
– |
r = ConcurrentHashMap.ForkJoinTasks.searchEntries |
1073 |
– |
(m, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null).invoke(); |
1074 |
– |
assertEquals((long)r, (long)(SIZE/2)); |
1075 |
– |
} |
1092 |
|
} |