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 |
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 { |
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); |
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); |
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 |
+ |
* 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 |
< |
* clear removes all pairs |
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); |
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(); |
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(); |
171 |
|
} |
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(); |
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(); |
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(); |
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(); |
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(); |
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(); |
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(); |
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(); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
< |
* Values.toArray contains all values |
269 |
> |
* Values.toArray contains all values |
270 |
|
*/ |
271 |
|
public void testValuesToArray() { |
272 |
|
ConcurrentHashMap map = map5(); |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
< |
* entrySet.toArray contains all entries |
285 |
> |
* entrySet.toArray contains all entries |
286 |
|
*/ |
287 |
|
public void testEntrySetToArray() { |
288 |
|
ConcurrentHashMap map = map5(); |
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(); |
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(); |
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(); |
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 |
|
*/ |
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(); |
419 |
|
map.remove(four, "A"); |
420 |
|
assertEquals(4, map.size()); |
421 |
|
assertTrue(map.containsKey(four)); |
321 |
– |
|
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
< |
* size returns the correct values |
425 |
> |
* size returns the correct values |
426 |
|
*/ |
427 |
|
public void testSize() { |
428 |
|
ConcurrentHashMap map = map5(); |
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 |
|
} |
444 |
|
|
584 |
|
} catch (NullPointerException success) {} |
585 |
|
} |
586 |
|
|
487 |
– |
|
587 |
|
/** |
588 |
|
* replace(x, null) throws NPE |
589 |
|
*/ |
617 |
|
} catch (NullPointerException success) {} |
618 |
|
} |
619 |
|
|
521 |
– |
|
620 |
|
/** |
621 |
|
* remove(null) throws NPE |
622 |
|
*/ |
654 |
|
* A deserialized map equals original |
655 |
|
*/ |
656 |
|
public void testSerialization() throws Exception { |
657 |
< |
ConcurrentHashMap q = map5(); |
657 |
> |
Map x = map5(); |
658 |
> |
Map y = serialClone(x); |
659 |
|
|
660 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
661 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
662 |
< |
out.writeObject(q); |
663 |
< |
out.close(); |
565 |
< |
|
566 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
567 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
568 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
569 |
< |
assertEquals(q.size(), r.size()); |
570 |
< |
assertTrue(q.equals(r)); |
571 |
< |
assertTrue(r.equals(q)); |
660 |
> |
assertNotSame(x, y); |
661 |
> |
assertEquals(x.size(), y.size()); |
662 |
> |
assertEquals(x, y); |
663 |
> |
assertEquals(y, x); |
664 |
|
} |
665 |
|
|
574 |
– |
|
666 |
|
/** |
667 |
|
* SetValue of an EntrySet entry sets value in the map. |
668 |
|
*/ |
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"); |
595 |
< |
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 |
|
|
688 |
|
} |