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