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(); |
70 |
|
return m; |
73 |
|
} |
74 |
|
} |
75 |
|
|
80 |
– |
|
76 |
|
static void runTest(NavigableMap s, Integer[] key) { |
77 |
|
shuffle(key); |
78 |
|
int size = key.length; |
91 |
|
} |
92 |
|
} |
93 |
|
timer.finish(); |
94 |
< |
reallyAssert (sum == expect * iters); |
94 |
> |
reallyAssert(sum == expect * iters); |
95 |
|
} |
96 |
|
|
97 |
|
static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
101 |
|
if (s.remove(key[i]) != null) ++sum; |
102 |
|
} |
103 |
|
timer.finish(); |
104 |
< |
reallyAssert (sum == expect); |
104 |
> |
reallyAssert(sum == expect); |
105 |
|
} |
106 |
|
|
107 |
|
static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
111 |
|
if (s.put(key[i], absent[i & absentMask]) == null) ++sum; |
112 |
|
} |
113 |
|
timer.finish(); |
114 |
< |
reallyAssert (sum == expect); |
114 |
> |
reallyAssert(sum == expect); |
115 |
|
} |
116 |
|
|
117 |
|
static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
121 |
|
if (s.containsKey(key[i])) ++sum; |
122 |
|
} |
123 |
|
timer.finish(); |
124 |
< |
reallyAssert (sum == expect); |
124 |
> |
reallyAssert(sum == expect); |
125 |
|
} |
126 |
|
|
127 |
|
static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
131 |
|
if (s.remove(key[i]) != null) ++sum; |
132 |
|
} |
133 |
|
timer.finish(); |
134 |
< |
reallyAssert (sum == expect); |
134 |
> |
reallyAssert(sum == expect); |
135 |
|
} |
136 |
|
|
137 |
|
static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
142 |
|
if (s.get(k2[i & absentMask]) != null) ++sum; |
143 |
|
} |
144 |
|
timer.finish(); |
145 |
< |
reallyAssert (sum == n); |
145 |
> |
reallyAssert(sum == n); |
146 |
|
} |
147 |
|
|
148 |
|
static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
153 |
|
if (s.containsKey(k2[i & absentMask])) ++sum; |
154 |
|
} |
155 |
|
timer.finish(); |
156 |
< |
reallyAssert (sum == n); |
156 |
> |
reallyAssert(sum == n); |
157 |
|
} |
158 |
|
|
159 |
|
static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
163 |
|
if (s.get(key[i]) != null) ++sum; |
164 |
|
} |
165 |
|
timer.finish(); |
166 |
< |
reallyAssert (sum == expect); |
166 |
> |
reallyAssert(sum == expect); |
167 |
|
} |
168 |
|
|
174 |
– |
|
169 |
|
static void t9(NavigableMap s) { |
170 |
|
int sum = 0; |
171 |
|
int iters = 20; |
174 |
|
for (int i = 0; i < absentSize; i += step) |
175 |
|
if (s.containsValue(absent[i])) ++sum; |
176 |
|
timer.finish(); |
177 |
< |
reallyAssert (sum != 0); |
177 |
> |
reallyAssert(sum != 0); |
178 |
|
} |
179 |
|
|
180 |
|
static void higherTest(NavigableMap s) { |
187 |
|
e = s.higherEntry(e.getKey()); |
188 |
|
} |
189 |
|
timer.finish(); |
190 |
< |
reallyAssert (sum == iters); |
190 |
> |
reallyAssert(sum == iters); |
191 |
|
} |
192 |
|
|
193 |
|
static void lowerTest(NavigableMap s) { |
200 |
|
e = s.higherEntry(e.getKey()); |
201 |
|
} |
202 |
|
timer.finish(); |
203 |
< |
reallyAssert (sum == iters); |
203 |
> |
reallyAssert(sum == iters); |
204 |
|
} |
205 |
|
|
206 |
|
static void ceilingTest(NavigableMap s) { |
214 |
|
++sum; |
215 |
|
} |
216 |
|
timer.finish(); |
217 |
< |
reallyAssert (sum == iters); |
217 |
> |
reallyAssert(sum == iters); |
218 |
|
} |
219 |
|
|
220 |
|
static void floorTest(NavigableMap s) { |
228 |
|
++sum; |
229 |
|
} |
230 |
|
timer.finish(); |
231 |
< |
reallyAssert (sum == iters-1); |
231 |
> |
reallyAssert(sum == iters-1); |
232 |
|
} |
233 |
|
|
240 |
– |
|
234 |
|
static void ktest(NavigableMap s, int size, Integer[] key) { |
235 |
|
timer.start("ContainsKey ", size); |
236 |
|
Set ks = s.keySet(); |
239 |
|
if (ks.contains(key[i])) ++sum; |
240 |
|
} |
241 |
|
timer.finish(); |
242 |
< |
reallyAssert (sum == size); |
242 |
> |
reallyAssert(sum == size); |
243 |
|
} |
244 |
|
|
252 |
– |
|
245 |
|
static void ittest1(NavigableMap s, int size) { |
246 |
|
int sum = 0; |
247 |
|
timer.start("Iter Key ", size); |
250 |
|
++sum; |
251 |
|
} |
252 |
|
timer.finish(); |
253 |
< |
reallyAssert (sum == size); |
253 |
> |
reallyAssert(sum == size); |
254 |
|
} |
255 |
|
|
256 |
|
static void ittest2(NavigableMap s, int size) { |
261 |
|
++sum; |
262 |
|
} |
263 |
|
timer.finish(); |
264 |
< |
reallyAssert (sum == size); |
264 |
> |
reallyAssert(sum == size); |
265 |
|
} |
266 |
|
static void ittest3(NavigableMap s, int size) { |
267 |
|
int sum = 0; |
271 |
|
++sum; |
272 |
|
} |
273 |
|
timer.finish(); |
274 |
< |
reallyAssert (sum == size); |
274 |
> |
reallyAssert(sum == size); |
275 |
|
} |
276 |
|
|
277 |
|
static void ittest(NavigableMap s, int size) { |
288 |
|
++sum; |
289 |
|
} |
290 |
|
timer.finish(); |
291 |
< |
reallyAssert (sum == size); |
291 |
> |
reallyAssert(sum == size); |
292 |
|
} |
293 |
|
|
302 |
– |
|
294 |
|
static void rittest(NavigableMap s, int size) { |
295 |
|
rittest1(s, size); |
296 |
|
// rittest2(s, size); |
297 |
|
} |
298 |
|
|
308 |
– |
|
299 |
|
static void rtest(NavigableMap s, int size) { |
300 |
|
timer.start("Remove (iterator) ", size); |
301 |
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
314 |
|
timer.finish(); |
315 |
|
} |
316 |
|
|
327 |
– |
|
317 |
|
static void dtest(NavigableMap s, int size, Integer[] key) { |
318 |
|
timer.start("Put (putAll) ", size * 2); |
319 |
|
NavigableMap s2 = null; |
326 |
|
|
327 |
|
timer.start("Iter Equals ", size * 2); |
328 |
|
boolean eqt = s2.equals(s) && s.equals(s2); |
329 |
< |
reallyAssert (eqt); |
329 |
> |
reallyAssert(eqt); |
330 |
|
timer.finish(); |
331 |
|
|
332 |
|
timer.start("Iter HashCode ", size * 2); |
333 |
|
int shc = s.hashCode(); |
334 |
|
int s2hc = s2.hashCode(); |
335 |
< |
reallyAssert (shc == s2hc); |
335 |
> |
reallyAssert(shc == s2hc); |
336 |
|
timer.finish(); |
337 |
|
|
338 |
|
timer.start("Put (present) ", size); |
347 |
|
if (es2.contains(entry)) ++sum; |
348 |
|
} |
349 |
|
timer.finish(); |
350 |
< |
reallyAssert (sum == size); |
350 |
> |
reallyAssert(sum == size); |
351 |
|
|
352 |
|
t6("Get ", size, s2, key, absent); |
353 |
|
|
355 |
|
s2.put(key[size-1], absent[0]); |
356 |
|
timer.start("Iter Equals ", size * 2); |
357 |
|
eqt = s2.equals(s) && s.equals(s2); |
358 |
< |
reallyAssert (!eqt); |
358 |
> |
reallyAssert(!eqt); |
359 |
|
timer.finish(); |
360 |
|
|
361 |
|
timer.start("Iter HashCode ", size * 2); |
362 |
|
int s1h = s.hashCode(); |
363 |
|
int s2h = s2.hashCode(); |
364 |
< |
reallyAssert (s1h != s2h); |
364 |
> |
reallyAssert(s1h != s2h); |
365 |
|
timer.finish(); |
366 |
|
|
367 |
|
s2.put(key[size-1], hold); |
372 |
|
es.remove(s2i.next()); |
373 |
|
timer.finish(); |
374 |
|
|
375 |
< |
reallyAssert (s.isEmpty()); |
375 |
> |
reallyAssert(s.isEmpty()); |
376 |
|
|
377 |
|
timer.start("Clear ", size); |
378 |
|
s2.clear(); |
379 |
|
timer.finish(); |
380 |
< |
reallyAssert (s2.isEmpty() && s.isEmpty()); |
380 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
381 |
|
} |
382 |
|
|
394 |
– |
|
395 |
– |
|
383 |
|
static void test(NavigableMap s, Integer[] key) { |
384 |
|
int size = key.length; |
385 |
|
|
421 |
|
|
422 |
|
static void printStats() { |
423 |
|
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
424 |
< |
Map.Entry e = (Map.Entry)(it.next()); |
425 |
< |
Stats stats = ((Stats)(e.getValue())); |
424 |
> |
Map.Entry e = (Map.Entry) it.next(); |
425 |
> |
Stats stats = (Stats) e.getValue(); |
426 |
|
int n = stats.number; |
427 |
|
double t; |
428 |
|
if (n > 0) |
441 |
|
startTime = System.currentTimeMillis(); |
442 |
|
} |
443 |
|
|
457 |
– |
|
444 |
|
String classify() { |
445 |
|
if (name.startsWith("Get")) |
446 |
|
return "Get "; |
457 |
|
void finish() { |
458 |
|
long endTime = System.currentTimeMillis(); |
459 |
|
long time = endTime - startTime; |
460 |
< |
double timePerOp = ((double)time)/numOps; |
460 |
> |
double timePerOp = (double) time /numOps; |
461 |
|
|
462 |
|
Object st = accum.get(name); |
463 |
|
if (st == null) |
480 |
|
if (timePerOp < stats.least) stats.least = timePerOp; |
481 |
|
} |
482 |
|
} |
497 |
– |
|
483 |
|
} |
499 |
– |
|
484 |
|
} |
485 |
|
|
486 |
|
static class Stats { |
494 |
|
|
495 |
|
static void shuffle(Integer[] keys) { |
496 |
|
int size = keys.length; |
497 |
< |
for (int i=size; i>1; i--) { |
497 |
> |
for (int i = size; i > 1; i--) { |
498 |
|
int r = rng.nextInt(i); |
499 |
|
Integer t = keys[i-1]; |
500 |
|
keys[i-1] = keys[r]; |