9 |
|
import java.util.function.*; |
10 |
|
import java.util.concurrent.atomic.LongAdder; |
11 |
|
import java.util.concurrent.ConcurrentHashMap; |
12 |
+ |
import java.util.concurrent.ConcurrentHashMap.KeySetView; |
13 |
|
|
14 |
|
public class ConcurrentHashMap8Test extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* computeIfAbsent does not replace if the key is already present |
57 |
> |
* computeIfAbsent does not replace if the key is already present |
58 |
|
*/ |
59 |
|
public void testComputeIfAbsent2() { |
60 |
|
ConcurrentHashMap map = map5(); |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* computeIfPresent does not replace if the key is already present |
74 |
> |
* computeIfPresent does not replace if the key is already present |
75 |
|
*/ |
76 |
|
public void testComputeIfPresent() { |
77 |
|
ConcurrentHashMap map = map5(); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* compute does not replace if the function returns null |
91 |
> |
* compute does not replace if the function returns null |
92 |
|
*/ |
93 |
|
public void testCompute() { |
94 |
|
ConcurrentHashMap map = map5(); |
166 |
|
return a; |
167 |
|
} |
168 |
|
|
169 |
+ |
/* |
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 |
|
/** |
183 |
|
* Default-constructed set is empty |
184 |
|
*/ |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
+ |
* 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 |
|
* keySet.addAll adds each element from the given collection |
215 |
|
*/ |
216 |
|
public void testAddAll() { |
256 |
|
} |
257 |
|
|
258 |
|
/** |
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 |
+ |
|
302 |
+ |
/** |
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 |
+ |
|
319 |
+ |
/** |
320 |
|
* keyset.clear removes all elements from the set |
321 |
|
*/ |
322 |
|
public void testClear() { |
537 |
|
assertEquals(y, x); |
538 |
|
} |
539 |
|
|
442 |
– |
|
540 |
|
static final int SIZE = 10000; |
541 |
|
static ConcurrentHashMap<Long, Long> longMap; |
542 |
|
|
734 |
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
735 |
|
} |
736 |
|
|
640 |
– |
|
737 |
|
/** |
738 |
|
* reduceKeysSequentially accumulates across all keys, |
739 |
|
*/ |
754 |
|
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
755 |
|
} |
756 |
|
|
661 |
– |
|
757 |
|
/** |
758 |
|
* reduceEntriesSequentially accumulates across all entries |
759 |
|
*/ |
856 |
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
857 |
|
} |
858 |
|
|
764 |
– |
|
859 |
|
/** |
860 |
|
* reduceKeysToLongSequentially accumulates mapped keys |
861 |
|
*/ |