4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.function.*; |
10 |
< |
import java.util.concurrent.atomic.LongAdder; |
7 |
> |
import static java.util.Spliterator.CONCURRENT; |
8 |
> |
import static java.util.Spliterator.DISTINCT; |
9 |
> |
import static java.util.Spliterator.NONNULL; |
10 |
> |
|
11 |
> |
import java.util.AbstractMap; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.Iterator; |
15 |
> |
import java.util.Map; |
16 |
> |
import java.util.NoSuchElementException; |
17 |
> |
import java.util.Set; |
18 |
> |
import java.util.Spliterator; |
19 |
|
import java.util.concurrent.ConcurrentHashMap; |
20 |
+ |
import java.util.concurrent.atomic.LongAdder; |
21 |
+ |
import java.util.function.BiFunction; |
22 |
+ |
|
23 |
+ |
import junit.framework.Test; |
24 |
+ |
import junit.framework.TestSuite; |
25 |
|
|
26 |
|
public class ConcurrentHashMap8Test extends JSR166TestCase { |
27 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run(suite()); |
28 |
> |
main(suite(), args); |
29 |
|
} |
30 |
|
public static Test suite() { |
31 |
|
return new TestSuite(ConcurrentHashMap8Test.class); |
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* computeIfAbsent does not replace if the key is already present |
69 |
> |
* computeIfAbsent does not replace if the key is already present |
70 |
|
*/ |
71 |
|
public void testComputeIfAbsent2() { |
72 |
|
ConcurrentHashMap map = map5(); |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* computeIfPresent does not replace if the key is already present |
86 |
> |
* computeIfPresent does not replace if the key is already present |
87 |
|
*/ |
88 |
|
public void testComputeIfPresent() { |
89 |
|
ConcurrentHashMap map = map5(); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* compute does not replace if the function returns null |
103 |
> |
* compute does not replace if the function returns null |
104 |
|
*/ |
105 |
|
public void testCompute() { |
106 |
|
ConcurrentHashMap map = map5(); |
162 |
|
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
163 |
|
assertTrue(a.isEmpty()); |
164 |
|
for (int i = 0; i < n; i++) |
165 |
< |
a.add(i); |
166 |
< |
assertFalse(a.isEmpty()); |
165 |
> |
assertTrue(a.add(i)); |
166 |
> |
assertEquals(n == 0, a.isEmpty()); |
167 |
|
assertEquals(n, a.size()); |
168 |
|
return a; |
169 |
|
} |
172 |
|
Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet(); |
173 |
|
assertTrue(a.isEmpty()); |
174 |
|
for (int i = 0; i < elements.length; i++) |
175 |
< |
a.add(elements[i]); |
175 |
> |
assertTrue(a.add(elements[i])); |
176 |
|
assertFalse(a.isEmpty()); |
177 |
|
assertEquals(elements.length, a.size()); |
178 |
|
return a; |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
+ |
* replaceAll replaces all matching values. |
183 |
+ |
*/ |
184 |
+ |
public void testReplaceAll() { |
185 |
+ |
ConcurrentHashMap<Integer, String> map = map5(); |
186 |
+ |
map.replaceAll((x, y) -> { return x > 3 ? "Z" : y; }); |
187 |
+ |
assertEquals("A", map.get(one)); |
188 |
+ |
assertEquals("B", map.get(two)); |
189 |
+ |
assertEquals("C", map.get(three)); |
190 |
+ |
assertEquals("Z", map.get(four)); |
191 |
+ |
assertEquals("Z", map.get(five)); |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
/** |
195 |
|
* Default-constructed set is empty |
196 |
|
*/ |
197 |
|
public void testNewKeySet() { |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
+ |
* keySet.add adds the key with the established value to the map; |
204 |
+ |
* remove removes it. |
205 |
+ |
*/ |
206 |
+ |
public void testKeySetAddRemove() { |
207 |
+ |
ConcurrentHashMap map = map5(); |
208 |
+ |
Set set1 = map.keySet(); |
209 |
+ |
Set set2 = map.keySet(true); |
210 |
+ |
set2.add(six); |
211 |
+ |
assertTrue(((ConcurrentHashMap.KeySetView)set2).getMap() == map); |
212 |
+ |
assertTrue(((ConcurrentHashMap.KeySetView)set1).getMap() == map); |
213 |
+ |
assertEquals(set2.size(), map.size()); |
214 |
+ |
assertEquals(set1.size(), map.size()); |
215 |
+ |
assertTrue((Boolean)map.get(six)); |
216 |
+ |
assertTrue(set1.contains(six)); |
217 |
+ |
assertTrue(set2.contains(six)); |
218 |
+ |
set2.remove(six); |
219 |
+ |
assertNull(map.get(six)); |
220 |
+ |
assertFalse(set1.contains(six)); |
221 |
+ |
assertFalse(set2.contains(six)); |
222 |
+ |
} |
223 |
+ |
|
224 |
+ |
/** |
225 |
|
* keySet.addAll adds each element from the given collection |
226 |
|
*/ |
227 |
|
public void testAddAll() { |
228 |
|
Set full = populatedSet(3); |
229 |
< |
Vector v = new Vector(); |
230 |
< |
v.add(three); |
231 |
< |
v.add(four); |
184 |
< |
v.add(five); |
185 |
< |
full.addAll(v); |
229 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
230 |
> |
assertEquals(6, full.size()); |
231 |
> |
assertFalse(full.addAll(Arrays.asList(three, four, five))); |
232 |
|
assertEquals(6, full.size()); |
233 |
|
} |
234 |
|
|
238 |
|
*/ |
239 |
|
public void testAddAll2() { |
240 |
|
Set full = populatedSet(3); |
241 |
< |
Vector v = new Vector(); |
242 |
< |
v.add(three); |
243 |
< |
v.add(four); |
244 |
< |
v.add(one); // will not add this element |
199 |
< |
full.addAll(v); |
241 |
> |
// "one" is duplicate and will not be added |
242 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, one))); |
243 |
> |
assertEquals(5, full.size()); |
244 |
> |
assertFalse(full.addAll(Arrays.asList(three, four, one))); |
245 |
|
assertEquals(5, full.size()); |
246 |
|
} |
247 |
|
|
250 |
|
*/ |
251 |
|
public void testAdd2() { |
252 |
|
Set full = populatedSet(3); |
253 |
< |
full.add(one); |
253 |
> |
assertFalse(full.add(one)); |
254 |
|
assertEquals(3, full.size()); |
255 |
|
} |
256 |
|
|
259 |
|
*/ |
260 |
|
public void testAdd3() { |
261 |
|
Set full = populatedSet(3); |
262 |
< |
full.add(three); |
262 |
> |
assertTrue(full.add(three)); |
263 |
> |
assertTrue(full.contains(three)); |
264 |
> |
assertFalse(full.add(three)); |
265 |
|
assertTrue(full.contains(three)); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
+ |
* keySet.add throws UnsupportedOperationException if no default |
270 |
+ |
* mapped value |
271 |
+ |
*/ |
272 |
+ |
public void testAdd4() { |
273 |
+ |
Set full = map5().keySet(); |
274 |
+ |
try { |
275 |
+ |
full.add(three); |
276 |
+ |
shouldThrow(); |
277 |
+ |
} catch (UnsupportedOperationException success) {} |
278 |
+ |
} |
279 |
+ |
|
280 |
+ |
/** |
281 |
+ |
* keySet.add throws NullPointerException if the specified key is |
282 |
+ |
* null |
283 |
+ |
*/ |
284 |
+ |
public void testAdd5() { |
285 |
+ |
Set full = populatedSet(3); |
286 |
+ |
try { |
287 |
+ |
full.add(null); |
288 |
+ |
shouldThrow(); |
289 |
+ |
} catch (NullPointerException success) {} |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
/** |
293 |
+ |
* KeySetView.getMappedValue returns the map's mapped value |
294 |
+ |
*/ |
295 |
+ |
public void testGetMappedValue() { |
296 |
+ |
ConcurrentHashMap map = map5(); |
297 |
+ |
assertNull(map.keySet().getMappedValue()); |
298 |
+ |
try { |
299 |
+ |
map.keySet(null); |
300 |
+ |
shouldThrow(); |
301 |
+ |
} catch (NullPointerException success) {} |
302 |
+ |
ConcurrentHashMap.KeySetView set = map.keySet(one); |
303 |
+ |
assertFalse(set.add(one)); |
304 |
+ |
assertTrue(set.add(six)); |
305 |
+ |
assertTrue(set.add(seven)); |
306 |
+ |
assertTrue(set.getMappedValue() == one); |
307 |
+ |
assertTrue(map.get(one) != one); |
308 |
+ |
assertTrue(map.get(six) == one); |
309 |
+ |
assertTrue(map.get(seven) == one); |
310 |
+ |
} |
311 |
+ |
|
312 |
+ |
void checkSpliteratorCharacteristics(Spliterator<?> sp, |
313 |
+ |
int requiredCharacteristics) { |
314 |
+ |
assertEquals(requiredCharacteristics, |
315 |
+ |
requiredCharacteristics & sp.characteristics()); |
316 |
+ |
} |
317 |
+ |
|
318 |
+ |
/** |
319 |
+ |
* KeySetView.spliterator returns spliterator over the elements in this set |
320 |
+ |
*/ |
321 |
+ |
public void testKeySetSpliterator() { |
322 |
+ |
LongAdder adder = new LongAdder(); |
323 |
+ |
ConcurrentHashMap map = map5(); |
324 |
+ |
Set set = map.keySet(); |
325 |
+ |
Spliterator<Integer> sp = set.spliterator(); |
326 |
+ |
checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL); |
327 |
+ |
assertEquals(sp.estimateSize(), map.size()); |
328 |
+ |
Spliterator<Integer> sp2 = sp.trySplit(); |
329 |
+ |
sp.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
330 |
+ |
long v = adder.sumThenReset(); |
331 |
+ |
sp2.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
332 |
+ |
long v2 = adder.sum(); |
333 |
+ |
assertEquals(v + v2, 15); |
334 |
+ |
} |
335 |
+ |
|
336 |
+ |
/** |
337 |
|
* keyset.clear removes all elements from the set |
338 |
|
*/ |
339 |
|
public void testClear() { |
373 |
|
* KeySet.containsAll returns true for collections with subset of elements |
374 |
|
*/ |
375 |
|
public void testContainsAll() { |
376 |
< |
Set full = populatedSet(3); |
377 |
< |
Vector v = new Vector(); |
378 |
< |
v.add(one); |
379 |
< |
v.add(two); |
380 |
< |
assertTrue(full.containsAll(v)); |
381 |
< |
v.add(six); |
267 |
< |
assertFalse(full.containsAll(v)); |
376 |
> |
Collection full = populatedSet(3); |
377 |
> |
assertTrue(full.containsAll(Arrays.asList())); |
378 |
> |
assertTrue(full.containsAll(Arrays.asList(one))); |
379 |
> |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
380 |
> |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
381 |
> |
assertFalse(full.containsAll(Arrays.asList(six))); |
382 |
|
} |
383 |
|
|
384 |
|
/** |
385 |
|
* KeySet.isEmpty is true when empty, else false |
386 |
|
*/ |
387 |
|
public void testIsEmpty() { |
388 |
< |
Set empty = ConcurrentHashMap.newKeySet(); |
389 |
< |
Set full = populatedSet(3); |
276 |
< |
assertTrue(empty.isEmpty()); |
277 |
< |
assertFalse(full.isEmpty()); |
388 |
> |
assertTrue(populatedSet(0).isEmpty()); |
389 |
> |
assertFalse(populatedSet(3).isEmpty()); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
405 |
|
Integer[] elements = new Integer[size]; |
406 |
|
for (int i = 0; i < size; i++) |
407 |
|
elements[i] = i; |
408 |
< |
Collections.shuffle(Arrays.asList(elements)); |
408 |
> |
shuffle(elements); |
409 |
|
Collection<Integer> full = populatedSet(elements); |
410 |
|
|
411 |
|
Iterator it = full.iterator(); |
413 |
|
assertTrue(it.hasNext()); |
414 |
|
it.next(); |
415 |
|
} |
416 |
< |
assertFalse(it.hasNext()); |
417 |
< |
try { |
418 |
< |
it.next(); |
419 |
< |
shouldThrow(); |
420 |
< |
} catch (NoSuchElementException success) {} |
416 |
> |
assertIteratorExhausted(it); |
417 |
> |
} |
418 |
> |
|
419 |
> |
/** |
420 |
> |
* iterator of empty collections has no elements |
421 |
> |
*/ |
422 |
> |
public void testEmptyIterator() { |
423 |
> |
assertIteratorExhausted(ConcurrentHashMap.newKeySet().iterator()); |
424 |
> |
assertIteratorExhausted(new ConcurrentHashMap().entrySet().iterator()); |
425 |
> |
assertIteratorExhausted(new ConcurrentHashMap().values().iterator()); |
426 |
> |
assertIteratorExhausted(new ConcurrentHashMap().keySet().iterator()); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
457 |
|
*/ |
458 |
|
public void testRemoveAll() { |
459 |
|
Set full = populatedSet(3); |
460 |
< |
Vector v = new Vector(); |
461 |
< |
v.add(one); |
462 |
< |
v.add(two); |
345 |
< |
full.removeAll(v); |
460 |
> |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
461 |
> |
assertEquals(1, full.size()); |
462 |
> |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
463 |
|
assertEquals(1, full.size()); |
464 |
|
} |
465 |
|
|
495 |
|
Integer[] elements = new Integer[size]; |
496 |
|
for (int i = 0; i < size; i++) |
497 |
|
elements[i] = i; |
498 |
< |
Collections.shuffle(Arrays.asList(elements)); |
498 |
> |
shuffle(elements); |
499 |
|
Collection<Integer> full = populatedSet(elements); |
500 |
|
|
501 |
|
assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray()))); |
515 |
|
a = new Integer[0]; |
516 |
|
assertSame(a, empty.toArray(a)); |
517 |
|
|
518 |
< |
a = new Integer[size/2]; |
518 |
> |
a = new Integer[size / 2]; |
519 |
|
Arrays.fill(a, 42); |
520 |
|
assertSame(a, empty.toArray(a)); |
521 |
|
assertNull(a[0]); |
525 |
|
Integer[] elements = new Integer[size]; |
526 |
|
for (int i = 0; i < size; i++) |
527 |
|
elements[i] = i; |
528 |
< |
Collections.shuffle(Arrays.asList(elements)); |
528 |
> |
shuffle(elements); |
529 |
|
Collection<Integer> full = populatedSet(elements); |
530 |
|
|
531 |
|
Arrays.fill(a, 42); |
548 |
|
Set x = populatedSet(size); |
549 |
|
Set y = serialClone(x); |
550 |
|
|
551 |
< |
assertTrue(x != y); |
551 |
> |
assertNotSame(x, y); |
552 |
|
assertEquals(x.size(), y.size()); |
436 |
– |
assertEquals(x.toString(), y.toString()); |
437 |
– |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
553 |
|
assertEquals(x, y); |
554 |
|
assertEquals(y, x); |
555 |
|
} |
556 |
|
|
442 |
– |
|
557 |
|
static final int SIZE = 10000; |
558 |
|
static ConcurrentHashMap<Long, Long> longMap; |
559 |
|
|
751 |
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
752 |
|
} |
753 |
|
|
640 |
– |
|
754 |
|
/** |
755 |
|
* reduceKeysSequentially accumulates across all keys, |
756 |
|
*/ |
771 |
|
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
772 |
|
} |
773 |
|
|
661 |
– |
|
774 |
|
/** |
775 |
|
* reduceEntriesSequentially accumulates across all entries |
776 |
|
*/ |
873 |
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
874 |
|
} |
875 |
|
|
764 |
– |
|
876 |
|
/** |
877 |
|
* reduceKeysToLongSequentially accumulates mapped keys |
878 |
|
*/ |
1001 |
|
public void testSearchValuesSequentially() { |
1002 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1003 |
|
Long r; |
1004 |
< |
r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
1004 |
> |
r = m.searchValues(Long.MAX_VALUE, |
1005 |
> |
(Long x) -> (x.longValue() == (long)(SIZE/2)) ? x : null); |
1006 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1007 |
< |
r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); |
1007 |
> |
r = m.searchValues(Long.MAX_VALUE, |
1008 |
> |
(Long x) -> (x.longValue() < 0L) ? x : null); |
1009 |
|
assertNull(r); |
1010 |
|
} |
1011 |
|
|