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); |
34 |
|
return map; |
35 |
|
} |
36 |
|
|
37 |
+ |
// classes for testing Comparable fallbacks |
38 |
+ |
static class BI implements Comparable<BI> { |
39 |
+ |
private final int value; |
40 |
+ |
BI(int value) { this.value = value; } |
41 |
+ |
public int compareTo(BI other) { |
42 |
+ |
return Integer.compare(value, other.value); |
43 |
+ |
} |
44 |
+ |
public boolean equals(Object x) { |
45 |
+ |
return (x instanceof BI) && ((BI)x).value == value; |
46 |
+ |
} |
47 |
+ |
public int hashCode() { return 42; } |
48 |
+ |
} |
49 |
+ |
static class CI extends BI { CI(int value) { super(value); } } |
50 |
+ |
static class DI extends BI { DI(int value) { super(value); } } |
51 |
+ |
|
52 |
+ |
static class BS implements Comparable<BS> { |
53 |
+ |
private final String value; |
54 |
+ |
BS(String value) { this.value = value; } |
55 |
+ |
public int compareTo(BS other) { |
56 |
+ |
return value.compareTo(other.value); |
57 |
+ |
} |
58 |
+ |
public boolean equals(Object x) { |
59 |
+ |
return (x instanceof BS) && value.equals(((BS)x).value); |
60 |
+ |
} |
61 |
+ |
public int hashCode() { return 42; } |
62 |
+ |
} |
63 |
+ |
|
64 |
+ |
static class LexicographicList<E extends Comparable<E>> extends ArrayList<E> |
65 |
+ |
implements Comparable<LexicographicList<E>> { |
66 |
+ |
LexicographicList(Collection<E> c) { super(c); } |
67 |
+ |
LexicographicList(E e) { super(Collections.singleton(e)); } |
68 |
+ |
public int compareTo(LexicographicList<E> other) { |
69 |
+ |
int common = Math.min(size(), other.size()); |
70 |
+ |
int r = 0; |
71 |
+ |
for (int i = 0; i < common; i++) { |
72 |
+ |
if ((r = get(i).compareTo(other.get(i))) != 0) |
73 |
+ |
break; |
74 |
+ |
} |
75 |
+ |
if (r == 0) |
76 |
+ |
r = Integer.compare(size(), other.size()); |
77 |
+ |
return r; |
78 |
+ |
} |
79 |
+ |
private static final long serialVersionUID = 0; |
80 |
+ |
} |
81 |
+ |
|
82 |
+ |
/** |
83 |
+ |
* Inserted elements that are subclasses of the same Comparable |
84 |
+ |
* class are found. |
85 |
+ |
*/ |
86 |
+ |
public void testComparableFamily() { |
87 |
+ |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
88 |
+ |
for (int i = 0; i < 1000; i++) { |
89 |
+ |
assertTrue(m.put(new CI(i), true) == null); |
90 |
+ |
} |
91 |
+ |
for (int i = 0; i < 1000; i++) { |
92 |
+ |
assertTrue(m.containsKey(new CI(i))); |
93 |
+ |
assertTrue(m.containsKey(new DI(i))); |
94 |
+ |
} |
95 |
+ |
} |
96 |
+ |
|
97 |
+ |
/** |
98 |
+ |
* Elements of classes with erased generic type parameters based |
99 |
+ |
* on Comparable can be inserted and found. |
100 |
+ |
*/ |
101 |
+ |
public void testGenericComparable() { |
102 |
+ |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
103 |
+ |
for (int i = 0; i < 1000; i++) { |
104 |
+ |
BI bi = new BI(i); |
105 |
+ |
BS bs = new BS(String.valueOf(i)); |
106 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
107 |
+ |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
108 |
+ |
assertTrue(m.putIfAbsent(bis, true) == null); |
109 |
+ |
assertTrue(m.containsKey(bis)); |
110 |
+ |
if (m.putIfAbsent(bss, true) == null) |
111 |
+ |
assertTrue(m.containsKey(bss)); |
112 |
+ |
assertTrue(m.containsKey(bis)); |
113 |
+ |
} |
114 |
+ |
for (int i = 0; i < 1000; i++) { |
115 |
+ |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
116 |
+ |
} |
117 |
+ |
} |
118 |
+ |
|
119 |
+ |
/** |
120 |
+ |
* Elements of non-comparable classes equal to those of classes |
121 |
+ |
* with erased generic type parameters based on Comparable can be |
122 |
+ |
* inserted and found. |
123 |
+ |
*/ |
124 |
+ |
public void testGenericComparable2() { |
125 |
+ |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
126 |
+ |
for (int i = 0; i < 1000; i++) { |
127 |
+ |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
128 |
+ |
} |
129 |
+ |
|
130 |
+ |
for (int i = 0; i < 1000; i++) { |
131 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
132 |
+ |
assertTrue(m.containsKey(bis)); |
133 |
+ |
} |
134 |
+ |
} |
135 |
+ |
|
136 |
|
/** |
137 |
|
* clear removes all pairs |
138 |
|
*/ |
139 |
|
public void testClear() { |
140 |
|
ConcurrentHashMap map = map5(); |
141 |
|
map.clear(); |
142 |
< |
assertEquals(map.size(), 0); |
142 |
> |
assertEquals(0, map.size()); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
648 |
|
* A deserialized map equals original |
649 |
|
*/ |
650 |
|
public void testSerialization() throws Exception { |
651 |
< |
ConcurrentHashMap q = map5(); |
651 |
> |
Map x = map5(); |
652 |
> |
Map y = serialClone(x); |
653 |
|
|
654 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
655 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
656 |
< |
out.writeObject(q); |
657 |
< |
out.close(); |
560 |
< |
|
561 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
562 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
563 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
564 |
< |
assertEquals(q.size(), r.size()); |
565 |
< |
assertTrue(q.equals(r)); |
566 |
< |
assertTrue(r.equals(q)); |
654 |
> |
assertTrue(x != y); |
655 |
> |
assertEquals(x.size(), y.size()); |
656 |
> |
assertEquals(x, y); |
657 |
> |
assertEquals(y, x); |
658 |
|
} |
659 |
|
|
660 |
|
/** |
668 |
|
map.put(new Integer(i), new Integer(i)); |
669 |
|
assertFalse(map.isEmpty()); |
670 |
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
671 |
< |
|
672 |
< |
// assert that entry1 is not 16 |
673 |
< |
assertTrue("entry is 16, test not valid", |
674 |
< |
!entry1.getKey().equals(new Integer(16))); |
675 |
< |
|
676 |
< |
// remove 16 (a different key) from map |
677 |
< |
// which just happens to cause entry1 to be cloned in map |
678 |
< |
map.remove(new Integer(16)); |
679 |
< |
entry1.setValue("XYZ"); |
589 |
< |
assertTrue(map.containsValue("XYZ")); // fails |
671 |
> |
// Unless it happens to be first (in which case remainder of |
672 |
> |
// test is skipped), remove a possibly-colliding key from map |
673 |
> |
// which, under some implementations, may cause entry1 to be |
674 |
> |
// cloned in map |
675 |
> |
if (!entry1.getKey().equals(new Integer(16))) { |
676 |
> |
map.remove(new Integer(16)); |
677 |
> |
entry1.setValue("XYZ"); |
678 |
> |
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
679 |
> |
} |
680 |
|
} |
681 |
|
|
682 |
|
} |