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