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