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.Collections; |
15 |
> |
import java.util.Iterator; |
16 |
> |
import java.util.Map; |
17 |
> |
import java.util.NoSuchElementException; |
18 |
> |
import java.util.Set; |
19 |
> |
import java.util.Spliterator; |
20 |
> |
import java.util.Vector; |
21 |
|
import java.util.concurrent.ConcurrentHashMap; |
22 |
< |
import java.util.concurrent.ConcurrentHashMap.KeySetView; |
22 |
> |
import java.util.concurrent.atomic.LongAdder; |
23 |
> |
import java.util.function.BiFunction; |
24 |
> |
|
25 |
> |
import junit.framework.Test; |
26 |
> |
import junit.framework.TestSuite; |
27 |
|
|
28 |
|
public class ConcurrentHashMap8Test extends JSR166TestCase { |
29 |
|
public static void main(String[] args) { |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* computeIfAbsent does not replace if the key is already present |
71 |
> |
* computeIfAbsent does not replace if the key is already present |
72 |
|
*/ |
73 |
|
public void testComputeIfAbsent2() { |
74 |
|
ConcurrentHashMap map = map5(); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
88 |
< |
* computeIfPresent does not replace if the key is already present |
88 |
> |
* computeIfPresent does not replace if the key is already present |
89 |
|
*/ |
90 |
|
public void testComputeIfPresent() { |
91 |
|
ConcurrentHashMap map = map5(); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* compute does not replace if the function returns null |
105 |
> |
* compute does not replace if the function returns null |
106 |
|
*/ |
107 |
|
public void testCompute() { |
108 |
|
ConcurrentHashMap map = map5(); |
210 |
|
Set set1 = map.keySet(); |
211 |
|
Set set2 = map.keySet(true); |
212 |
|
set2.add(six); |
213 |
< |
assertTrue(((KeySetView)set2).getMap() == map); |
214 |
< |
assertTrue(((KeySetView)set1).getMap() == map); |
213 |
> |
assertTrue(((ConcurrentHashMap.KeySetView)set2).getMap() == map); |
214 |
> |
assertTrue(((ConcurrentHashMap.KeySetView)set1).getMap() == map); |
215 |
|
assertEquals(set2.size(), map.size()); |
216 |
|
assertEquals(set1.size(), map.size()); |
217 |
|
assertTrue((Boolean)map.get(six)); |
269 |
|
assertTrue(full.contains(three)); |
270 |
|
} |
271 |
|
|
272 |
+ |
/** |
273 |
+ |
* keySet.add throws UnsupportedOperationException if no default |
274 |
+ |
* mapped value |
275 |
+ |
*/ |
276 |
+ |
public void testAdd4() { |
277 |
+ |
Set full = map5().keySet(); |
278 |
+ |
try { |
279 |
+ |
full.add(three); |
280 |
+ |
shouldThrow(); |
281 |
+ |
} catch (UnsupportedOperationException e){} |
282 |
+ |
} |
283 |
+ |
|
284 |
+ |
/** |
285 |
+ |
* keySet.add throws NullPointerException if the specified key is |
286 |
+ |
* null |
287 |
+ |
*/ |
288 |
+ |
public void testAdd5() { |
289 |
+ |
Set full = populatedSet(3); |
290 |
+ |
try { |
291 |
+ |
full.add(null); |
292 |
+ |
shouldThrow(); |
293 |
+ |
} catch (NullPointerException e){} |
294 |
+ |
} |
295 |
+ |
|
296 |
+ |
/** |
297 |
+ |
* KeySetView.getMappedValue returns the map's mapped value |
298 |
+ |
*/ |
299 |
+ |
public void testGetMappedValue() { |
300 |
+ |
ConcurrentHashMap map = map5(); |
301 |
+ |
assertNull(map.keySet().getMappedValue()); |
302 |
+ |
try { |
303 |
+ |
map.keySet(null); |
304 |
+ |
shouldThrow(); |
305 |
+ |
} catch (NullPointerException e) {} |
306 |
+ |
ConcurrentHashMap.KeySetView set = map.keySet(one); |
307 |
+ |
set.add(one); |
308 |
+ |
set.add(six); |
309 |
+ |
set.add(seven); |
310 |
+ |
assertTrue(set.getMappedValue() == one); |
311 |
+ |
assertTrue(map.get(one) != one); |
312 |
+ |
assertTrue(map.get(six) == one); |
313 |
+ |
assertTrue(map.get(seven) == one); |
314 |
+ |
} |
315 |
+ |
|
316 |
+ |
void checkSpliteratorCharacteristics(Spliterator<?> sp, |
317 |
+ |
int requiredCharacteristics) { |
318 |
+ |
assertEquals(requiredCharacteristics, |
319 |
+ |
requiredCharacteristics & sp.characteristics()); |
320 |
+ |
} |
321 |
|
|
322 |
< |
/** |
323 |
< |
* keySet.add throws UnsupportedOperationException if no default |
324 |
< |
* mapped value |
325 |
< |
*/ |
326 |
< |
public void testAdd4() { |
327 |
< |
Set full = map5().keySet(); |
328 |
< |
try { |
329 |
< |
full.add(three); |
330 |
< |
shouldThrow(); |
331 |
< |
} catch (UnsupportedOperationException e){} |
332 |
< |
} |
333 |
< |
|
334 |
< |
/** |
335 |
< |
* keySet.add throws NullPointerException if the specified key is |
336 |
< |
* null |
337 |
< |
*/ |
338 |
< |
public void testAdd5() { |
276 |
< |
Set full = populatedSet(3); |
277 |
< |
try { |
278 |
< |
full.add(null); |
279 |
< |
shouldThrow(); |
280 |
< |
} catch (NullPointerException e){} |
281 |
< |
} |
282 |
< |
|
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 |
< |
/** |
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 |
< |
|
322 |
> |
/** |
323 |
> |
* KeySetView.spliterator returns spliterator over the elements in this set |
324 |
> |
*/ |
325 |
> |
public void testKeySetSpliterator() { |
326 |
> |
LongAdder adder = new LongAdder(); |
327 |
> |
ConcurrentHashMap map = map5(); |
328 |
> |
Set set = map.keySet(); |
329 |
> |
Spliterator<Integer> sp = set.spliterator(); |
330 |
> |
checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL); |
331 |
> |
assertEquals(sp.estimateSize(), map.size()); |
332 |
> |
Spliterator<Integer> sp2 = sp.trySplit(); |
333 |
> |
sp.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
334 |
> |
long v = adder.sumThenReset(); |
335 |
> |
sp2.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
336 |
> |
long v2 = adder.sum(); |
337 |
> |
assertEquals(v + v2, 15); |
338 |
> |
} |
339 |
|
|
340 |
|
/** |
341 |
|
* keyset.clear removes all elements from the set |
552 |
|
|
553 |
|
assertNotSame(x, y); |
554 |
|
assertEquals(x.size(), y.size()); |
536 |
– |
assertEquals(x.toString(), y.toString()); |
537 |
– |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
555 |
|
assertEquals(x, y); |
556 |
|
assertEquals(y, x); |
557 |
|
} |