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