1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
> |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
> |
* domain, as explained at |
5 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
> |
* Pat Fisher, Mike Judd. |
8 |
|
*/ |
9 |
|
|
10 |
< |
import junit.framework.*; |
11 |
< |
import java.util.*; |
12 |
< |
import java.util.concurrent.*; |
10 |
> |
import java.util.ArrayList; |
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Collections; |
14 |
|
import java.util.Enumeration; |
15 |
< |
import java.io.*; |
15 |
> |
import java.util.Iterator; |
16 |
> |
import java.util.Map; |
17 |
> |
import java.util.Random; |
18 |
> |
import java.util.Set; |
19 |
> |
import java.util.concurrent.ConcurrentHashMap; |
20 |
|
|
21 |
< |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
21 |
> |
import junit.framework.Test; |
22 |
> |
|
23 |
> |
public class ConcurrentHashMapTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run (suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
< |
return new TestSuite(ConcurrentHashMapTest.class); |
28 |
> |
class Implementation implements MapImplementation { |
29 |
> |
public Class<?> klazz() { return ConcurrentHashMap.class; } |
30 |
> |
public Map emptyMap() { return new ConcurrentHashMap(); } |
31 |
> |
public boolean isConcurrent() { return true; } |
32 |
> |
public boolean permitsNullKeys() { return false; } |
33 |
> |
public boolean permitsNullValues() { return false; } |
34 |
> |
public boolean supportsSetValue() { return true; } |
35 |
> |
} |
36 |
> |
return newTestSuite( |
37 |
> |
ConcurrentHashMapTest.class, |
38 |
> |
MapTest.testSuite(new Implementation())); |
39 |
|
} |
40 |
|
|
41 |
< |
private static ConcurrentHashMap map5() { |
42 |
< |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
41 |
> |
/** |
42 |
> |
* Returns a new map from Items 1-5 to Strings "A"-"E". |
43 |
> |
*/ |
44 |
> |
private static ConcurrentHashMap<Item, String> map5() { |
45 |
> |
ConcurrentHashMap<Item, String> map = new ConcurrentHashMap<>(5); |
46 |
|
assertTrue(map.isEmpty()); |
47 |
< |
map.put(one, "A"); |
48 |
< |
map.put(two, "B"); |
49 |
< |
map.put(three, "C"); |
50 |
< |
map.put(four, "D"); |
51 |
< |
map.put(five, "E"); |
47 |
> |
map.put(one, "A"); |
48 |
> |
map.put(two, "B"); |
49 |
> |
map.put(three, "C"); |
50 |
> |
map.put(four, "D"); |
51 |
> |
map.put(five, "E"); |
52 |
|
assertFalse(map.isEmpty()); |
53 |
< |
assertEquals(5, map.size()); |
54 |
< |
return map; |
53 |
> |
mustEqual(5, map.size()); |
54 |
> |
return map; |
55 |
> |
} |
56 |
> |
|
57 |
> |
// classes for testing Comparable fallbacks |
58 |
> |
static class BI implements Comparable<BI> { |
59 |
> |
private final int value; |
60 |
> |
BI(int value) { this.value = value; } |
61 |
> |
public int compareTo(BI other) { |
62 |
> |
return Integer.compare(value, other.value); |
63 |
> |
} |
64 |
> |
public boolean equals(Object x) { |
65 |
> |
return (x instanceof BI) && ((BI)x).value == value; |
66 |
> |
} |
67 |
> |
public int hashCode() { return 42; } |
68 |
> |
} |
69 |
> |
static class CI extends BI { CI(int value) { super(value); } } |
70 |
> |
static class DI extends BI { DI(int value) { super(value); } } |
71 |
> |
|
72 |
> |
static class BS implements Comparable<BS> { |
73 |
> |
private final String value; |
74 |
> |
BS(String value) { this.value = value; } |
75 |
> |
public int compareTo(BS other) { |
76 |
> |
return value.compareTo(other.value); |
77 |
> |
} |
78 |
> |
public boolean equals(Object x) { |
79 |
> |
return (x instanceof BS) && value.equals(((BS)x).value); |
80 |
> |
} |
81 |
> |
public int hashCode() { return 42; } |
82 |
> |
} |
83 |
> |
|
84 |
> |
static class LexicographicList<E extends Comparable<E>> extends ArrayList<E> |
85 |
> |
implements Comparable<LexicographicList<E>> { |
86 |
> |
LexicographicList(Collection<E> c) { super(c); } |
87 |
> |
LexicographicList(E e) { super(Collections.singleton(e)); } |
88 |
> |
public int compareTo(LexicographicList<E> other) { |
89 |
> |
int common = Math.min(size(), other.size()); |
90 |
> |
int r = 0; |
91 |
> |
for (int i = 0; i < common; i++) { |
92 |
> |
if ((r = get(i).compareTo(other.get(i))) != 0) |
93 |
> |
break; |
94 |
> |
} |
95 |
> |
if (r == 0) |
96 |
> |
r = Integer.compare(size(), other.size()); |
97 |
> |
return r; |
98 |
> |
} |
99 |
> |
private static final long serialVersionUID = 0; |
100 |
> |
} |
101 |
> |
|
102 |
> |
static class CollidingObject { |
103 |
> |
final String value; |
104 |
> |
CollidingObject(final String value) { this.value = value; } |
105 |
> |
public int hashCode() { return this.value.hashCode() & 1; } |
106 |
> |
public boolean equals(final Object obj) { |
107 |
> |
return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value); |
108 |
> |
} |
109 |
> |
} |
110 |
> |
|
111 |
> |
static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> { |
112 |
> |
ComparableCollidingObject(final String value) { super(value); } |
113 |
> |
public int compareTo(final ComparableCollidingObject o) { |
114 |
> |
return value.compareTo(o.value); |
115 |
> |
} |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
< |
* clear removes all key-element pairs from the map |
119 |
> |
* Inserted elements that are subclasses of the same Comparable |
120 |
> |
* class are found. |
121 |
|
*/ |
122 |
< |
public void testClear(){ |
123 |
< |
ConcurrentHashMap map = map5(); |
124 |
< |
map.clear(); |
125 |
< |
assertEquals(map.size(), 0); |
122 |
> |
public void testComparableFamily() { |
123 |
> |
int size = 500; // makes measured test run time -> 60ms |
124 |
> |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
125 |
> |
for (int i = 0; i < size; i++) { |
126 |
> |
assertNull(m.put(new CI(i), true)); |
127 |
> |
} |
128 |
> |
for (int i = 0; i < size; i++) { |
129 |
> |
assertTrue(m.containsKey(new CI(i))); |
130 |
> |
assertTrue(m.containsKey(new DI(i))); |
131 |
> |
} |
132 |
|
} |
133 |
|
|
134 |
|
/** |
135 |
< |
* contains gives the appropriate value |
135 |
> |
* Elements of classes with erased generic type parameters based |
136 |
> |
* on Comparable can be inserted and found. |
137 |
|
*/ |
138 |
< |
public void testContains(){ |
139 |
< |
ConcurrentHashMap map = map5(); |
140 |
< |
assertTrue(map.contains("A")); |
141 |
< |
assertFalse(map.contains("Z")); |
138 |
> |
public void testGenericComparable() { |
139 |
> |
int size = 120; // makes measured test run time -> 60ms |
140 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
141 |
> |
for (int i = 0; i < size; i++) { |
142 |
> |
BI bi = new BI(i); |
143 |
> |
BS bs = new BS(String.valueOf(i)); |
144 |
> |
LexicographicList<BI> bis = new LexicographicList<>(bi); |
145 |
> |
LexicographicList<BS> bss = new LexicographicList<>(bs); |
146 |
> |
assertNull(m.putIfAbsent(bis, true)); |
147 |
> |
assertTrue(m.containsKey(bis)); |
148 |
> |
if (m.putIfAbsent(bss, true) == null) |
149 |
> |
assertTrue(m.containsKey(bss)); |
150 |
> |
assertTrue(m.containsKey(bis)); |
151 |
> |
} |
152 |
> |
for (int i = 0; i < size; i++) { |
153 |
> |
assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); |
154 |
> |
} |
155 |
|
} |
156 |
< |
|
156 |
> |
|
157 |
> |
/** |
158 |
> |
* Elements of non-comparable classes equal to those of classes |
159 |
> |
* with erased generic type parameters based on Comparable can be |
160 |
> |
* inserted and found. |
161 |
> |
*/ |
162 |
> |
public void testGenericComparable2() { |
163 |
> |
int size = 500; // makes measured test run time -> 60ms |
164 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
165 |
> |
for (int i = 0; i < size; i++) { |
166 |
> |
m.put(Collections.singletonList(new BI(i)), true); |
167 |
> |
} |
168 |
> |
|
169 |
> |
for (int i = 0; i < size; i++) { |
170 |
> |
LexicographicList<BI> bis = new LexicographicList<>(new BI(i)); |
171 |
> |
assertTrue(m.containsKey(bis)); |
172 |
> |
} |
173 |
> |
} |
174 |
> |
|
175 |
|
/** |
176 |
< |
* containsKey gives the appropriate value |
176 |
> |
* Mixtures of instances of comparable and non-comparable classes |
177 |
> |
* can be inserted and found. |
178 |
|
*/ |
179 |
< |
public void testContainsKey(){ |
180 |
< |
ConcurrentHashMap map = map5(); |
181 |
< |
assertTrue(map.containsKey(one)); |
182 |
< |
assertFalse(map.containsKey(new Integer(100))); |
179 |
> |
public void testMixedComparable() { |
180 |
> |
int size = 1200; // makes measured test run time -> 35ms |
181 |
> |
ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>(); |
182 |
> |
Random rng = new Random(); |
183 |
> |
for (int i = 0; i < size; i++) { |
184 |
> |
Object x; |
185 |
> |
switch (rng.nextInt(4)) { |
186 |
> |
case 0: |
187 |
> |
x = new Object(); |
188 |
> |
break; |
189 |
> |
case 1: |
190 |
> |
x = new CollidingObject(Integer.toString(i)); |
191 |
> |
break; |
192 |
> |
default: |
193 |
> |
x = new ComparableCollidingObject(Integer.toString(i)); |
194 |
> |
} |
195 |
> |
assertNull(map.put(x, x)); |
196 |
> |
} |
197 |
> |
int count = 0; |
198 |
> |
for (Object k : map.keySet()) { |
199 |
> |
mustEqual(map.get(k), k); |
200 |
> |
++count; |
201 |
> |
} |
202 |
> |
mustEqual(count, size); |
203 |
> |
mustEqual(map.size(), size); |
204 |
> |
for (Object k : map.keySet()) { |
205 |
> |
mustEqual(map.put(k, k), k); |
206 |
> |
} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* Identical to normal contains |
210 |
> |
* clear removes all pairs |
211 |
|
*/ |
212 |
< |
public void testContainsValue(){ |
213 |
< |
ConcurrentHashMap map = map5(); |
214 |
< |
assertTrue(map.contains("A")); |
212 |
> |
public void testClear() { |
213 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
214 |
> |
map.clear(); |
215 |
> |
mustEqual(0, map.size()); |
216 |
> |
} |
217 |
> |
|
218 |
> |
/** |
219 |
> |
* Maps with same contents are equal |
220 |
> |
*/ |
221 |
> |
public void testEquals() { |
222 |
> |
ConcurrentHashMap<Item,String> map1 = map5(); |
223 |
> |
ConcurrentHashMap<Item,String> map2 = map5(); |
224 |
> |
mustEqual(map1, map2); |
225 |
> |
mustEqual(map2, map1); |
226 |
> |
map1.clear(); |
227 |
> |
assertFalse(map1.equals(map2)); |
228 |
> |
assertFalse(map2.equals(map1)); |
229 |
> |
} |
230 |
> |
|
231 |
> |
/** |
232 |
> |
* hashCode() equals sum of each key.hashCode ^ value.hashCode |
233 |
> |
*/ |
234 |
> |
public void testHashCode() { |
235 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
236 |
> |
int sum = 0; |
237 |
> |
for (Map.Entry<Item,String> e : map.entrySet()) |
238 |
> |
sum += e.getKey().hashCode() ^ e.getValue().hashCode(); |
239 |
> |
mustEqual(sum, map.hashCode()); |
240 |
> |
} |
241 |
> |
|
242 |
> |
/** |
243 |
> |
* contains returns true for contained value |
244 |
> |
*/ |
245 |
> |
public void testContains() { |
246 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
247 |
> |
assertTrue(map.contains("A")); |
248 |
|
assertFalse(map.contains("Z")); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
< |
* enumeration returns an enumeration containing the correct |
73 |
< |
* elements |
252 |
> |
* containsKey returns true for contained key |
253 |
|
*/ |
254 |
< |
public void testEnumeration(){ |
255 |
< |
ConcurrentHashMap map = map5(); |
256 |
< |
Enumeration e = map.elements(); |
257 |
< |
int count = 0; |
79 |
< |
while(e.hasMoreElements()){ |
80 |
< |
count++; |
81 |
< |
e.nextElement(); |
82 |
< |
} |
83 |
< |
assertEquals(5, count); |
254 |
> |
public void testContainsKey() { |
255 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
256 |
> |
assertTrue(map.containsKey(one)); |
257 |
> |
assertFalse(map.containsKey(zero)); |
258 |
|
} |
259 |
|
|
260 |
|
/** |
261 |
< |
* Clone creates an equal map |
261 |
> |
* containsValue returns true for held values |
262 |
|
*/ |
263 |
< |
public void testClone(){ |
264 |
< |
ConcurrentHashMap map = map5(); |
265 |
< |
ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone()); |
266 |
< |
assertEquals(map, m2); |
263 |
> |
public void testContainsValue() { |
264 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
265 |
> |
assertTrue(map.containsValue("A")); |
266 |
> |
assertFalse(map.containsValue("Z")); |
267 |
|
} |
268 |
|
|
269 |
|
/** |
270 |
< |
* get returns the correct element at the given index |
270 |
> |
* enumeration returns an enumeration containing the correct |
271 |
> |
* elements |
272 |
|
*/ |
273 |
< |
public void testGet(){ |
274 |
< |
ConcurrentHashMap map = map5(); |
275 |
< |
assertEquals("A", (String)map.get(one)); |
273 |
> |
public void testEnumeration() { |
274 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
275 |
> |
Enumeration<String> e = map.elements(); |
276 |
> |
int count = 0; |
277 |
> |
while (e.hasMoreElements()) { |
278 |
> |
count++; |
279 |
> |
e.nextElement(); |
280 |
> |
} |
281 |
> |
mustEqual(5, count); |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
< |
* get on a nonexistant key returns null |
285 |
> |
* get returns the correct element at the given key, |
286 |
> |
* or null if not present |
287 |
|
*/ |
288 |
< |
public void testGet2(){ |
289 |
< |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
288 |
> |
@SuppressWarnings("CollectionIncompatibleType") |
289 |
> |
public void testGet() { |
290 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
291 |
> |
mustEqual("A", map.get(one)); |
292 |
> |
ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<>(); |
293 |
> |
assertNull(map.get("anything")); |
294 |
|
assertNull(empty.get("anything")); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
< |
* Simple test to verify isEmpty returns the correct value |
298 |
> |
* isEmpty is true of empty map and false for non-empty |
299 |
|
*/ |
300 |
< |
public void testIsEmpty(){ |
301 |
< |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
302 |
< |
ConcurrentHashMap map = map5(); |
303 |
< |
assertTrue(empty.isEmpty()); |
300 |
> |
public void testIsEmpty() { |
301 |
> |
ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<>(); |
302 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
303 |
> |
assertTrue(empty.isEmpty()); |
304 |
|
assertFalse(map.isEmpty()); |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
< |
* keys returns an enumeration containing all the keys from the map |
308 |
> |
* keys returns an enumeration containing all the keys from the map |
309 |
> |
*/ |
310 |
> |
public void testKeys() { |
311 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
312 |
> |
Enumeration<Item> e = map.keys(); |
313 |
> |
int count = 0; |
314 |
> |
while (e.hasMoreElements()) { |
315 |
> |
count++; |
316 |
> |
e.nextElement(); |
317 |
> |
} |
318 |
> |
mustEqual(5, count); |
319 |
> |
} |
320 |
> |
|
321 |
> |
/** |
322 |
> |
* keySet returns a Set containing all the keys |
323 |
> |
*/ |
324 |
> |
public void testKeySet() { |
325 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
326 |
> |
Set<Item> s = map.keySet(); |
327 |
> |
mustEqual(5, s.size()); |
328 |
> |
mustContain(s, one); |
329 |
> |
mustContain(s, two); |
330 |
> |
mustContain(s, three); |
331 |
> |
mustContain(s, four); |
332 |
> |
mustContain(s, five); |
333 |
> |
} |
334 |
> |
|
335 |
> |
/** |
336 |
> |
* Test keySet().removeAll on empty map |
337 |
> |
*/ |
338 |
> |
public void testKeySet_empty_removeAll() { |
339 |
> |
ConcurrentHashMap<Item, String> map = new ConcurrentHashMap<>(); |
340 |
> |
Set<Item> set = map.keySet(); |
341 |
> |
set.removeAll(Collections.emptyList()); |
342 |
> |
assertTrue(map.isEmpty()); |
343 |
> |
assertTrue(set.isEmpty()); |
344 |
> |
// following is test for JDK-8163353 |
345 |
> |
set.removeAll(Collections.emptySet()); |
346 |
> |
assertTrue(map.isEmpty()); |
347 |
> |
assertTrue(set.isEmpty()); |
348 |
> |
} |
349 |
> |
|
350 |
> |
/** |
351 |
> |
* keySet.toArray returns contains all keys |
352 |
> |
*/ |
353 |
> |
public void testKeySetToArray() { |
354 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
355 |
> |
Set<Item> s = map.keySet(); |
356 |
> |
Object[] ar = s.toArray(); |
357 |
> |
assertTrue(s.containsAll(Arrays.asList(ar))); |
358 |
> |
mustEqual(5, ar.length); |
359 |
> |
ar[0] = minusTen; |
360 |
> |
assertFalse(s.containsAll(Arrays.asList(ar))); |
361 |
> |
} |
362 |
> |
|
363 |
> |
/** |
364 |
> |
* Values.toArray contains all values |
365 |
> |
*/ |
366 |
> |
public void testValuesToArray() { |
367 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
368 |
> |
Collection<String> v = map.values(); |
369 |
> |
String[] ar = v.toArray(new String[0]); |
370 |
> |
ArrayList<String> s = new ArrayList<>(Arrays.asList(ar)); |
371 |
> |
mustEqual(5, ar.length); |
372 |
> |
assertTrue(s.contains("A")); |
373 |
> |
assertTrue(s.contains("B")); |
374 |
> |
assertTrue(s.contains("C")); |
375 |
> |
assertTrue(s.contains("D")); |
376 |
> |
assertTrue(s.contains("E")); |
377 |
> |
} |
378 |
> |
|
379 |
> |
/** |
380 |
> |
* entrySet.toArray contains all entries |
381 |
> |
*/ |
382 |
> |
public void testEntrySetToArray() { |
383 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
384 |
> |
Set<Map.Entry<Item,String>> s = map.entrySet(); |
385 |
> |
Object[] ar = s.toArray(); |
386 |
> |
mustEqual(5, ar.length); |
387 |
> |
for (int i = 0; i < 5; ++i) { |
388 |
> |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
389 |
> |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
390 |
> |
} |
391 |
> |
} |
392 |
> |
|
393 |
> |
/** |
394 |
> |
* values collection contains all values |
395 |
|
*/ |
396 |
< |
public void testKeys(){ |
397 |
< |
ConcurrentHashMap map = map5(); |
398 |
< |
Enumeration e = map.keys(); |
399 |
< |
int count = 0; |
400 |
< |
while(e.hasMoreElements()){ |
401 |
< |
count++; |
402 |
< |
e.nextElement(); |
403 |
< |
} |
404 |
< |
assertEquals(5, count); |
396 |
> |
public void testValues() { |
397 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
398 |
> |
Collection<String> s = map.values(); |
399 |
> |
mustEqual(5, s.size()); |
400 |
> |
assertTrue(s.contains("A")); |
401 |
> |
assertTrue(s.contains("B")); |
402 |
> |
assertTrue(s.contains("C")); |
403 |
> |
assertTrue(s.contains("D")); |
404 |
> |
assertTrue(s.contains("E")); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
< |
* keySet returns a Set containing all the keys |
409 |
< |
*/ |
410 |
< |
public void testKeySet(){ |
411 |
< |
ConcurrentHashMap map = map5(); |
412 |
< |
Set s = map.keySet(); |
413 |
< |
assertEquals(5, s.size()); |
414 |
< |
assertTrue(s.contains(one)); |
143 |
< |
assertTrue(s.contains(two)); |
144 |
< |
assertTrue(s.contains(three)); |
145 |
< |
assertTrue(s.contains(four)); |
146 |
< |
assertTrue(s.contains(five)); |
147 |
< |
} |
148 |
< |
|
149 |
< |
public void testValues(){ |
150 |
< |
ConcurrentHashMap map = map5(); |
151 |
< |
Collection s = map.values(); |
152 |
< |
assertEquals(5, s.size()); |
153 |
< |
assertTrue(s.contains("A")); |
154 |
< |
assertTrue(s.contains("B")); |
155 |
< |
assertTrue(s.contains("C")); |
156 |
< |
assertTrue(s.contains("D")); |
157 |
< |
assertTrue(s.contains("E")); |
158 |
< |
} |
159 |
< |
|
160 |
< |
public void testEntrySet(){ |
161 |
< |
ConcurrentHashMap map = map5(); |
162 |
< |
Set s = map.entrySet(); |
163 |
< |
assertEquals(5, s.size()); |
164 |
< |
Iterator it = s.iterator(); |
408 |
> |
* entrySet contains all pairs |
409 |
> |
*/ |
410 |
> |
public void testEntrySet() { |
411 |
> |
ConcurrentHashMap<Item, String> map = map5(); |
412 |
> |
Set<Map.Entry<Item,String>> s = map.entrySet(); |
413 |
> |
mustEqual(5, s.size()); |
414 |
> |
Iterator<Map.Entry<Item,String>> it = s.iterator(); |
415 |
|
while (it.hasNext()) { |
416 |
< |
Map.Entry e = (Map.Entry) it.next(); |
417 |
< |
assertTrue( |
416 |
> |
Map.Entry<Item,String> e = it.next(); |
417 |
> |
assertTrue( |
418 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
419 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
420 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
< |
* putAll adds all key-value pairs from the given map |
427 |
> |
* putAll adds all key-value pairs from the given map |
428 |
|
*/ |
429 |
< |
public void testPutAll(){ |
430 |
< |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
431 |
< |
ConcurrentHashMap map = map5(); |
432 |
< |
empty.putAll(map); |
433 |
< |
assertEquals(5, empty.size()); |
434 |
< |
assertTrue(empty.containsKey(one)); |
435 |
< |
assertTrue(empty.containsKey(two)); |
436 |
< |
assertTrue(empty.containsKey(three)); |
437 |
< |
assertTrue(empty.containsKey(four)); |
438 |
< |
assertTrue(empty.containsKey(five)); |
429 |
> |
public void testPutAll() { |
430 |
> |
ConcurrentHashMap<Item,String> p = new ConcurrentHashMap<>(); |
431 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
432 |
> |
p.putAll(map); |
433 |
> |
mustEqual(5, p.size()); |
434 |
> |
assertTrue(p.containsKey(one)); |
435 |
> |
assertTrue(p.containsKey(two)); |
436 |
> |
assertTrue(p.containsKey(three)); |
437 |
> |
assertTrue(p.containsKey(four)); |
438 |
> |
assertTrue(p.containsKey(five)); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* putIfAbsent works when the given key is not present |
442 |
> |
* putIfAbsent works when the given key is not present |
443 |
|
*/ |
444 |
< |
public void testPutIfAbsent(){ |
445 |
< |
ConcurrentHashMap map = map5(); |
446 |
< |
map.putIfAbsent(new Integer(6), "Z"); |
447 |
< |
assertTrue(map.containsKey(new Integer(6))); |
444 |
> |
public void testPutIfAbsent() { |
445 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
446 |
> |
map.putIfAbsent(six, "Z"); |
447 |
> |
assertTrue(map.containsKey(six)); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
< |
* putIfAbsent does not add the pair if the key is already present |
451 |
> |
* putIfAbsent does not add the pair if the key is already present |
452 |
|
*/ |
453 |
< |
public void testPutIfAbsent2(){ |
454 |
< |
ConcurrentHashMap map = map5(); |
455 |
< |
assertEquals("A", map.putIfAbsent(one, "Z")); |
453 |
> |
public void testPutIfAbsent2() { |
454 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
455 |
> |
mustEqual("A", map.putIfAbsent(one, "Z")); |
456 |
|
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* remove removes the correct key-value pair from the map |
459 |
> |
* replace fails when the given key is not present |
460 |
|
*/ |
461 |
< |
public void testRemove(){ |
462 |
< |
ConcurrentHashMap map = map5(); |
463 |
< |
map.remove(five); |
464 |
< |
assertEquals(4, map.size()); |
215 |
< |
assertFalse(map.containsKey(five)); |
461 |
> |
public void testReplace() { |
462 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
463 |
> |
assertNull(map.replace(six, "Z")); |
464 |
> |
assertFalse(map.containsKey(six)); |
465 |
|
} |
466 |
|
|
467 |
< |
public void testRemove2(){ |
468 |
< |
ConcurrentHashMap map = map5(); |
469 |
< |
map.remove(five, "E"); |
470 |
< |
assertEquals(4, map.size()); |
471 |
< |
assertFalse(map.containsKey(five)); |
472 |
< |
map.remove(four, "A"); |
473 |
< |
assertEquals(4, map.size()); |
474 |
< |
assertTrue(map.containsKey(four)); |
467 |
> |
/** |
468 |
> |
* replace succeeds if the key is already present |
469 |
> |
*/ |
470 |
> |
public void testReplace2() { |
471 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
472 |
> |
assertNotNull(map.replace(one, "Z")); |
473 |
> |
mustEqual("Z", map.get(one)); |
474 |
> |
} |
475 |
> |
|
476 |
> |
/** |
477 |
> |
* replace value fails when the given key not mapped to expected value |
478 |
> |
*/ |
479 |
> |
public void testReplaceValue() { |
480 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
481 |
> |
mustEqual("A", map.get(one)); |
482 |
> |
assertFalse(map.replace(one, "Z", "Z")); |
483 |
> |
mustEqual("A", map.get(one)); |
484 |
> |
} |
485 |
|
|
486 |
+ |
/** |
487 |
+ |
* replace value succeeds when the given key mapped to expected value |
488 |
+ |
*/ |
489 |
+ |
public void testReplaceValue2() { |
490 |
+ |
ConcurrentHashMap<Item,String> map = map5(); |
491 |
+ |
mustEqual("A", map.get(one)); |
492 |
+ |
assertTrue(map.replace(one, "A", "Z")); |
493 |
+ |
mustEqual("Z", map.get(one)); |
494 |
+ |
} |
495 |
+ |
|
496 |
+ |
/** |
497 |
+ |
* remove removes the correct key-value pair from the map |
498 |
+ |
*/ |
499 |
+ |
public void testRemove() { |
500 |
+ |
ConcurrentHashMap<Item,String> map = map5(); |
501 |
+ |
map.remove(five); |
502 |
+ |
mustEqual(4, map.size()); |
503 |
+ |
assertFalse(map.containsKey(five)); |
504 |
|
} |
505 |
|
|
506 |
|
/** |
507 |
< |
* size returns the correct values |
507 |
> |
* remove(key,value) removes only if pair present |
508 |
|
*/ |
509 |
< |
public void testSize(){ |
510 |
< |
ConcurrentHashMap map = map5(); |
511 |
< |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
512 |
< |
assertEquals(0, empty.size()); |
513 |
< |
assertEquals(5, map.size()); |
509 |
> |
public void testRemove2() { |
510 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
511 |
> |
map.remove(five, "E"); |
512 |
> |
mustEqual(4, map.size()); |
513 |
> |
assertFalse(map.containsKey(five)); |
514 |
> |
map.remove(four, "A"); |
515 |
> |
mustEqual(4, map.size()); |
516 |
> |
assertTrue(map.containsKey(four)); |
517 |
|
} |
518 |
|
|
519 |
< |
public void testToString(){ |
520 |
< |
ConcurrentHashMap map = map5(); |
519 |
> |
/** |
520 |
> |
* size returns the correct values |
521 |
> |
*/ |
522 |
> |
public void testSize() { |
523 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
524 |
> |
ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<>(); |
525 |
> |
mustEqual(0, empty.size()); |
526 |
> |
mustEqual(5, map.size()); |
527 |
> |
} |
528 |
> |
|
529 |
> |
/** |
530 |
> |
* toString contains toString of elements |
531 |
> |
*/ |
532 |
> |
public void testToString() { |
533 |
> |
ConcurrentHashMap<Item,String> map = map5(); |
534 |
|
String s = map.toString(); |
535 |
|
for (int i = 1; i <= 5; ++i) { |
536 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
536 |
> |
assertTrue(s.contains(String.valueOf(i))); |
537 |
|
} |
538 |
< |
} |
538 |
> |
} |
539 |
|
|
540 |
|
// Exception tests |
541 |
< |
|
542 |
< |
public void testConstructor1(){ |
543 |
< |
try{ |
544 |
< |
new ConcurrentHashMap(-1,0,1); |
545 |
< |
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
546 |
< |
}catch(IllegalArgumentException e){} |
547 |
< |
} |
548 |
< |
|
549 |
< |
public void testConstructor2(){ |
550 |
< |
try{ |
551 |
< |
new ConcurrentHashMap(1,0,-1); |
552 |
< |
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
553 |
< |
}catch(IllegalArgumentException e){} |
554 |
< |
} |
555 |
< |
|
556 |
< |
public void testConstructor3(){ |
557 |
< |
try{ |
558 |
< |
new ConcurrentHashMap(-1); |
559 |
< |
fail("ConcurrentHashMap(int) should throw Illegal Argument Exception"); |
560 |
< |
}catch(IllegalArgumentException e){} |
561 |
< |
} |
562 |
< |
|
563 |
< |
public void testGet_NullPointerException(){ |
564 |
< |
try{ |
565 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
541 |
> |
|
542 |
> |
/** |
543 |
> |
* Cannot create with only negative capacity |
544 |
> |
*/ |
545 |
> |
public void testConstructor1() { |
546 |
> |
try { |
547 |
> |
new ConcurrentHashMap<Item,String>(-1); |
548 |
> |
shouldThrow(); |
549 |
> |
} catch (IllegalArgumentException success) {} |
550 |
> |
} |
551 |
> |
|
552 |
> |
/** |
553 |
> |
* Constructor (initialCapacity, loadFactor) throws |
554 |
> |
* IllegalArgumentException if either argument is negative |
555 |
> |
*/ |
556 |
> |
public void testConstructor2() { |
557 |
> |
try { |
558 |
> |
new ConcurrentHashMap<Item,String>(-1, .75f); |
559 |
> |
shouldThrow(); |
560 |
> |
} catch (IllegalArgumentException success) {} |
561 |
> |
|
562 |
> |
try { |
563 |
> |
new ConcurrentHashMap<Item,String>(16, -1); |
564 |
> |
shouldThrow(); |
565 |
> |
} catch (IllegalArgumentException success) {} |
566 |
> |
} |
567 |
> |
|
568 |
> |
/** |
569 |
> |
* Constructor (initialCapacity, loadFactor, concurrencyLevel) |
570 |
> |
* throws IllegalArgumentException if any argument is negative |
571 |
> |
*/ |
572 |
> |
public void testConstructor3() { |
573 |
> |
try { |
574 |
> |
new ConcurrentHashMap<Item,String>(-1, .75f, 1); |
575 |
> |
shouldThrow(); |
576 |
> |
} catch (IllegalArgumentException success) {} |
577 |
> |
|
578 |
> |
try { |
579 |
> |
new ConcurrentHashMap<Item,String>(16, -1, 1); |
580 |
> |
shouldThrow(); |
581 |
> |
} catch (IllegalArgumentException success) {} |
582 |
> |
|
583 |
> |
try { |
584 |
> |
new ConcurrentHashMap<Item,String>(16, .75f, -1); |
585 |
> |
shouldThrow(); |
586 |
> |
} catch (IllegalArgumentException success) {} |
587 |
> |
} |
588 |
> |
|
589 |
> |
/** |
590 |
> |
* ConcurrentHashMap(map) throws NullPointerException if the given |
591 |
> |
* map is null |
592 |
> |
*/ |
593 |
> |
public void testConstructor4() { |
594 |
> |
try { |
595 |
> |
new ConcurrentHashMap<Item,String>(null); |
596 |
> |
shouldThrow(); |
597 |
> |
} catch (NullPointerException success) {} |
598 |
> |
} |
599 |
> |
|
600 |
> |
/** |
601 |
> |
* ConcurrentHashMap(map) creates a new map with the same mappings |
602 |
> |
* as the given map |
603 |
> |
*/ |
604 |
> |
public void testConstructor5() { |
605 |
> |
ConcurrentHashMap<Item,String> map1 = map5(); |
606 |
> |
ConcurrentHashMap<Item,String> map2 = new ConcurrentHashMap<>(map1); |
607 |
> |
assertTrue(map2.equals(map1)); |
608 |
> |
map2.put(one, "F"); |
609 |
> |
assertFalse(map2.equals(map1)); |
610 |
> |
} |
611 |
> |
|
612 |
> |
/** |
613 |
> |
* get(null) throws NPE |
614 |
> |
*/ |
615 |
> |
public void testGet_NullPointerException() { |
616 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
617 |
> |
try { |
618 |
|
c.get(null); |
619 |
< |
fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception"); |
620 |
< |
}catch(NullPointerException e){} |
619 |
> |
shouldThrow(); |
620 |
> |
} catch (NullPointerException success) {} |
621 |
|
} |
622 |
|
|
623 |
< |
public void testContainsKey_NullPointerException(){ |
624 |
< |
try{ |
625 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
623 |
> |
/** |
624 |
> |
* containsKey(null) throws NPE |
625 |
> |
*/ |
626 |
> |
public void testContainsKey_NullPointerException() { |
627 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
628 |
> |
try { |
629 |
|
c.containsKey(null); |
630 |
< |
fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception"); |
631 |
< |
}catch(NullPointerException e){} |
630 |
> |
shouldThrow(); |
631 |
> |
} catch (NullPointerException success) {} |
632 |
|
} |
633 |
|
|
634 |
< |
public void testContainsValue_NullPointerException(){ |
635 |
< |
try{ |
636 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
634 |
> |
/** |
635 |
> |
* containsValue(null) throws NPE |
636 |
> |
*/ |
637 |
> |
public void testContainsValue_NullPointerException() { |
638 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
639 |
> |
try { |
640 |
|
c.containsValue(null); |
641 |
< |
fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception"); |
642 |
< |
}catch(NullPointerException e){} |
641 |
> |
shouldThrow(); |
642 |
> |
} catch (NullPointerException success) {} |
643 |
|
} |
644 |
|
|
645 |
< |
public void testContains_NullPointerException(){ |
646 |
< |
try{ |
647 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
645 |
> |
/** |
646 |
> |
* contains(null) throws NPE |
647 |
> |
*/ |
648 |
> |
public void testContains_NullPointerException() { |
649 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
650 |
> |
try { |
651 |
|
c.contains(null); |
652 |
< |
fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception"); |
653 |
< |
}catch(NullPointerException e){} |
652 |
> |
shouldThrow(); |
653 |
> |
} catch (NullPointerException success) {} |
654 |
|
} |
655 |
|
|
656 |
< |
public void testPut1_NullPointerException(){ |
657 |
< |
try{ |
658 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
656 |
> |
/** |
657 |
> |
* put(null,x) throws NPE |
658 |
> |
*/ |
659 |
> |
public void testPut1_NullPointerException() { |
660 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
661 |
> |
try { |
662 |
|
c.put(null, "whatever"); |
663 |
< |
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
664 |
< |
}catch(NullPointerException e){} |
663 |
> |
shouldThrow(); |
664 |
> |
} catch (NullPointerException success) {} |
665 |
|
} |
666 |
|
|
667 |
< |
public void testPut2_NullPointerException(){ |
668 |
< |
try{ |
669 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
670 |
< |
c.put("whatever", null); |
671 |
< |
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
672 |
< |
}catch(NullPointerException e){} |
667 |
> |
/** |
668 |
> |
* put(x, null) throws NPE |
669 |
> |
*/ |
670 |
> |
public void testPut2_NullPointerException() { |
671 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
672 |
> |
try { |
673 |
> |
c.put(zero, null); |
674 |
> |
shouldThrow(); |
675 |
> |
} catch (NullPointerException success) {} |
676 |
|
} |
677 |
|
|
678 |
< |
public void testPutIfAbsent1_NullPointerException(){ |
679 |
< |
try{ |
680 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
678 |
> |
/** |
679 |
> |
* putIfAbsent(null, x) throws NPE |
680 |
> |
*/ |
681 |
> |
public void testPutIfAbsent1_NullPointerException() { |
682 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
683 |
> |
try { |
684 |
|
c.putIfAbsent(null, "whatever"); |
685 |
< |
fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
686 |
< |
}catch(NullPointerException e){} |
685 |
> |
shouldThrow(); |
686 |
> |
} catch (NullPointerException success) {} |
687 |
|
} |
688 |
|
|
689 |
< |
public void testPutIfAbsent2_NullPointerException(){ |
690 |
< |
try{ |
691 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
692 |
< |
c.putIfAbsent("whatever", null); |
693 |
< |
fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
694 |
< |
}catch(NullPointerException e){} |
689 |
> |
/** |
690 |
> |
* replace(null, x) throws NPE |
691 |
> |
*/ |
692 |
> |
public void testReplace_NullPointerException() { |
693 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
694 |
> |
try { |
695 |
> |
c.replace(null, "whatever"); |
696 |
> |
shouldThrow(); |
697 |
> |
} catch (NullPointerException success) {} |
698 |
|
} |
699 |
|
|
700 |
+ |
/** |
701 |
+ |
* replace(null, x, y) throws NPE |
702 |
+ |
*/ |
703 |
+ |
public void testReplaceValue_NullPointerException() { |
704 |
+ |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
705 |
+ |
try { |
706 |
+ |
c.replace(null, "A", "B"); |
707 |
+ |
shouldThrow(); |
708 |
+ |
} catch (NullPointerException success) {} |
709 |
+ |
} |
710 |
+ |
|
711 |
+ |
/** |
712 |
+ |
* putIfAbsent(x, null) throws NPE |
713 |
+ |
*/ |
714 |
+ |
public void testPutIfAbsent2_NullPointerException() { |
715 |
+ |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
716 |
+ |
try { |
717 |
+ |
c.putIfAbsent(zero, null); |
718 |
+ |
shouldThrow(); |
719 |
+ |
} catch (NullPointerException success) {} |
720 |
+ |
} |
721 |
+ |
|
722 |
+ |
/** |
723 |
+ |
* replace(x, null) throws NPE |
724 |
+ |
*/ |
725 |
+ |
public void testReplace2_NullPointerException() { |
726 |
+ |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
727 |
+ |
try { |
728 |
+ |
c.replace(one, null); |
729 |
+ |
shouldThrow(); |
730 |
+ |
} catch (NullPointerException success) {} |
731 |
+ |
} |
732 |
|
|
733 |
< |
public void testRemove1_NullPointerException(){ |
734 |
< |
try{ |
735 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
736 |
< |
c.put("sadsdf", "asdads"); |
733 |
> |
/** |
734 |
> |
* replace(x, null, y) throws NPE |
735 |
> |
*/ |
736 |
> |
public void testReplaceValue2_NullPointerException() { |
737 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
738 |
> |
try { |
739 |
> |
c.replace(one, null, "A"); |
740 |
> |
shouldThrow(); |
741 |
> |
} catch (NullPointerException success) {} |
742 |
> |
} |
743 |
> |
|
744 |
> |
/** |
745 |
> |
* replace(x, y, null) throws NPE |
746 |
> |
*/ |
747 |
> |
public void testReplaceValue3_NullPointerException() { |
748 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
749 |
> |
try { |
750 |
> |
c.replace(zero, "A", null); |
751 |
> |
shouldThrow(); |
752 |
> |
} catch (NullPointerException success) {} |
753 |
> |
} |
754 |
> |
|
755 |
> |
/** |
756 |
> |
* remove(null) throws NPE |
757 |
> |
*/ |
758 |
> |
public void testRemove1_NullPointerException() { |
759 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
760 |
> |
c.put(one, "asdads"); |
761 |
> |
try { |
762 |
|
c.remove(null); |
763 |
< |
fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione"); |
764 |
< |
}catch(NullPointerException e){} |
763 |
> |
shouldThrow(); |
764 |
> |
} catch (NullPointerException success) {} |
765 |
|
} |
766 |
|
|
767 |
< |
public void testRemove2_NullPointerException(){ |
768 |
< |
try{ |
769 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
770 |
< |
c.put("sadsdf", "asdads"); |
767 |
> |
/** |
768 |
> |
* remove(null, x) throws NPE |
769 |
> |
*/ |
770 |
> |
public void testRemove2_NullPointerException() { |
771 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
772 |
> |
c.put(one, "asdads"); |
773 |
> |
try { |
774 |
|
c.remove(null, "whatever"); |
775 |
< |
fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione"); |
776 |
< |
}catch(NullPointerException e){} |
775 |
> |
shouldThrow(); |
776 |
> |
} catch (NullPointerException success) {} |
777 |
|
} |
778 |
|
|
779 |
< |
public void testSerialization() { |
780 |
< |
ConcurrentHashMap q = map5(); |
779 |
> |
/** |
780 |
> |
* remove(x, null) returns false |
781 |
> |
*/ |
782 |
> |
public void testRemove3() { |
783 |
> |
ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5); |
784 |
> |
c.put(one, "asdads"); |
785 |
> |
assertFalse(c.remove(one, null)); |
786 |
> |
} |
787 |
|
|
788 |
< |
try { |
789 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
790 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
791 |
< |
out.writeObject(q); |
792 |
< |
out.close(); |
788 |
> |
/** |
789 |
> |
* A deserialized/reserialized map equals original |
790 |
> |
*/ |
791 |
> |
public void testSerialization() throws Exception { |
792 |
> |
Map<Item,String> x = map5(); |
793 |
> |
Map<Item,String> y = serialClone(x); |
794 |
|
|
795 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
796 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
797 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
798 |
< |
assertEquals(q.size(), r.size()); |
799 |
< |
assertTrue(q.equals(r)); |
800 |
< |
assertTrue(r.equals(q)); |
801 |
< |
} catch(Exception e){ |
802 |
< |
e.printStackTrace(); |
803 |
< |
fail("unexpected exception"); |
795 |
> |
assertNotSame(x, y); |
796 |
> |
mustEqual(x.size(), y.size()); |
797 |
> |
mustEqual(x, y); |
798 |
> |
mustEqual(y, x); |
799 |
> |
} |
800 |
> |
|
801 |
> |
/** |
802 |
> |
* SetValue of an EntrySet entry sets value in the map. |
803 |
> |
*/ |
804 |
> |
@SuppressWarnings("unchecked") |
805 |
> |
public void testSetValueWriteThrough() { |
806 |
> |
// Adapted from a bug report by Eric Zoerner |
807 |
> |
ConcurrentHashMap<Object,Object> map = new ConcurrentHashMap<>(2, 5.0f, 1); |
808 |
> |
assertTrue(map.isEmpty()); |
809 |
> |
for (int i = 0; i < 20; i++) |
810 |
> |
map.put(itemFor(i), itemFor(i)); |
811 |
> |
assertFalse(map.isEmpty()); |
812 |
> |
Item key = itemFor(16); |
813 |
> |
Map.Entry<Object,Object> entry1 = map.entrySet().iterator().next(); |
814 |
> |
// Unless it happens to be first (in which case remainder of |
815 |
> |
// test is skipped), remove a possibly-colliding key from map |
816 |
> |
// which, under some implementations, may cause entry1 to be |
817 |
> |
// cloned in map |
818 |
> |
if (!entry1.getKey().equals(key)) { |
819 |
> |
map.remove(key); |
820 |
> |
entry1.setValue("XYZ"); |
821 |
> |
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
822 |
> |
} |
823 |
> |
} |
824 |
> |
|
825 |
> |
/** |
826 |
> |
* Tests performance of removeAll when the other collection is much smaller. |
827 |
> |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
828 |
> |
*/ |
829 |
> |
public void testRemoveAll_performance() { |
830 |
> |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
831 |
> |
final int iterations = expensiveTests ? 500 : 2; |
832 |
> |
final ConcurrentHashMap<Item, Item> map = new ConcurrentHashMap<>(); |
833 |
> |
for (int i = 0; i < mapSize; i++) { |
834 |
> |
Item I = itemFor(i); |
835 |
> |
map.put(I, I); |
836 |
|
} |
837 |
+ |
Set<Item> keySet = map.keySet(); |
838 |
+ |
Collection<Item> removeMe = Arrays.asList(new Item[] { minusOne, minusTwo }); |
839 |
+ |
for (int i = 0; i < iterations; i++) |
840 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
841 |
+ |
mustEqual(mapSize, map.size()); |
842 |
+ |
} |
843 |
+ |
|
844 |
+ |
public void testReentrantComputeIfAbsent() { |
845 |
+ |
ConcurrentHashMap<Item, Item> map = new ConcurrentHashMap<>(16); |
846 |
+ |
try { |
847 |
+ |
for (int i = 0; i < 100; i++) { // force a resize |
848 |
+ |
map.computeIfAbsent(new Item(i), key -> new Item(findValue(map, key))); |
849 |
+ |
} |
850 |
+ |
fail("recursive computeIfAbsent should throw IllegalStateException"); |
851 |
+ |
} catch (IllegalStateException success) {} |
852 |
|
} |
853 |
|
|
854 |
< |
|
854 |
> |
private static Item findValue(ConcurrentHashMap<Item, Item> map, |
855 |
> |
Item key) { |
856 |
> |
return (key.value % 5 == 0) ? key : |
857 |
> |
map.computeIfAbsent(new Item(key.value + 1), k -> new Item(findValue(map, k))); |
858 |
> |
} |
859 |
|
} |