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"); |
160 |
|
* can be inserted and found. |
161 |
|
*/ |
162 |
|
public void testMixedComparable() { |
163 |
< |
int size = 10000; |
164 |
< |
ConcurrentHashMap<Object, Object> map = |
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++) { |
167 |
> |
for (int i = 0; i < size; i++) { |
168 |
|
Object x; |
169 |
< |
switch(rng.nextInt(4)) { |
169 |
> |
switch (rng.nextInt(4)) { |
170 |
|
case 0: |
171 |
|
x = new Object(); |
172 |
|
break; |
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 |
|
*/ |
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 |
|
*/ |