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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
/** |
7 |
|
* @test |
8 |
|
* @synopsis Times and checks basic set operations |
9 |
|
*/ |
10 |
– |
import java.util.*; |
10 |
|
import java.io.*; |
11 |
+ |
import java.util.*; |
12 |
|
|
13 |
|
public class NavigableSetCheck { |
14 |
|
|
25 |
|
} |
26 |
|
|
27 |
|
public static void main(String[] args) throws Exception { |
28 |
< |
Class setClass = null; |
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) { |
35 |
> |
} catch (ClassNotFoundException e) { |
36 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
37 |
|
} |
38 |
|
} |
39 |
|
|
40 |
– |
|
40 |
|
if (args.length > 1) |
41 |
|
numTests = Integer.parseInt(args[1]); |
42 |
|
|
45 |
|
|
46 |
|
System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size); |
47 |
|
|
49 |
– |
|
48 |
|
absentSize = 8; |
49 |
|
while (absentSize < size) absentSize <<= 1; |
50 |
|
absentMask = absentSize - 1; |
57 |
|
for (int i = 0; i < size; ++i) |
58 |
|
key[i] = new Integer(i * 2 + 1); |
59 |
|
|
62 |
– |
|
60 |
|
for (int rep = 0; rep < numTests; ++rep) { |
61 |
|
runTest(newSet(setClass), key); |
62 |
|
} |
63 |
|
|
64 |
|
TestTimer.printStats(); |
68 |
– |
|
65 |
|
} |
66 |
|
|
67 |
< |
static NavigableSet newSet(Class cl) { |
67 |
> |
static NavigableSet newSet(Class<?> cl) { |
68 |
|
try { |
69 |
< |
NavigableSet m = (NavigableSet)cl.newInstance(); |
70 |
< |
return m; |
75 |
< |
} catch(Exception e) { |
69 |
> |
return (NavigableSet) cl.getConstructor().newInstance(); |
70 |
> |
} catch (Exception e) { |
71 |
|
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
|
} |
73 |
|
} |
74 |
|
|
80 |
– |
|
75 |
|
static void runTest(NavigableSet s, Integer[] key) { |
76 |
|
shuffle(key); |
77 |
|
int size = key.length; |
90 |
|
} |
91 |
|
} |
92 |
|
timer.finish(); |
93 |
< |
reallyAssert (sum == expect * iters); |
93 |
> |
reallyAssert(sum == expect * iters); |
94 |
|
} |
95 |
|
|
96 |
|
static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
100 |
|
if (s.remove(key[i])) ++sum; |
101 |
|
} |
102 |
|
timer.finish(); |
103 |
< |
reallyAssert (sum == expect); |
103 |
> |
reallyAssert(sum == expect); |
104 |
|
} |
105 |
|
|
106 |
|
static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
110 |
|
if (s.add(key[i])) ++sum; |
111 |
|
} |
112 |
|
timer.finish(); |
113 |
< |
reallyAssert (sum == expect); |
113 |
> |
reallyAssert(sum == expect); |
114 |
|
} |
115 |
|
|
116 |
|
static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
120 |
|
if (s.contains(key[i])) ++sum; |
121 |
|
} |
122 |
|
timer.finish(); |
123 |
< |
reallyAssert (sum == expect); |
123 |
> |
reallyAssert(sum == expect); |
124 |
|
} |
125 |
|
|
126 |
|
static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
130 |
|
if (s.remove(key[i])) ++sum; |
131 |
|
} |
132 |
|
timer.finish(); |
133 |
< |
reallyAssert (sum == expect); |
133 |
> |
reallyAssert(sum == expect); |
134 |
|
} |
135 |
|
|
136 |
|
static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
141 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
142 |
|
} |
143 |
|
timer.finish(); |
144 |
< |
reallyAssert (sum == n); |
144 |
> |
reallyAssert(sum == n); |
145 |
|
} |
146 |
|
|
147 |
|
static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
152 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
153 |
|
} |
154 |
|
timer.finish(); |
155 |
< |
reallyAssert (sum == n); |
155 |
> |
reallyAssert(sum == n); |
156 |
|
} |
157 |
|
|
158 |
|
static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
162 |
|
if (s.contains(key[i])) ++sum; |
163 |
|
} |
164 |
|
timer.finish(); |
165 |
< |
reallyAssert (sum == expect); |
165 |
> |
reallyAssert(sum == expect); |
166 |
|
} |
167 |
|
|
174 |
– |
|
168 |
|
static void higherTest(NavigableSet s) { |
169 |
|
int sum = 0; |
170 |
|
int iters = s.size(); |
175 |
|
e = s.higher(e); |
176 |
|
} |
177 |
|
timer.finish(); |
178 |
< |
reallyAssert (sum == iters); |
178 |
> |
reallyAssert(sum == iters); |
179 |
|
} |
180 |
|
|
181 |
|
static void lowerTest(NavigableSet s) { |
188 |
|
e = s.higher(e); |
189 |
|
} |
190 |
|
timer.finish(); |
191 |
< |
reallyAssert (sum == iters); |
191 |
> |
reallyAssert(sum == iters); |
192 |
|
} |
193 |
|
|
194 |
|
static void ceilingTest(NavigableSet s) { |
202 |
|
++sum; |
203 |
|
} |
204 |
|
timer.finish(); |
205 |
< |
reallyAssert (sum == iters); |
205 |
> |
reallyAssert(sum == iters); |
206 |
|
} |
207 |
|
|
208 |
|
static void floorTest(NavigableSet s) { |
216 |
|
++sum; |
217 |
|
} |
218 |
|
timer.finish(); |
219 |
< |
reallyAssert (sum == iters-1); |
219 |
> |
reallyAssert(sum == iters-1); |
220 |
|
} |
221 |
|
|
229 |
– |
|
222 |
|
static void ktest(NavigableSet s, int size, Integer[] key) { |
223 |
|
timer.start("Contains ", size); |
224 |
|
int sum = 0; |
226 |
|
if (s.contains(key[i])) ++sum; |
227 |
|
} |
228 |
|
timer.finish(); |
229 |
< |
reallyAssert (sum == size); |
229 |
> |
reallyAssert(sum == size); |
230 |
|
} |
231 |
|
|
240 |
– |
|
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) |
236 |
> |
if (it.next() != MISSING) |
237 |
|
++sum; |
238 |
|
} |
239 |
|
timer.finish(); |
240 |
< |
reallyAssert (sum == size); |
240 |
> |
reallyAssert(sum == size); |
241 |
|
} |
242 |
|
|
243 |
|
static void ittest(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) |
251 |
> |
if (it.next() != MISSING) |
252 |
|
++sum; |
253 |
|
} |
254 |
|
timer.finish(); |
255 |
< |
reallyAssert (sum == size); |
255 |
> |
reallyAssert(sum == size); |
256 |
|
} |
257 |
|
|
258 |
|
static void rittest(NavigableSet s, int size) { |
259 |
|
rittest1(s, size); |
260 |
|
} |
261 |
|
|
271 |
– |
|
262 |
|
static void rtest(NavigableSet s, int size) { |
263 |
|
timer.start("Remove (iterator) ", size); |
264 |
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
268 |
|
timer.finish(); |
269 |
|
} |
270 |
|
|
281 |
– |
|
282 |
– |
|
271 |
|
static void dtest(NavigableSet s, int size, Integer[] key) { |
272 |
|
timer.start("Add (addAll) ", size * 2); |
273 |
< |
NavigableSet s2 = null; |
273 |
> |
final NavigableSet s2; |
274 |
|
try { |
275 |
< |
s2 = (NavigableSet) (s.getClass().newInstance()); |
275 |
> |
s2 = (NavigableSet) s.getClass().getConstructor().newInstance(); |
276 |
|
s2.addAll(s); |
277 |
|
} |
278 |
|
catch (Exception e) { e.printStackTrace(); return; } |
280 |
|
|
281 |
|
timer.start("Iter Equals ", size * 2); |
282 |
|
boolean eqt = s2.equals(s) && s.equals(s2); |
283 |
< |
reallyAssert (eqt); |
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); |
289 |
> |
reallyAssert(shc == s2hc); |
290 |
|
timer.finish(); |
291 |
|
|
292 |
|
timer.start("Add (present) ", size); |
300 |
|
timer.start("Iter Equals ", size * 2); |
301 |
|
eqt = s2.equals(s) && s.equals(s2); |
302 |
|
if (as2) |
303 |
< |
reallyAssert (!eqt); |
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); |
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()); |
317 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
318 |
|
} |
319 |
|
|
332 |
– |
|
333 |
– |
|
320 |
|
static void test(NavigableSet s, Integer[] key) { |
321 |
|
int size = key.length; |
322 |
|
|
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())); |
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) |
377 |
|
startTime = System.currentTimeMillis(); |
378 |
|
} |
379 |
|
|
394 |
– |
|
380 |
|
String classify() { |
381 |
|
if (name.startsWith("Contains")) |
382 |
|
return "Contains "; |
393 |
|
void finish() { |
394 |
|
long endTime = System.currentTimeMillis(); |
395 |
|
long time = endTime - startTime; |
396 |
< |
double timePerOp = ((double)time)/numOps; |
396 |
> |
double timePerOp = (double) time / numOps; |
397 |
|
|
398 |
|
Object st = accum.get(name); |
399 |
|
if (st == null) |
432 |
|
|
433 |
|
static void shuffle(Integer[] keys) { |
434 |
|
int size = keys.length; |
435 |
< |
for (int i=size; i>1; i--) { |
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]; |