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