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 |
|
|
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; |
73 |
|
} |
74 |
|
} |
75 |
|
|
80 |
– |
|
76 |
|
static void runTest(NavigableSet s, Integer[] key) { |
77 |
|
shuffle(key); |
78 |
|
int size = key.length; |
91 |
|
} |
92 |
|
} |
93 |
|
timer.finish(); |
94 |
< |
reallyAssert (sum == expect * iters); |
94 |
> |
reallyAssert(sum == expect * iters); |
95 |
|
} |
96 |
|
|
97 |
|
static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
101 |
|
if (s.remove(key[i])) ++sum; |
102 |
|
} |
103 |
|
timer.finish(); |
104 |
< |
reallyAssert (sum == expect); |
104 |
> |
reallyAssert(sum == expect); |
105 |
|
} |
106 |
|
|
107 |
|
static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
111 |
|
if (s.add(key[i])) ++sum; |
112 |
|
} |
113 |
|
timer.finish(); |
114 |
< |
reallyAssert (sum == expect); |
114 |
> |
reallyAssert(sum == expect); |
115 |
|
} |
116 |
|
|
117 |
|
static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
121 |
|
if (s.contains(key[i])) ++sum; |
122 |
|
} |
123 |
|
timer.finish(); |
124 |
< |
reallyAssert (sum == expect); |
124 |
> |
reallyAssert(sum == expect); |
125 |
|
} |
126 |
|
|
127 |
|
static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
131 |
|
if (s.remove(key[i])) ++sum; |
132 |
|
} |
133 |
|
timer.finish(); |
134 |
< |
reallyAssert (sum == expect); |
134 |
> |
reallyAssert(sum == expect); |
135 |
|
} |
136 |
|
|
137 |
|
static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
142 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
143 |
|
} |
144 |
|
timer.finish(); |
145 |
< |
reallyAssert (sum == n); |
145 |
> |
reallyAssert(sum == n); |
146 |
|
} |
147 |
|
|
148 |
|
static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
153 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
154 |
|
} |
155 |
|
timer.finish(); |
156 |
< |
reallyAssert (sum == n); |
156 |
> |
reallyAssert(sum == n); |
157 |
|
} |
158 |
|
|
159 |
|
static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
163 |
|
if (s.contains(key[i])) ++sum; |
164 |
|
} |
165 |
|
timer.finish(); |
166 |
< |
reallyAssert (sum == expect); |
166 |
> |
reallyAssert(sum == expect); |
167 |
|
} |
168 |
|
|
174 |
– |
|
169 |
|
static void higherTest(NavigableSet s) { |
170 |
|
int sum = 0; |
171 |
|
int iters = s.size(); |
176 |
|
e = s.higher(e); |
177 |
|
} |
178 |
|
timer.finish(); |
179 |
< |
reallyAssert (sum == iters); |
179 |
> |
reallyAssert(sum == iters); |
180 |
|
} |
181 |
|
|
182 |
|
static void lowerTest(NavigableSet s) { |
189 |
|
e = s.higher(e); |
190 |
|
} |
191 |
|
timer.finish(); |
192 |
< |
reallyAssert (sum == iters); |
192 |
> |
reallyAssert(sum == iters); |
193 |
|
} |
194 |
|
|
195 |
|
static void ceilingTest(NavigableSet s) { |
203 |
|
++sum; |
204 |
|
} |
205 |
|
timer.finish(); |
206 |
< |
reallyAssert (sum == iters); |
206 |
> |
reallyAssert(sum == iters); |
207 |
|
} |
208 |
|
|
209 |
|
static void floorTest(NavigableSet s) { |
217 |
|
++sum; |
218 |
|
} |
219 |
|
timer.finish(); |
220 |
< |
reallyAssert (sum == iters-1); |
220 |
> |
reallyAssert(sum == iters-1); |
221 |
|
} |
222 |
|
|
229 |
– |
|
223 |
|
static void ktest(NavigableSet s, int size, Integer[] key) { |
224 |
|
timer.start("Contains ", size); |
225 |
|
int sum = 0; |
227 |
|
if (s.contains(key[i])) ++sum; |
228 |
|
} |
229 |
|
timer.finish(); |
230 |
< |
reallyAssert (sum == size); |
230 |
> |
reallyAssert(sum == size); |
231 |
|
} |
232 |
|
|
240 |
– |
|
233 |
|
static void ittest1(NavigableSet s, int size) { |
234 |
|
int sum = 0; |
235 |
|
timer.start("Iter Key ", size); |
238 |
|
++sum; |
239 |
|
} |
240 |
|
timer.finish(); |
241 |
< |
reallyAssert (sum == size); |
241 |
> |
reallyAssert(sum == size); |
242 |
|
} |
243 |
|
|
244 |
|
static void ittest(NavigableSet s, int size) { |
253 |
|
++sum; |
254 |
|
} |
255 |
|
timer.finish(); |
256 |
< |
reallyAssert (sum == size); |
256 |
> |
reallyAssert(sum == size); |
257 |
|
} |
258 |
|
|
259 |
|
static void rittest(NavigableSet s, int size) { |
260 |
|
rittest1(s, size); |
261 |
|
} |
262 |
|
|
271 |
– |
|
263 |
|
static void rtest(NavigableSet s, int size) { |
264 |
|
timer.start("Remove (iterator) ", size); |
265 |
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
269 |
|
timer.finish(); |
270 |
|
} |
271 |
|
|
281 |
– |
|
282 |
– |
|
272 |
|
static void dtest(NavigableSet s, int size, Integer[] key) { |
273 |
|
timer.start("Add (addAll) ", size * 2); |
274 |
|
NavigableSet s2 = null; |
281 |
|
|
282 |
|
timer.start("Iter Equals ", size * 2); |
283 |
|
boolean eqt = s2.equals(s) && s.equals(s2); |
284 |
< |
reallyAssert (eqt); |
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); |
290 |
> |
reallyAssert(shc == s2hc); |
291 |
|
timer.finish(); |
292 |
|
|
293 |
|
timer.start("Add (present) ", size); |
301 |
|
timer.start("Iter Equals ", size * 2); |
302 |
|
eqt = s2.equals(s) && s.equals(s2); |
303 |
|
if (as2) |
304 |
< |
reallyAssert (!eqt); |
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); |
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()); |
318 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
319 |
|
} |
320 |
|
|
332 |
– |
|
333 |
– |
|
321 |
|
static void test(NavigableSet s, Integer[] key) { |
322 |
|
int size = key.length; |
323 |
|
|
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())); |
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) |
378 |
|
startTime = System.currentTimeMillis(); |
379 |
|
} |
380 |
|
|
394 |
– |
|
381 |
|
String classify() { |
382 |
|
if (name.startsWith("Contains")) |
383 |
|
return "Contains "; |
394 |
|
void finish() { |
395 |
|
long endTime = System.currentTimeMillis(); |
396 |
|
long time = endTime - startTime; |
397 |
< |
double timePerOp = ((double)time)/numOps; |
397 |
> |
double timePerOp = (double) time / numOps; |
398 |
|
|
399 |
|
Object st = accum.get(name); |
400 |
|
if (st == null) |
433 |
|
|
434 |
|
static void shuffle(Integer[] keys) { |
435 |
|
int size = keys.length; |
436 |
< |
for (int i=size; i>1; i--) { |
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]; |