21 |
|
/** |
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 Integer.compare(value, other.value); |
45 |
> |
return compare(value, other.value); |
46 |
|
} |
47 |
|
public boolean equals(Object x) { |
48 |
|
return (x instanceof BI) && ((BI)x).value == value; |
76 |
|
break; |
77 |
|
} |
78 |
|
if (r == 0) |
79 |
< |
r = Integer.compare(size(), other.size()); |
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 = new ConcurrentHashMap<>(); |
107 |
< |
for (int i = 0; i < 1000; i++) { |
106 |
> |
int size = 500; // makes measured test run time -> 60ms |
107 |
> |
ConcurrentHashMap<BI, Boolean> m = |
108 |
> |
new ConcurrentHashMap<BI, Boolean>(); |
109 |
> |
for (int i = 0; i < size; i++) { |
110 |
|
assertTrue(m.put(new CI(i), true) == null); |
111 |
|
} |
112 |
< |
for (int i = 0; i < 1000; i++) { |
112 |
> |
for (int i = 0; i < size; i++) { |
113 |
|
assertTrue(m.containsKey(new CI(i))); |
114 |
|
assertTrue(m.containsKey(new DI(i))); |
115 |
|
} |
120 |
|
* on Comparable can be inserted and found. |
121 |
|
*/ |
122 |
|
public void testGenericComparable() { |
123 |
< |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
124 |
< |
for (int i = 0; i < 1000; i++) { |
123 |
> |
int size = 120; // makes measured test run time -> 60ms |
124 |
> |
ConcurrentHashMap<Object, Boolean> m = |
125 |
> |
new ConcurrentHashMap<Object, Boolean>(); |
126 |
> |
for (int i = 0; i < size; i++) { |
127 |
|
BI bi = new BI(i); |
128 |
|
BS bs = new BS(String.valueOf(i)); |
129 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
134 |
|
assertTrue(m.containsKey(bss)); |
135 |
|
assertTrue(m.containsKey(bis)); |
136 |
|
} |
137 |
< |
for (int i = 0; i < 1000; i++) { |
138 |
< |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
137 |
> |
for (int i = 0; i < size; i++) { |
138 |
> |
assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); |
139 |
|
} |
140 |
|
} |
141 |
|
|
145 |
|
* inserted and found. |
146 |
|
*/ |
147 |
|
public void testGenericComparable2() { |
148 |
< |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
149 |
< |
for (int i = 0; i < 1000; i++) { |
150 |
< |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
148 |
> |
int size = 500; // makes measured test run time -> 60ms |
149 |
> |
ConcurrentHashMap<Object, Boolean> m = |
150 |
> |
new ConcurrentHashMap<Object, Boolean>(); |
151 |
> |
for (int i = 0; i < size; i++) { |
152 |
> |
m.put(Collections.singletonList(new BI(i)), true); |
153 |
|
} |
154 |
|
|
155 |
< |
for (int i = 0; i < 1000; i++) { |
155 |
> |
for (int i = 0; i < size; i++) { |
156 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
157 |
|
assertTrue(m.containsKey(bis)); |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
+ |
* Mixtures of instances of comparable and non-comparable classes |
163 |
+ |
* can be inserted and found. |
164 |
+ |
*/ |
165 |
+ |
public void testMixedComparable() { |
166 |
+ |
int size = 1200; // makes measured test run time -> 35ms |
167 |
+ |
ConcurrentHashMap<Object, Object> map = |
168 |
+ |
new ConcurrentHashMap<Object, Object>(); |
169 |
+ |
Random rng = new Random(); |
170 |
+ |
for (int i = 0; i < size; i++) { |
171 |
+ |
Object x; |
172 |
+ |
switch (rng.nextInt(4)) { |
173 |
+ |
case 0: |
174 |
+ |
x = new Object(); |
175 |
+ |
break; |
176 |
+ |
case 1: |
177 |
+ |
x = new CollidingObject(Integer.toString(i)); |
178 |
+ |
break; |
179 |
+ |
default: |
180 |
+ |
x = new ComparableCollidingObject(Integer.toString(i)); |
181 |
+ |
} |
182 |
+ |
assertNull(map.put(x, x)); |
183 |
+ |
} |
184 |
+ |
int count = 0; |
185 |
+ |
for (Object k : map.keySet()) { |
186 |
+ |
assertEquals(map.get(k), k); |
187 |
+ |
++count; |
188 |
+ |
} |
189 |
+ |
assertEquals(count, size); |
190 |
+ |
assertEquals(map.size(), size); |
191 |
+ |
for (Object k : map.keySet()) { |
192 |
+ |
assertEquals(map.put(k, k), k); |
193 |
+ |
} |
194 |
+ |
} |
195 |
+ |
|
196 |
+ |
/** |
197 |
|
* clear removes all pairs |
198 |
|
*/ |
199 |
|
public void testClear() { |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
+ |
* hashCode() equals sum of each key.hashCode ^ value.hashCode |
220 |
+ |
*/ |
221 |
+ |
public void testHashCode() { |
222 |
+ |
ConcurrentHashMap<Integer,String> map = map5(); |
223 |
+ |
int sum = 0; |
224 |
+ |
for (Map.Entry<Integer,String> e : map.entrySet()) |
225 |
+ |
sum += e.getKey().hashCode() ^ e.getValue().hashCode(); |
226 |
+ |
assertEquals(sum, map.hashCode()); |
227 |
+ |
} |
228 |
+ |
|
229 |
+ |
/** |
230 |
|
* contains returns true for contained value |
231 |
|
*/ |
232 |
|
public void testContains() { |
510 |
|
// Exception tests |
511 |
|
|
512 |
|
/** |
513 |
< |
* Cannot create with negative capacity |
513 |
> |
* Cannot create with only negative capacity |
514 |
|
*/ |
515 |
|
public void testConstructor1() { |
516 |
|
try { |
517 |
< |
new ConcurrentHashMap(-1,0,1); |
517 |
> |
new ConcurrentHashMap(-1); |
518 |
|
shouldThrow(); |
519 |
|
} catch (IllegalArgumentException success) {} |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
< |
* Cannot create with negative concurrency level |
524 |
< |
*/ |
523 |
> |
* Constructor (initialCapacity, loadFactor) throws |
524 |
> |
* IllegalArgumentException if either argument is negative |
525 |
> |
*/ |
526 |
|
public void testConstructor2() { |
527 |
|
try { |
528 |
< |
new ConcurrentHashMap(1,0,-1); |
528 |
> |
new ConcurrentHashMap(-1, .75f); |
529 |
|
shouldThrow(); |
530 |
< |
} catch (IllegalArgumentException success) {} |
530 |
> |
} catch (IllegalArgumentException e) {} |
531 |
> |
|
532 |
> |
try { |
533 |
> |
new ConcurrentHashMap(16, -1); |
534 |
> |
shouldThrow(); |
535 |
> |
} catch (IllegalArgumentException e) {} |
536 |
|
} |
537 |
|
|
538 |
|
/** |
539 |
< |
* Cannot create with only negative capacity |
539 |
> |
* Constructor (initialCapacity, loadFactor, concurrencyLevel) |
540 |
> |
* throws IllegalArgumentException if any argument is negative |
541 |
|
*/ |
542 |
|
public void testConstructor3() { |
543 |
|
try { |
544 |
< |
new ConcurrentHashMap(-1); |
544 |
> |
new ConcurrentHashMap(-1, .75f, 1); |
545 |
|
shouldThrow(); |
546 |
< |
} catch (IllegalArgumentException success) {} |
546 |
> |
} catch (IllegalArgumentException e) {} |
547 |
> |
|
548 |
> |
try { |
549 |
> |
new ConcurrentHashMap(16, -1, 1); |
550 |
> |
shouldThrow(); |
551 |
> |
} catch (IllegalArgumentException e) {} |
552 |
> |
|
553 |
> |
try { |
554 |
> |
new ConcurrentHashMap(16, .75f, -1); |
555 |
> |
shouldThrow(); |
556 |
> |
} catch (IllegalArgumentException e) {} |
557 |
> |
} |
558 |
> |
|
559 |
> |
/** |
560 |
> |
* ConcurrentHashMap(map) throws NullPointerException if the given |
561 |
> |
* map is null |
562 |
> |
*/ |
563 |
> |
public void testConstructor4() { |
564 |
> |
try { |
565 |
> |
new ConcurrentHashMap(null); |
566 |
> |
shouldThrow(); |
567 |
> |
} catch (NullPointerException e) {} |
568 |
> |
} |
569 |
> |
|
570 |
> |
/** |
571 |
> |
* ConcurrentHashMap(map) creates a new map with the same mappings |
572 |
> |
* as the given map |
573 |
> |
*/ |
574 |
> |
public void testConstructor5() { |
575 |
> |
ConcurrentHashMap map1 = map5(); |
576 |
> |
ConcurrentHashMap map2 = new ConcurrentHashMap(map5()); |
577 |
> |
assertTrue(map2.equals(map1)); |
578 |
> |
map2.put(one, "F"); |
579 |
> |
assertFalse(map2.equals(map1)); |
580 |
|
} |
581 |
|
|
582 |
+ |
|
583 |
|
/** |
584 |
|
* get(null) throws NPE |
585 |
|
*/ |
763 |
|
Map x = map5(); |
764 |
|
Map y = serialClone(x); |
765 |
|
|
766 |
< |
assertTrue(x != y); |
766 |
> |
assertNotSame(x, y); |
767 |
|
assertEquals(x.size(), y.size()); |
768 |
|
assertEquals(x, y); |
769 |
|
assertEquals(y, x); |