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