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