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; |
166 |
|
reallyAssert(sum == expect); |
167 |
|
} |
168 |
|
|
174 |
– |
|
169 |
|
static void t9(NavigableMap s) { |
170 |
|
int sum = 0; |
171 |
|
int iters = 20; |
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(); |
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); |
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; |
380 |
|
reallyAssert(s2.isEmpty() && s.isEmpty()); |
381 |
|
} |
382 |
|
|
394 |
– |
|
395 |
– |
|
383 |
|
static void test(NavigableMap s, Integer[] key) { |
384 |
|
int size = key.length; |
385 |
|
|
441 |
|
startTime = System.currentTimeMillis(); |
442 |
|
} |
443 |
|
|
457 |
– |
|
444 |
|
String classify() { |
445 |
|
if (name.startsWith("Get")) |
446 |
|
return "Get "; |
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]; |