author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 19806 | dda89341ee2d |
child 42669 | 0b8c4fd7cc85 |
permissions | -rw-r--r-- |
12859 | 1 |
/* |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
12859 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
26 |
* @bug 8005698 |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
27 |
* @run main InPlaceOpsCollisions -shortrun |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
28 |
* @summary Ensure overrides of in-place operations in Maps behave well with lots of collisions. |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
29 |
* @author Brent Christian |
12859 | 30 |
*/ |
31 |
import java.util.*; |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
32 |
import java.util.function.*; |
12859 | 33 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
34 |
public class InPlaceOpsCollisions { |
12859 | 35 |
|
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
36 |
/** |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
37 |
* Number of elements per map. |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
38 |
*/ |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
39 |
private static final int TEST_SIZE = 5000; |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
40 |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
19806
diff
changeset
|
41 |
static final class HashableInteger implements Comparable<HashableInteger> { |
12859 | 42 |
|
43 |
final int value; |
|
44 |
final int hashmask; //yes duplication |
|
45 |
||
46 |
HashableInteger(int value, int hashmask) { |
|
47 |
this.value = value; |
|
48 |
this.hashmask = hashmask; |
|
49 |
} |
|
50 |
||
51 |
@Override |
|
52 |
public boolean equals(Object obj) { |
|
53 |
if (obj instanceof HashableInteger) { |
|
54 |
HashableInteger other = (HashableInteger) obj; |
|
55 |
||
56 |
return other.value == value; |
|
57 |
} |
|
58 |
||
59 |
return false; |
|
60 |
} |
|
61 |
||
62 |
@Override |
|
63 |
public int hashCode() { |
|
64 |
return value % hashmask; |
|
65 |
} |
|
66 |
||
67 |
@Override |
|
68 |
public int compareTo(HashableInteger o) { |
|
69 |
return value - o.value; |
|
70 |
} |
|
71 |
||
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
72 |
@Override |
12859 | 73 |
public String toString() { |
74 |
return Integer.toString(value); |
|
75 |
} |
|
76 |
} |
|
77 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
78 |
static HashableInteger EXTRA_INT_VAL; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
79 |
static String EXTRA_STRING_VAL; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
80 |
|
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
81 |
private static Object[][] makeTestData(int size) { |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
82 |
HashableInteger UNIQUE_OBJECTS[] = new HashableInteger[size]; |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
83 |
HashableInteger COLLIDING_OBJECTS[] = new HashableInteger[size]; |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
84 |
String UNIQUE_STRINGS[] = new String[size]; |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
85 |
String COLLIDING_STRINGS[] = new String[size]; |
12859 | 86 |
|
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
87 |
for (int i = 0; i < size; i++) { |
12859 | 88 |
UNIQUE_OBJECTS[i] = new HashableInteger(i, Integer.MAX_VALUE); |
89 |
COLLIDING_OBJECTS[i] = new HashableInteger(i, 10); |
|
90 |
UNIQUE_STRINGS[i] = unhash(i); |
|
91 |
COLLIDING_STRINGS[i] = (0 == i % 2) |
|
92 |
? UNIQUE_STRINGS[i / 2] |
|
93 |
: "\u0000\u0000\u0000\u0000\u0000" + COLLIDING_STRINGS[i - 1]; |
|
94 |
} |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
95 |
EXTRA_INT_VAL = new HashableInteger(size, Integer.MAX_VALUE); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
96 |
EXTRA_STRING_VAL = new String ("Extra Value"); |
12859 | 97 |
|
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
98 |
return new Object[][] { |
12859 | 99 |
new Object[]{"Unique Objects", UNIQUE_OBJECTS}, |
100 |
new Object[]{"Colliding Objects", COLLIDING_OBJECTS}, |
|
101 |
new Object[]{"Unique Strings", UNIQUE_STRINGS}, |
|
102 |
new Object[]{"Colliding Strings", COLLIDING_STRINGS} |
|
103 |
}; |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Returns a string with a hash equal to the argument. |
|
108 |
* |
|
109 |
* @return string with a hash equal to the argument. |
|
110 |
*/ |
|
111 |
public static String unhash(int target) { |
|
112 |
StringBuilder answer = new StringBuilder(); |
|
113 |
if (target < 0) { |
|
114 |
// String with hash of Integer.MIN_VALUE, 0x80000000 |
|
115 |
answer.append("\\u0915\\u0009\\u001e\\u000c\\u0002"); |
|
116 |
||
117 |
if (target == Integer.MIN_VALUE) { |
|
118 |
return answer.toString(); |
|
119 |
} |
|
120 |
// Find target without sign bit set |
|
121 |
target = target & Integer.MAX_VALUE; |
|
122 |
} |
|
123 |
||
124 |
unhash0(answer, target); |
|
125 |
return answer.toString(); |
|
126 |
} |
|
127 |
||
128 |
private static void unhash0(StringBuilder partial, int target) { |
|
129 |
int div = target / 31; |
|
130 |
int rem = target % 31; |
|
131 |
||
132 |
if (div <= Character.MAX_VALUE) { |
|
133 |
if (div != 0) { |
|
134 |
partial.append((char) div); |
|
135 |
} |
|
136 |
partial.append((char) rem); |
|
137 |
} else { |
|
138 |
unhash0(partial, div); |
|
139 |
partial.append((char) rem); |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
private static void realMain(String[] args) throws Throwable { |
|
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
144 |
boolean shortRun = args.length > 0 && args[0].equals("-shortrun"); |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
145 |
|
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
146 |
Object[][] mapKeys = makeTestData(shortRun ? (TEST_SIZE / 2) : TEST_SIZE); |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
147 |
|
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
148 |
// loop through data sets |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
149 |
for (Object[] keys_desc : mapKeys) { |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
150 |
Map<Object, Object>[] maps = (Map<Object, Object>[]) new Map[]{ |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
151 |
new HashMap<>(), |
12859 | 152 |
new LinkedHashMap<>(), |
153 |
}; |
|
154 |
||
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
155 |
// for each map type. |
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
156 |
for (Map<Object, Object> map : maps) { |
12859 | 157 |
String desc = (String) keys_desc[0]; |
158 |
Object[] keys = (Object[]) keys_desc[1]; |
|
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
159 |
try { |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
160 |
testInPlaceOps(map, desc, keys); |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
161 |
} catch(Exception all) { |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
162 |
unexpected("Failed for " + map.getClass().getName() + " with " + desc, all); |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
163 |
} |
12859 | 164 |
} |
165 |
} |
|
166 |
} |
|
167 |
||
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
168 |
private static <T> void testInsertion(Map<T, T> map, String keys_desc, T[] keys) { |
12859 | 169 |
check("map empty", (map.size() == 0) && map.isEmpty()); |
170 |
||
171 |
for (int i = 0; i < keys.length; i++) { |
|
172 |
check(String.format("insertion: map expected size m%d != i%d", map.size(), i), |
|
173 |
map.size() == i); |
|
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
174 |
check(String.format("insertion: put(%s[%d])", keys_desc, i), null == map.put(keys[i], keys[i])); |
12859 | 175 |
check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
176 |
check(String.format("insertion: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i])); |
12859 | 177 |
} |
178 |
||
179 |
check(String.format("map expected size m%d != k%d", map.size(), keys.length), |
|
180 |
map.size() == keys.length); |
|
181 |
} |
|
182 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
183 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
184 |
private static <T> void testInPlaceOps(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
185 |
System.out.println(map.getClass() + " : " + keys_desc + ", testInPlaceOps"); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
186 |
System.out.flush(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
187 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
188 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
189 |
testPutIfAbsent(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
190 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
191 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
192 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
193 |
testRemoveMapping(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
194 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
195 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
196 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
197 |
testReplaceOldValue(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
198 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
199 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
200 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
201 |
testReplaceIfMapped(map, keys_desc, keys); |
12859 | 202 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
203 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
204 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
205 |
testComputeIfAbsent(map, keys_desc, keys, (k) -> getExtraVal(keys[0])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
206 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
207 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
208 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
209 |
testComputeIfAbsent(map, keys_desc, keys, (k) -> null); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
210 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
211 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
212 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
213 |
testComputeIfPresent(map, keys_desc, keys, (k, v) -> getExtraVal(keys[0])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
214 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
215 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
216 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
217 |
testComputeIfPresent(map, keys_desc, keys, (k, v) -> null); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
218 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
219 |
if (!keys_desc.contains("Strings")) { // avoid parseInt() number format error |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
220 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
221 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
222 |
testComputeNonNull(map, keys_desc, keys); |
12859 | 223 |
} |
224 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
225 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
226 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
227 |
testComputeNull(map, keys_desc, keys); |
12859 | 228 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
229 |
if (!keys_desc.contains("Strings")) { // avoid parseInt() number format error |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
230 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
231 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
232 |
testMergeNonNull(map, keys_desc, keys); |
12859 | 233 |
} |
234 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
235 |
map.clear(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
236 |
testInsertion(map, keys_desc, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
237 |
testMergeNull(map, keys_desc, keys); |
12859 | 238 |
} |
239 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
240 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
241 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
242 |
private static <T> void testPutIfAbsent(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
243 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
244 |
T retVal; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
245 |
removeOddKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
246 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
247 |
retVal = map.putIfAbsent(keys[i], extraVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
248 |
if (i % 2 == 0) { // even: not absent, not put |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
249 |
check(String.format("putIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == keys[i]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
250 |
check(String.format("putIfAbsent: get(%s[%d])", keys_desc, i), keys[i] == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
251 |
check(String.format("putIfAbsent: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
252 |
} else { // odd: absent, was put |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
253 |
check(String.format("putIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == null); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
254 |
check(String.format("putIfAbsent: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
255 |
check(String.format("putIfAbsent: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
256 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
257 |
check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
258 |
} |
12859 | 259 |
check(String.format("map expected size m%d != k%d", map.size(), keys.length), |
260 |
map.size() == keys.length); |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
261 |
} |
12859 | 262 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
263 |
private static <T> void testRemoveMapping(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
264 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
265 |
boolean removed; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
266 |
int removes = 0; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
267 |
remapOddKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
268 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
269 |
removed = map.remove(keys[i], keys[i]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
270 |
if (i % 2 == 0) { // even: original mapping, should be removed |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
271 |
check(String.format("removeMapping: retVal(%s[%d])", keys_desc, i), removed); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
272 |
check(String.format("removeMapping: get(%s[%d])", keys_desc, i), null == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
273 |
check(String.format("removeMapping: !containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
274 |
check(String.format("removeMapping: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
275 |
removes++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
276 |
} else { // odd: new mapping, not removed |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
277 |
check(String.format("removeMapping: retVal(%s[%d])", keys_desc, i), !removed); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
278 |
check(String.format("removeMapping: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
279 |
check(String.format("removeMapping: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
280 |
check(String.format("removeMapping: containsValue(%s[%d])", keys_desc, i), map.containsValue(extraVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
281 |
} |
12859 | 282 |
} |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
283 |
check(String.format("map expected size m%d != k%d", map.size(), keys.length - removes), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
284 |
map.size() == keys.length - removes); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
285 |
} |
12859 | 286 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
287 |
private static <T> void testReplaceOldValue(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
288 |
// remap odds to extraVal |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
289 |
// call replace to replace for extraVal, for all keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
290 |
// check that all keys map to value from keys array |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
291 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
292 |
boolean replaced; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
293 |
remapOddKeys(map, keys); |
12859 | 294 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
295 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
296 |
replaced = map.replace(keys[i], extraVal, keys[i]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
297 |
if (i % 2 == 0) { // even: original mapping, should not be replaced |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
298 |
check(String.format("replaceOldValue: retVal(%s[%d])", keys_desc, i), !replaced); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
299 |
} else { // odd: new mapping, should be replaced |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
300 |
check(String.format("replaceOldValue: get(%s[%d])", keys_desc, i), replaced); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
301 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
302 |
check(String.format("replaceOldValue: get(%s[%d])", keys_desc, i), keys[i] == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
303 |
check(String.format("replaceOldValue: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
304 |
check(String.format("replaceOldValue: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
305 |
// removes++; |
12859 | 306 |
} |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
307 |
check(String.format("replaceOldValue: !containsValue(%s[%s])", keys_desc, extraVal.toString()), !map.containsValue(extraVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
308 |
check(String.format("map expected size m%d != k%d", map.size(), keys.length), |
12859 | 309 |
map.size() == keys.length); |
310 |
} |
|
311 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
312 |
// TODO: Test case for key mapped to null value |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
313 |
private static <T> void testReplaceIfMapped(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
314 |
// remove odd keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
315 |
// call replace for all keys[] |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
316 |
// odd keys should remain absent, even keys should be mapped to EXTRA, no value from keys[] should be in map |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
317 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
318 |
int expectedSize1 = 0; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
319 |
removeOddKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
320 |
int expectedSize2 = map.size(); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
321 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
322 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
323 |
T retVal = map.replace(keys[i], extraVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
324 |
if (i % 2 == 0) { // even: still in map, should be replaced |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
325 |
check(String.format("replaceIfMapped: retVal(%s[%d])", keys_desc, i), retVal == keys[i]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
326 |
check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
327 |
check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
328 |
expectedSize1++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
329 |
} else { // odd: was removed, should not be replaced |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
330 |
check(String.format("replaceIfMapped: retVal(%s[%d])", keys_desc, i), retVal == null); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
331 |
check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), null == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
332 |
check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
333 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
334 |
check(String.format("replaceIfMapped: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
335 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
336 |
check(String.format("replaceIfMapped: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
337 |
check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
338 |
map.size() == expectedSize1); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
339 |
check(String.format("map expected size#2 m%d != k%d", map.size(), expectedSize2), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
340 |
map.size() == expectedSize2); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
341 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
342 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
343 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
344 |
private static <T> void testComputeIfAbsent(Map<T, T> map, String keys_desc, T[] keys, |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
345 |
Function<T,T> mappingFunction) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
346 |
// remove a third of the keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
347 |
// call computeIfAbsent for all keys, func returns EXTRA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
348 |
// check that removed keys now -> EXTRA, other keys -> original val |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
349 |
T expectedVal = mappingFunction.apply(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
350 |
T retVal; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
351 |
int expectedSize = 0; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
352 |
removeThirdKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
353 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
354 |
retVal = map.computeIfAbsent(keys[i], mappingFunction); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
355 |
if (i % 3 != 2) { // key present, not computed |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
356 |
check(String.format("computeIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == keys[i]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
357 |
check(String.format("computeIfAbsent: get(%s[%d])", keys_desc, i), keys[i] == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
358 |
check(String.format("computeIfAbsent: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
359 |
check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
360 |
expectedSize++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
361 |
} else { // key absent, computed unless function return null |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
362 |
check(String.format("computeIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == expectedVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
363 |
check(String.format("computeIfAbsent: get(%s[%d])", keys_desc, i), expectedVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
364 |
check(String.format("computeIfAbsent: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
365 |
// mapping should not be added if function returns null |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
366 |
check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]) != (expectedVal == null)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
367 |
if (expectedVal != null) { expectedSize++; } |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
368 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
369 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
370 |
if (expectedVal != null) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
371 |
check(String.format("computeIfAbsent: containsValue(%s[%s])", keys_desc, expectedVal), map.containsValue(expectedVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
372 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
373 |
check(String.format("map expected size m%d != k%d", map.size(), expectedSize), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
374 |
map.size() == expectedSize); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
375 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
376 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
377 |
private static <T> void testComputeIfPresent(Map<T, T> map, String keys_desc, T[] keys, |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
378 |
BiFunction<T,T,T> mappingFunction) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
379 |
// remove a third of the keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
380 |
// call testComputeIfPresent for all keys[] |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
381 |
// removed keys should remain absent, even keys should be mapped to $RESULT |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
382 |
// no value from keys[] should be in map |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
383 |
T funcResult = mappingFunction.apply(keys[0], keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
384 |
int expectedSize1 = 0; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
385 |
removeThirdKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
386 |
|
12859 | 387 |
for (int i = 0; i < keys.length; i++) { |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
388 |
T retVal = map.computeIfPresent(keys[i], mappingFunction); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
389 |
if (i % 3 != 2) { // key present |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
390 |
if (funcResult == null) { // was removed |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
391 |
check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
392 |
} else { // value was replaced |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
393 |
check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
394 |
expectedSize1++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
395 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
396 |
check(String.format("computeIfPresent: retVal(%s[%s])", keys_desc, i), retVal == funcResult); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
397 |
check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), funcResult == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
398 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
399 |
} else { // odd: was removed, should not be replaced |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
400 |
check(String.format("replaceIfMapped: retVal(%s[%d])", keys_desc, i), retVal == null); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
401 |
check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), null == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
402 |
check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
403 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
404 |
check(String.format("replaceIfMapped: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
405 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
406 |
check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
407 |
map.size() == expectedSize1); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
408 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
409 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
410 |
private static <T> void testComputeNonNull(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
411 |
// remove a third of the keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
412 |
// call compute() for all keys[] |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
413 |
// all keys should be present: removed keys -> EXTRA, others to k-1 |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
414 |
BiFunction<T,T,T> mappingFunction = (k, v) -> { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
415 |
if (v == null) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
416 |
return getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
417 |
} else { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
418 |
return keys[Integer.parseInt(k.toString()) - 1]; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
419 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
420 |
}; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
421 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
422 |
removeThirdKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
423 |
for (int i = 1; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
424 |
T retVal = map.compute(keys[i], mappingFunction); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
425 |
if (i % 3 != 2) { // key present, should be mapped to k-1 |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
426 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == keys[i-1]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
427 |
check(String.format("compute: get(%s[%d])", keys_desc, i), keys[i-1] == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
428 |
} else { // odd: was removed, should be replaced with EXTRA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
429 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
430 |
check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
431 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
432 |
check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
433 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
434 |
check(String.format("map expected size#1 m%d != k%d", map.size(), keys.length), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
435 |
map.size() == keys.length); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
436 |
check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
437 |
check(String.format("compute: !containsValue(%s,[null])", keys_desc), !map.containsValue(null)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
438 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
439 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
440 |
private static <T> void testComputeNull(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
441 |
// remove a third of the keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
442 |
// call compute() for all keys[] |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
443 |
// removed keys should -> EXTRA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
444 |
// for other keys: func returns null, should have no mapping |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
445 |
BiFunction<T,T,T> mappingFunction = (k, v) -> { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
446 |
// if absent/null -> EXTRA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
447 |
// if present -> null |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
448 |
if (v == null) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
449 |
return getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
450 |
} else { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
451 |
return null; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
452 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
453 |
}; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
454 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
455 |
int expectedSize = 0; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
456 |
removeThirdKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
457 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
458 |
T retVal = map.compute(keys[i], mappingFunction); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
459 |
if (i % 3 != 2) { // key present, func returned null, should be absent from map |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
460 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == null); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
461 |
check(String.format("compute: get(%s[%d])", keys_desc, i), null == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
462 |
check(String.format("compute: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
463 |
check(String.format("compute: containsValue(%s[%s])", keys_desc, i), !map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
464 |
} else { // odd: was removed, should now be mapped to EXTRA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
465 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
466 |
check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
467 |
check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
468 |
expectedSize++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
469 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
470 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
471 |
check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
472 |
check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
473 |
map.size() == expectedSize); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
474 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
475 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
476 |
private static <T> void testMergeNonNull(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
477 |
// remove a third of the keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
478 |
// call merge() for all keys[] |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
479 |
// all keys should be present: removed keys now -> EXTRA, other keys -> k-1 |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
480 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
481 |
// Map to preceding key |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
482 |
BiFunction<T,T,T> mappingFunction = (k, v) -> keys[Integer.parseInt(k.toString()) - 1]; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
483 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
484 |
removeThirdKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
485 |
for (int i = 1; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
486 |
T retVal = map.merge(keys[i], extraVal, mappingFunction); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
487 |
if (i % 3 != 2) { // key present, should be mapped to k-1 |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
488 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == keys[i-1]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
489 |
check(String.format("compute: get(%s[%d])", keys_desc, i), keys[i-1] == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
490 |
} else { // odd: was removed, should be replaced with EXTRA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
491 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
492 |
check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
493 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
494 |
check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
495 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
496 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
497 |
check(String.format("map expected size#1 m%d != k%d", map.size(), keys.length), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
498 |
map.size() == keys.length); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
499 |
check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
500 |
check(String.format("compute: !containsValue(%s,[null])", keys_desc), !map.containsValue(null)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
501 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
502 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
503 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
504 |
private static <T> void testMergeNull(Map<T, T> map, String keys_desc, T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
505 |
// remove a third of the keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
506 |
// call merge() for all keys[] |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
507 |
// result: removed keys -> EXTRA, other keys absent |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
508 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
509 |
BiFunction<T,T,T> mappingFunction = (k, v) -> null; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
510 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
511 |
int expectedSize = 0; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
512 |
removeThirdKeys(map, keys); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
513 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
514 |
T retVal = map.merge(keys[i], extraVal, mappingFunction); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
515 |
if (i % 3 != 2) { // key present, func returned null, should be absent from map |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
516 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == null); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
517 |
check(String.format("compute: get(%s[%d])", keys_desc, i), null == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
518 |
check(String.format("compute: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
519 |
} else { // odd: was removed, should now be mapped to EXTRA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
520 |
check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
521 |
check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
522 |
check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
523 |
expectedSize++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
524 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
525 |
check(String.format("compute: containsValue(%s[%s])", keys_desc, i), !map.containsValue(keys[i])); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
526 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
527 |
check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal)); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
528 |
check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
529 |
map.size() == expectedSize); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
530 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
531 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
532 |
/* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
533 |
* Return the EXTRA val for the key type being used |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
534 |
*/ |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
535 |
private static <T> T getExtraVal(T key) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
536 |
if (key instanceof HashableInteger) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
537 |
return (T)EXTRA_INT_VAL; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
538 |
} else { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
539 |
return (T)EXTRA_STRING_VAL; |
12859 | 540 |
} |
541 |
} |
|
542 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
543 |
/* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
544 |
* Remove half of the keys |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
545 |
*/ |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
546 |
private static <T> void removeOddKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
547 |
int removes = 0; |
12859 | 548 |
for (int i = 0; i < keys.length; i++) { |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
549 |
if (i % 2 != 0) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
550 |
map.remove(keys[i]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
551 |
removes++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
552 |
} |
12859 | 553 |
} |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
554 |
check(String.format("map expected size m%d != k%d", map.size(), keys.length - removes), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
555 |
map.size() == keys.length - removes); |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
556 |
} |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
557 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
558 |
/* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
559 |
* Remove every third key |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
560 |
* This will hopefully leave some removed keys in TreeBins for, e.g., computeIfAbsent |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
561 |
* w/ a func that returns null. |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
562 |
* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
563 |
* TODO: consider using this in other tests (and maybe adding a remapThirdKeys) |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
564 |
*/ |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
565 |
private static <T> void removeThirdKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
566 |
int removes = 0; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
567 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
568 |
if (i % 3 == 2) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
569 |
map.remove(keys[i]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
570 |
removes++; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
571 |
} |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
572 |
} |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
573 |
check(String.format("map expected size m%d != k%d", map.size(), keys.length - removes), |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
574 |
map.size() == keys.length - removes); |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
575 |
} |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
576 |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
577 |
/* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
578 |
* Re-map the odd-numbered keys to map to the EXTRA value |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
579 |
*/ |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
580 |
private static <T> void remapOddKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
581 |
T extraVal = getExtraVal(keys[0]); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
582 |
for (int i = 0; i < keys.length; i++) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
583 |
if (i % 2 != 0) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
584 |
map.put(keys[i], extraVal); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
13817
diff
changeset
|
585 |
} |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
586 |
} |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
587 |
} |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
588 |
|
12859 | 589 |
//--------------------- Infrastructure --------------------------- |
590 |
static volatile int passed = 0, failed = 0; |
|
591 |
||
592 |
static void pass() { |
|
593 |
passed++; |
|
594 |
} |
|
595 |
||
596 |
static void fail() { |
|
597 |
failed++; |
|
598 |
(new Error("Failure")).printStackTrace(System.err); |
|
599 |
} |
|
600 |
||
601 |
static void fail(String msg) { |
|
602 |
failed++; |
|
603 |
(new Error("Failure: " + msg)).printStackTrace(System.err); |
|
604 |
} |
|
605 |
||
606 |
static void abort() { |
|
607 |
fail(); |
|
608 |
System.exit(1); |
|
609 |
} |
|
610 |
||
611 |
static void abort(String msg) { |
|
612 |
fail(msg); |
|
613 |
System.exit(1); |
|
614 |
} |
|
615 |
||
616 |
static void unexpected(String msg, Throwable t) { |
|
617 |
System.err.println("Unexpected: " + msg); |
|
618 |
unexpected(t); |
|
619 |
} |
|
620 |
||
621 |
static void unexpected(Throwable t) { |
|
622 |
failed++; |
|
623 |
t.printStackTrace(System.err); |
|
624 |
} |
|
625 |
||
626 |
static void check(boolean cond) { |
|
627 |
if (cond) { |
|
628 |
pass(); |
|
629 |
} else { |
|
630 |
fail(); |
|
631 |
} |
|
632 |
} |
|
633 |
||
634 |
static void check(String desc, boolean cond) { |
|
635 |
if (cond) { |
|
636 |
pass(); |
|
637 |
} else { |
|
638 |
fail(desc); |
|
639 |
} |
|
640 |
} |
|
641 |
||
642 |
static void equal(Object x, Object y) { |
|
643 |
if (Objects.equals(x, y)) { |
|
644 |
pass(); |
|
645 |
} else { |
|
646 |
fail(x + " not equal to " + y); |
|
647 |
} |
|
648 |
} |
|
649 |
||
650 |
public static void main(String[] args) throws Throwable { |
|
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
651 |
Thread.currentThread().setName(Collisions.class.getName()); |
12859 | 652 |
// Thread.currentThread().setPriority(Thread.MAX_PRIORITY); |
653 |
try { |
|
654 |
realMain(args); |
|
655 |
} catch (Throwable t) { |
|
656 |
unexpected(t); |
|
657 |
} |
|
658 |
||
659 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
660 |
if (failed > 0) { |
|
661 |
throw new Error("Some tests failed"); |
|
662 |
} |
|
663 |
} |
|
664 |
} |