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 |
|
|
* 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 |
dl |
1.3 |
* 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 |
dl |
1.1 |
* computeIfAbsent adds when the given key is not present |
63 |
|
|
*/ |
64 |
|
|
public void testComputeIfAbsent() { |
65 |
|
|
ConcurrentHashMap 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 |
|
|
ConcurrentHashMap map = map5(); |
75 |
jsr166 |
1.33 |
assertEquals("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 |
|
|
ConcurrentHashMap 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 |
|
|
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 |
jsr166 |
1.14 |
* compute does not replace if the function returns null |
106 |
dl |
1.1 |
*/ |
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 |
dl |
1.3 |
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 |
jsr166 |
1.27 |
assertTrue(a.add(i)); |
168 |
jsr166 |
1.23 |
assertEquals(n == 0, a.isEmpty()); |
169 |
dl |
1.3 |
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 |
jsr166 |
1.27 |
assertTrue(a.add(elements[i])); |
178 |
dl |
1.3 |
assertFalse(a.isEmpty()); |
179 |
|
|
assertEquals(elements.length, a.size()); |
180 |
|
|
return a; |
181 |
|
|
} |
182 |
|
|
|
183 |
jsr166 |
1.19 |
/** |
184 |
dl |
1.12 |
* replaceAll replaces all matching values. |
185 |
|
|
*/ |
186 |
|
|
public void testReplaceAll() { |
187 |
|
|
ConcurrentHashMap<Integer, String> map = map5(); |
188 |
jsr166 |
1.36 |
map.replaceAll((x, y) -> (x > 3) ? "Z" : y); |
189 |
dl |
1.12 |
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 |
dl |
1.3 |
/** |
197 |
|
|
* Default-constructed set is empty |
198 |
|
|
*/ |
199 |
|
|
public void testNewKeySet() { |
200 |
|
|
Set a = ConcurrentHashMap.newKeySet(); |
201 |
|
|
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 |
|
|
ConcurrentHashMap map = map5(); |
210 |
|
|
Set set1 = map.keySet(); |
211 |
|
|
Set set2 = map.keySet(true); |
212 |
|
|
set2.add(six); |
213 |
jsr166 |
1.34 |
assertSame(map, ((ConcurrentHashMap.KeySetView)set2).getMap()); |
214 |
|
|
assertSame(map, ((ConcurrentHashMap.KeySetView)set1).getMap()); |
215 |
dl |
1.12 |
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 |
dl |
1.3 |
* keySet.addAll adds each element from the given collection |
228 |
|
|
*/ |
229 |
|
|
public void testAddAll() { |
230 |
|
|
Set full = populatedSet(3); |
231 |
jsr166 |
1.23 |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
232 |
|
|
assertEquals(6, full.size()); |
233 |
|
|
assertFalse(full.addAll(Arrays.asList(three, four, five))); |
234 |
dl |
1.3 |
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 |
jsr166 |
1.23 |
// "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 |
dl |
1.3 |
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 |
jsr166 |
1.24 |
assertFalse(full.add(one)); |
256 |
dl |
1.3 |
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 |
jsr166 |
1.24 |
assertTrue(full.add(three)); |
265 |
|
|
assertTrue(full.contains(three)); |
266 |
|
|
assertFalse(full.add(three)); |
267 |
dl |
1.3 |
assertTrue(full.contains(three)); |
268 |
|
|
} |
269 |
|
|
|
270 |
jsr166 |
1.13 |
/** |
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 |
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 |
|
|
Set full = populatedSet(3); |
288 |
|
|
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 |
|
|
ConcurrentHashMap map = map5(); |
299 |
|
|
assertNull(map.keySet().getMappedValue()); |
300 |
|
|
try { |
301 |
|
|
map.keySet(null); |
302 |
|
|
shouldThrow(); |
303 |
jsr166 |
1.26 |
} catch (NullPointerException success) {} |
304 |
jsr166 |
1.18 |
ConcurrentHashMap.KeySetView set = map.keySet(one); |
305 |
jsr166 |
1.27 |
assertFalse(set.add(one)); |
306 |
|
|
assertTrue(set.add(six)); |
307 |
|
|
assertTrue(set.add(seven)); |
308 |
jsr166 |
1.34 |
assertSame(one, set.getMappedValue()); |
309 |
|
|
assertNotSame(one, map.get(one)); |
310 |
|
|
assertSame(one, map.get(six)); |
311 |
|
|
assertSame(one, map.get(seven)); |
312 |
jsr166 |
1.13 |
} |
313 |
dl |
1.12 |
|
314 |
jsr166 |
1.15 |
void checkSpliteratorCharacteristics(Spliterator<?> sp, |
315 |
|
|
int requiredCharacteristics) { |
316 |
|
|
assertEquals(requiredCharacteristics, |
317 |
|
|
requiredCharacteristics & sp.characteristics()); |
318 |
|
|
} |
319 |
jsr166 |
1.16 |
|
320 |
jsr166 |
1.13 |
/** |
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 |
jsr166 |
1.15 |
checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL); |
329 |
jsr166 |
1.13 |
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 |
dl |
1.12 |
|
338 |
dl |
1.3 |
/** |
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 |
jsr166 |
1.23 |
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 |
dl |
1.3 |
} |
385 |
|
|
|
386 |
|
|
/** |
387 |
|
|
* KeySet.isEmpty is true when empty, else false |
388 |
|
|
*/ |
389 |
|
|
public void testIsEmpty() { |
390 |
jsr166 |
1.23 |
assertTrue(populatedSet(0).isEmpty()); |
391 |
|
|
assertFalse(populatedSet(3).isEmpty()); |
392 |
dl |
1.3 |
} |
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 |
jsr166 |
1.30 |
shuffle(elements); |
411 |
dl |
1.3 |
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 |
jsr166 |
1.22 |
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 |
dl |
1.3 |
} |
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 |
jsr166 |
1.23 |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
463 |
|
|
assertEquals(1, full.size()); |
464 |
|
|
assertFalse(full.removeAll(Arrays.asList(one, two))); |
465 |
dl |
1.3 |
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 |
jsr166 |
1.30 |
shuffle(elements); |
501 |
dl |
1.3 |
Collection<Integer> full = populatedSet(elements); |
502 |
jsr166 |
1.4 |
|
503 |
dl |
1.3 |
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 |
jsr166 |
1.29 |
a = new Integer[size / 2]; |
521 |
dl |
1.3 |
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 |
jsr166 |
1.30 |
shuffle(elements); |
531 |
dl |
1.3 |
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 |
jsr166 |
1.35 |
* A deserialized/reserialized set equals original |
547 |
dl |
1.3 |
*/ |
548 |
|
|
public void testSerialization() throws Exception { |
549 |
|
|
int size = 20; |
550 |
|
|
Set x = populatedSet(size); |
551 |
|
|
Set y = serialClone(x); |
552 |
|
|
|
553 |
jsr166 |
1.10 |
assertNotSame(x, y); |
554 |
dl |
1.3 |
assertEquals(x.size(), y.size()); |
555 |
|
|
assertEquals(x, y); |
556 |
|
|
assertEquals(y, x); |
557 |
|
|
} |
558 |
|
|
|
559 |
dl |
1.2 |
static final int SIZE = 10000; |
560 |
|
|
static ConcurrentHashMap<Long, Long> longMap; |
561 |
jsr166 |
1.4 |
|
562 |
dl |
1.2 |
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 |
dl |
1.3 |
// 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 |
jsr166 |
1.4 |
(Long.valueOf(x.getKey().longValue() + y.getKey().longValue()), |
576 |
dl |
1.3 |
Long.valueOf(1L)); |
577 |
|
|
} |
578 |
|
|
} |
579 |
|
|
|
580 |
dl |
1.2 |
/** |
581 |
dl |
1.3 |
* forEachKeySequentially traverses all keys |
582 |
dl |
1.2 |
*/ |
583 |
dl |
1.3 |
public void testForEachKeySequentially() { |
584 |
dl |
1.2 |
LongAdder adder = new LongAdder(); |
585 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
586 |
dl |
1.9 |
m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); |
587 |
dl |
1.2 |
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
588 |
dl |
1.3 |
} |
589 |
|
|
|
590 |
|
|
/** |
591 |
|
|
* forEachValueSequentially traverses all values |
592 |
|
|
*/ |
593 |
|
|
public void testForEachValueSequentially() { |
594 |
|
|
LongAdder adder = new LongAdder(); |
595 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
596 |
dl |
1.9 |
m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); |
597 |
dl |
1.2 |
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
598 |
dl |
1.3 |
} |
599 |
|
|
|
600 |
|
|
/** |
601 |
|
|
* forEachSequentially traverses all mappings |
602 |
|
|
*/ |
603 |
|
|
public void testForEachSequentially() { |
604 |
|
|
LongAdder adder = new LongAdder(); |
605 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
606 |
dl |
1.9 |
m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
607 |
dl |
1.2 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
608 |
dl |
1.3 |
} |
609 |
|
|
|
610 |
|
|
/** |
611 |
|
|
* forEachEntrySequentially traverses all entries |
612 |
|
|
*/ |
613 |
|
|
public void testForEachEntrySequentially() { |
614 |
|
|
LongAdder adder = new LongAdder(); |
615 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
616 |
dl |
1.9 |
m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
617 |
dl |
1.2 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
618 |
dl |
1.3 |
} |
619 |
|
|
|
620 |
|
|
/** |
621 |
|
|
* forEachKeyInParallel traverses all keys |
622 |
|
|
*/ |
623 |
|
|
public void testForEachKeyInParallel() { |
624 |
|
|
LongAdder adder = new LongAdder(); |
625 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
626 |
dl |
1.9 |
m.forEachKey(1L, (Long x) -> adder.add(x.longValue())); |
627 |
dl |
1.2 |
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); |
628 |
dl |
1.3 |
} |
629 |
|
|
|
630 |
|
|
/** |
631 |
|
|
* forEachValueInParallel traverses all values |
632 |
|
|
*/ |
633 |
|
|
public void testForEachValueInParallel() { |
634 |
|
|
LongAdder adder = new LongAdder(); |
635 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
636 |
dl |
1.9 |
m.forEachValue(1L, (Long x) -> adder.add(x.longValue())); |
637 |
dl |
1.2 |
assertEquals(adder.sum(), SIZE * (SIZE - 1)); |
638 |
dl |
1.3 |
} |
639 |
|
|
|
640 |
|
|
/** |
641 |
|
|
* forEachInParallel traverses all mappings |
642 |
|
|
*/ |
643 |
|
|
public void testForEachInParallel() { |
644 |
|
|
LongAdder adder = new LongAdder(); |
645 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
646 |
dl |
1.9 |
m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())); |
647 |
dl |
1.2 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
648 |
dl |
1.3 |
} |
649 |
|
|
|
650 |
|
|
/** |
651 |
|
|
* forEachEntryInParallel traverses all entries |
652 |
|
|
*/ |
653 |
|
|
public void testForEachEntryInParallel() { |
654 |
|
|
LongAdder adder = new LongAdder(); |
655 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
656 |
dl |
1.9 |
m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); |
657 |
dl |
1.2 |
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
658 |
|
|
} |
659 |
|
|
|
660 |
|
|
/** |
661 |
dl |
1.3 |
* Mapped forEachKeySequentially traverses the given |
662 |
|
|
* transformations of all keys |
663 |
dl |
1.2 |
*/ |
664 |
dl |
1.3 |
public void testMappedForEachKeySequentially() { |
665 |
dl |
1.2 |
LongAdder adder = new LongAdder(); |
666 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
667 |
dl |
1.9 |
m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
668 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
669 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
670 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
680 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
681 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
682 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
692 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
693 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
694 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
704 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
705 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
706 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
716 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
717 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); |
718 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
728 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
729 |
|
|
assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); |
730 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
740 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
741 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
742 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), |
752 |
dl |
1.2 |
(Long x) -> adder.add(x.longValue())); |
753 |
|
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
754 |
|
|
} |
755 |
|
|
|
756 |
dl |
1.3 |
/** |
757 |
|
|
* reduceKeysSequentially accumulates across all keys, |
758 |
|
|
*/ |
759 |
|
|
public void testReduceKeysSequentially() { |
760 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
761 |
|
|
Long r; |
762 |
dl |
1.9 |
r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
763 |
dl |
1.3 |
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
764 |
|
|
} |
765 |
|
|
|
766 |
dl |
1.2 |
/** |
767 |
dl |
1.3 |
* reduceValuesSequentially accumulates across all values |
768 |
dl |
1.2 |
*/ |
769 |
dl |
1.3 |
public void testReduceValuesSequentially() { |
770 |
dl |
1.2 |
ConcurrentHashMap<Long, Long> m = longMap(); |
771 |
|
|
Long r; |
772 |
dl |
1.9 |
r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
773 |
dl |
1.2 |
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
774 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
r = m.reduceEntries(Long.MAX_VALUE, new AddKeys()); |
783 |
dl |
1.3 |
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 |
dl |
1.9 |
r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
793 |
dl |
1.3 |
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 |
dl |
1.9 |
r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
803 |
dl |
1.2 |
assertEquals((long)r, (long)SIZE * (SIZE - 1)); |
804 |
dl |
1.3 |
} |
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 |
dl |
1.9 |
r = m.reduceEntries(1L, new AddKeys()); |
813 |
dl |
1.3 |
assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); |
814 |
|
|
} |
815 |
|
|
|
816 |
jsr166 |
1.7 |
/** |
817 |
dl |
1.3 |
* Mapped reduceKeysSequentially accumulates mapped keys |
818 |
|
|
*/ |
819 |
|
|
public void testMapReduceKeysSequentially() { |
820 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
821 |
dl |
1.9 |
Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
822 |
dl |
1.3 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
823 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
824 |
|
|
} |
825 |
|
|
|
826 |
jsr166 |
1.7 |
/** |
827 |
dl |
1.3 |
* Mapped reduceValuesSequentially accumulates mapped values |
828 |
|
|
*/ |
829 |
|
|
public void testMapReduceValuesSequentially() { |
830 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
831 |
dl |
1.9 |
Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), |
832 |
dl |
1.3 |
(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 |
dl |
1.9 |
Long r = m.reduce(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
842 |
dl |
1.2 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
843 |
jsr166 |
1.4 |
|
844 |
dl |
1.2 |
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
845 |
dl |
1.3 |
} |
846 |
|
|
|
847 |
jsr166 |
1.7 |
/** |
848 |
dl |
1.3 |
* Mapped reduceKeysInParallel, accumulates mapped keys |
849 |
|
|
*/ |
850 |
|
|
public void testMapReduceKeysInParallel() { |
851 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
852 |
dl |
1.9 |
Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
853 |
dl |
1.3 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
854 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
855 |
|
|
} |
856 |
|
|
|
857 |
jsr166 |
1.7 |
/** |
858 |
dl |
1.3 |
* Mapped reduceValuesInParallel accumulates mapped values |
859 |
|
|
*/ |
860 |
|
|
public void testMapReduceValuesInParallel() { |
861 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
862 |
dl |
1.9 |
Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()), |
863 |
dl |
1.3 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
864 |
|
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); |
865 |
|
|
} |
866 |
dl |
1.2 |
|
867 |
dl |
1.3 |
/** |
868 |
|
|
* reduceInParallel accumulate across all transformed mappings |
869 |
|
|
*/ |
870 |
|
|
public void testMappedReduceInParallel() { |
871 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
872 |
|
|
Long r; |
873 |
dl |
1.9 |
r = m.reduce(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
874 |
dl |
1.2 |
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
875 |
|
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
876 |
|
|
} |
877 |
|
|
|
878 |
jsr166 |
1.7 |
/** |
879 |
dl |
1.3 |
* reduceKeysToLongSequentially accumulates mapped keys |
880 |
dl |
1.2 |
*/ |
881 |
dl |
1.3 |
public void testReduceKeysToLongSequentially() { |
882 |
dl |
1.2 |
ConcurrentHashMap<Long, Long> m = longMap(); |
883 |
dl |
1.9 |
long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum); |
884 |
dl |
1.2 |
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
885 |
dl |
1.3 |
} |
886 |
dl |
1.2 |
|
887 |
jsr166 |
1.7 |
/** |
888 |
dl |
1.3 |
* reduceKeysToIntSequentially accumulates mapped keys |
889 |
|
|
*/ |
890 |
|
|
public void testReduceKeysToIntSequentially() { |
891 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
892 |
dl |
1.9 |
int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); |
893 |
jsr166 |
1.5 |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
894 |
dl |
1.3 |
} |
895 |
|
|
|
896 |
jsr166 |
1.7 |
/** |
897 |
dl |
1.3 |
* reduceKeysToDoubleSequentially accumulates mapped keys |
898 |
|
|
*/ |
899 |
|
|
public void testReduceKeysToDoubleSequentially() { |
900 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
901 |
dl |
1.9 |
double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
902 |
dl |
1.2 |
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
903 |
dl |
1.3 |
} |
904 |
dl |
1.2 |
|
905 |
jsr166 |
1.7 |
/** |
906 |
dl |
1.3 |
* reduceValuesToLongSequentially accumulates mapped values |
907 |
|
|
*/ |
908 |
|
|
public void testReduceValuesToLongSequentially() { |
909 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
910 |
dl |
1.9 |
long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum); |
911 |
dl |
1.2 |
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
912 |
dl |
1.3 |
} |
913 |
|
|
|
914 |
jsr166 |
1.7 |
/** |
915 |
dl |
1.3 |
* reduceValuesToIntSequentially accumulates mapped values |
916 |
|
|
*/ |
917 |
|
|
public void testReduceValuesToIntSequentially() { |
918 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
919 |
dl |
1.9 |
int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); |
920 |
jsr166 |
1.5 |
assertEquals(ir, SIZE * (SIZE - 1)); |
921 |
dl |
1.3 |
} |
922 |
dl |
1.2 |
|
923 |
jsr166 |
1.7 |
/** |
924 |
dl |
1.3 |
* reduceValuesToDoubleSequentially accumulates mapped values |
925 |
|
|
*/ |
926 |
|
|
public void testReduceValuesToDoubleSequentially() { |
927 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
928 |
dl |
1.9 |
double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
929 |
dl |
1.2 |
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
930 |
dl |
1.3 |
} |
931 |
dl |
1.2 |
|
932 |
jsr166 |
1.7 |
/** |
933 |
dl |
1.3 |
* reduceKeysToLongInParallel accumulates mapped keys |
934 |
|
|
*/ |
935 |
|
|
public void testReduceKeysToLongInParallel() { |
936 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
937 |
dl |
1.9 |
long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); |
938 |
dl |
1.2 |
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
939 |
dl |
1.3 |
} |
940 |
|
|
|
941 |
jsr166 |
1.7 |
/** |
942 |
dl |
1.3 |
* reduceKeysToIntInParallel accumulates mapped keys |
943 |
|
|
*/ |
944 |
|
|
public void testReduceKeysToIntInParallel() { |
945 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
946 |
dl |
1.9 |
int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); |
947 |
jsr166 |
1.5 |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
948 |
dl |
1.3 |
} |
949 |
|
|
|
950 |
jsr166 |
1.7 |
/** |
951 |
dl |
1.3 |
* reduceKeysToDoubleInParallel accumulates mapped values |
952 |
|
|
*/ |
953 |
|
|
public void testReduceKeysToDoubleInParallel() { |
954 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
955 |
dl |
1.9 |
double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
956 |
dl |
1.2 |
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
957 |
dl |
1.3 |
} |
958 |
dl |
1.2 |
|
959 |
jsr166 |
1.7 |
/** |
960 |
dl |
1.3 |
* reduceValuesToLongInParallel accumulates mapped values |
961 |
|
|
*/ |
962 |
|
|
public void testReduceValuesToLongInParallel() { |
963 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
964 |
dl |
1.9 |
long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); |
965 |
dl |
1.2 |
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
966 |
dl |
1.3 |
} |
967 |
|
|
|
968 |
jsr166 |
1.7 |
/** |
969 |
dl |
1.3 |
* reduceValuesToIntInParallel accumulates mapped values |
970 |
|
|
*/ |
971 |
|
|
public void testReduceValuesToIntInParallel() { |
972 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
973 |
dl |
1.9 |
int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); |
974 |
jsr166 |
1.5 |
assertEquals(ir, SIZE * (SIZE - 1)); |
975 |
dl |
1.3 |
} |
976 |
|
|
|
977 |
jsr166 |
1.7 |
/** |
978 |
dl |
1.3 |
* reduceValuesToDoubleInParallel accumulates mapped values |
979 |
|
|
*/ |
980 |
|
|
public void testReduceValuesToDoubleInParallel() { |
981 |
|
|
ConcurrentHashMap<Long, Long> m = longMap(); |
982 |
dl |
1.9 |
double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); |
983 |
dl |
1.2 |
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
984 |
|
|
} |
985 |
|
|
|
986 |
|
|
/** |
987 |
dl |
1.3 |
* searchKeysSequentially returns a non-null result of search |
988 |
|
|
* function, or null if none |
989 |
dl |
1.2 |
*/ |
990 |
dl |
1.3 |
public void testSearchKeysSequentially() { |
991 |
dl |
1.2 |
ConcurrentHashMap<Long, Long> m = longMap(); |
992 |
|
|
Long r; |
993 |
dl |
1.9 |
r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
994 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
995 |
dl |
1.9 |
r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); |
996 |
dl |
1.3 |
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 |
jsr166 |
1.20 |
r = m.searchValues(Long.MAX_VALUE, |
1007 |
|
|
(Long x) -> (x.longValue() == (long)(SIZE/2)) ? x : null); |
1008 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
1009 |
jsr166 |
1.20 |
r = m.searchValues(Long.MAX_VALUE, |
1010 |
|
|
(Long x) -> (x.longValue() < 0L) ? x : null); |
1011 |
dl |
1.3 |
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 |
dl |
1.9 |
r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1022 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
1023 |
dl |
1.9 |
r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null); |
1024 |
dl |
1.3 |
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 |
dl |
1.9 |
r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
1035 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
1036 |
dl |
1.9 |
r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1037 |
dl |
1.2 |
assertNull(r); |
1038 |
dl |
1.3 |
} |
1039 |
dl |
1.2 |
|
1040 |
dl |
1.3 |
/** |
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 |
dl |
1.9 |
r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1048 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
1049 |
dl |
1.9 |
r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null); |
1050 |
dl |
1.3 |
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 |
dl |
1.9 |
r = m.searchValues(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1061 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
1062 |
dl |
1.9 |
r = m.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null); |
1063 |
dl |
1.3 |
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 |
dl |
1.9 |
r = m.search(1L, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1074 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
1075 |
dl |
1.9 |
r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null); |
1076 |
dl |
1.3 |
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 |
dl |
1.9 |
r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
1087 |
dl |
1.2 |
assertEquals((long)r, (long)(SIZE/2)); |
1088 |
dl |
1.9 |
r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1089 |
dl |
1.2 |
assertNull(r); |
1090 |
|
|
} |
1091 |
|
|
|
1092 |
jsr166 |
1.32 |
/** |
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 |
jsr166 |
1.33 |
result += map.computeIfAbsent(i % mapSize, k -> k + k); |
1111 |
jsr166 |
1.32 |
if (result == -42) throw new Error(); |
1112 |
|
|
}}; |
1113 |
|
|
for (int i = 0; i < threads; i++) |
1114 |
|
|
pool.execute(r); |
1115 |
|
|
} |
1116 |
|
|
} |
1117 |
|
|
|
1118 |
dl |
1.1 |
} |