1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
/** |
7 |
|
* @test |
8 |
|
* @synopsis Times and checks basic map operations |
9 |
|
*/ |
10 |
– |
import java.util.*; |
10 |
|
import java.io.*; |
11 |
+ |
import java.util.*; |
12 |
|
|
13 |
|
public class NavigableMapCheck { |
14 |
|
|
25 |
|
} |
26 |
|
|
27 |
|
public static void main(String[] args) throws Exception { |
28 |
< |
Class mapClass = null; |
28 |
> |
Class<?> mapClass = null; |
29 |
|
int numTests = 50; |
30 |
|
int size = 50000; |
31 |
|
|
37 |
|
} |
38 |
|
} |
39 |
|
|
40 |
– |
|
40 |
|
if (args.length > 1) |
41 |
|
numTests = Integer.parseInt(args[1]); |
42 |
|
|
45 |
|
|
46 |
|
System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size); |
47 |
|
|
49 |
– |
|
48 |
|
absentSize = 8; |
49 |
|
while (absentSize < size) absentSize <<= 1; |
50 |
|
absentMask = absentSize - 1; |
57 |
|
for (int i = 0; i < size; ++i) |
58 |
|
key[i] = new Integer(i * 2 + 1); |
59 |
|
|
62 |
– |
|
60 |
|
for (int rep = 0; rep < numTests; ++rep) { |
61 |
|
runTest(newMap(mapClass), key); |
62 |
|
} |
63 |
|
|
64 |
|
TestTimer.printStats(); |
68 |
– |
|
65 |
|
} |
66 |
|
|
67 |
< |
static NavigableMap newMap(Class cl) { |
67 |
> |
static NavigableMap newMap(Class<?> cl) { |
68 |
|
try { |
69 |
< |
NavigableMap m = (NavigableMap) cl.newInstance(); |
74 |
< |
return m; |
69 |
> |
return (NavigableMap) cl.getConstructor().newInstance(); |
70 |
|
} catch (Exception e) { |
71 |
|
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
|
} |
73 |
|
} |
74 |
|
|
80 |
– |
|
75 |
|
static void runTest(NavigableMap s, Integer[] key) { |
76 |
|
shuffle(key); |
77 |
|
int size = key.length; |
90 |
|
} |
91 |
|
} |
92 |
|
timer.finish(); |
93 |
< |
reallyAssert (sum == expect * iters); |
93 |
> |
reallyAssert(sum == expect * iters); |
94 |
|
} |
95 |
|
|
96 |
|
static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
100 |
|
if (s.remove(key[i]) != null) ++sum; |
101 |
|
} |
102 |
|
timer.finish(); |
103 |
< |
reallyAssert (sum == expect); |
103 |
> |
reallyAssert(sum == expect); |
104 |
|
} |
105 |
|
|
106 |
|
static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
110 |
|
if (s.put(key[i], absent[i & absentMask]) == null) ++sum; |
111 |
|
} |
112 |
|
timer.finish(); |
113 |
< |
reallyAssert (sum == expect); |
113 |
> |
reallyAssert(sum == expect); |
114 |
|
} |
115 |
|
|
116 |
|
static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
120 |
|
if (s.containsKey(key[i])) ++sum; |
121 |
|
} |
122 |
|
timer.finish(); |
123 |
< |
reallyAssert (sum == expect); |
123 |
> |
reallyAssert(sum == expect); |
124 |
|
} |
125 |
|
|
126 |
|
static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
130 |
|
if (s.remove(key[i]) != null) ++sum; |
131 |
|
} |
132 |
|
timer.finish(); |
133 |
< |
reallyAssert (sum == expect); |
133 |
> |
reallyAssert(sum == expect); |
134 |
|
} |
135 |
|
|
136 |
|
static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
141 |
|
if (s.get(k2[i & absentMask]) != null) ++sum; |
142 |
|
} |
143 |
|
timer.finish(); |
144 |
< |
reallyAssert (sum == n); |
144 |
> |
reallyAssert(sum == n); |
145 |
|
} |
146 |
|
|
147 |
|
static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
152 |
|
if (s.containsKey(k2[i & absentMask])) ++sum; |
153 |
|
} |
154 |
|
timer.finish(); |
155 |
< |
reallyAssert (sum == n); |
155 |
> |
reallyAssert(sum == n); |
156 |
|
} |
157 |
|
|
158 |
|
static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
162 |
|
if (s.get(key[i]) != null) ++sum; |
163 |
|
} |
164 |
|
timer.finish(); |
165 |
< |
reallyAssert (sum == expect); |
165 |
> |
reallyAssert(sum == expect); |
166 |
|
} |
167 |
|
|
174 |
– |
|
168 |
|
static void t9(NavigableMap s) { |
169 |
|
int sum = 0; |
170 |
|
int iters = 20; |
173 |
|
for (int i = 0; i < absentSize; i += step) |
174 |
|
if (s.containsValue(absent[i])) ++sum; |
175 |
|
timer.finish(); |
176 |
< |
reallyAssert (sum != 0); |
176 |
> |
reallyAssert(sum != 0); |
177 |
|
} |
178 |
|
|
179 |
|
static void higherTest(NavigableMap s) { |
186 |
|
e = s.higherEntry(e.getKey()); |
187 |
|
} |
188 |
|
timer.finish(); |
189 |
< |
reallyAssert (sum == iters); |
189 |
> |
reallyAssert(sum == iters); |
190 |
|
} |
191 |
|
|
192 |
|
static void lowerTest(NavigableMap s) { |
199 |
|
e = s.higherEntry(e.getKey()); |
200 |
|
} |
201 |
|
timer.finish(); |
202 |
< |
reallyAssert (sum == iters); |
202 |
> |
reallyAssert(sum == iters); |
203 |
|
} |
204 |
|
|
205 |
|
static void ceilingTest(NavigableMap s) { |
213 |
|
++sum; |
214 |
|
} |
215 |
|
timer.finish(); |
216 |
< |
reallyAssert (sum == iters); |
216 |
> |
reallyAssert(sum == iters); |
217 |
|
} |
218 |
|
|
219 |
|
static void floorTest(NavigableMap s) { |
227 |
|
++sum; |
228 |
|
} |
229 |
|
timer.finish(); |
230 |
< |
reallyAssert (sum == iters-1); |
230 |
> |
reallyAssert(sum == iters-1); |
231 |
|
} |
232 |
|
|
240 |
– |
|
233 |
|
static void ktest(NavigableMap s, int size, Integer[] key) { |
234 |
|
timer.start("ContainsKey ", size); |
235 |
|
Set ks = s.keySet(); |
238 |
|
if (ks.contains(key[i])) ++sum; |
239 |
|
} |
240 |
|
timer.finish(); |
241 |
< |
reallyAssert (sum == size); |
241 |
> |
reallyAssert(sum == size); |
242 |
|
} |
243 |
|
|
252 |
– |
|
244 |
|
static void ittest1(NavigableMap s, int size) { |
245 |
|
int sum = 0; |
246 |
|
timer.start("Iter Key ", size); |
249 |
|
++sum; |
250 |
|
} |
251 |
|
timer.finish(); |
252 |
< |
reallyAssert (sum == size); |
252 |
> |
reallyAssert(sum == size); |
253 |
|
} |
254 |
|
|
255 |
|
static void ittest2(NavigableMap s, int size) { |
260 |
|
++sum; |
261 |
|
} |
262 |
|
timer.finish(); |
263 |
< |
reallyAssert (sum == size); |
263 |
> |
reallyAssert(sum == size); |
264 |
|
} |
265 |
|
static void ittest3(NavigableMap s, int size) { |
266 |
|
int sum = 0; |
270 |
|
++sum; |
271 |
|
} |
272 |
|
timer.finish(); |
273 |
< |
reallyAssert (sum == size); |
273 |
> |
reallyAssert(sum == size); |
274 |
|
} |
275 |
|
|
276 |
|
static void ittest(NavigableMap s, int size) { |
287 |
|
++sum; |
288 |
|
} |
289 |
|
timer.finish(); |
290 |
< |
reallyAssert (sum == size); |
290 |
> |
reallyAssert(sum == size); |
291 |
|
} |
292 |
|
|
302 |
– |
|
293 |
|
static void rittest(NavigableMap s, int size) { |
294 |
|
rittest1(s, size); |
295 |
|
// rittest2(s, size); |
296 |
|
} |
297 |
|
|
308 |
– |
|
298 |
|
static void rtest(NavigableMap s, int size) { |
299 |
|
timer.start("Remove (iterator) ", size); |
300 |
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
313 |
|
timer.finish(); |
314 |
|
} |
315 |
|
|
327 |
– |
|
316 |
|
static void dtest(NavigableMap s, int size, Integer[] key) { |
317 |
|
timer.start("Put (putAll) ", size * 2); |
318 |
< |
NavigableMap s2 = null; |
318 |
> |
final NavigableMap s2; |
319 |
|
try { |
320 |
< |
s2 = (NavigableMap) (s.getClass().newInstance()); |
320 |
> |
s2 = (NavigableMap) s.getClass().getConstructor().newInstance(); |
321 |
|
s2.putAll(s); |
322 |
|
} |
323 |
|
catch (Exception e) { e.printStackTrace(); return; } |
325 |
|
|
326 |
|
timer.start("Iter Equals ", size * 2); |
327 |
|
boolean eqt = s2.equals(s) && s.equals(s2); |
328 |
< |
reallyAssert (eqt); |
328 |
> |
reallyAssert(eqt); |
329 |
|
timer.finish(); |
330 |
|
|
331 |
|
timer.start("Iter HashCode ", size * 2); |
332 |
|
int shc = s.hashCode(); |
333 |
|
int s2hc = s2.hashCode(); |
334 |
< |
reallyAssert (shc == s2hc); |
334 |
> |
reallyAssert(shc == s2hc); |
335 |
|
timer.finish(); |
336 |
|
|
337 |
|
timer.start("Put (present) ", size); |
346 |
|
if (es2.contains(entry)) ++sum; |
347 |
|
} |
348 |
|
timer.finish(); |
349 |
< |
reallyAssert (sum == size); |
349 |
> |
reallyAssert(sum == size); |
350 |
|
|
351 |
|
t6("Get ", size, s2, key, absent); |
352 |
|
|
354 |
|
s2.put(key[size-1], absent[0]); |
355 |
|
timer.start("Iter Equals ", size * 2); |
356 |
|
eqt = s2.equals(s) && s.equals(s2); |
357 |
< |
reallyAssert (!eqt); |
357 |
> |
reallyAssert(!eqt); |
358 |
|
timer.finish(); |
359 |
|
|
360 |
|
timer.start("Iter HashCode ", size * 2); |
361 |
|
int s1h = s.hashCode(); |
362 |
|
int s2h = s2.hashCode(); |
363 |
< |
reallyAssert (s1h != s2h); |
363 |
> |
reallyAssert(s1h != s2h); |
364 |
|
timer.finish(); |
365 |
|
|
366 |
|
s2.put(key[size-1], hold); |
371 |
|
es.remove(s2i.next()); |
372 |
|
timer.finish(); |
373 |
|
|
374 |
< |
reallyAssert (s.isEmpty()); |
374 |
> |
reallyAssert(s.isEmpty()); |
375 |
|
|
376 |
|
timer.start("Clear ", size); |
377 |
|
s2.clear(); |
378 |
|
timer.finish(); |
379 |
< |
reallyAssert (s2.isEmpty() && s.isEmpty()); |
379 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
380 |
|
} |
381 |
|
|
394 |
– |
|
395 |
– |
|
382 |
|
static void test(NavigableMap s, Integer[] key) { |
383 |
|
int size = key.length; |
384 |
|
|
440 |
|
startTime = System.currentTimeMillis(); |
441 |
|
} |
442 |
|
|
457 |
– |
|
443 |
|
String classify() { |
444 |
|
if (name.startsWith("Get")) |
445 |
|
return "Get "; |
456 |
|
void finish() { |
457 |
|
long endTime = System.currentTimeMillis(); |
458 |
|
long time = endTime - startTime; |
459 |
< |
double timePerOp = ((double)time)/numOps; |
459 |
> |
double timePerOp = (double) time /numOps; |
460 |
|
|
461 |
|
Object st = accum.get(name); |
462 |
|
if (st == null) |
479 |
|
if (timePerOp < stats.least) stats.least = timePerOp; |
480 |
|
} |
481 |
|
} |
497 |
– |
|
482 |
|
} |
499 |
– |
|
483 |
|
} |
484 |
|
|
485 |
|
static class Stats { |
493 |
|
|
494 |
|
static void shuffle(Integer[] keys) { |
495 |
|
int size = keys.length; |
496 |
< |
for (int i=size; i>1; i--) { |
496 |
> |
for (int i = size; i > 1; i--) { |
497 |
|
int r = rng.nextInt(i); |
498 |
|
Integer t = keys[i-1]; |
499 |
|
keys[i-1] = keys[r]; |