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) { |
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() { |
255 |
|
assertTrue(full.contains(three)); |
256 |
|
} |
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 |
+ |
/** |
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 |
+ |
|
320 |
+ |
|
321 |
|
/** |
322 |
|
* keyset.clear removes all elements from the set |
323 |
|
*/ |