6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
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 |
+ |
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()); |
31 |
|
/** |
32 |
|
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
33 |
|
*/ |
34 |
< |
private static ConcurrentHashMap map5() { |
35 |
< |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
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"); |
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 |
+ |
* Inserted elements that are subclasses of the same Comparable |
113 |
+ |
* class are found. |
114 |
+ |
*/ |
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 |
+ |
* Elements of classes with erased generic type parameters based |
130 |
+ |
* on Comparable can be inserted and found. |
131 |
+ |
*/ |
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 |
+ |
|
152 |
+ |
/** |
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 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 |
|
*/ |
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() { |
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 |
|
/** |
521 |
|
// Exception tests |
522 |
|
|
523 |
|
/** |
524 |
< |
* Cannot create with negative capacity |
524 |
> |
* Cannot create with only negative capacity |
525 |
|
*/ |
526 |
|
public void testConstructor1() { |
527 |
|
try { |
528 |
< |
new ConcurrentHashMap(-1,0,1); |
528 |
> |
new ConcurrentHashMap(-1); |
529 |
|
shouldThrow(); |
530 |
|
} catch (IllegalArgumentException success) {} |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
< |
* Cannot create with negative concurrency level |
535 |
< |
*/ |
534 |
> |
* Constructor (initialCapacity, loadFactor) throws |
535 |
> |
* IllegalArgumentException if either argument is negative |
536 |
> |
*/ |
537 |
|
public void testConstructor2() { |
538 |
|
try { |
539 |
< |
new ConcurrentHashMap(1,0,-1); |
539 |
> |
new ConcurrentHashMap(-1, .75f); |
540 |
|
shouldThrow(); |
541 |
< |
} catch (IllegalArgumentException success) {} |
541 |
> |
} catch (IllegalArgumentException e) {} |
542 |
> |
|
543 |
> |
try { |
544 |
> |
new ConcurrentHashMap(16, -1); |
545 |
> |
shouldThrow(); |
546 |
> |
} catch (IllegalArgumentException e) {} |
547 |
|
} |
548 |
|
|
549 |
|
/** |
550 |
< |
* Cannot create with only negative capacity |
550 |
> |
* Constructor (initialCapacity, loadFactor, concurrencyLevel) |
551 |
> |
* throws IllegalArgumentException if any argument is negative |
552 |
|
*/ |
553 |
|
public void testConstructor3() { |
554 |
|
try { |
555 |
< |
new ConcurrentHashMap(-1); |
555 |
> |
new ConcurrentHashMap(-1, .75f, 1); |
556 |
|
shouldThrow(); |
557 |
< |
} catch (IllegalArgumentException success) {} |
557 |
> |
} catch (IllegalArgumentException e) {} |
558 |
> |
|
559 |
> |
try { |
560 |
> |
new ConcurrentHashMap(16, -1, 1); |
561 |
> |
shouldThrow(); |
562 |
> |
} catch (IllegalArgumentException e) {} |
563 |
> |
|
564 |
> |
try { |
565 |
> |
new ConcurrentHashMap(16, .75f, -1); |
566 |
> |
shouldThrow(); |
567 |
> |
} catch (IllegalArgumentException e) {} |
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 e) {} |
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 |
|
/** |
773 |
|
Map x = map5(); |
774 |
|
Map y = serialClone(x); |
775 |
|
|
776 |
< |
assertTrue(x != y); |
776 |
> |
assertNotSame(x, y); |
777 |
|
assertEquals(x.size(), y.size()); |
778 |
|
assertEquals(x, y); |
779 |
|
assertEquals(y, x); |