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) { |
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<Integer, String> map5() { |
25 |
> |
ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5); |
26 |
|
assertTrue(map.isEmpty()); |
27 |
|
map.put(one, "A"); |
28 |
|
map.put(two, "B"); |
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 |
+ |
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 |
+ |
/** |
102 |
+ |
* Inserted elements that are subclasses of the same Comparable |
103 |
+ |
* class are found. |
104 |
+ |
*/ |
105 |
+ |
public void testComparableFamily() { |
106 |
+ |
ConcurrentHashMap<BI, Boolean> m = |
107 |
+ |
new ConcurrentHashMap<BI, Boolean>(); |
108 |
+ |
for (int i = 0; i < 1000; i++) { |
109 |
+ |
assertTrue(m.put(new CI(i), true) == null); |
110 |
+ |
} |
111 |
+ |
for (int i = 0; i < 1000; i++) { |
112 |
+ |
assertTrue(m.containsKey(new CI(i))); |
113 |
+ |
assertTrue(m.containsKey(new DI(i))); |
114 |
+ |
} |
115 |
+ |
} |
116 |
+ |
|
117 |
+ |
/** |
118 |
+ |
* Elements of classes with erased generic type parameters based |
119 |
+ |
* on Comparable can be inserted and found. |
120 |
+ |
*/ |
121 |
+ |
public void testGenericComparable() { |
122 |
+ |
ConcurrentHashMap<Object, Boolean> m = |
123 |
+ |
new ConcurrentHashMap<Object, Boolean>(); |
124 |
+ |
for (int i = 0; i < 1000; i++) { |
125 |
+ |
BI bi = new BI(i); |
126 |
+ |
BS bs = new BS(String.valueOf(i)); |
127 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
128 |
+ |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
129 |
+ |
assertTrue(m.putIfAbsent(bis, true) == null); |
130 |
+ |
assertTrue(m.containsKey(bis)); |
131 |
+ |
if (m.putIfAbsent(bss, true) == null) |
132 |
+ |
assertTrue(m.containsKey(bss)); |
133 |
+ |
assertTrue(m.containsKey(bis)); |
134 |
+ |
} |
135 |
+ |
for (int i = 0; i < 1000; i++) { |
136 |
+ |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
137 |
+ |
} |
138 |
+ |
} |
139 |
+ |
|
140 |
+ |
/** |
141 |
+ |
* Elements of non-comparable classes equal to those of classes |
142 |
+ |
* with erased generic type parameters based on Comparable can be |
143 |
+ |
* inserted and found. |
144 |
+ |
*/ |
145 |
+ |
public void testGenericComparable2() { |
146 |
+ |
ConcurrentHashMap<Object, Boolean> m = |
147 |
+ |
new ConcurrentHashMap<Object, Boolean>(); |
148 |
+ |
for (int i = 0; i < 1000; i++) { |
149 |
+ |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
150 |
+ |
} |
151 |
+ |
|
152 |
+ |
for (int i = 0; i < 1000; i++) { |
153 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
154 |
+ |
assertTrue(m.containsKey(bis)); |
155 |
+ |
} |
156 |
+ |
} |
157 |
+ |
|
158 |
+ |
/** |
159 |
+ |
* Mixtures of instances of comparable and non-comparable classes |
160 |
+ |
* can be inserted and found. |
161 |
+ |
*/ |
162 |
+ |
public void testMixedComparable() { |
163 |
+ |
int size = 10000; |
164 |
+ |
ConcurrentHashMap<Object, Object> map = |
165 |
+ |
new ConcurrentHashMap<Object, Object>(); |
166 |
+ |
Random rng = new Random(1370014958369218000L); |
167 |
+ |
for (int i = 0; i < size; i++) { |
168 |
+ |
Object x; |
169 |
+ |
switch (rng.nextInt(4)) { |
170 |
+ |
case 0: |
171 |
+ |
x = new Object(); |
172 |
+ |
break; |
173 |
+ |
case 1: |
174 |
+ |
x = new CollidingObject(Integer.toString(i)); |
175 |
+ |
break; |
176 |
+ |
default: |
177 |
+ |
x = new ComparableCollidingObject(Integer.toString(i)); |
178 |
+ |
} |
179 |
+ |
assertNull(map.put(x, x)); |
180 |
+ |
} |
181 |
+ |
int count = 0; |
182 |
+ |
for (Object k : map.keySet()) { |
183 |
+ |
assertEquals(map.get(k), k); |
184 |
+ |
++count; |
185 |
+ |
} |
186 |
+ |
assertEquals(count, size); |
187 |
+ |
assertEquals(map.size(), size); |
188 |
+ |
for (Object k : map.keySet()) { |
189 |
+ |
assertEquals(map.put(k, k), k); |
190 |
+ |
} |
191 |
+ |
} |
192 |
+ |
|
193 |
|
/** |
194 |
|
* clear removes all pairs |
195 |
|
*/ |
196 |
|
public void testClear() { |
197 |
|
ConcurrentHashMap map = map5(); |
198 |
|
map.clear(); |
199 |
< |
assertEquals(map.size(), 0); |
199 |
> |
assertEquals(0, map.size()); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
212 |
|
assertFalse(map2.equals(map1)); |
213 |
|
} |
214 |
|
|
215 |
+ |
|
216 |
+ |
/** |
217 |
+ |
* hashCode() equals sum of each key.hashCode ^ value.hashCode |
218 |
+ |
*/ |
219 |
+ |
public void testHashCode() { |
220 |
+ |
ConcurrentHashMap<Integer,String> map = map5(); |
221 |
+ |
int sum = 0; |
222 |
+ |
for (Map.Entry<Integer,String> e : map.entrySet()) |
223 |
+ |
sum += e.getKey().hashCode() ^ e.getValue().hashCode(); |
224 |
+ |
assertEquals(sum, map.hashCode()); |
225 |
+ |
} |
226 |
+ |
|
227 |
|
/** |
228 |
|
* contains returns true for contained value |
229 |
|
*/ |
441 |
|
assertEquals("Z", map.get(one)); |
442 |
|
} |
443 |
|
|
278 |
– |
|
444 |
|
/** |
445 |
|
* replace value fails when the given key not mapped to expected value |
446 |
|
*/ |
461 |
|
assertEquals("Z", map.get(one)); |
462 |
|
} |
463 |
|
|
299 |
– |
|
464 |
|
/** |
465 |
|
* remove removes the correct key-value pair from the map |
466 |
|
*/ |
501 |
|
ConcurrentHashMap map = map5(); |
502 |
|
String s = map.toString(); |
503 |
|
for (int i = 1; i <= 5; ++i) { |
504 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
504 |
> |
assertTrue(s.contains(String.valueOf(i))); |
505 |
|
} |
506 |
|
} |
507 |
|
|
508 |
|
// Exception tests |
509 |
|
|
510 |
|
/** |
511 |
< |
* Cannot create with negative capacity |
511 |
> |
* Cannot create with only negative capacity |
512 |
|
*/ |
513 |
|
public void testConstructor1() { |
514 |
|
try { |
515 |
< |
new ConcurrentHashMap(-1,0,1); |
515 |
> |
new ConcurrentHashMap(-1); |
516 |
|
shouldThrow(); |
517 |
|
} catch (IllegalArgumentException success) {} |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
< |
* Cannot create with negative concurrency level |
522 |
< |
*/ |
521 |
> |
* Constructor (initialCapacity, loadFactor) throws |
522 |
> |
* IllegalArgumentException if either argument is negative |
523 |
> |
*/ |
524 |
|
public void testConstructor2() { |
525 |
|
try { |
526 |
< |
new ConcurrentHashMap(1,0,-1); |
526 |
> |
new ConcurrentHashMap(-1, .75f); |
527 |
|
shouldThrow(); |
528 |
< |
} catch (IllegalArgumentException success) {} |
528 |
> |
} catch (IllegalArgumentException e) {} |
529 |
> |
|
530 |
> |
try { |
531 |
> |
new ConcurrentHashMap(16, -1); |
532 |
> |
shouldThrow(); |
533 |
> |
} catch (IllegalArgumentException e) {} |
534 |
|
} |
535 |
+ |
|
536 |
+ |
/** |
537 |
+ |
* Constructor (initialCapacity, loadFactor, concurrencyLevel) |
538 |
+ |
* throws IllegalArgumentException if any argument is negative |
539 |
+ |
*/ |
540 |
+ |
public void testConstructor3() { |
541 |
+ |
try { |
542 |
+ |
new ConcurrentHashMap(-1, .75f, 1); |
543 |
+ |
shouldThrow(); |
544 |
+ |
} catch (IllegalArgumentException e) {} |
545 |
+ |
|
546 |
+ |
try { |
547 |
+ |
new ConcurrentHashMap(16, -1, 1); |
548 |
+ |
shouldThrow(); |
549 |
+ |
} catch (IllegalArgumentException e) {} |
550 |
+ |
|
551 |
+ |
try { |
552 |
+ |
new ConcurrentHashMap(16, .75f, -1); |
553 |
+ |
shouldThrow(); |
554 |
+ |
} catch (IllegalArgumentException e) {} |
555 |
+ |
} |
556 |
|
|
557 |
|
/** |
558 |
< |
* Cannot create with only negative capacity |
558 |
> |
* ConcurrentHashMap(map) throws NullPointerException if the given |
559 |
> |
* map is null |
560 |
|
*/ |
561 |
< |
public void testConstructor3() { |
561 |
> |
public void testConstructor4() { |
562 |
|
try { |
563 |
< |
new ConcurrentHashMap(-1); |
563 |
> |
new ConcurrentHashMap(null); |
564 |
|
shouldThrow(); |
565 |
< |
} catch (IllegalArgumentException success) {} |
565 |
> |
} catch (NullPointerException e) {} |
566 |
> |
} |
567 |
> |
|
568 |
> |
/** |
569 |
> |
* ConcurrentHashMap(map) creates a new map with the same mappings |
570 |
> |
* as the given map |
571 |
> |
*/ |
572 |
> |
public void testConstructor5() { |
573 |
> |
ConcurrentHashMap map1 = map5(); |
574 |
> |
ConcurrentHashMap map2 = new ConcurrentHashMap(map5()); |
575 |
> |
assertTrue(map2.equals(map1)); |
576 |
> |
map2.put(one, "F"); |
577 |
> |
assertFalse(map2.equals(map1)); |
578 |
|
} |
579 |
|
|
580 |
+ |
|
581 |
|
/** |
582 |
|
* get(null) throws NPE |
583 |
|
*/ |
688 |
|
} catch (NullPointerException success) {} |
689 |
|
} |
690 |
|
|
486 |
– |
|
691 |
|
/** |
692 |
|
* replace(x, null) throws NPE |
693 |
|
*/ |
721 |
|
} catch (NullPointerException success) {} |
722 |
|
} |
723 |
|
|
520 |
– |
|
724 |
|
/** |
725 |
|
* remove(null) throws NPE |
726 |
|
*/ |
758 |
|
* A deserialized map equals original |
759 |
|
*/ |
760 |
|
public void testSerialization() throws Exception { |
761 |
< |
ConcurrentHashMap q = map5(); |
761 |
> |
Map x = map5(); |
762 |
> |
Map y = serialClone(x); |
763 |
|
|
764 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
765 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
766 |
< |
out.writeObject(q); |
767 |
< |
out.close(); |
564 |
< |
|
565 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
566 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
567 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
568 |
< |
assertEquals(q.size(), r.size()); |
569 |
< |
assertTrue(q.equals(r)); |
570 |
< |
assertTrue(r.equals(q)); |
764 |
> |
assertNotSame(x, y); |
765 |
> |
assertEquals(x.size(), y.size()); |
766 |
> |
assertEquals(x, y); |
767 |
> |
assertEquals(y, x); |
768 |
|
} |
769 |
|
|
573 |
– |
|
770 |
|
/** |
771 |
|
* SetValue of an EntrySet entry sets value in the map. |
772 |
|
*/ |
778 |
|
map.put(new Integer(i), new Integer(i)); |
779 |
|
assertFalse(map.isEmpty()); |
780 |
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
781 |
< |
|
782 |
< |
// assert that entry1 is not 16 |
783 |
< |
assertTrue("entry is 16, test not valid", |
784 |
< |
!entry1.getKey().equals(new Integer(16))); |
785 |
< |
|
786 |
< |
// remove 16 (a different key) from map |
787 |
< |
// which just happens to cause entry1 to be cloned in map |
788 |
< |
map.remove(new Integer(16)); |
789 |
< |
entry1.setValue("XYZ"); |
594 |
< |
assertTrue(map.containsValue("XYZ")); // fails |
781 |
> |
// Unless it happens to be first (in which case remainder of |
782 |
> |
// test is skipped), remove a possibly-colliding key from map |
783 |
> |
// which, under some implementations, may cause entry1 to be |
784 |
> |
// cloned in map |
785 |
> |
if (!entry1.getKey().equals(new Integer(16))) { |
786 |
> |
map.remove(new Integer(16)); |
787 |
> |
entry1.setValue("XYZ"); |
788 |
> |
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
789 |
> |
} |
790 |
|
} |
791 |
|
|
792 |
|
} |