6 |
|
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
+ |
import static java.util.Spliterator.*; |
10 |
|
import java.util.function.*; |
11 |
|
import java.util.concurrent.atomic.LongAdder; |
12 |
|
import java.util.concurrent.ConcurrentHashMap; |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
< |
* computeIfAbsent does not replace if the key is already present |
58 |
> |
* computeIfAbsent does not replace if the key is already present |
59 |
|
*/ |
60 |
|
public void testComputeIfAbsent2() { |
61 |
|
ConcurrentHashMap map = map5(); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* computeIfPresent does not replace if the key is already present |
75 |
> |
* computeIfPresent does not replace if the key is already present |
76 |
|
*/ |
77 |
|
public void testComputeIfPresent() { |
78 |
|
ConcurrentHashMap map = map5(); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* compute does not replace if the function returns null |
92 |
> |
* compute does not replace if the function returns null |
93 |
|
*/ |
94 |
|
public void testCompute() { |
95 |
|
ConcurrentHashMap map = map5(); |
256 |
|
assertTrue(full.contains(three)); |
257 |
|
} |
258 |
|
|
259 |
+ |
/** |
260 |
+ |
* keySet.add throws UnsupportedOperationException if no default |
261 |
+ |
* mapped value |
262 |
+ |
*/ |
263 |
+ |
public void testAdd4() { |
264 |
+ |
Set full = map5().keySet(); |
265 |
+ |
try { |
266 |
+ |
full.add(three); |
267 |
+ |
shouldThrow(); |
268 |
+ |
} catch (UnsupportedOperationException e){} |
269 |
+ |
} |
270 |
+ |
|
271 |
+ |
/** |
272 |
+ |
* keySet.add throws NullPointerException if the specified key is |
273 |
+ |
* null |
274 |
+ |
*/ |
275 |
+ |
public void testAdd5() { |
276 |
+ |
Set full = populatedSet(3); |
277 |
+ |
try { |
278 |
+ |
full.add(null); |
279 |
+ |
shouldThrow(); |
280 |
+ |
} catch (NullPointerException e){} |
281 |
+ |
} |
282 |
|
|
283 |
< |
/** |
284 |
< |
* keySet.add throws UnsupportedOperationException if no default |
285 |
< |
* mapped value |
286 |
< |
*/ |
287 |
< |
public void testAdd4() { |
288 |
< |
Set full = map5().keySet(); |
289 |
< |
try { |
290 |
< |
full.add(three); |
291 |
< |
shouldThrow(); |
292 |
< |
} catch (UnsupportedOperationException e){} |
293 |
< |
} |
294 |
< |
|
295 |
< |
/** |
296 |
< |
* keySet.add throws NullPointerException if the specified key is |
297 |
< |
* null |
298 |
< |
*/ |
299 |
< |
public void testAdd5() { |
300 |
< |
Set full = populatedSet(3); |
301 |
< |
try { |
302 |
< |
full.add(null); |
303 |
< |
shouldThrow(); |
304 |
< |
} catch (NullPointerException e){} |
305 |
< |
} |
306 |
< |
|
307 |
< |
/** |
308 |
< |
* KeySetView.getMappedValue returns the map's mapped value |
309 |
< |
*/ |
310 |
< |
public void testGetMappedValue() { |
311 |
< |
ConcurrentHashMap map = map5(); |
312 |
< |
assertNull(map.keySet().getMappedValue()); |
313 |
< |
try { |
314 |
< |
map.keySet(null); |
315 |
< |
shouldThrow(); |
316 |
< |
} catch (NullPointerException e) {} |
317 |
< |
KeySetView set = map.keySet(one); |
318 |
< |
set.add(one); |
319 |
< |
set.add(six); |
320 |
< |
set.add(seven); |
321 |
< |
assertTrue(set.getMappedValue() == one); |
322 |
< |
assertTrue(map.get(one) != one); |
323 |
< |
assertTrue(map.get(six) == one); |
324 |
< |
assertTrue(map.get(seven) == one); |
325 |
< |
} |
302 |
< |
|
303 |
< |
/** |
304 |
< |
* KeySetView.spliterator returns spliterator over the elements in this set |
305 |
< |
*/ |
306 |
< |
public void testKeySetSpliterator() { |
307 |
< |
LongAdder adder = new LongAdder(); |
308 |
< |
ConcurrentHashMap map = map5(); |
309 |
< |
Set set = map.keySet(); |
310 |
< |
Spliterator<Integer> sp = set.spliterator(); |
311 |
< |
assertEquals(sp.estimateSize(), map.size()); |
312 |
< |
Spliterator<Integer> sp2 = sp.trySplit(); |
313 |
< |
sp.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
314 |
< |
long v = adder.sumThenReset(); |
315 |
< |
sp2.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
316 |
< |
long v2 = adder.sum(); |
317 |
< |
assertEquals(v + v2, 15); |
318 |
< |
} |
319 |
< |
|
283 |
> |
/** |
284 |
> |
* KeySetView.getMappedValue returns the map's mapped value |
285 |
> |
*/ |
286 |
> |
public void testGetMappedValue() { |
287 |
> |
ConcurrentHashMap map = map5(); |
288 |
> |
assertNull(map.keySet().getMappedValue()); |
289 |
> |
try { |
290 |
> |
map.keySet(null); |
291 |
> |
shouldThrow(); |
292 |
> |
} catch (NullPointerException e) {} |
293 |
> |
KeySetView set = map.keySet(one); |
294 |
> |
set.add(one); |
295 |
> |
set.add(six); |
296 |
> |
set.add(seven); |
297 |
> |
assertTrue(set.getMappedValue() == one); |
298 |
> |
assertTrue(map.get(one) != one); |
299 |
> |
assertTrue(map.get(six) == one); |
300 |
> |
assertTrue(map.get(seven) == one); |
301 |
> |
} |
302 |
> |
|
303 |
> |
void checkSpliteratorCharacteristics(Spliterator<?> sp, |
304 |
> |
int requiredCharacteristics) { |
305 |
> |
assertEquals(requiredCharacteristics, |
306 |
> |
requiredCharacteristics & sp.characteristics()); |
307 |
> |
} |
308 |
> |
|
309 |
> |
/** |
310 |
> |
* KeySetView.spliterator returns spliterator over the elements in this set |
311 |
> |
*/ |
312 |
> |
public void testKeySetSpliterator() { |
313 |
> |
LongAdder adder = new LongAdder(); |
314 |
> |
ConcurrentHashMap map = map5(); |
315 |
> |
Set set = map.keySet(); |
316 |
> |
Spliterator<Integer> sp = set.spliterator(); |
317 |
> |
checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL); |
318 |
> |
assertEquals(sp.estimateSize(), map.size()); |
319 |
> |
Spliterator<Integer> sp2 = sp.trySplit(); |
320 |
> |
sp.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
321 |
> |
long v = adder.sumThenReset(); |
322 |
> |
sp2.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
323 |
> |
long v2 = adder.sum(); |
324 |
> |
assertEquals(v + v2, 15); |
325 |
> |
} |
326 |
|
|
327 |
|
/** |
328 |
|
* keyset.clear removes all elements from the set |