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