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(); |
180 |
|
return a; |
181 |
|
} |
182 |
|
|
183 |
< |
/* |
183 |
> |
/** |
184 |
|
* replaceAll replaces all matching values. |
185 |
|
*/ |
186 |
|
public void testReplaceAll() { |
187 |
|
ConcurrentHashMap<Integer, String> map = map5(); |
188 |
< |
map.replaceAll((x, y) -> {return x > 3 ? "Z" : y;}); |
188 |
> |
map.replaceAll((x, y) -> { return x > 3 ? "Z" : y; }); |
189 |
|
assertEquals("A", map.get(one)); |
190 |
|
assertEquals("B", map.get(two)); |
191 |
|
assertEquals("C", map.get(three)); |
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)); |
223 |
|
assertFalse(set2.contains(six)); |
224 |
|
} |
225 |
|
|
212 |
– |
|
226 |
|
/** |
227 |
|
* keySet.addAll adds each element from the given collection |
228 |
|
*/ |
268 |
|
assertTrue(full.contains(three)); |
269 |
|
} |
270 |
|
|
271 |
+ |
/** |
272 |
+ |
* keySet.add throws UnsupportedOperationException if no default |
273 |
+ |
* mapped value |
274 |
+ |
*/ |
275 |
+ |
public void testAdd4() { |
276 |
+ |
Set full = map5().keySet(); |
277 |
+ |
try { |
278 |
+ |
full.add(three); |
279 |
+ |
shouldThrow(); |
280 |
+ |
} catch (UnsupportedOperationException 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 |
< |
} |
326 |
< |
|
327 |
< |
/** |
328 |
< |
* KeySetView.spliterator returns spliterator over the elements in this set |
329 |
< |
*/ |
330 |
< |
public void testKeySetSpliterator() { |
331 |
< |
LongAdder adder = new LongAdder(); |
332 |
< |
ConcurrentHashMap map = map5(); |
333 |
< |
Set set = map.keySet(); |
334 |
< |
Spliterator<Integer> sp = set.spliterator(); |
335 |
< |
assertEquals(sp.estimateSize(), map.size()); |
336 |
< |
Spliterator<Integer> sp2 = sp.trySplit(); |
337 |
< |
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 |
> |
* keySet.add throws NullPointerException if the specified key is |
285 |
> |
* null |
286 |
> |
*/ |
287 |
> |
public void testAdd5() { |
288 |
> |
Set full = populatedSet(3); |
289 |
> |
try { |
290 |
> |
full.add(null); |
291 |
> |
shouldThrow(); |
292 |
> |
} catch (NullPointerException e){} |
293 |
> |
} |
294 |
> |
|
295 |
> |
/** |
296 |
> |
* KeySetView.getMappedValue returns the map's mapped value |
297 |
> |
*/ |
298 |
> |
public void testGetMappedValue() { |
299 |
> |
ConcurrentHashMap map = map5(); |
300 |
> |
assertNull(map.keySet().getMappedValue()); |
301 |
> |
try { |
302 |
> |
map.keySet(null); |
303 |
> |
shouldThrow(); |
304 |
> |
} catch (NullPointerException e) {} |
305 |
> |
ConcurrentHashMap.KeySetView set = map.keySet(one); |
306 |
> |
set.add(one); |
307 |
> |
set.add(six); |
308 |
> |
set.add(seven); |
309 |
> |
assertTrue(set.getMappedValue() == one); |
310 |
> |
assertTrue(map.get(one) != one); |
311 |
> |
assertTrue(map.get(six) == one); |
312 |
> |
assertTrue(map.get(seven) == one); |
313 |
> |
} |
314 |
> |
|
315 |
> |
void checkSpliteratorCharacteristics(Spliterator<?> sp, |
316 |
> |
int requiredCharacteristics) { |
317 |
> |
assertEquals(requiredCharacteristics, |
318 |
> |
requiredCharacteristics & sp.characteristics()); |
319 |
> |
} |
320 |
> |
|
321 |
> |
/** |
322 |
> |
* KeySetView.spliterator returns spliterator over the elements in this set |
323 |
> |
*/ |
324 |
> |
public void testKeySetSpliterator() { |
325 |
> |
LongAdder adder = new LongAdder(); |
326 |
> |
ConcurrentHashMap map = map5(); |
327 |
> |
Set set = map.keySet(); |
328 |
> |
Spliterator<Integer> sp = set.spliterator(); |
329 |
> |
checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL); |
330 |
> |
assertEquals(sp.estimateSize(), map.size()); |
331 |
> |
Spliterator<Integer> sp2 = sp.trySplit(); |
332 |
> |
sp.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
333 |
> |
long v = adder.sumThenReset(); |
334 |
> |
sp2.forEachRemaining((Integer x) -> adder.add(x.longValue())); |
335 |
> |
long v2 = adder.sum(); |
336 |
> |
assertEquals(v + v2, 15); |
337 |
> |
} |
338 |
|
|
339 |
|
/** |
340 |
|
* keyset.clear removes all elements from the set |
419 |
|
assertTrue(it.hasNext()); |
420 |
|
it.next(); |
421 |
|
} |
422 |
< |
assertFalse(it.hasNext()); |
423 |
< |
try { |
424 |
< |
it.next(); |
425 |
< |
shouldThrow(); |
426 |
< |
} catch (NoSuchElementException success) {} |
422 |
> |
assertIteratorExhausted(it); |
423 |
> |
} |
424 |
> |
|
425 |
> |
/** |
426 |
> |
* iterator of empty collections has no elements |
427 |
> |
*/ |
428 |
> |
public void testEmptyIterator() { |
429 |
> |
assertIteratorExhausted(ConcurrentHashMap.newKeySet().iterator()); |
430 |
> |
assertIteratorExhausted(new ConcurrentHashMap().entrySet().iterator()); |
431 |
> |
assertIteratorExhausted(new ConcurrentHashMap().values().iterator()); |
432 |
> |
assertIteratorExhausted(new ConcurrentHashMap().keySet().iterator()); |
433 |
|
} |
434 |
|
|
435 |
|
/** |
557 |
|
|
558 |
|
assertNotSame(x, y); |
559 |
|
assertEquals(x.size(), y.size()); |
536 |
– |
assertEquals(x.toString(), y.toString()); |
537 |
– |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
560 |
|
assertEquals(x, y); |
561 |
|
assertEquals(y, x); |
562 |
|
} |
1008 |
|
public void testSearchValuesSequentially() { |
1009 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1010 |
|
Long r; |
1011 |
< |
r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
1011 |
> |
r = m.searchValues(Long.MAX_VALUE, |
1012 |
> |
(Long x) -> (x.longValue() == (long)(SIZE/2)) ? x : null); |
1013 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1014 |
< |
r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); |
1014 |
> |
r = m.searchValues(Long.MAX_VALUE, |
1015 |
> |
(Long x) -> (x.longValue() < 0L) ? x : null); |
1016 |
|
assertNull(r); |
1017 |
|
} |
1018 |
|
|