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/publicdomain/zero/1.0/ |
5 |
+ |
*/ |
6 |
|
/** |
7 |
|
* @test |
8 |
|
* @synopsis Times and checks basic map operations |
9 |
|
*/ |
5 |
– |
import java.util.*; |
10 |
|
import java.io.*; |
11 |
+ |
import java.util.*; |
12 |
|
|
13 |
|
public class IntMapCheck { |
14 |
|
static int absentSize; |
22 |
|
} |
23 |
|
|
24 |
|
public static void main(String[] args) throws Exception { |
25 |
< |
Class mapClass = java.util.concurrent.ConcurrentHashMap.class; |
25 |
> |
Class<?> mapClass = java.util.concurrent.ConcurrentHashMap.class; |
26 |
|
int numTests = 50; |
27 |
< |
int size = 50000; |
27 |
> |
int size = 75000; |
28 |
|
|
29 |
|
if (args.length > 0) { |
30 |
|
try { |
31 |
|
mapClass = Class.forName(args[0]); |
32 |
< |
} catch(ClassNotFoundException e) { |
32 |
> |
} catch (ClassNotFoundException e) { |
33 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
34 |
|
} |
35 |
|
} |
36 |
|
|
37 |
< |
|
33 |
< |
if (args.length > 1) |
37 |
> |
if (args.length > 1) |
38 |
|
numTests = Integer.parseInt(args[1]); |
39 |
|
|
40 |
< |
if (args.length > 2) |
40 |
> |
if (args.length > 2) |
41 |
|
size = Integer.parseInt(args[2]); |
42 |
|
|
43 |
|
boolean doSerializeTest = args.length > 3; |
45 |
|
System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size); |
46 |
|
|
47 |
|
absentSize = 4; |
48 |
< |
while (absentSize <= size) absentSize <<= 1; |
48 |
> |
while (absentSize < size) absentSize <<= 1; |
49 |
|
absentMask = absentSize-1; |
50 |
|
absent = new Integer[absentSize]; |
51 |
< |
for (int i = 0; i < absentSize; ++i) |
52 |
< |
absent[i] = new Integer(2 * (i - 1) + 1); |
51 |
> |
for (int i = 0; i < absentSize/2; ++i) |
52 |
> |
absent[i] = Integer.valueOf(-i - 1); |
53 |
> |
for (int i = absentSize/2; i < absentSize; ++i) |
54 |
> |
absent[i] = Integer.valueOf(size + i + 1); |
55 |
|
|
56 |
|
Integer[] key = new Integer[size]; |
57 |
< |
for (int i = 0; i < size; ++i) |
58 |
< |
key[i] = new Integer(2 * i); |
57 |
> |
for (int i = 0; i < size; ++i) |
58 |
> |
key[i] = Integer.valueOf(i); |
59 |
|
|
60 |
|
for (int rep = 0; rep < numTests; ++rep) { |
55 |
– |
shuffle(absent); |
61 |
|
runTest(newMap(mapClass), key); |
62 |
+ |
if ((rep & 3) == 3 && rep < numTests - 1) { |
63 |
+ |
shuffle(key); |
64 |
+ |
// Thread.sleep(50); |
65 |
+ |
} |
66 |
|
} |
67 |
|
|
68 |
|
TestTimer.printStats(); |
69 |
|
|
61 |
– |
|
70 |
|
if (doSerializeTest) |
71 |
|
stest(newMap(mapClass), size); |
72 |
|
} |
73 |
|
|
74 |
< |
static Map<Integer,Integer> newMap(Class cl) { |
74 |
> |
static Map<Integer,Integer> newMap(Class<?> cl) { |
75 |
|
try { |
76 |
< |
Map m = (Map<Integer,Integer>)cl.newInstance(); |
77 |
< |
return m; |
70 |
< |
} catch(Exception e) { |
76 |
> |
return (Map<Integer,Integer>) cl.getConstructor().newInstance(); |
77 |
> |
} catch (Exception e) { |
78 |
|
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
79 |
|
} |
80 |
|
} |
81 |
|
|
75 |
– |
|
82 |
|
static void runTest(Map<Integer,Integer> s, Integer[] key) { |
77 |
– |
shuffle(key); |
83 |
|
int size = key.length; |
84 |
|
long startTime = System.nanoTime(); |
85 |
|
test(s, key); |
86 |
|
long time = System.nanoTime() - startTime; |
87 |
|
} |
88 |
|
|
89 |
< |
|
85 |
< |
static void t1(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
89 |
> |
static void t1(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect, int iters) { |
90 |
|
int sum = 0; |
87 |
– |
int iters = 4; |
91 |
|
timer.start(nm, n * iters); |
92 |
|
for (int j = 0; j < iters; ++j) { |
93 |
|
for (int i = 0; i < n; i++) { |
94 |
|
if (s.get(key[i]) != null) ++sum; |
95 |
|
} |
96 |
|
} |
97 |
< |
timer.finish(); |
98 |
< |
reallyAssert (sum == expect * iters); |
97 |
> |
timer.finish(); |
98 |
> |
reallyAssert(sum == expect * iters); |
99 |
> |
} |
100 |
> |
|
101 |
> |
static void t1Boxed(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
102 |
> |
int sum = 0; |
103 |
> |
int iters = 8; |
104 |
> |
timer.start(nm, n * iters); |
105 |
> |
for (int j = 0; j < iters; ++j) { |
106 |
> |
for (int i = 0; i < n; i++) { |
107 |
> |
if (s.get(i) != i) ++sum; |
108 |
> |
} |
109 |
> |
} |
110 |
> |
timer.finish(); |
111 |
> |
reallyAssert(sum == expect * iters); |
112 |
|
} |
113 |
|
|
114 |
|
static void t2(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
117 |
|
for (int i = 0; i < n; i++) { |
118 |
|
if (s.remove(key[i]) != null) ++sum; |
119 |
|
} |
120 |
< |
timer.finish(); |
121 |
< |
reallyAssert (sum == expect); |
120 |
> |
timer.finish(); |
121 |
> |
reallyAssert(sum == expect); |
122 |
|
} |
123 |
|
|
124 |
|
static void t3(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
129 |
|
Integer v = absent[i & absentMask]; |
130 |
|
if (s.put(k, v) == null) ++sum; |
131 |
|
} |
132 |
< |
timer.finish(); |
133 |
< |
reallyAssert (sum == expect); |
132 |
> |
timer.finish(); |
133 |
> |
reallyAssert(sum == expect); |
134 |
|
} |
135 |
|
|
136 |
|
static void t4(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
139 |
|
for (int i = 0; i < n; i++) { |
140 |
|
if (s.containsKey(key[i])) ++sum; |
141 |
|
} |
142 |
< |
timer.finish(); |
143 |
< |
reallyAssert (sum == expect); |
142 |
> |
timer.finish(); |
143 |
> |
reallyAssert(sum == expect); |
144 |
|
} |
145 |
|
|
146 |
|
static void t5(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
149 |
|
for (int i = n-2; i >= 0; i-=2) { |
150 |
|
if (s.remove(key[i]) != null) ++sum; |
151 |
|
} |
152 |
< |
timer.finish(); |
153 |
< |
reallyAssert (sum == expect); |
152 |
> |
timer.finish(); |
153 |
> |
reallyAssert(sum == expect); |
154 |
|
} |
155 |
|
|
156 |
|
static void t6(String nm, int n, Map<Integer,Integer> s, Integer[] k1, Integer[] k2) { |
160 |
|
if (s.get(k1[i]) != null) ++sum; |
161 |
|
if (s.get(k2[i & absentMask]) != null) ++sum; |
162 |
|
} |
163 |
< |
timer.finish(); |
164 |
< |
reallyAssert (sum == n); |
163 |
> |
timer.finish(); |
164 |
> |
reallyAssert(sum == n); |
165 |
|
} |
166 |
|
|
167 |
|
static void t7(String nm, int n, Map<Integer,Integer> s, Integer[] k1, Integer[] k2) { |
171 |
|
if (s.containsKey(k1[i])) ++sum; |
172 |
|
if (s.containsKey(k2[i & absentMask])) ++sum; |
173 |
|
} |
174 |
< |
timer.finish(); |
175 |
< |
reallyAssert (sum == n); |
174 |
> |
timer.finish(); |
175 |
> |
reallyAssert(sum == n); |
176 |
|
} |
177 |
|
|
178 |
|
static void t8(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
181 |
|
for (int i = 0; i < n; i++) { |
182 |
|
if (s.get(key[i]) != null) ++sum; |
183 |
|
} |
184 |
< |
timer.finish(); |
185 |
< |
reallyAssert (sum == expect); |
184 |
> |
timer.finish(); |
185 |
> |
reallyAssert(sum == expect); |
186 |
|
} |
187 |
|
|
172 |
– |
|
188 |
|
static void t9(Map<Integer,Integer> s) { |
189 |
|
int sum = 0; |
190 |
|
int iters = 20; |
192 |
|
int step = absentSize / iters; |
193 |
|
for (int i = 0; i < absentSize; i += step) |
194 |
|
if (s.containsValue(absent[i])) ++sum; |
195 |
< |
timer.finish(); |
196 |
< |
reallyAssert (sum != 0); |
195 |
> |
timer.finish(); |
196 |
> |
reallyAssert(sum != 0); |
197 |
|
} |
198 |
|
|
184 |
– |
|
199 |
|
static void ktest(Map<Integer,Integer> s, int size, Integer[] key) { |
200 |
|
timer.start("ContainsKey ", size); |
201 |
|
Set ks = s.keySet(); |
203 |
|
for (int i = 0; i < size; i++) { |
204 |
|
if (ks.contains(key[i])) ++sum; |
205 |
|
} |
206 |
< |
timer.finish(); |
207 |
< |
reallyAssert (sum == size); |
206 |
> |
timer.finish(); |
207 |
> |
reallyAssert(sum == size); |
208 |
|
} |
209 |
|
|
196 |
– |
|
210 |
|
static void ittest1(Map<Integer,Integer> s, int size) { |
211 |
|
int sum = 0; |
212 |
|
timer.start("Iter Key ", size); |
213 |
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
214 |
< |
if(it.next() != MISSING) |
214 |
> |
if (it.next() != MISSING) |
215 |
|
++sum; |
216 |
|
} |
217 |
< |
timer.finish(); |
218 |
< |
reallyAssert (sum == size); |
217 |
> |
timer.finish(); |
218 |
> |
// if (sum != size) |
219 |
> |
// System.out.println("iters " + sum + " size " + size); |
220 |
> |
reallyAssert(sum == size); |
221 |
|
} |
222 |
|
|
223 |
|
static void ittest2(Map<Integer,Integer> s, int size) { |
224 |
|
int sum = 0; |
225 |
|
timer.start("Iter Value ", size); |
226 |
|
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
227 |
< |
if(it.next() != MISSING) |
227 |
> |
if (it.next() != MISSING) |
228 |
|
++sum; |
229 |
|
} |
230 |
< |
timer.finish(); |
231 |
< |
reallyAssert (sum == size); |
230 |
> |
timer.finish(); |
231 |
> |
// if (sum != size) |
232 |
> |
// System.out.println("iters " + sum + " size " + size); |
233 |
> |
reallyAssert(sum == size); |
234 |
|
} |
235 |
|
static void ittest3(Map<Integer,Integer> s, int size) { |
236 |
|
int sum = 0; |
237 |
|
timer.start("Iter Entry ", size); |
238 |
|
for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { |
239 |
< |
if(it.next() != MISSING) |
239 |
> |
if (it.next() != MISSING) |
240 |
|
++sum; |
241 |
|
} |
242 |
< |
timer.finish(); |
243 |
< |
reallyAssert (sum == size); |
242 |
> |
timer.finish(); |
243 |
> |
reallyAssert(sum == size); |
244 |
|
} |
245 |
|
|
246 |
|
static void ittest4(Map<Integer,Integer> s, int size, int pos) { |
247 |
|
IdentityHashMap seen = new IdentityHashMap(size); |
248 |
< |
reallyAssert (s.size() == size); |
248 |
> |
reallyAssert(s.size() == size); |
249 |
|
int sum = 0; |
250 |
|
timer.start("Iter XEntry ", size); |
251 |
< |
Iterator it = s.entrySet().iterator(); |
251 |
> |
Iterator it = s.entrySet().iterator(); |
252 |
|
Integer k = null; |
253 |
|
Integer v = null; |
254 |
|
for (int i = 0; i < size-pos; ++i) { |
259 |
|
if (v != MISSING) |
260 |
|
++sum; |
261 |
|
} |
262 |
< |
reallyAssert (s.containsKey(k)); |
262 |
> |
reallyAssert(s.containsKey(k)); |
263 |
|
it.remove(); |
264 |
< |
reallyAssert (!s.containsKey(k)); |
264 |
> |
reallyAssert(!s.containsKey(k)); |
265 |
|
while (it.hasNext()) { |
266 |
|
Map.Entry<Integer,Integer> x = (Map.Entry<Integer,Integer>)(it.next()); |
267 |
< |
Integer k2 = (Integer)x.getKey(); |
267 |
> |
Integer k2 = x.getKey(); |
268 |
|
seen.put(k2, k2); |
269 |
|
if (k2 != MISSING) |
270 |
|
++sum; |
271 |
|
} |
272 |
|
|
273 |
< |
reallyAssert (s.size() == size-1); |
273 |
> |
reallyAssert(s.size() == size-1); |
274 |
|
s.put(k, v); |
275 |
< |
reallyAssert (seen.size() == size); |
276 |
< |
timer.finish(); |
277 |
< |
reallyAssert (sum == size); |
278 |
< |
reallyAssert (s.size() == size); |
275 |
> |
reallyAssert(seen.size() == size); |
276 |
> |
timer.finish(); |
277 |
> |
reallyAssert(sum == size); |
278 |
> |
reallyAssert(s.size() == size); |
279 |
|
} |
280 |
|
|
264 |
– |
|
281 |
|
static void ittest(Map<Integer,Integer> s, int size) { |
282 |
< |
ittest1(s, size); |
283 |
< |
ittest2(s, size); |
284 |
< |
ittest3(s, size); |
285 |
< |
// for (int i = 0; i < size-1; ++i) |
282 |
> |
for (int i = 0; i < 4; ++i) { |
283 |
> |
ittest1(s, size); |
284 |
> |
ittest2(s, size); |
285 |
> |
ittest3(s, size); |
286 |
> |
} |
287 |
> |
// for (int i = 0; i < size-1; ++i) |
288 |
|
// ittest4(s, size, i); |
289 |
|
} |
290 |
|
|
296 |
|
if (en.nextElement() != MISSING) |
297 |
|
++sum; |
298 |
|
} |
299 |
< |
timer.finish(); |
300 |
< |
reallyAssert (sum == size); |
299 |
> |
timer.finish(); |
300 |
> |
reallyAssert(sum == size); |
301 |
|
} |
302 |
|
|
303 |
|
static void entest2(Hashtable ht, int size) { |
307 |
|
if (en.nextElement() != MISSING) |
308 |
|
++sum; |
309 |
|
} |
310 |
< |
timer.finish(); |
311 |
< |
reallyAssert (sum == size); |
310 |
> |
timer.finish(); |
311 |
> |
reallyAssert(sum == size); |
312 |
|
} |
313 |
|
|
296 |
– |
|
314 |
|
static void entest3(Hashtable ht, int size) { |
315 |
|
int sum = 0; |
316 |
|
|
317 |
|
timer.start("Iterf Enumeration Key ", size); |
318 |
< |
Enumeration en = ht.keys(); |
318 |
> |
Enumeration en = ht.keys(); |
319 |
|
for (int i = 0; i < size; ++i) { |
320 |
|
if (en.nextElement() != MISSING) |
321 |
|
++sum; |
322 |
|
} |
323 |
< |
timer.finish(); |
324 |
< |
reallyAssert (sum == size); |
323 |
> |
timer.finish(); |
324 |
> |
reallyAssert(sum == size); |
325 |
|
} |
326 |
|
|
327 |
|
static void entest4(Hashtable ht, int size) { |
328 |
|
int sum = 0; |
329 |
|
timer.start("Iterf Enumeration Value", size); |
330 |
< |
Enumeration en = ht.elements(); |
330 |
> |
Enumeration en = ht.elements(); |
331 |
|
for (int i = 0; i < size; ++i) { |
332 |
|
if (en.nextElement() != MISSING) |
333 |
|
++sum; |
334 |
|
} |
335 |
< |
timer.finish(); |
336 |
< |
reallyAssert (sum == size); |
335 |
> |
timer.finish(); |
336 |
> |
reallyAssert(sum == size); |
337 |
|
} |
338 |
|
|
339 |
|
static void entest(Map<Integer,Integer> s, int size) { |
340 |
|
if (s instanceof Hashtable) { |
341 |
< |
Hashtable ht = (Hashtable)s; |
341 |
> |
Hashtable ht = (Hashtable) s; |
342 |
|
// entest3(ht, size); |
343 |
|
// entest4(ht, size); |
344 |
|
entest1(ht, size); |
356 |
|
it.next(); |
357 |
|
it.remove(); |
358 |
|
} |
359 |
< |
timer.finish(); |
360 |
< |
} |
344 |
< |
|
345 |
< |
static void rvtest(Map<Integer,Integer> s, int size) { |
346 |
< |
timer.start("Remove (iterator) ", size); |
347 |
< |
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
348 |
< |
it.next(); |
349 |
< |
it.remove(); |
350 |
< |
} |
351 |
< |
timer.finish(); |
352 |
< |
} |
353 |
< |
|
354 |
< |
|
355 |
< |
static void dtest(Map<Integer,Integer> s, int size, Integer[] key) { |
356 |
< |
timer.start("Put (putAll) ", size * 2); |
357 |
< |
Map<Integer,Integer> s2 = null; |
358 |
< |
try { |
359 |
< |
s2 = (Map<Integer,Integer>) (s.getClass().newInstance()); |
360 |
< |
s2.putAll(s); |
361 |
< |
} |
362 |
< |
catch (Exception e) { e.printStackTrace(); return; } |
363 |
< |
timer.finish(); |
364 |
< |
|
365 |
< |
timer.start("Iter Equals ", size * 2); |
366 |
< |
boolean eqt = s2.equals(s) && s.equals(s2); |
367 |
< |
reallyAssert (eqt); |
368 |
< |
timer.finish(); |
369 |
< |
|
370 |
< |
timer.start("Iter HashCode ", size * 2); |
371 |
< |
int shc = s.hashCode(); |
372 |
< |
int s2hc = s2.hashCode(); |
373 |
< |
reallyAssert (shc == s2hc); |
374 |
< |
timer.finish(); |
375 |
< |
|
376 |
< |
timer.start("Put (present) ", size); |
377 |
< |
s2.putAll(s); |
378 |
< |
timer.finish(); |
379 |
< |
|
380 |
< |
timer.start("Iter EntrySet contains ", size * 2); |
381 |
< |
Set es2 = s2.entrySet(); |
382 |
< |
int sum = 0; |
383 |
< |
for (Iterator i1 = s.entrySet().iterator(); i1.hasNext(); ) { |
384 |
< |
Object entry = i1.next(); |
385 |
< |
if (es2.contains(entry)) ++sum; |
386 |
< |
} |
387 |
< |
timer.finish(); |
388 |
< |
reallyAssert (sum == size); |
389 |
< |
|
390 |
< |
t6("Get ", size, s2, key, absent); |
391 |
< |
|
392 |
< |
Integer hold = s2.get(key[size-1]); |
393 |
< |
s2.put(key[size-1], absent[0]); |
394 |
< |
timer.start("Iter Equals ", size * 2); |
395 |
< |
eqt = s2.equals(s) && s.equals(s2); |
396 |
< |
reallyAssert (!eqt); |
397 |
< |
timer.finish(); |
398 |
< |
|
399 |
< |
timer.start("Iter HashCode ", size * 2); |
400 |
< |
int s1h = s.hashCode(); |
401 |
< |
int s2h = s2.hashCode(); |
402 |
< |
reallyAssert (s1h != s2h); |
403 |
< |
timer.finish(); |
404 |
< |
|
405 |
< |
s2.put(key[size-1], hold); |
406 |
< |
timer.start("Remove (iterator) ", size * 2); |
407 |
< |
Iterator s2i = s2.entrySet().iterator(); |
408 |
< |
Set es = s.entrySet(); |
409 |
< |
while (s2i.hasNext()) |
410 |
< |
reallyAssert(es.remove(s2i.next())); |
411 |
< |
timer.finish(); |
412 |
< |
|
413 |
< |
if (!s.isEmpty()) System.out.println(s); |
414 |
< |
reallyAssert (s.isEmpty()); |
415 |
< |
|
416 |
< |
timer.start("Clear ", size); |
417 |
< |
s2.clear(); |
418 |
< |
timer.finish(); |
419 |
< |
reallyAssert (s2.isEmpty() && s.isEmpty()); |
359 |
> |
reallyAssert(s.isEmpty()); |
360 |
> |
timer.finish(); |
361 |
|
} |
362 |
|
|
363 |
|
static void stest(Map<Integer,Integer> s, int size) throws Exception { |
364 |
< |
if (!(s instanceof Serializable)) |
364 |
> |
if (!(s instanceof Serializable)) |
365 |
|
return; |
366 |
|
System.out.print("Serialize : "); |
367 |
< |
|
367 |
> |
|
368 |
|
for (int i = 0; i < size; i++) { |
369 |
< |
s.put(new Integer(i), new Integer(1)); |
369 |
> |
s.put(Integer.valueOf(i), Integer.valueOf(1)); |
370 |
|
} |
371 |
|
|
372 |
|
long startTime = System.nanoTime(); |
386 |
|
System.out.print(time + "ms"); |
387 |
|
|
388 |
|
if (s instanceof IdentityHashMap) return; |
389 |
< |
reallyAssert (s.equals(m)); |
389 |
> |
reallyAssert(s.equals(m)); |
390 |
|
} |
391 |
|
|
451 |
– |
|
392 |
|
static void test(Map<Integer,Integer> s, Integer[] key) { |
393 |
|
int size = key.length; |
394 |
|
|
395 |
|
t3("Put (absent) ", size, s, key, size); |
396 |
+ |
reallyAssert(s.size() == size); |
397 |
+ |
t1("Get (present) ", size, s, key, size, 8); |
398 |
+ |
t1Boxed("Get boxed (present) ", size, s, key, size); |
399 |
+ |
ittest1(s, size); |
400 |
|
t3("Put (present) ", size, s, key, 0); |
401 |
+ |
reallyAssert(s.size() == size); |
402 |
|
t7("ContainsKey ", size, s, key, absent); |
403 |
|
t4("ContainsKey ", size, s, key, size); |
404 |
|
ktest(s, size, key); |
405 |
|
t4("ContainsKey ", absentSize, s, absent, 0); |
406 |
|
t6("Get ", size, s, key, absent); |
407 |
< |
t1("Get (present) ", size, s, key, size); |
408 |
< |
t1("Get (absent) ", absentSize, s, absent, 0); |
407 |
> |
t1("Get (present) ", size, s, key, size, 8); |
408 |
> |
t1("Get (absent) ", absentSize, s, absent, 0, 1); |
409 |
> |
reallyAssert(s.size() == size); |
410 |
|
t2("Remove (absent) ", absentSize, s, absent, 0); |
411 |
+ |
reallyAssert(s.size() == size); |
412 |
|
t5("Remove (present) ", size, s, key, size / 2); |
413 |
+ |
reallyAssert(s.size() == size / 2); |
414 |
+ |
t1("Get ", size, s, key, size / 2, 8); |
415 |
+ |
ittest1(s, size / 2); |
416 |
|
t3("Put (half present) ", size, s, key, size / 2); |
417 |
+ |
reallyAssert(s.size() == size); |
418 |
+ |
t1("Get (present) ", size, s, key, size, 4); |
419 |
|
|
468 |
– |
ittest(s, size); |
420 |
|
entest(s, size); |
421 |
|
t9(s); |
422 |
< |
rtest(s, size); |
423 |
< |
|
422 |
> |
reallyAssert(s.size() == size); |
423 |
> |
timer.start("Clear ", size); |
424 |
> |
s.clear(); |
425 |
> |
timer.finish(); |
426 |
> |
t1("Get (absent) ", size, s, key, 0, 1); |
427 |
|
t4("ContainsKey ", size, s, key, 0); |
428 |
|
t2("Remove (absent) ", size, s, key, 0); |
429 |
|
t3("Put (presized) ", size, s, key, size); |
430 |
< |
dtest(s, size, key); |
430 |
> |
t1("Get (present) ", size, s, key, size, 4); |
431 |
> |
reallyAssert(s.size() == size); |
432 |
> |
ittest(s, size); |
433 |
> |
rtest(s, size); |
434 |
> |
reallyAssert(s.size() == 0); |
435 |
> |
timer.start("Clear ", size); |
436 |
> |
s.clear(); |
437 |
> |
timer.finish(); |
438 |
> |
t3("Put (presized) ", size, s, key, size); |
439 |
> |
|
440 |
> |
timer.start("Put (putAll) ", size * 2); |
441 |
> |
final Map<Integer,Integer> s2; |
442 |
> |
try { |
443 |
> |
s2 = (Map<Integer,Integer>) |
444 |
> |
s.getClass().getConstructor().newInstance(); |
445 |
> |
s2.putAll(s); |
446 |
> |
} |
447 |
> |
catch (Exception e) { e.printStackTrace(); return; } |
448 |
> |
timer.finish(); |
449 |
> |
|
450 |
> |
timer.start("Iter Equals ", size * 2); |
451 |
> |
boolean eqt = s2.equals(s) && s.equals(s2); |
452 |
> |
reallyAssert(eqt); |
453 |
> |
timer.finish(); |
454 |
> |
|
455 |
> |
timer.start("Iter HashCode ", size * 2); |
456 |
> |
int shc = s.hashCode(); |
457 |
> |
int s2hc = s2.hashCode(); |
458 |
> |
reallyAssert(shc == s2hc); |
459 |
> |
timer.finish(); |
460 |
> |
|
461 |
> |
timer.start("Put (present) ", size * 2); |
462 |
> |
s2.putAll(s); |
463 |
> |
timer.finish(); |
464 |
> |
|
465 |
> |
timer.start("Put (present) ", size); |
466 |
> |
int ipsum = 0; |
467 |
> |
for (Iterator i0 = s.entrySet().iterator(); i0.hasNext(); ) { |
468 |
> |
Map.Entry<Integer,Integer> me = (Map.Entry<Integer,Integer>)(i0.next()); |
469 |
> |
if (s2.put(me.getKey(), me.getValue()) != null) |
470 |
> |
++ipsum; |
471 |
> |
} |
472 |
> |
reallyAssert(ipsum == s.size()); |
473 |
> |
timer.finish(); |
474 |
> |
|
475 |
> |
timer.start("Iter EntrySet contains ", size * 2); |
476 |
> |
Set es2 = s2.entrySet(); |
477 |
> |
int sum = 0; |
478 |
> |
for (Iterator i1 = s.entrySet().iterator(); i1.hasNext(); ) { |
479 |
> |
Object entry = i1.next(); |
480 |
> |
if (es2.contains(entry)) ++sum; |
481 |
> |
} |
482 |
> |
timer.finish(); |
483 |
> |
reallyAssert(sum == size); |
484 |
> |
|
485 |
> |
Integer hold = s2.get(key[size-1]); |
486 |
> |
s2.put(key[size-1], absent[0]); |
487 |
> |
timer.start("Iter Equals ", size * 2); |
488 |
> |
eqt = s2.equals(s) && s.equals(s2); |
489 |
> |
reallyAssert(!eqt); |
490 |
> |
timer.finish(); |
491 |
> |
|
492 |
> |
timer.start("Iter HashCode ", size * 2); |
493 |
> |
int s1h = s.hashCode(); |
494 |
> |
int s2h = s2.hashCode(); |
495 |
> |
reallyAssert(s1h != s2h); |
496 |
> |
timer.finish(); |
497 |
> |
|
498 |
> |
s2.put(key[size-1], hold); |
499 |
> |
timer.start("Remove (present) ", size * 2); |
500 |
> |
Iterator s2i = s2.entrySet().iterator(); |
501 |
> |
Set es = s.entrySet(); |
502 |
> |
while (s2i.hasNext()) |
503 |
> |
reallyAssert(es.remove(s2i.next())); |
504 |
> |
timer.finish(); |
505 |
> |
|
506 |
> |
reallyAssert(s.isEmpty()); |
507 |
> |
|
508 |
> |
timer.start("Clear ", size); |
509 |
> |
s2.clear(); |
510 |
> |
timer.finish(); |
511 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
512 |
|
} |
513 |
|
|
514 |
|
static class TestTimer { |
518 |
|
private String cname; |
519 |
|
|
520 |
|
static final java.util.TreeMap accum = new java.util.TreeMap(); |
521 |
< |
|
521 |
> |
|
522 |
|
static void printStats() { |
523 |
|
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
524 |
|
Map.Entry e = (Map.Entry)(it.next()); |
525 |
< |
Stats stats = ((Stats)(e.getValue())); |
525 |
> |
Stats stats = ((Stats) (e.getValue())); |
526 |
|
long n = stats.number; |
527 |
|
double t; |
528 |
< |
if (n > 0) |
528 |
> |
if (n > 0) |
529 |
|
t = stats.sum / n; |
530 |
|
else |
531 |
|
t = stats.least; |
532 |
< |
long nano = Math.round(1000000.0 * t); |
532 |
> |
long nano = Math.round(t); |
533 |
|
System.out.println(e.getKey() + ": " + nano); |
534 |
|
} |
535 |
|
} |
536 |
< |
|
536 |
> |
|
537 |
|
void start(String name, long numOps) { |
538 |
|
this.name = name; |
539 |
|
this.cname = classify(); |
540 |
|
this.numOps = numOps; |
541 |
|
startTime = System.nanoTime(); |
542 |
|
} |
508 |
– |
|
543 |
|
|
544 |
|
String classify() { |
545 |
|
if (name.startsWith("Get")) |
550 |
|
return "Remove "; |
551 |
|
else if (name.startsWith("Iter")) |
552 |
|
return "Iter "; |
553 |
< |
else |
553 |
> |
else |
554 |
|
return null; |
555 |
|
} |
556 |
|
|
557 |
|
void finish() { |
558 |
|
long endTime = System.nanoTime(); |
559 |
|
long time = endTime - startTime; |
560 |
< |
double timePerOp = (((double)time)/numOps) / 1000000; |
560 |
> |
double timePerOp = ((double) time)/numOps; |
561 |
|
|
562 |
|
Object st = accum.get(name); |
563 |
|
if (st == null) |
592 |
|
Stats(double t) { least = t; } |
593 |
|
} |
594 |
|
|
595 |
< |
static Random rng = new Random(); |
595 |
> |
static Random rng = new Random(3152688); |
596 |
|
|
597 |
|
static void shuffle(Integer[] keys) { |
598 |
|
int size = keys.length; |
599 |
< |
for (int i=size; i>1; i--) { |
599 |
> |
for (int i = size; i > 1; i--) { |
600 |
|
int r = rng.nextInt(i); |
601 |
|
Integer t = keys[i-1]; |
602 |
|
keys[i-1] = keys[r]; |
605 |
|
} |
606 |
|
|
607 |
|
} |
574 |
– |
|