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; |
13 |
+ |
import java.util.concurrent.ConcurrentHashMap.KeySetView; |
14 |
|
|
15 |
|
public class ConcurrentHashMap8Test extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
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(); |
167 |
|
return a; |
168 |
|
} |
169 |
|
|
170 |
+ |
/* |
171 |
+ |
* replaceAll replaces all matching values. |
172 |
+ |
*/ |
173 |
+ |
public void testReplaceAll() { |
174 |
+ |
ConcurrentHashMap<Integer, String> map = map5(); |
175 |
+ |
map.replaceAll((x, y) -> {return x > 3 ? "Z" : y;}); |
176 |
+ |
assertEquals("A", map.get(one)); |
177 |
+ |
assertEquals("B", map.get(two)); |
178 |
+ |
assertEquals("C", map.get(three)); |
179 |
+ |
assertEquals("Z", map.get(four)); |
180 |
+ |
assertEquals("Z", map.get(five)); |
181 |
+ |
} |
182 |
+ |
|
183 |
|
/** |
184 |
|
* Default-constructed set is empty |
185 |
|
*/ |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
+ |
* keySet.add adds the key with the established value to the map; |
193 |
+ |
* remove removes it. |
194 |
+ |
*/ |
195 |
+ |
public void testKeySetAddRemove() { |
196 |
+ |
ConcurrentHashMap map = map5(); |
197 |
+ |
Set set1 = map.keySet(); |
198 |
+ |
Set set2 = map.keySet(true); |
199 |
+ |
set2.add(six); |
200 |
+ |
assertTrue(((KeySetView)set2).getMap() == map); |
201 |
+ |
assertTrue(((KeySetView)set1).getMap() == map); |
202 |
+ |
assertEquals(set2.size(), map.size()); |
203 |
+ |
assertEquals(set1.size(), map.size()); |
204 |
+ |
assertTrue((Boolean)map.get(six)); |
205 |
+ |
assertTrue(set1.contains(six)); |
206 |
+ |
assertTrue(set2.contains(six)); |
207 |
+ |
set2.remove(six); |
208 |
+ |
assertNull(map.get(six)); |
209 |
+ |
assertFalse(set1.contains(six)); |
210 |
+ |
assertFalse(set2.contains(six)); |
211 |
+ |
} |
212 |
+ |
|
213 |
+ |
|
214 |
+ |
/** |
215 |
|
* keySet.addAll adds each element from the given collection |
216 |
|
*/ |
217 |
|
public void testAddAll() { |
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 |
+ |
* 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 |
329 |
|
*/ |
330 |
|
public void testClear() { |
537 |
|
Set x = populatedSet(size); |
538 |
|
Set y = serialClone(x); |
539 |
|
|
540 |
< |
assertTrue(x != y); |
540 |
> |
assertNotSame(x, y); |
541 |
|
assertEquals(x.size(), y.size()); |
436 |
– |
assertEquals(x.toString(), y.toString()); |
437 |
– |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
542 |
|
assertEquals(x, y); |
543 |
|
assertEquals(y, x); |
544 |
|
} |
545 |
|
|
442 |
– |
|
546 |
|
static final int SIZE = 10000; |
547 |
|
static ConcurrentHashMap<Long, Long> longMap; |
548 |
|
|
740 |
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
741 |
|
} |
742 |
|
|
640 |
– |
|
743 |
|
/** |
744 |
|
* reduceKeysSequentially accumulates across all keys, |
745 |
|
*/ |
760 |
|
assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); |
761 |
|
} |
762 |
|
|
661 |
– |
|
763 |
|
/** |
764 |
|
* reduceEntriesSequentially accumulates across all entries |
765 |
|
*/ |
862 |
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
863 |
|
} |
864 |
|
|
764 |
– |
|
865 |
|
/** |
866 |
|
* reduceKeysToLongSequentially accumulates mapped keys |
867 |
|
*/ |