1 |
|
/* |
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/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
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.*; |
12 |
< |
import java.util.Enumeration; |
13 |
< |
import java.io.*; |
11 |
> |
import java.util.concurrent.ConcurrentHashMap; |
12 |
|
|
13 |
< |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
13 |
> |
public class ConcurrentHashMapTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
< |
return new TestSuite(ConcurrentHashMapTest.class); |
18 |
> |
return new TestSuite(ConcurrentHashMapTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
23 |
|
*/ |
24 |
< |
private static ConcurrentHashMap map5() { |
25 |
< |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
24 |
> |
private static ConcurrentHashMap map5() { |
25 |
> |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
26 |
|
assertTrue(map.isEmpty()); |
27 |
< |
map.put(one, "A"); |
28 |
< |
map.put(two, "B"); |
29 |
< |
map.put(three, "C"); |
30 |
< |
map.put(four, "D"); |
31 |
< |
map.put(five, "E"); |
27 |
> |
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 |
|
assertFalse(map.isEmpty()); |
33 |
|
assertEquals(5, map.size()); |
34 |
< |
return map; |
34 |
> |
return map; |
35 |
> |
} |
36 |
> |
|
37 |
> |
/** 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 |
> |
// 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 |
> |
return compare(value, other.value); |
46 |
> |
} |
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 |
> |
r = compare(size(), other.size()); |
80 |
> |
return r; |
81 |
> |
} |
82 |
> |
private static final long serialVersionUID = 0; |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* clear removes all pairs |
86 |
> |
* Inserted elements that are subclasses of the same Comparable |
87 |
> |
* class are found. |
88 |
> |
*/ |
89 |
> |
public void testComparableFamily() { |
90 |
> |
ConcurrentHashMap<BI, Boolean> m = |
91 |
> |
new ConcurrentHashMap<BI, Boolean>(); |
92 |
> |
for (int i = 0; i < 1000; i++) { |
93 |
> |
assertTrue(m.put(new CI(i), true) == null); |
94 |
> |
} |
95 |
> |
for (int i = 0; i < 1000; i++) { |
96 |
> |
assertTrue(m.containsKey(new CI(i))); |
97 |
> |
assertTrue(m.containsKey(new DI(i))); |
98 |
> |
} |
99 |
> |
} |
100 |
> |
|
101 |
> |
/** |
102 |
> |
* Elements of classes with erased generic type parameters based |
103 |
> |
* on Comparable can be inserted and found. |
104 |
> |
*/ |
105 |
> |
public void testGenericComparable() { |
106 |
> |
ConcurrentHashMap<Object, Boolean> m = |
107 |
> |
new ConcurrentHashMap<Object, Boolean>(); |
108 |
> |
for (int i = 0; i < 1000; i++) { |
109 |
> |
BI bi = new BI(i); |
110 |
> |
BS bs = new BS(String.valueOf(i)); |
111 |
> |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
112 |
> |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
113 |
> |
assertTrue(m.putIfAbsent(bis, true) == null); |
114 |
> |
assertTrue(m.containsKey(bis)); |
115 |
> |
if (m.putIfAbsent(bss, true) == null) |
116 |
> |
assertTrue(m.containsKey(bss)); |
117 |
> |
assertTrue(m.containsKey(bis)); |
118 |
> |
} |
119 |
> |
for (int i = 0; i < 1000; i++) { |
120 |
> |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
121 |
> |
} |
122 |
> |
} |
123 |
> |
|
124 |
> |
/** |
125 |
> |
* Elements of non-comparable classes equal to those of classes |
126 |
> |
* with erased generic type parameters based on Comparable can be |
127 |
> |
* inserted and found. |
128 |
> |
*/ |
129 |
> |
public void testGenericComparable2() { |
130 |
> |
ConcurrentHashMap<Object, Boolean> m = |
131 |
> |
new ConcurrentHashMap<Object, Boolean>(); |
132 |
> |
for (int i = 0; i < 1000; i++) { |
133 |
> |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
134 |
> |
} |
135 |
> |
|
136 |
> |
for (int i = 0; i < 1000; i++) { |
137 |
> |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
138 |
> |
assertTrue(m.containsKey(bis)); |
139 |
> |
} |
140 |
> |
} |
141 |
> |
|
142 |
> |
/** |
143 |
> |
* clear removes all pairs |
144 |
|
*/ |
145 |
|
public void testClear() { |
146 |
|
ConcurrentHashMap map = map5(); |
147 |
< |
map.clear(); |
148 |
< |
assertEquals(map.size(), 0); |
147 |
> |
map.clear(); |
148 |
> |
assertEquals(0, map.size()); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
< |
* Maps with same contents are equal |
152 |
> |
* Maps with same contents are equal |
153 |
|
*/ |
154 |
|
public void testEquals() { |
155 |
|
ConcurrentHashMap map1 = map5(); |
156 |
|
ConcurrentHashMap map2 = map5(); |
157 |
|
assertEquals(map1, map2); |
158 |
|
assertEquals(map2, map1); |
159 |
< |
map1.clear(); |
159 |
> |
map1.clear(); |
160 |
|
assertFalse(map1.equals(map2)); |
161 |
|
assertFalse(map2.equals(map1)); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* contains returns true for contained value |
165 |
> |
* contains returns true for contained value |
166 |
|
*/ |
167 |
|
public void testContains() { |
168 |
|
ConcurrentHashMap map = map5(); |
169 |
< |
assertTrue(map.contains("A")); |
169 |
> |
assertTrue(map.contains("A")); |
170 |
|
assertFalse(map.contains("Z")); |
171 |
|
} |
172 |
< |
|
172 |
> |
|
173 |
|
/** |
174 |
< |
* containsKey returns true for contained key |
174 |
> |
* containsKey returns true for contained key |
175 |
|
*/ |
176 |
|
public void testContainsKey() { |
177 |
|
ConcurrentHashMap map = map5(); |
178 |
< |
assertTrue(map.containsKey(one)); |
178 |
> |
assertTrue(map.containsKey(one)); |
179 |
|
assertFalse(map.containsKey(zero)); |
180 |
|
} |
181 |
|
|
182 |
|
/** |
183 |
< |
* containsValue returns true for held values |
183 |
> |
* containsValue returns true for held values |
184 |
|
*/ |
185 |
|
public void testContainsValue() { |
186 |
|
ConcurrentHashMap map = map5(); |
187 |
< |
assertTrue(map.containsValue("A")); |
187 |
> |
assertTrue(map.containsValue("A")); |
188 |
|
assertFalse(map.containsValue("Z")); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* enumeration returns an enumeration containing the correct |
193 |
< |
* elements |
192 |
> |
* enumeration returns an enumeration containing the correct |
193 |
> |
* elements |
194 |
|
*/ |
195 |
|
public void testEnumeration() { |
196 |
|
ConcurrentHashMap map = map5(); |
197 |
< |
Enumeration e = map.elements(); |
198 |
< |
int count = 0; |
199 |
< |
while(e.hasMoreElements()){ |
200 |
< |
count++; |
201 |
< |
e.nextElement(); |
202 |
< |
} |
203 |
< |
assertEquals(5, count); |
197 |
> |
Enumeration e = map.elements(); |
198 |
> |
int count = 0; |
199 |
> |
while (e.hasMoreElements()) { |
200 |
> |
count++; |
201 |
> |
e.nextElement(); |
202 |
> |
} |
203 |
> |
assertEquals(5, count); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* get returns the correct element at the given key, |
208 |
< |
* or null if not present |
207 |
> |
* get returns the correct element at the given key, |
208 |
> |
* or null if not present |
209 |
|
*/ |
210 |
|
public void testGet() { |
211 |
|
ConcurrentHashMap map = map5(); |
212 |
< |
assertEquals("A", (String)map.get(one)); |
212 |
> |
assertEquals("A", (String)map.get(one)); |
213 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
214 |
|
assertNull(map.get("anything")); |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
< |
* isEmpty is true of empty map and false for non-empty |
218 |
> |
* isEmpty is true of empty map and false for non-empty |
219 |
|
*/ |
220 |
|
public void testIsEmpty() { |
221 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
222 |
|
ConcurrentHashMap map = map5(); |
223 |
< |
assertTrue(empty.isEmpty()); |
223 |
> |
assertTrue(empty.isEmpty()); |
224 |
|
assertFalse(map.isEmpty()); |
225 |
|
} |
226 |
|
|
227 |
|
/** |
228 |
< |
* keys returns an enumeration containing all the keys from the map |
228 |
> |
* keys returns an enumeration containing all the keys from the map |
229 |
|
*/ |
230 |
|
public void testKeys() { |
231 |
|
ConcurrentHashMap map = map5(); |
232 |
< |
Enumeration e = map.keys(); |
233 |
< |
int count = 0; |
234 |
< |
while(e.hasMoreElements()){ |
235 |
< |
count++; |
236 |
< |
e.nextElement(); |
237 |
< |
} |
238 |
< |
assertEquals(5, count); |
232 |
> |
Enumeration e = map.keys(); |
233 |
> |
int count = 0; |
234 |
> |
while (e.hasMoreElements()) { |
235 |
> |
count++; |
236 |
> |
e.nextElement(); |
237 |
> |
} |
238 |
> |
assertEquals(5, count); |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
< |
* keySet returns a Set containing all the keys |
242 |
> |
* keySet returns a Set containing all the keys |
243 |
|
*/ |
244 |
|
public void testKeySet() { |
245 |
|
ConcurrentHashMap map = map5(); |
246 |
< |
Set s = map.keySet(); |
247 |
< |
assertEquals(5, s.size()); |
248 |
< |
assertTrue(s.contains(one)); |
249 |
< |
assertTrue(s.contains(two)); |
250 |
< |
assertTrue(s.contains(three)); |
251 |
< |
assertTrue(s.contains(four)); |
252 |
< |
assertTrue(s.contains(five)); |
246 |
> |
Set s = map.keySet(); |
247 |
> |
assertEquals(5, s.size()); |
248 |
> |
assertTrue(s.contains(one)); |
249 |
> |
assertTrue(s.contains(two)); |
250 |
> |
assertTrue(s.contains(three)); |
251 |
> |
assertTrue(s.contains(four)); |
252 |
> |
assertTrue(s.contains(five)); |
253 |
|
} |
254 |
|
|
255 |
|
/** |
256 |
< |
* keySet.toArray returns contains all keys |
256 |
> |
* keySet.toArray returns contains all keys |
257 |
|
*/ |
258 |
|
public void testKeySetToArray() { |
259 |
|
ConcurrentHashMap map = map5(); |
260 |
< |
Set s = map.keySet(); |
260 |
> |
Set s = map.keySet(); |
261 |
|
Object[] ar = s.toArray(); |
262 |
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
263 |
< |
assertEquals(5, ar.length); |
263 |
> |
assertEquals(5, ar.length); |
264 |
|
ar[0] = m10; |
265 |
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
< |
* Values.toArray contains all values |
269 |
> |
* Values.toArray contains all values |
270 |
|
*/ |
271 |
|
public void testValuesToArray() { |
272 |
|
ConcurrentHashMap map = map5(); |
273 |
< |
Collection v = map.values(); |
273 |
> |
Collection v = map.values(); |
274 |
|
Object[] ar = v.toArray(); |
275 |
|
ArrayList s = new ArrayList(Arrays.asList(ar)); |
276 |
< |
assertEquals(5, ar.length); |
277 |
< |
assertTrue(s.contains("A")); |
278 |
< |
assertTrue(s.contains("B")); |
279 |
< |
assertTrue(s.contains("C")); |
280 |
< |
assertTrue(s.contains("D")); |
281 |
< |
assertTrue(s.contains("E")); |
276 |
> |
assertEquals(5, ar.length); |
277 |
> |
assertTrue(s.contains("A")); |
278 |
> |
assertTrue(s.contains("B")); |
279 |
> |
assertTrue(s.contains("C")); |
280 |
> |
assertTrue(s.contains("D")); |
281 |
> |
assertTrue(s.contains("E")); |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
< |
* entrySet.toArray contains all entries |
285 |
> |
* entrySet.toArray contains all entries |
286 |
|
*/ |
287 |
|
public void testEntrySetToArray() { |
288 |
|
ConcurrentHashMap map = map5(); |
289 |
< |
Set s = map.entrySet(); |
289 |
> |
Set s = map.entrySet(); |
290 |
|
Object[] ar = s.toArray(); |
291 |
|
assertEquals(5, ar.length); |
292 |
|
for (int i = 0; i < 5; ++i) { |
300 |
|
*/ |
301 |
|
public void testValues() { |
302 |
|
ConcurrentHashMap map = map5(); |
303 |
< |
Collection s = map.values(); |
304 |
< |
assertEquals(5, s.size()); |
305 |
< |
assertTrue(s.contains("A")); |
306 |
< |
assertTrue(s.contains("B")); |
307 |
< |
assertTrue(s.contains("C")); |
308 |
< |
assertTrue(s.contains("D")); |
309 |
< |
assertTrue(s.contains("E")); |
303 |
> |
Collection s = map.values(); |
304 |
> |
assertEquals(5, s.size()); |
305 |
> |
assertTrue(s.contains("A")); |
306 |
> |
assertTrue(s.contains("B")); |
307 |
> |
assertTrue(s.contains("C")); |
308 |
> |
assertTrue(s.contains("D")); |
309 |
> |
assertTrue(s.contains("E")); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
314 |
|
*/ |
315 |
|
public void testEntrySet() { |
316 |
|
ConcurrentHashMap map = map5(); |
317 |
< |
Set s = map.entrySet(); |
318 |
< |
assertEquals(5, s.size()); |
317 |
> |
Set s = map.entrySet(); |
318 |
> |
assertEquals(5, s.size()); |
319 |
|
Iterator it = s.iterator(); |
320 |
|
while (it.hasNext()) { |
321 |
|
Map.Entry e = (Map.Entry) it.next(); |
322 |
< |
assertTrue( |
322 |
> |
assertTrue( |
323 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
324 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
325 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* putAll adds all key-value pairs from the given map |
332 |
> |
* putAll adds all key-value pairs from the given map |
333 |
|
*/ |
334 |
|
public void testPutAll() { |
335 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
336 |
|
ConcurrentHashMap map = map5(); |
337 |
< |
empty.putAll(map); |
338 |
< |
assertEquals(5, empty.size()); |
339 |
< |
assertTrue(empty.containsKey(one)); |
340 |
< |
assertTrue(empty.containsKey(two)); |
341 |
< |
assertTrue(empty.containsKey(three)); |
342 |
< |
assertTrue(empty.containsKey(four)); |
343 |
< |
assertTrue(empty.containsKey(five)); |
337 |
> |
empty.putAll(map); |
338 |
> |
assertEquals(5, empty.size()); |
339 |
> |
assertTrue(empty.containsKey(one)); |
340 |
> |
assertTrue(empty.containsKey(two)); |
341 |
> |
assertTrue(empty.containsKey(three)); |
342 |
> |
assertTrue(empty.containsKey(four)); |
343 |
> |
assertTrue(empty.containsKey(five)); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* putIfAbsent works when the given key is not present |
347 |
> |
* putIfAbsent works when the given key is not present |
348 |
|
*/ |
349 |
|
public void testPutIfAbsent() { |
350 |
|
ConcurrentHashMap map = map5(); |
351 |
< |
map.putIfAbsent(six, "Z"); |
351 |
> |
map.putIfAbsent(six, "Z"); |
352 |
|
assertTrue(map.containsKey(six)); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
< |
* putIfAbsent does not add the pair if the key is already present |
356 |
> |
* putIfAbsent does not add the pair if the key is already present |
357 |
|
*/ |
358 |
|
public void testPutIfAbsent2() { |
359 |
|
ConcurrentHashMap map = map5(); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
< |
* replace fails when the given key is not present |
364 |
> |
* replace fails when the given key is not present |
365 |
|
*/ |
366 |
|
public void testReplace() { |
367 |
|
ConcurrentHashMap map = map5(); |
368 |
< |
assertNull(map.replace(six, "Z")); |
368 |
> |
assertNull(map.replace(six, "Z")); |
369 |
|
assertFalse(map.containsKey(six)); |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
< |
* replace succeeds if the key is already present |
373 |
> |
* replace succeeds if the key is already present |
374 |
|
*/ |
375 |
|
public void testReplace2() { |
376 |
|
ConcurrentHashMap map = map5(); |
378 |
|
assertEquals("Z", map.get(one)); |
379 |
|
} |
380 |
|
|
278 |
– |
|
381 |
|
/** |
382 |
|
* replace value fails when the given key not mapped to expected value |
383 |
|
*/ |
384 |
|
public void testReplaceValue() { |
385 |
|
ConcurrentHashMap map = map5(); |
386 |
|
assertEquals("A", map.get(one)); |
387 |
< |
assertFalse(map.replace(one, "Z", "Z")); |
387 |
> |
assertFalse(map.replace(one, "Z", "Z")); |
388 |
|
assertEquals("A", map.get(one)); |
389 |
|
} |
390 |
|
|
394 |
|
public void testReplaceValue2() { |
395 |
|
ConcurrentHashMap map = map5(); |
396 |
|
assertEquals("A", map.get(one)); |
397 |
< |
assertTrue(map.replace(one, "A", "Z")); |
397 |
> |
assertTrue(map.replace(one, "A", "Z")); |
398 |
|
assertEquals("Z", map.get(one)); |
399 |
|
} |
400 |
|
|
299 |
– |
|
401 |
|
/** |
402 |
< |
* remove removes the correct key-value pair from the map |
402 |
> |
* remove removes the correct key-value pair from the map |
403 |
|
*/ |
404 |
|
public void testRemove() { |
405 |
|
ConcurrentHashMap map = map5(); |
406 |
< |
map.remove(five); |
407 |
< |
assertEquals(4, map.size()); |
408 |
< |
assertFalse(map.containsKey(five)); |
406 |
> |
map.remove(five); |
407 |
> |
assertEquals(4, map.size()); |
408 |
> |
assertFalse(map.containsKey(five)); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
413 |
|
*/ |
414 |
|
public void testRemove2() { |
415 |
|
ConcurrentHashMap map = map5(); |
416 |
< |
map.remove(five, "E"); |
417 |
< |
assertEquals(4, map.size()); |
418 |
< |
assertFalse(map.containsKey(five)); |
419 |
< |
map.remove(four, "A"); |
420 |
< |
assertEquals(4, map.size()); |
421 |
< |
assertTrue(map.containsKey(four)); |
321 |
< |
|
416 |
> |
map.remove(five, "E"); |
417 |
> |
assertEquals(4, map.size()); |
418 |
> |
assertFalse(map.containsKey(five)); |
419 |
> |
map.remove(four, "A"); |
420 |
> |
assertEquals(4, map.size()); |
421 |
> |
assertTrue(map.containsKey(four)); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
< |
* size returns the correct values |
425 |
> |
* size returns the correct values |
426 |
|
*/ |
427 |
|
public void testSize() { |
428 |
|
ConcurrentHashMap map = map5(); |
429 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
430 |
< |
assertEquals(0, empty.size()); |
431 |
< |
assertEquals(5, map.size()); |
430 |
> |
assertEquals(0, empty.size()); |
431 |
> |
assertEquals(5, map.size()); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
438 |
|
ConcurrentHashMap map = map5(); |
439 |
|
String s = map.toString(); |
440 |
|
for (int i = 1; i <= 5; ++i) { |
441 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
441 |
> |
assertTrue(s.contains(String.valueOf(i))); |
442 |
|
} |
443 |
< |
} |
443 |
> |
} |
444 |
|
|
445 |
|
// Exception tests |
446 |
< |
|
446 |
> |
|
447 |
|
/** |
448 |
< |
* Cannot create with negative capacity |
448 |
> |
* Cannot create with negative capacity |
449 |
|
*/ |
450 |
|
public void testConstructor1() { |
451 |
|
try { |
452 |
|
new ConcurrentHashMap(-1,0,1); |
453 |
|
shouldThrow(); |
454 |
< |
} catch(IllegalArgumentException e){} |
454 |
> |
} catch (IllegalArgumentException success) {} |
455 |
|
} |
456 |
|
|
457 |
|
/** |
461 |
|
try { |
462 |
|
new ConcurrentHashMap(1,0,-1); |
463 |
|
shouldThrow(); |
464 |
< |
} catch(IllegalArgumentException e){} |
464 |
> |
} catch (IllegalArgumentException success) {} |
465 |
|
} |
466 |
|
|
467 |
|
/** |
471 |
|
try { |
472 |
|
new ConcurrentHashMap(-1); |
473 |
|
shouldThrow(); |
474 |
< |
} catch(IllegalArgumentException e){} |
474 |
> |
} catch (IllegalArgumentException success) {} |
475 |
|
} |
476 |
|
|
477 |
|
/** |
482 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
483 |
|
c.get(null); |
484 |
|
shouldThrow(); |
485 |
< |
} catch(NullPointerException e){} |
485 |
> |
} catch (NullPointerException success) {} |
486 |
|
} |
487 |
|
|
488 |
|
/** |
493 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
494 |
|
c.containsKey(null); |
495 |
|
shouldThrow(); |
496 |
< |
} catch(NullPointerException e){} |
496 |
> |
} catch (NullPointerException success) {} |
497 |
|
} |
498 |
|
|
499 |
|
/** |
504 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
505 |
|
c.containsValue(null); |
506 |
|
shouldThrow(); |
507 |
< |
} catch(NullPointerException e){} |
507 |
> |
} catch (NullPointerException success) {} |
508 |
|
} |
509 |
|
|
510 |
|
/** |
515 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
516 |
|
c.contains(null); |
517 |
|
shouldThrow(); |
518 |
< |
} catch(NullPointerException e){} |
518 |
> |
} catch (NullPointerException success) {} |
519 |
|
} |
520 |
|
|
521 |
|
/** |
526 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
527 |
|
c.put(null, "whatever"); |
528 |
|
shouldThrow(); |
529 |
< |
} catch(NullPointerException e){} |
529 |
> |
} catch (NullPointerException success) {} |
530 |
|
} |
531 |
|
|
532 |
|
/** |
537 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
538 |
|
c.put("whatever", null); |
539 |
|
shouldThrow(); |
540 |
< |
} catch(NullPointerException e){} |
540 |
> |
} catch (NullPointerException success) {} |
541 |
|
} |
542 |
|
|
543 |
|
/** |
548 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
549 |
|
c.putIfAbsent(null, "whatever"); |
550 |
|
shouldThrow(); |
551 |
< |
} catch(NullPointerException e){} |
551 |
> |
} catch (NullPointerException success) {} |
552 |
|
} |
553 |
|
|
554 |
|
/** |
559 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
560 |
|
c.replace(null, "whatever"); |
561 |
|
shouldThrow(); |
562 |
< |
} catch(NullPointerException e){} |
562 |
> |
} catch (NullPointerException success) {} |
563 |
|
} |
564 |
|
|
565 |
|
/** |
570 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
571 |
|
c.replace(null, one, "whatever"); |
572 |
|
shouldThrow(); |
573 |
< |
} catch(NullPointerException e){} |
573 |
> |
} catch (NullPointerException success) {} |
574 |
|
} |
575 |
|
|
576 |
|
/** |
581 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
582 |
|
c.putIfAbsent("whatever", null); |
583 |
|
shouldThrow(); |
584 |
< |
} catch(NullPointerException e){} |
584 |
> |
} catch (NullPointerException success) {} |
585 |
|
} |
586 |
|
|
487 |
– |
|
587 |
|
/** |
588 |
|
* replace(x, null) throws NPE |
589 |
|
*/ |
592 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
593 |
|
c.replace("whatever", null); |
594 |
|
shouldThrow(); |
595 |
< |
} catch(NullPointerException e){} |
595 |
> |
} catch (NullPointerException success) {} |
596 |
|
} |
597 |
|
|
598 |
|
/** |
603 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
604 |
|
c.replace("whatever", null, "A"); |
605 |
|
shouldThrow(); |
606 |
< |
} catch(NullPointerException e){} |
606 |
> |
} catch (NullPointerException success) {} |
607 |
|
} |
608 |
|
|
609 |
|
/** |
614 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
615 |
|
c.replace("whatever", one, null); |
616 |
|
shouldThrow(); |
617 |
< |
} catch(NullPointerException e){} |
617 |
> |
} catch (NullPointerException success) {} |
618 |
|
} |
619 |
|
|
521 |
– |
|
620 |
|
/** |
621 |
|
* remove(null) throws NPE |
622 |
|
*/ |
626 |
|
c.put("sadsdf", "asdads"); |
627 |
|
c.remove(null); |
628 |
|
shouldThrow(); |
629 |
< |
} catch(NullPointerException e){} |
629 |
> |
} catch (NullPointerException success) {} |
630 |
|
} |
631 |
|
|
632 |
|
/** |
638 |
|
c.put("sadsdf", "asdads"); |
639 |
|
c.remove(null, "whatever"); |
640 |
|
shouldThrow(); |
641 |
< |
} catch(NullPointerException e){} |
641 |
> |
} catch (NullPointerException success) {} |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
|
* remove(x, null) returns false |
646 |
|
*/ |
647 |
|
public void testRemove3() { |
648 |
< |
try { |
649 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
650 |
< |
c.put("sadsdf", "asdads"); |
553 |
< |
assertFalse(c.remove("sadsdf", null)); |
554 |
< |
} catch(NullPointerException e){ |
555 |
< |
fail(); |
556 |
< |
} |
648 |
> |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
649 |
> |
c.put("sadsdf", "asdads"); |
650 |
> |
assertFalse(c.remove("sadsdf", null)); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
654 |
|
* A deserialized map equals original |
655 |
|
*/ |
656 |
< |
public void testSerialization() { |
657 |
< |
ConcurrentHashMap q = map5(); |
658 |
< |
|
659 |
< |
try { |
660 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
661 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
662 |
< |
out.writeObject(q); |
663 |
< |
out.close(); |
570 |
< |
|
571 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
572 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
573 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
574 |
< |
assertEquals(q.size(), r.size()); |
575 |
< |
assertTrue(q.equals(r)); |
576 |
< |
assertTrue(r.equals(q)); |
577 |
< |
} catch(Exception e){ |
578 |
< |
e.printStackTrace(); |
579 |
< |
unexpectedException(); |
580 |
< |
} |
656 |
> |
public void testSerialization() throws Exception { |
657 |
> |
Map x = map5(); |
658 |
> |
Map y = serialClone(x); |
659 |
> |
|
660 |
> |
assertNotSame(x, y); |
661 |
> |
assertEquals(x.size(), y.size()); |
662 |
> |
assertEquals(x, y); |
663 |
> |
assertEquals(y, x); |
664 |
|
} |
665 |
|
|
583 |
– |
|
666 |
|
/** |
667 |
|
* SetValue of an EntrySet entry sets value in the map. |
668 |
|
*/ |
669 |
|
public void testSetValueWriteThrough() { |
670 |
< |
// Adapted from a bug report by Eric Zoerner |
670 |
> |
// Adapted from a bug report by Eric Zoerner |
671 |
|
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
672 |
|
assertTrue(map.isEmpty()); |
673 |
|
for (int i = 0; i < 20; i++) |
674 |
|
map.put(new Integer(i), new Integer(i)); |
675 |
|
assertFalse(map.isEmpty()); |
676 |
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
677 |
< |
|
678 |
< |
// assert that entry1 is not 16 |
679 |
< |
assertTrue("entry is 16, test not valid", |
680 |
< |
!entry1.getKey().equals(new Integer(16))); |
681 |
< |
|
682 |
< |
// remove 16 (a different key) from map |
683 |
< |
// which just happens to cause entry1 to be cloned in map |
684 |
< |
map.remove(new Integer(16)); |
685 |
< |
entry1.setValue("XYZ"); |
604 |
< |
assertTrue(map.containsValue("XYZ")); // fails |
677 |
> |
// Unless it happens to be first (in which case remainder of |
678 |
> |
// test is skipped), remove a possibly-colliding key from map |
679 |
> |
// which, under some implementations, may cause entry1 to be |
680 |
> |
// cloned in map |
681 |
> |
if (!entry1.getKey().equals(new Integer(16))) { |
682 |
> |
map.remove(new Integer(16)); |
683 |
> |
entry1.setValue("XYZ"); |
684 |
> |
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
685 |
> |
} |
686 |
|
} |
687 |
< |
|
687 |
> |
|
688 |
|
} |