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()); |
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 < 1000; i++) { |
119 |
> |
for (int i = 0; i < size; i++) { |
120 |
|
assertTrue(m.put(new CI(i), true) == null); |
121 |
|
} |
122 |
< |
for (int i = 0; i < 1000; i++) { |
122 |
> |
for (int i = 0; i < size; i++) { |
123 |
|
assertTrue(m.containsKey(new CI(i))); |
124 |
|
assertTrue(m.containsKey(new DI(i))); |
125 |
|
} |
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 < 1000; i++) { |
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); |
144 |
|
assertTrue(m.containsKey(bss)); |
145 |
|
assertTrue(m.containsKey(bis)); |
146 |
|
} |
147 |
< |
for (int i = 0; i < 1000; i++) { |
148 |
< |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
147 |
> |
for (int i = 0; i < size; i++) { |
148 |
> |
assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); |
149 |
|
} |
150 |
|
} |
151 |
|
|
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 < 1000; i++) { |
162 |
< |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
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 < 1000; i++) { |
165 |
> |
for (int i = 0; i < size; i++) { |
166 |
|
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
167 |
|
assertTrue(m.containsKey(bis)); |
168 |
|
} |
173 |
|
* can be inserted and found. |
174 |
|
*/ |
175 |
|
public void testMixedComparable() { |
176 |
< |
int size = 10000; |
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(1370014958369218000L); |
179 |
> |
Random rng = new Random(); |
180 |
|
for (int i = 0; i < size; i++) { |
181 |
|
Object x; |
182 |
|
switch (rng.nextInt(4)) { |
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 |
|
/** |
538 |
|
try { |
539 |
|
new ConcurrentHashMap(-1, .75f); |
540 |
|
shouldThrow(); |
541 |
< |
} catch (IllegalArgumentException e) {} |
541 |
> |
} catch (IllegalArgumentException success) {} |
542 |
|
|
543 |
|
try { |
544 |
|
new ConcurrentHashMap(16, -1); |
545 |
|
shouldThrow(); |
546 |
< |
} catch (IllegalArgumentException e) {} |
546 |
> |
} catch (IllegalArgumentException success) {} |
547 |
|
} |
548 |
|
|
549 |
|
/** |
554 |
|
try { |
555 |
|
new ConcurrentHashMap(-1, .75f, 1); |
556 |
|
shouldThrow(); |
557 |
< |
} catch (IllegalArgumentException e) {} |
557 |
> |
} catch (IllegalArgumentException success) {} |
558 |
|
|
559 |
|
try { |
560 |
|
new ConcurrentHashMap(16, -1, 1); |
561 |
|
shouldThrow(); |
562 |
< |
} catch (IllegalArgumentException e) {} |
562 |
> |
} catch (IllegalArgumentException success) {} |
563 |
|
|
564 |
|
try { |
565 |
|
new ConcurrentHashMap(16, .75f, -1); |
566 |
|
shouldThrow(); |
567 |
< |
} catch (IllegalArgumentException e) {} |
567 |
> |
} catch (IllegalArgumentException success) {} |
568 |
|
} |
569 |
|
|
570 |
|
/** |
575 |
|
try { |
576 |
|
new ConcurrentHashMap(null); |
577 |
|
shouldThrow(); |
578 |
< |
} catch (NullPointerException e) {} |
578 |
> |
} catch (NullPointerException success) {} |
579 |
|
} |
580 |
|
|
581 |
|
/** |
590 |
|
assertFalse(map2.equals(map1)); |
591 |
|
} |
592 |
|
|
579 |
– |
|
593 |
|
/** |
594 |
|
* get(null) throws NPE |
595 |
|
*/ |
596 |
|
public void testGet_NullPointerException() { |
597 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
598 |
|
try { |
585 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
599 |
|
c.get(null); |
600 |
|
shouldThrow(); |
601 |
|
} catch (NullPointerException success) {} |
605 |
|
* containsKey(null) throws NPE |
606 |
|
*/ |
607 |
|
public void testContainsKey_NullPointerException() { |
608 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
609 |
|
try { |
596 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
610 |
|
c.containsKey(null); |
611 |
|
shouldThrow(); |
612 |
|
} catch (NullPointerException success) {} |
616 |
|
* containsValue(null) throws NPE |
617 |
|
*/ |
618 |
|
public void testContainsValue_NullPointerException() { |
619 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
620 |
|
try { |
607 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
621 |
|
c.containsValue(null); |
622 |
|
shouldThrow(); |
623 |
|
} catch (NullPointerException success) {} |
627 |
|
* contains(null) throws NPE |
628 |
|
*/ |
629 |
|
public void testContains_NullPointerException() { |
630 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
631 |
|
try { |
618 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
632 |
|
c.contains(null); |
633 |
|
shouldThrow(); |
634 |
|
} catch (NullPointerException success) {} |
638 |
|
* put(null,x) throws NPE |
639 |
|
*/ |
640 |
|
public void testPut1_NullPointerException() { |
641 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
642 |
|
try { |
629 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
643 |
|
c.put(null, "whatever"); |
644 |
|
shouldThrow(); |
645 |
|
} catch (NullPointerException success) {} |
649 |
|
* put(x, null) throws NPE |
650 |
|
*/ |
651 |
|
public void testPut2_NullPointerException() { |
652 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
653 |
|
try { |
640 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
654 |
|
c.put("whatever", null); |
655 |
|
shouldThrow(); |
656 |
|
} catch (NullPointerException success) {} |
660 |
|
* putIfAbsent(null, x) throws NPE |
661 |
|
*/ |
662 |
|
public void testPutIfAbsent1_NullPointerException() { |
663 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
664 |
|
try { |
651 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
665 |
|
c.putIfAbsent(null, "whatever"); |
666 |
|
shouldThrow(); |
667 |
|
} catch (NullPointerException success) {} |
671 |
|
* replace(null, x) throws NPE |
672 |
|
*/ |
673 |
|
public void testReplace_NullPointerException() { |
674 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
675 |
|
try { |
662 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
676 |
|
c.replace(null, "whatever"); |
677 |
|
shouldThrow(); |
678 |
|
} catch (NullPointerException success) {} |
682 |
|
* replace(null, x, y) throws NPE |
683 |
|
*/ |
684 |
|
public void testReplaceValue_NullPointerException() { |
685 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
686 |
|
try { |
673 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
687 |
|
c.replace(null, one, "whatever"); |
688 |
|
shouldThrow(); |
689 |
|
} catch (NullPointerException success) {} |
693 |
|
* putIfAbsent(x, null) throws NPE |
694 |
|
*/ |
695 |
|
public void testPutIfAbsent2_NullPointerException() { |
696 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
697 |
|
try { |
684 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
698 |
|
c.putIfAbsent("whatever", null); |
699 |
|
shouldThrow(); |
700 |
|
} catch (NullPointerException success) {} |
704 |
|
* replace(x, null) throws NPE |
705 |
|
*/ |
706 |
|
public void testReplace2_NullPointerException() { |
707 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
708 |
|
try { |
695 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
709 |
|
c.replace("whatever", null); |
710 |
|
shouldThrow(); |
711 |
|
} catch (NullPointerException success) {} |
715 |
|
* replace(x, null, y) throws NPE |
716 |
|
*/ |
717 |
|
public void testReplaceValue2_NullPointerException() { |
718 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
719 |
|
try { |
706 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
720 |
|
c.replace("whatever", null, "A"); |
721 |
|
shouldThrow(); |
722 |
|
} catch (NullPointerException success) {} |
726 |
|
* replace(x, y, null) throws NPE |
727 |
|
*/ |
728 |
|
public void testReplaceValue3_NullPointerException() { |
729 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
730 |
|
try { |
717 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
731 |
|
c.replace("whatever", one, null); |
732 |
|
shouldThrow(); |
733 |
|
} catch (NullPointerException success) {} |
737 |
|
* remove(null) throws NPE |
738 |
|
*/ |
739 |
|
public void testRemove1_NullPointerException() { |
740 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
741 |
+ |
c.put("sadsdf", "asdads"); |
742 |
|
try { |
728 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
729 |
– |
c.put("sadsdf", "asdads"); |
743 |
|
c.remove(null); |
744 |
|
shouldThrow(); |
745 |
|
} catch (NullPointerException success) {} |
749 |
|
* remove(null, x) throws NPE |
750 |
|
*/ |
751 |
|
public void testRemove2_NullPointerException() { |
752 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
753 |
+ |
c.put("sadsdf", "asdads"); |
754 |
|
try { |
740 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
741 |
– |
c.put("sadsdf", "asdads"); |
755 |
|
c.remove(null, "whatever"); |
756 |
|
shouldThrow(); |
757 |
|
} catch (NullPointerException success) {} |