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