1 |
dl |
1.1 |
/** |
2 |
|
|
* @test |
3 |
|
|
* @synopsis Times and checks basic set operations |
4 |
|
|
*/ |
5 |
|
|
import java.util.*; |
6 |
|
|
import java.io.*; |
7 |
|
|
|
8 |
|
|
public class NavigableSetCheck { |
9 |
|
|
|
10 |
|
|
static int absentSize; |
11 |
|
|
static int absentMask; |
12 |
|
|
static Integer[] absent; |
13 |
|
|
|
14 |
|
|
static final Integer MISSING = new Integer(Integer.MIN_VALUE); |
15 |
|
|
|
16 |
|
|
static TestTimer timer = new TestTimer(); |
17 |
|
|
|
18 |
|
|
static void reallyAssert(boolean b) { |
19 |
|
|
if (!b) throw new Error("Failed Assertion"); |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
public static void main(String[] args) throws Exception { |
23 |
|
|
Class setClass = null; |
24 |
|
|
int numTests = 50; |
25 |
|
|
int size = 50000; |
26 |
|
|
|
27 |
|
|
if (args.length > 0) { |
28 |
|
|
try { |
29 |
|
|
setClass = Class.forName(args[0]); |
30 |
|
|
} catch(ClassNotFoundException e) { |
31 |
|
|
throw new RuntimeException("Class " + args[0] + " not found."); |
32 |
|
|
} |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
if (args.length > 1) |
37 |
|
|
numTests = Integer.parseInt(args[1]); |
38 |
|
|
|
39 |
|
|
if (args.length > 2) |
40 |
|
|
size = Integer.parseInt(args[2]); |
41 |
|
|
|
42 |
|
|
System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size); |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
absentSize = 8; |
46 |
|
|
while (absentSize < size) absentSize <<= 1; |
47 |
|
|
absentMask = absentSize - 1; |
48 |
|
|
absent = new Integer[absentSize]; |
49 |
|
|
|
50 |
|
|
for (int i = 0; i < absentSize; ++i) |
51 |
|
|
absent[i] = new Integer(i * 2); |
52 |
|
|
|
53 |
|
|
Integer[] key = new Integer[size]; |
54 |
|
|
for (int i = 0; i < size; ++i) |
55 |
|
|
key[i] = new Integer(i * 2 + 1); |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
for (int rep = 0; rep < numTests; ++rep) { |
59 |
|
|
runTest(newSet(setClass), key); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
TestTimer.printStats(); |
63 |
|
|
|
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
static NavigableSet newSet(Class cl) { |
67 |
|
|
try { |
68 |
|
|
NavigableSet m = (NavigableSet)cl.newInstance(); |
69 |
|
|
return m; |
70 |
|
|
} catch(Exception e) { |
71 |
|
|
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
|
|
} |
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 |
|
|
|
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 |
|
|
|
225 |
|
|
static void ktest(NavigableSet s, int size, Integer[] key) { |
226 |
|
|
timer.start("Contains ", size); |
227 |
|
|
int sum = 0; |
228 |
|
|
for (int i = 0; i < size; i++) { |
229 |
|
|
if (s.contains(key[i])) ++sum; |
230 |
|
|
} |
231 |
|
|
timer.finish(); |
232 |
|
|
reallyAssert (sum == size); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
|
236 |
|
|
static void ittest1(NavigableSet s, int size) { |
237 |
|
|
int sum = 0; |
238 |
|
|
timer.start("Iter Key ", size); |
239 |
|
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
240 |
|
|
if(it.next() != MISSING) |
241 |
|
|
++sum; |
242 |
|
|
} |
243 |
|
|
timer.finish(); |
244 |
|
|
reallyAssert (sum == size); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
static void ittest(NavigableSet s, int size) { |
248 |
|
|
ittest1(s, size); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
static void rittest1(NavigableSet s, int size) { |
252 |
|
|
int sum = 0; |
253 |
|
|
timer.start("Desc Iter Key ", size); |
254 |
|
|
for (Iterator it = s.descendingIterator(); it.hasNext(); ) { |
255 |
|
|
if(it.next() != MISSING) |
256 |
|
|
++sum; |
257 |
|
|
} |
258 |
|
|
timer.finish(); |
259 |
|
|
reallyAssert (sum == size); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
static void rittest(NavigableSet s, int size) { |
263 |
|
|
rittest1(s, size); |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
|
267 |
|
|
static void rtest(NavigableSet s, int size) { |
268 |
|
|
timer.start("Remove (iterator) ", size); |
269 |
|
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
270 |
|
|
it.next(); |
271 |
|
|
it.remove(); |
272 |
|
|
} |
273 |
|
|
timer.finish(); |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
static void dtest(NavigableSet s, int size, Integer[] key) { |
279 |
|
|
timer.start("Add (addAll) ", size * 2); |
280 |
|
|
NavigableSet s2 = null; |
281 |
|
|
try { |
282 |
|
|
s2 = (NavigableSet) (s.getClass().newInstance()); |
283 |
|
|
s2.addAll(s); |
284 |
|
|
} |
285 |
|
|
catch (Exception e) { e.printStackTrace(); return; } |
286 |
|
|
timer.finish(); |
287 |
|
|
|
288 |
|
|
timer.start("Iter Equals ", size * 2); |
289 |
|
|
boolean eqt = s2.equals(s) && s.equals(s2); |
290 |
|
|
reallyAssert (eqt); |
291 |
|
|
timer.finish(); |
292 |
|
|
|
293 |
|
|
timer.start("Iter HashCode ", size * 2); |
294 |
|
|
int shc = s.hashCode(); |
295 |
|
|
int s2hc = s2.hashCode(); |
296 |
|
|
reallyAssert (shc == s2hc); |
297 |
|
|
timer.finish(); |
298 |
|
|
|
299 |
|
|
timer.start("Add (present) ", size); |
300 |
|
|
s2.addAll(s); |
301 |
|
|
timer.finish(); |
302 |
|
|
|
303 |
|
|
t6("Contains ", size, s2, key, absent); |
304 |
|
|
|
305 |
|
|
boolean as2 = s2.add(absent[absentSize-1]); |
306 |
|
|
reallyAssert(as2); |
307 |
|
|
timer.start("Iter Equals ", size * 2); |
308 |
|
|
eqt = s2.equals(s) && s.equals(s2); |
309 |
|
|
if (as2) |
310 |
|
|
reallyAssert (!eqt); |
311 |
|
|
timer.finish(); |
312 |
|
|
|
313 |
|
|
timer.start("Iter HashCode ", size * 2); |
314 |
|
|
int s1h = s.hashCode(); |
315 |
|
|
int s2h = s2.hashCode(); |
316 |
|
|
if (as2) |
317 |
|
|
reallyAssert (s1h != s2h); |
318 |
|
|
timer.finish(); |
319 |
|
|
|
320 |
|
|
timer.start("Clear ", size); |
321 |
|
|
s.clear(); |
322 |
|
|
s2.clear(); |
323 |
|
|
timer.finish(); |
324 |
|
|
reallyAssert (s2.isEmpty() && s.isEmpty()); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
static void test(NavigableSet s, Integer[] key) { |
330 |
|
|
int size = key.length; |
331 |
|
|
|
332 |
|
|
t3("Add (absent) ", size, s, key, size); |
333 |
|
|
t3("Add (present) ", size, s, key, 0); |
334 |
|
|
t7("ContainsKey ", size, s, key, absent); |
335 |
|
|
t4("ContainsKey ", size, s, key, size); |
336 |
|
|
ktest(s, size, key); |
337 |
|
|
t4("Contains ", absentSize, s, absent, 0); |
338 |
|
|
t6("Contains ", size, s, key, absent); |
339 |
|
|
t1("Contains (present) ", size, s, key, size); |
340 |
|
|
t1("Contains (absent) ", absentSize, s, absent, 0); |
341 |
|
|
t2("Remove (absent) ", absentSize, s, absent, 0); |
342 |
|
|
t5("Remove (present) ", size, s, key, size / 2); |
343 |
|
|
t3("Add (half present) ", size, s, key, size / 2); |
344 |
|
|
|
345 |
|
|
ittest(s, size); |
346 |
|
|
rittest(s, size); |
347 |
|
|
higherTest(s); |
348 |
|
|
ceilingTest(s); |
349 |
|
|
floorTest(s); |
350 |
|
|
lowerTest(s); |
351 |
|
|
rtest(s, size); |
352 |
|
|
|
353 |
|
|
t4("Contains ", size, s, key, 0); |
354 |
|
|
t2("Remove (absent) ", size, s, key, 0); |
355 |
|
|
t3("Add (presized) ", size, s, key, size); |
356 |
|
|
dtest(s, size, key); |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
static class TestTimer { |
360 |
|
|
private String name; |
361 |
|
|
private long numOps; |
362 |
|
|
private long startTime; |
363 |
|
|
private String cname; |
364 |
|
|
|
365 |
|
|
static final java.util.TreeMap accum = new java.util.TreeMap(); |
366 |
|
|
|
367 |
|
|
static void printStats() { |
368 |
|
|
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
369 |
|
|
Map.Entry e = (Map.Entry)(it.next()); |
370 |
|
|
Stats stats = ((Stats)(e.getValue())); |
371 |
|
|
int n = stats.number; |
372 |
|
|
double t; |
373 |
|
|
if (n > 0) |
374 |
|
|
t = stats.sum / n; |
375 |
|
|
else |
376 |
|
|
t = stats.least; |
377 |
|
|
long nano = Math.round(1000000.0 * t); |
378 |
|
|
System.out.println(e.getKey() + ": " + nano); |
379 |
|
|
} |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
void start(String name, long numOps) { |
383 |
|
|
this.name = name; |
384 |
|
|
this.cname = classify(); |
385 |
|
|
this.numOps = numOps; |
386 |
|
|
startTime = System.currentTimeMillis(); |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
|
390 |
|
|
String classify() { |
391 |
|
|
if (name.startsWith("Contains")) |
392 |
|
|
return "Contains "; |
393 |
|
|
else if (name.startsWith("Add")) |
394 |
|
|
return "Add "; |
395 |
|
|
else if (name.startsWith("Remove")) |
396 |
|
|
return "Remove "; |
397 |
|
|
else if (name.startsWith("Iter")) |
398 |
|
|
return "Iter "; |
399 |
|
|
else |
400 |
|
|
return null; |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
void finish() { |
404 |
|
|
long endTime = System.currentTimeMillis(); |
405 |
|
|
long time = endTime - startTime; |
406 |
|
|
double timePerOp = ((double)time)/numOps; |
407 |
|
|
|
408 |
|
|
Object st = accum.get(name); |
409 |
|
|
if (st == null) |
410 |
|
|
accum.put(name, new Stats(timePerOp)); |
411 |
|
|
else { |
412 |
|
|
Stats stats = (Stats) st; |
413 |
|
|
stats.sum += timePerOp; |
414 |
|
|
stats.number++; |
415 |
|
|
if (timePerOp < stats.least) stats.least = timePerOp; |
416 |
|
|
} |
417 |
|
|
|
418 |
|
|
if (cname != null) { |
419 |
|
|
st = accum.get(cname); |
420 |
|
|
if (st == null) |
421 |
|
|
accum.put(cname, new Stats(timePerOp)); |
422 |
|
|
else { |
423 |
|
|
Stats stats = (Stats) st; |
424 |
|
|
stats.sum += timePerOp; |
425 |
|
|
stats.number++; |
426 |
|
|
if (timePerOp < stats.least) stats.least = timePerOp; |
427 |
|
|
} |
428 |
|
|
} |
429 |
|
|
|
430 |
|
|
} |
431 |
|
|
|
432 |
|
|
} |
433 |
|
|
|
434 |
|
|
static class Stats { |
435 |
|
|
double sum = 0; |
436 |
|
|
double least; |
437 |
|
|
int number = 0; |
438 |
|
|
Stats(double t) { least = t; } |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
static Random rng = new Random(); |
442 |
|
|
|
443 |
|
|
static void shuffle(Integer[] keys) { |
444 |
|
|
int size = keys.length; |
445 |
|
|
for (int i=size; i>1; i--) { |
446 |
|
|
int r = rng.nextInt(i); |
447 |
|
|
Integer t = keys[i-1]; |
448 |
|
|
keys[i-1] = keys[r]; |
449 |
|
|
keys[r] = t; |
450 |
|
|
} |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
} |
454 |
|
|
|