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.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 |
< |
junit.textui.TestRunner.run(suite()); |
30 |
> |
main(suite(), args); |
31 |
|
} |
32 |
|
public static Test suite() { |
33 |
|
return new TestSuite(ConcurrentHashMap8Test.class); |
49 |
|
return map; |
50 |
|
} |
51 |
|
|
37 |
– |
// classes for testing Comparable fallbacks |
38 |
– |
static class BI implements Comparable<BI> { |
39 |
– |
private final int value; |
40 |
– |
BI(int value) { this.value = value; } |
41 |
– |
public int compareTo(BI other) { |
42 |
– |
return Integer.compare(value, other.value); |
43 |
– |
} |
44 |
– |
public boolean equals(Object x) { |
45 |
– |
return (x instanceof BI) && ((BI)x).value == value; |
46 |
– |
} |
47 |
– |
public int hashCode() { return 42; } |
48 |
– |
} |
49 |
– |
static class CI extends BI { CI(int value) { super(value); } } |
50 |
– |
static class DI extends BI { DI(int value) { super(value); } } |
51 |
– |
|
52 |
– |
static class BS implements Comparable<BS> { |
53 |
– |
private final String value; |
54 |
– |
BS(String value) { this.value = value; } |
55 |
– |
public int compareTo(BS other) { |
56 |
– |
return value.compareTo(other.value); |
57 |
– |
} |
58 |
– |
public boolean equals(Object x) { |
59 |
– |
return (x instanceof BS) && value.equals(((BS)x).value); |
60 |
– |
} |
61 |
– |
public int hashCode() { return 42; } |
62 |
– |
} |
63 |
– |
|
64 |
– |
static class LexicographicList<E extends Comparable<E>> extends ArrayList<E> |
65 |
– |
implements Comparable<LexicographicList<E>> { |
66 |
– |
LexicographicList(Collection<E> c) { super(c); } |
67 |
– |
LexicographicList(E e) { super(Collections.singleton(e)); } |
68 |
– |
public int compareTo(LexicographicList<E> other) { |
69 |
– |
int common = Math.min(size(), other.size()); |
70 |
– |
int r = 0; |
71 |
– |
for (int i = 0; i < common; i++) { |
72 |
– |
if ((r = get(i).compareTo(other.get(i))) != 0) |
73 |
– |
break; |
74 |
– |
} |
75 |
– |
if (r == 0) |
76 |
– |
r = Integer.compare(size(), other.size()); |
77 |
– |
return r; |
78 |
– |
} |
79 |
– |
private static final long serialVersionUID = 0; |
80 |
– |
} |
81 |
– |
|
82 |
– |
/** |
83 |
– |
* Inserted elements that are subclasses of the same Comparable |
84 |
– |
* class are found. |
85 |
– |
*/ |
86 |
– |
public void testComparableFamily() { |
87 |
– |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
88 |
– |
for (int i = 0; i < 1000; i++) { |
89 |
– |
assertTrue(m.put(new CI(i), true) == null); |
90 |
– |
} |
91 |
– |
for (int i = 0; i < 1000; i++) { |
92 |
– |
assertTrue(m.containsKey(new CI(i))); |
93 |
– |
assertTrue(m.containsKey(new DI(i))); |
94 |
– |
} |
95 |
– |
} |
96 |
– |
|
97 |
– |
/** |
98 |
– |
* Elements of classes with erased generic type parameters based |
99 |
– |
* on Comparable can be inserted and found. |
100 |
– |
*/ |
101 |
– |
public void testGenericComparable() { |
102 |
– |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
103 |
– |
for (int i = 0; i < 1000; i++) { |
104 |
– |
BI bi = new BI(i); |
105 |
– |
BS bs = new BS(String.valueOf(i)); |
106 |
– |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
107 |
– |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
108 |
– |
assertTrue(m.putIfAbsent(bis, true) == null); |
109 |
– |
assertTrue(m.containsKey(bis)); |
110 |
– |
if (m.putIfAbsent(bss, true) == null) |
111 |
– |
assertTrue(m.containsKey(bss)); |
112 |
– |
assertTrue(m.containsKey(bis)); |
113 |
– |
} |
114 |
– |
for (int i = 0; i < 1000; i++) { |
115 |
– |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
116 |
– |
} |
117 |
– |
} |
118 |
– |
|
119 |
– |
/** |
120 |
– |
* Elements of non-comparable classes equal to those of classes |
121 |
– |
* with erased generic type parameters based on Comparable can be |
122 |
– |
* inserted and found. |
123 |
– |
*/ |
124 |
– |
public void testGenericComparable2() { |
125 |
– |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
126 |
– |
for (int i = 0; i < 1000; i++) { |
127 |
– |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
128 |
– |
} |
129 |
– |
|
130 |
– |
for (int i = 0; i < 1000; i++) { |
131 |
– |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
132 |
– |
assertTrue(m.containsKey(bis)); |
133 |
– |
} |
134 |
– |
} |
135 |
– |
|
52 |
|
/** |
53 |
|
* getOrDefault returns value if present, else default |
54 |
|
*/ |
63 |
|
*/ |
64 |
|
public void testComputeIfAbsent() { |
65 |
|
ConcurrentHashMap map = map5(); |
66 |
< |
map.computeIfAbsent(six, (x) -> "Z"); |
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 |
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")); |
75 |
> |
assertEquals("A", map.computeIfAbsent(one, x -> "Z")); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
80 |
|
*/ |
81 |
|
public void testComputeIfAbsent3() { |
82 |
|
ConcurrentHashMap map = map5(); |
83 |
< |
map.computeIfAbsent(six, (x) -> null); |
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 |
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(); |
164 |
|
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
165 |
|
assertTrue(a.isEmpty()); |
166 |
|
for (int i = 0; i < n; i++) |
167 |
< |
a.add(i); |
168 |
< |
assertFalse(a.isEmpty()); |
167 |
> |
assertTrue(a.add(i)); |
168 |
> |
assertEquals(n == 0, a.isEmpty()); |
169 |
|
assertEquals(n, a.size()); |
170 |
|
return a; |
171 |
|
} |
174 |
|
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
175 |
|
assertTrue(a.isEmpty()); |
176 |
|
for (int i = 0; i < elements.length; i++) |
177 |
< |
a.add(elements[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) -> (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 |
+ |
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 |
< |
Vector v = new Vector(); |
232 |
< |
v.add(three); |
233 |
< |
v.add(four); |
283 |
< |
v.add(five); |
284 |
< |
full.addAll(v); |
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 |
|
|
240 |
|
*/ |
241 |
|
public void testAddAll2() { |
242 |
|
Set full = populatedSet(3); |
243 |
< |
Vector v = new Vector(); |
244 |
< |
v.add(three); |
245 |
< |
v.add(four); |
246 |
< |
v.add(one); // will not add this element |
298 |
< |
full.addAll(v); |
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 |
|
|
252 |
|
*/ |
253 |
|
public void testAdd2() { |
254 |
|
Set full = populatedSet(3); |
255 |
< |
full.add(one); |
255 |
> |
assertFalse(full.add(one)); |
256 |
|
assertEquals(3, full.size()); |
257 |
|
} |
258 |
|
|
261 |
|
*/ |
262 |
|
public void testAdd3() { |
263 |
|
Set full = populatedSet(3); |
264 |
< |
full.add(three); |
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() { |
375 |
|
* KeySet.containsAll returns true for collections with subset of elements |
376 |
|
*/ |
377 |
|
public void testContainsAll() { |
378 |
< |
Set full = populatedSet(3); |
379 |
< |
Vector v = new Vector(); |
380 |
< |
v.add(one); |
381 |
< |
v.add(two); |
382 |
< |
assertTrue(full.containsAll(v)); |
383 |
< |
v.add(six); |
366 |
< |
assertFalse(full.containsAll(v)); |
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 |
< |
Set empty = ConcurrentHashMap.newKeySet(); |
391 |
< |
Set full = populatedSet(3); |
375 |
< |
assertTrue(empty.isEmpty()); |
376 |
< |
assertFalse(full.isEmpty()); |
390 |
> |
assertTrue(populatedSet(0).isEmpty()); |
391 |
> |
assertFalse(populatedSet(3).isEmpty()); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
407 |
|
Integer[] elements = new Integer[size]; |
408 |
|
for (int i = 0; i < size; i++) |
409 |
|
elements[i] = i; |
410 |
< |
Collections.shuffle(Arrays.asList(elements)); |
410 |
> |
shuffle(elements); |
411 |
|
Collection<Integer> full = populatedSet(elements); |
412 |
|
|
413 |
|
Iterator it = full.iterator(); |
415 |
|
assertTrue(it.hasNext()); |
416 |
|
it.next(); |
417 |
|
} |
418 |
< |
assertFalse(it.hasNext()); |
419 |
< |
try { |
420 |
< |
it.next(); |
421 |
< |
shouldThrow(); |
422 |
< |
} catch (NoSuchElementException success) {} |
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 |
|
/** |
459 |
|
*/ |
460 |
|
public void testRemoveAll() { |
461 |
|
Set full = populatedSet(3); |
462 |
< |
Vector v = new Vector(); |
463 |
< |
v.add(one); |
464 |
< |
v.add(two); |
444 |
< |
full.removeAll(v); |
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 |
|
|
497 |
|
Integer[] elements = new Integer[size]; |
498 |
|
for (int i = 0; i < size; i++) |
499 |
|
elements[i] = i; |
500 |
< |
Collections.shuffle(Arrays.asList(elements)); |
500 |
> |
shuffle(elements); |
501 |
|
Collection<Integer> full = populatedSet(elements); |
502 |
|
|
503 |
|
assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray()))); |
517 |
|
a = new Integer[0]; |
518 |
|
assertSame(a, empty.toArray(a)); |
519 |
|
|
520 |
< |
a = new Integer[size/2]; |
520 |
> |
a = new Integer[size / 2]; |
521 |
|
Arrays.fill(a, 42); |
522 |
|
assertSame(a, empty.toArray(a)); |
523 |
|
assertNull(a[0]); |
527 |
|
Integer[] elements = new Integer[size]; |
528 |
|
for (int i = 0; i < size; i++) |
529 |
|
elements[i] = i; |
530 |
< |
Collections.shuffle(Arrays.asList(elements)); |
530 |
> |
shuffle(elements); |
531 |
|
Collection<Integer> full = populatedSet(elements); |
532 |
|
|
533 |
|
Arrays.fill(a, 42); |
543 |
|
} |
544 |
|
|
545 |
|
/** |
546 |
< |
* A deserialized serialized set is equal |
546 |
> |
* A deserialized/reserialized set equals original |
547 |
|
*/ |
548 |
|
public void testSerialization() throws Exception { |
549 |
|
int size = 20; |
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()); |
535 |
– |
assertEquals(x.toString(), y.toString()); |
536 |
– |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
555 |
|
assertEquals(x, y); |
556 |
|
assertEquals(y, x); |
557 |
|
} |
558 |
|
|
541 |
– |
|
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 |
|
|
739 |
– |
|
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 |
|
|
760 |
– |
|
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 |
|
|
863 |
– |
|
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 |
|
|
1092 |
|
/** |
1093 |
< |
* Invoking task versions of bulk methods has same effect as |
1094 |
< |
* parallel methods |
1095 |
< |
*/ |
1096 |
< |
public void testForkJoinTasks() { |
1097 |
< |
LongAdder adder = new LongAdder(); |
1098 |
< |
ConcurrentHashMap<Long, Long> m = longMap(); |
1099 |
< |
ConcurrentHashMap.ForkJoinTasks.forEachKey |
1100 |
< |
(m, (Long x) -> adder.add(x.longValue())).invoke(); |
1101 |
< |
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
1102 |
< |
adder.reset(); |
1103 |
< |
ConcurrentHashMap.ForkJoinTasks.forEachValue |
1104 |
< |
(m, (Long x) -> adder.add(x.longValue())).invoke(); |
1105 |
< |
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
1106 |
< |
adder.reset(); |
1107 |
< |
ConcurrentHashMap.ForkJoinTasks.forEach |
1108 |
< |
(m, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())).invoke(); |
1109 |
< |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1110 |
< |
adder.reset(); |
1111 |
< |
ConcurrentHashMap.ForkJoinTasks.forEachEntry |
1112 |
< |
(m, |
1113 |
< |
(Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())).invoke(); |
1114 |
< |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1115 |
< |
adder.reset(); |
1100 |
< |
ConcurrentHashMap.ForkJoinTasks.forEachKey |
1101 |
< |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1102 |
< |
(Long x) -> adder.add(x.longValue())).invoke(); |
1103 |
< |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
1104 |
< |
adder.reset(); |
1105 |
< |
ConcurrentHashMap.ForkJoinTasks.forEachValue |
1106 |
< |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1107 |
< |
(Long x) -> adder.add(x.longValue())).invoke(); |
1108 |
< |
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
1109 |
< |
adder.reset(); |
1110 |
< |
ConcurrentHashMap.ForkJoinTasks.forEach |
1111 |
< |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
1112 |
< |
(Long x) -> adder.add(x.longValue())).invoke(); |
1113 |
< |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1114 |
< |
adder.reset(); |
1115 |
< |
ConcurrentHashMap.ForkJoinTasks.forEachEntry |
1116 |
< |
(m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
1117 |
< |
(Long x) -> adder.add(x.longValue())).invoke(); |
1118 |
< |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1119 |
< |
adder.reset(); |
1120 |
< |
|
1121 |
< |
Long r; long lr; int ir; double dr; |
1122 |
< |
r = ConcurrentHashMap.ForkJoinTasks.reduceKeys |
1123 |
< |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1124 |
< |
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
1125 |
< |
r = ConcurrentHashMap.ForkJoinTasks.reduceValues |
1126 |
< |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1127 |
< |
assertEquals((long)r, (long)SIZE * (SIZE - 1)); |
1128 |
< |
r = ConcurrentHashMap.ForkJoinTasks.reduce |
1129 |
< |
(m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
1130 |
< |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1131 |
< |
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
1132 |
< |
r = ConcurrentHashMap.ForkJoinTasks.reduceEntries |
1133 |
< |
(m, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
1134 |
< |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1135 |
< |
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
1136 |
< |
r = ConcurrentHashMap.ForkJoinTasks.reduceKeys |
1137 |
< |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1138 |
< |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1139 |
< |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
1140 |
< |
lr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToLong |
1141 |
< |
(m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); |
1142 |
< |
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
1143 |
< |
ir = ConcurrentHashMap.ForkJoinTasks.reduceKeysToInt |
1144 |
< |
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
1145 |
< |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
1146 |
< |
dr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToDouble |
1147 |
< |
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
1148 |
< |
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
1149 |
< |
r = ConcurrentHashMap.ForkJoinTasks.reduceValues |
1150 |
< |
(m, (Long x) -> Long.valueOf(4 * x.longValue()), |
1151 |
< |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); |
1152 |
< |
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
1153 |
< |
lr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToLong |
1154 |
< |
(m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); |
1155 |
< |
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
1156 |
< |
ir = ConcurrentHashMap.ForkJoinTasks.reduceValuesToInt |
1157 |
< |
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
1158 |
< |
assertEquals(ir, SIZE * (SIZE - 1)); |
1159 |
< |
dr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToDouble |
1160 |
< |
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
1161 |
< |
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
1162 |
< |
r = ConcurrentHashMap.ForkJoinTasks.searchKeys |
1163 |
< |
(m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
1164 |
< |
assertEquals((long)r, (long)(SIZE/2)); |
1165 |
< |
r = ConcurrentHashMap.ForkJoinTasks.searchValues |
1166 |
< |
(m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
1167 |
< |
assertEquals((long)r, (long)(SIZE/2)); |
1168 |
< |
r = ConcurrentHashMap.ForkJoinTasks.search |
1169 |
< |
(m, (Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); |
1170 |
< |
assertEquals((long)r, (long)(SIZE/2)); |
1171 |
< |
r = ConcurrentHashMap.ForkJoinTasks.searchEntries |
1172 |
< |
(m, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null).invoke(); |
1173 |
< |
assertEquals((long)r, (long)(SIZE/2)); |
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 |
|
} |