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 |
|
} |
65 |
|
|
66 |
|
} |
67 |
|
|
68 |
< |
static NavigableSet newSet(Class cl) { |
68 |
> |
static NavigableSet newSet(Class<?> cl) { |
69 |
|
try { |
70 |
|
NavigableSet m = (NavigableSet) cl.newInstance(); |
71 |
|
return m; |
74 |
|
} |
75 |
|
} |
76 |
|
|
80 |
– |
|
77 |
|
static void runTest(NavigableSet s, Integer[] key) { |
78 |
|
shuffle(key); |
79 |
|
int size = key.length; |
92 |
|
} |
93 |
|
} |
94 |
|
timer.finish(); |
95 |
< |
reallyAssert (sum == expect * iters); |
95 |
> |
reallyAssert(sum == expect * iters); |
96 |
|
} |
97 |
|
|
98 |
|
static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
102 |
|
if (s.remove(key[i])) ++sum; |
103 |
|
} |
104 |
|
timer.finish(); |
105 |
< |
reallyAssert (sum == expect); |
105 |
> |
reallyAssert(sum == expect); |
106 |
|
} |
107 |
|
|
108 |
|
static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
112 |
|
if (s.add(key[i])) ++sum; |
113 |
|
} |
114 |
|
timer.finish(); |
115 |
< |
reallyAssert (sum == expect); |
115 |
> |
reallyAssert(sum == expect); |
116 |
|
} |
117 |
|
|
118 |
|
static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
122 |
|
if (s.contains(key[i])) ++sum; |
123 |
|
} |
124 |
|
timer.finish(); |
125 |
< |
reallyAssert (sum == expect); |
125 |
> |
reallyAssert(sum == expect); |
126 |
|
} |
127 |
|
|
128 |
|
static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
132 |
|
if (s.remove(key[i])) ++sum; |
133 |
|
} |
134 |
|
timer.finish(); |
135 |
< |
reallyAssert (sum == expect); |
135 |
> |
reallyAssert(sum == expect); |
136 |
|
} |
137 |
|
|
138 |
|
static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
143 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
144 |
|
} |
145 |
|
timer.finish(); |
146 |
< |
reallyAssert (sum == n); |
146 |
> |
reallyAssert(sum == n); |
147 |
|
} |
148 |
|
|
149 |
|
static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
154 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
155 |
|
} |
156 |
|
timer.finish(); |
157 |
< |
reallyAssert (sum == n); |
157 |
> |
reallyAssert(sum == n); |
158 |
|
} |
159 |
|
|
160 |
|
static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
164 |
|
if (s.contains(key[i])) ++sum; |
165 |
|
} |
166 |
|
timer.finish(); |
167 |
< |
reallyAssert (sum == expect); |
167 |
> |
reallyAssert(sum == expect); |
168 |
|
} |
169 |
|
|
174 |
– |
|
170 |
|
static void higherTest(NavigableSet s) { |
171 |
|
int sum = 0; |
172 |
|
int iters = s.size(); |
177 |
|
e = s.higher(e); |
178 |
|
} |
179 |
|
timer.finish(); |
180 |
< |
reallyAssert (sum == iters); |
180 |
> |
reallyAssert(sum == iters); |
181 |
|
} |
182 |
|
|
183 |
|
static void lowerTest(NavigableSet s) { |
190 |
|
e = s.higher(e); |
191 |
|
} |
192 |
|
timer.finish(); |
193 |
< |
reallyAssert (sum == iters); |
193 |
> |
reallyAssert(sum == iters); |
194 |
|
} |
195 |
|
|
196 |
|
static void ceilingTest(NavigableSet s) { |
204 |
|
++sum; |
205 |
|
} |
206 |
|
timer.finish(); |
207 |
< |
reallyAssert (sum == iters); |
207 |
> |
reallyAssert(sum == iters); |
208 |
|
} |
209 |
|
|
210 |
|
static void floorTest(NavigableSet s) { |
218 |
|
++sum; |
219 |
|
} |
220 |
|
timer.finish(); |
221 |
< |
reallyAssert (sum == iters-1); |
221 |
> |
reallyAssert(sum == iters-1); |
222 |
|
} |
223 |
|
|
229 |
– |
|
224 |
|
static void ktest(NavigableSet s, int size, Integer[] key) { |
225 |
|
timer.start("Contains ", size); |
226 |
|
int sum = 0; |
228 |
|
if (s.contains(key[i])) ++sum; |
229 |
|
} |
230 |
|
timer.finish(); |
231 |
< |
reallyAssert (sum == size); |
231 |
> |
reallyAssert(sum == size); |
232 |
|
} |
233 |
|
|
240 |
– |
|
234 |
|
static void ittest1(NavigableSet s, int size) { |
235 |
|
int sum = 0; |
236 |
|
timer.start("Iter Key ", size); |
239 |
|
++sum; |
240 |
|
} |
241 |
|
timer.finish(); |
242 |
< |
reallyAssert (sum == size); |
242 |
> |
reallyAssert(sum == size); |
243 |
|
} |
244 |
|
|
245 |
|
static void ittest(NavigableSet s, int size) { |
254 |
|
++sum; |
255 |
|
} |
256 |
|
timer.finish(); |
257 |
< |
reallyAssert (sum == size); |
257 |
> |
reallyAssert(sum == size); |
258 |
|
} |
259 |
|
|
260 |
|
static void rittest(NavigableSet s, int size) { |
261 |
|
rittest1(s, size); |
262 |
|
} |
263 |
|
|
271 |
– |
|
264 |
|
static void rtest(NavigableSet s, int size) { |
265 |
|
timer.start("Remove (iterator) ", size); |
266 |
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
270 |
|
timer.finish(); |
271 |
|
} |
272 |
|
|
281 |
– |
|
282 |
– |
|
273 |
|
static void dtest(NavigableSet s, int size, Integer[] key) { |
274 |
|
timer.start("Add (addAll) ", size * 2); |
275 |
|
NavigableSet s2 = null; |
282 |
|
|
283 |
|
timer.start("Iter Equals ", size * 2); |
284 |
|
boolean eqt = s2.equals(s) && s.equals(s2); |
285 |
< |
reallyAssert (eqt); |
285 |
> |
reallyAssert(eqt); |
286 |
|
timer.finish(); |
287 |
|
|
288 |
|
timer.start("Iter HashCode ", size * 2); |
289 |
|
int shc = s.hashCode(); |
290 |
|
int s2hc = s2.hashCode(); |
291 |
< |
reallyAssert (shc == s2hc); |
291 |
> |
reallyAssert(shc == s2hc); |
292 |
|
timer.finish(); |
293 |
|
|
294 |
|
timer.start("Add (present) ", size); |
302 |
|
timer.start("Iter Equals ", size * 2); |
303 |
|
eqt = s2.equals(s) && s.equals(s2); |
304 |
|
if (as2) |
305 |
< |
reallyAssert (!eqt); |
305 |
> |
reallyAssert(!eqt); |
306 |
|
timer.finish(); |
307 |
|
|
308 |
|
timer.start("Iter HashCode ", size * 2); |
309 |
|
int s1h = s.hashCode(); |
310 |
|
int s2h = s2.hashCode(); |
311 |
|
if (as2) |
312 |
< |
reallyAssert (s1h != s2h); |
312 |
> |
reallyAssert(s1h != s2h); |
313 |
|
timer.finish(); |
314 |
|
|
315 |
|
timer.start("Clear ", size); |
316 |
|
s.clear(); |
317 |
|
s2.clear(); |
318 |
|
timer.finish(); |
319 |
< |
reallyAssert (s2.isEmpty() && s.isEmpty()); |
319 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
320 |
|
} |
321 |
|
|
332 |
– |
|
333 |
– |
|
322 |
|
static void test(NavigableSet s, Integer[] key) { |
323 |
|
int size = key.length; |
324 |
|
|
379 |
|
startTime = System.currentTimeMillis(); |
380 |
|
} |
381 |
|
|
394 |
– |
|
382 |
|
String classify() { |
383 |
|
if (name.startsWith("Contains")) |
384 |
|
return "Contains "; |
434 |
|
|
435 |
|
static void shuffle(Integer[] keys) { |
436 |
|
int size = keys.length; |
437 |
< |
for (int i=size; i>1; i--) { |
437 |
> |
for (int i = size; i > 1; i--) { |
438 |
|
int r = rng.nextInt(i); |
439 |
|
Integer t = keys[i-1]; |
440 |
|
keys[i-1] = keys[r]; |