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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
/** |
7 |
* @test |
8 |
* @synopsis Times and checks basic set operations |
9 |
*/ |
10 |
import java.io.*; |
11 |
import java.util.*; |
12 |
|
13 |
public class NavigableSetCheck { |
14 |
|
15 |
static int absentSize; |
16 |
static int absentMask; |
17 |
static Integer[] absent; |
18 |
|
19 |
static final Integer MISSING = new Integer(Integer.MIN_VALUE); |
20 |
|
21 |
static TestTimer timer = new TestTimer(); |
22 |
|
23 |
static void reallyAssert(boolean b) { |
24 |
if (!b) throw new Error("Failed Assertion"); |
25 |
} |
26 |
|
27 |
public static void main(String[] args) throws Exception { |
28 |
Class<?> setClass = null; |
29 |
int numTests = 50; |
30 |
int size = 50000; |
31 |
|
32 |
if (args.length > 0) { |
33 |
try { |
34 |
setClass = Class.forName(args[0]); |
35 |
} catch (ClassNotFoundException e) { |
36 |
throw new RuntimeException("Class " + args[0] + " not found."); |
37 |
} |
38 |
} |
39 |
|
40 |
if (args.length > 1) |
41 |
numTests = Integer.parseInt(args[1]); |
42 |
|
43 |
if (args.length > 2) |
44 |
size = Integer.parseInt(args[2]); |
45 |
|
46 |
System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size); |
47 |
|
48 |
absentSize = 8; |
49 |
while (absentSize < size) absentSize <<= 1; |
50 |
absentMask = absentSize - 1; |
51 |
absent = new Integer[absentSize]; |
52 |
|
53 |
for (int i = 0; i < absentSize; ++i) |
54 |
absent[i] = new Integer(i * 2); |
55 |
|
56 |
Integer[] key = new Integer[size]; |
57 |
for (int i = 0; i < size; ++i) |
58 |
key[i] = new Integer(i * 2 + 1); |
59 |
|
60 |
for (int rep = 0; rep < numTests; ++rep) { |
61 |
runTest(newSet(setClass), key); |
62 |
} |
63 |
|
64 |
TestTimer.printStats(); |
65 |
} |
66 |
|
67 |
static NavigableSet newSet(Class<?> cl) { |
68 |
try { |
69 |
return (NavigableSet) cl.getConstructor().newInstance(); |
70 |
} catch (Exception e) { |
71 |
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
} |
73 |
} |
74 |
|
75 |
static void runTest(NavigableSet s, Integer[] key) { |
76 |
shuffle(key); |
77 |
int size = key.length; |
78 |
long startTime = System.currentTimeMillis(); |
79 |
test(s, key); |
80 |
long time = System.currentTimeMillis() - startTime; |
81 |
} |
82 |
|
83 |
static void t1(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
84 |
int sum = 0; |
85 |
int iters = 4; |
86 |
timer.start(nm, n * iters); |
87 |
for (int j = 0; j < iters; ++j) { |
88 |
for (int i = 0; i < n; i++) { |
89 |
if (s.contains(key[i])) ++sum; |
90 |
} |
91 |
} |
92 |
timer.finish(); |
93 |
reallyAssert(sum == expect * iters); |
94 |
} |
95 |
|
96 |
static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
97 |
int sum = 0; |
98 |
timer.start(nm, n); |
99 |
for (int i = 0; i < n; i++) { |
100 |
if (s.remove(key[i])) ++sum; |
101 |
} |
102 |
timer.finish(); |
103 |
reallyAssert(sum == expect); |
104 |
} |
105 |
|
106 |
static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
107 |
int sum = 0; |
108 |
timer.start(nm, n); |
109 |
for (int i = 0; i < n; i++) { |
110 |
if (s.add(key[i])) ++sum; |
111 |
} |
112 |
timer.finish(); |
113 |
reallyAssert(sum == expect); |
114 |
} |
115 |
|
116 |
static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
117 |
int sum = 0; |
118 |
timer.start(nm, n); |
119 |
for (int i = 0; i < n; i++) { |
120 |
if (s.contains(key[i])) ++sum; |
121 |
} |
122 |
timer.finish(); |
123 |
reallyAssert(sum == expect); |
124 |
} |
125 |
|
126 |
static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
127 |
int sum = 0; |
128 |
timer.start(nm, n/2); |
129 |
for (int i = n-2; i >= 0; i-=2) { |
130 |
if (s.remove(key[i])) ++sum; |
131 |
} |
132 |
timer.finish(); |
133 |
reallyAssert(sum == expect); |
134 |
} |
135 |
|
136 |
static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
137 |
int sum = 0; |
138 |
timer.start(nm, n * 2); |
139 |
for (int i = 0; i < n; i++) { |
140 |
if (s.contains(k1[i])) ++sum; |
141 |
if (s.contains(k2[i & absentMask])) ++sum; |
142 |
} |
143 |
timer.finish(); |
144 |
reallyAssert(sum == n); |
145 |
} |
146 |
|
147 |
static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
148 |
int sum = 0; |
149 |
timer.start(nm, n * 2); |
150 |
for (int i = 0; i < n; i++) { |
151 |
if (s.contains(k1[i])) ++sum; |
152 |
if (s.contains(k2[i & absentMask])) ++sum; |
153 |
} |
154 |
timer.finish(); |
155 |
reallyAssert(sum == n); |
156 |
} |
157 |
|
158 |
static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
159 |
int sum = 0; |
160 |
timer.start(nm, n); |
161 |
for (int i = 0; i < n; i++) { |
162 |
if (s.contains(key[i])) ++sum; |
163 |
} |
164 |
timer.finish(); |
165 |
reallyAssert(sum == expect); |
166 |
} |
167 |
|
168 |
static void higherTest(NavigableSet s) { |
169 |
int sum = 0; |
170 |
int iters = s.size(); |
171 |
timer.start("Higher ", iters); |
172 |
Object e = s.first(); |
173 |
while (e != null) { |
174 |
++sum; |
175 |
e = s.higher(e); |
176 |
} |
177 |
timer.finish(); |
178 |
reallyAssert(sum == iters); |
179 |
} |
180 |
|
181 |
static void lowerTest(NavigableSet s) { |
182 |
int sum = 0; |
183 |
int iters = s.size(); |
184 |
timer.start("Lower ", iters); |
185 |
Object e = s.first(); |
186 |
while (e != null) { |
187 |
++sum; |
188 |
e = s.higher(e); |
189 |
} |
190 |
timer.finish(); |
191 |
reallyAssert(sum == iters); |
192 |
} |
193 |
|
194 |
static void ceilingTest(NavigableSet s) { |
195 |
int sum = 0; |
196 |
int iters = s.size(); |
197 |
if (iters > absentSize) iters = absentSize; |
198 |
timer.start("Ceiling ", iters); |
199 |
for (int i = 0; i < iters; ++i) { |
200 |
Object e = s.ceiling(absent[i]); |
201 |
if (e != null) |
202 |
++sum; |
203 |
} |
204 |
timer.finish(); |
205 |
reallyAssert(sum == iters); |
206 |
} |
207 |
|
208 |
static void floorTest(NavigableSet s) { |
209 |
int sum = 0; |
210 |
int iters = s.size(); |
211 |
if (iters > absentSize) iters = absentSize; |
212 |
timer.start("Floor ", iters); |
213 |
for (int i = 1; i < iters; ++i) { |
214 |
Object e = s.floor(absent[i]); |
215 |
if (e != null) |
216 |
++sum; |
217 |
} |
218 |
timer.finish(); |
219 |
reallyAssert(sum == iters-1); |
220 |
} |
221 |
|
222 |
static void ktest(NavigableSet s, int size, Integer[] key) { |
223 |
timer.start("Contains ", size); |
224 |
int sum = 0; |
225 |
for (int i = 0; i < size; i++) { |
226 |
if (s.contains(key[i])) ++sum; |
227 |
} |
228 |
timer.finish(); |
229 |
reallyAssert(sum == size); |
230 |
} |
231 |
|
232 |
static void ittest1(NavigableSet s, int size) { |
233 |
int sum = 0; |
234 |
timer.start("Iter Key ", size); |
235 |
for (Iterator it = s.iterator(); it.hasNext(); ) { |
236 |
if (it.next() != MISSING) |
237 |
++sum; |
238 |
} |
239 |
timer.finish(); |
240 |
reallyAssert(sum == size); |
241 |
} |
242 |
|
243 |
static void ittest(NavigableSet s, int size) { |
244 |
ittest1(s, size); |
245 |
} |
246 |
|
247 |
static void rittest1(NavigableSet s, int size) { |
248 |
int sum = 0; |
249 |
timer.start("Desc Iter Key ", size); |
250 |
for (Iterator it = s.descendingIterator(); it.hasNext(); ) { |
251 |
if (it.next() != MISSING) |
252 |
++sum; |
253 |
} |
254 |
timer.finish(); |
255 |
reallyAssert(sum == size); |
256 |
} |
257 |
|
258 |
static void rittest(NavigableSet s, int size) { |
259 |
rittest1(s, size); |
260 |
} |
261 |
|
262 |
static void rtest(NavigableSet s, int size) { |
263 |
timer.start("Remove (iterator) ", size); |
264 |
for (Iterator it = s.iterator(); it.hasNext(); ) { |
265 |
it.next(); |
266 |
it.remove(); |
267 |
} |
268 |
timer.finish(); |
269 |
} |
270 |
|
271 |
static void dtest(NavigableSet s, int size, Integer[] key) { |
272 |
timer.start("Add (addAll) ", size * 2); |
273 |
final NavigableSet s2; |
274 |
try { |
275 |
s2 = (NavigableSet) s.getClass().getConstructor().newInstance(); |
276 |
s2.addAll(s); |
277 |
} |
278 |
catch (Exception e) { e.printStackTrace(); return; } |
279 |
timer.finish(); |
280 |
|
281 |
timer.start("Iter Equals ", size * 2); |
282 |
boolean eqt = s2.equals(s) && s.equals(s2); |
283 |
reallyAssert(eqt); |
284 |
timer.finish(); |
285 |
|
286 |
timer.start("Iter HashCode ", size * 2); |
287 |
int shc = s.hashCode(); |
288 |
int s2hc = s2.hashCode(); |
289 |
reallyAssert(shc == s2hc); |
290 |
timer.finish(); |
291 |
|
292 |
timer.start("Add (present) ", size); |
293 |
s2.addAll(s); |
294 |
timer.finish(); |
295 |
|
296 |
t6("Contains ", size, s2, key, absent); |
297 |
|
298 |
boolean as2 = s2.add(absent[absentSize-1]); |
299 |
reallyAssert(as2); |
300 |
timer.start("Iter Equals ", size * 2); |
301 |
eqt = s2.equals(s) && s.equals(s2); |
302 |
if (as2) |
303 |
reallyAssert(!eqt); |
304 |
timer.finish(); |
305 |
|
306 |
timer.start("Iter HashCode ", size * 2); |
307 |
int s1h = s.hashCode(); |
308 |
int s2h = s2.hashCode(); |
309 |
if (as2) |
310 |
reallyAssert(s1h != s2h); |
311 |
timer.finish(); |
312 |
|
313 |
timer.start("Clear ", size); |
314 |
s.clear(); |
315 |
s2.clear(); |
316 |
timer.finish(); |
317 |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
318 |
} |
319 |
|
320 |
static void test(NavigableSet s, Integer[] key) { |
321 |
int size = key.length; |
322 |
|
323 |
t3("Add (absent) ", size, s, key, size); |
324 |
t3("Add (present) ", size, s, key, 0); |
325 |
t7("ContainsKey ", size, s, key, absent); |
326 |
t4("ContainsKey ", size, s, key, size); |
327 |
ktest(s, size, key); |
328 |
t4("Contains ", absentSize, s, absent, 0); |
329 |
t6("Contains ", size, s, key, absent); |
330 |
t1("Contains (present) ", size, s, key, size); |
331 |
t1("Contains (absent) ", absentSize, s, absent, 0); |
332 |
t2("Remove (absent) ", absentSize, s, absent, 0); |
333 |
t5("Remove (present) ", size, s, key, size / 2); |
334 |
t3("Add (half present) ", size, s, key, size / 2); |
335 |
|
336 |
ittest(s, size); |
337 |
rittest(s, size); |
338 |
higherTest(s); |
339 |
ceilingTest(s); |
340 |
floorTest(s); |
341 |
lowerTest(s); |
342 |
rtest(s, size); |
343 |
|
344 |
t4("Contains ", size, s, key, 0); |
345 |
t2("Remove (absent) ", size, s, key, 0); |
346 |
t3("Add (presized) ", size, s, key, size); |
347 |
dtest(s, size, key); |
348 |
} |
349 |
|
350 |
static class TestTimer { |
351 |
private String name; |
352 |
private long numOps; |
353 |
private long startTime; |
354 |
private String cname; |
355 |
|
356 |
static final java.util.TreeMap accum = new java.util.TreeMap(); |
357 |
|
358 |
static void printStats() { |
359 |
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
360 |
Map.Entry e = (Map.Entry) it.next(); |
361 |
Stats stats = (Stats) e.getValue(); |
362 |
int n = stats.number; |
363 |
double t; |
364 |
if (n > 0) |
365 |
t = stats.sum / n; |
366 |
else |
367 |
t = stats.least; |
368 |
long nano = Math.round(1000000.0 * t); |
369 |
System.out.println(e.getKey() + ": " + nano); |
370 |
} |
371 |
} |
372 |
|
373 |
void start(String name, long numOps) { |
374 |
this.name = name; |
375 |
this.cname = classify(); |
376 |
this.numOps = numOps; |
377 |
startTime = System.currentTimeMillis(); |
378 |
} |
379 |
|
380 |
String classify() { |
381 |
if (name.startsWith("Contains")) |
382 |
return "Contains "; |
383 |
else if (name.startsWith("Add")) |
384 |
return "Add "; |
385 |
else if (name.startsWith("Remove")) |
386 |
return "Remove "; |
387 |
else if (name.startsWith("Iter")) |
388 |
return "Iter "; |
389 |
else |
390 |
return null; |
391 |
} |
392 |
|
393 |
void finish() { |
394 |
long endTime = System.currentTimeMillis(); |
395 |
long time = endTime - startTime; |
396 |
double timePerOp = (double) time / numOps; |
397 |
|
398 |
Object st = accum.get(name); |
399 |
if (st == null) |
400 |
accum.put(name, new Stats(timePerOp)); |
401 |
else { |
402 |
Stats stats = (Stats) st; |
403 |
stats.sum += timePerOp; |
404 |
stats.number++; |
405 |
if (timePerOp < stats.least) stats.least = timePerOp; |
406 |
} |
407 |
|
408 |
if (cname != null) { |
409 |
st = accum.get(cname); |
410 |
if (st == null) |
411 |
accum.put(cname, new Stats(timePerOp)); |
412 |
else { |
413 |
Stats stats = (Stats) st; |
414 |
stats.sum += timePerOp; |
415 |
stats.number++; |
416 |
if (timePerOp < stats.least) stats.least = timePerOp; |
417 |
} |
418 |
} |
419 |
|
420 |
} |
421 |
|
422 |
} |
423 |
|
424 |
static class Stats { |
425 |
double sum = 0; |
426 |
double least; |
427 |
int number = 0; |
428 |
Stats(double t) { least = t; } |
429 |
} |
430 |
|
431 |
static Random rng = new Random(); |
432 |
|
433 |
static void shuffle(Integer[] keys) { |
434 |
int size = keys.length; |
435 |
for (int i = size; i > 1; i--) { |
436 |
int r = rng.nextInt(i); |
437 |
Integer t = keys[i-1]; |
438 |
keys[i-1] = keys[r]; |
439 |
keys[r] = t; |
440 |
} |
441 |
} |
442 |
|
443 |
} |