ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.34
Committed: Sat Mar 18 20:42:20 2017 UTC (7 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.33: +6 -6 lines
Log Message:
better assertion style

File Contents

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