ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
Revision: 1.39
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.38: +2 -2 lines
Log Message:
use diamond <> pervasively

File Contents

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