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