9 |
|
* |
10 |
|
* When run with "s" second arg, this requires file "testwords", which |
11 |
|
* is best used with real words. We can't check in this file, but you |
12 |
< |
* can create one from a real dictonary (1 line per word) and then run |
12 |
> |
* can create one from a real dictionary (1 line per word) and then run |
13 |
|
* linux "shuf" to randomize entries. |
14 |
|
*/ |
15 |
|
import java.util.*; |
36 |
|
|
37 |
|
if (args.length == 0) |
38 |
|
System.out.println("Usage: MapCheck mapclass [int|float|string|object] [trials] [size] [serialtest]"); |
39 |
< |
|
39 |
> |
|
40 |
|
if (args.length > 0) { |
41 |
|
try { |
42 |
|
mapClass = Class.forName(args[0]); |
43 |
< |
} catch(ClassNotFoundException e) { |
43 |
> |
} catch (ClassNotFoundException e) { |
44 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
45 |
|
} |
46 |
< |
} |
46 |
> |
} |
47 |
|
|
48 |
|
if (args.length > 1) { |
49 |
|
String et = args[1].toLowerCase(); |
53 |
|
eclass = java.lang.Float.class; |
54 |
|
else if (et.startsWith("s")) |
55 |
|
eclass = java.lang.String.class; |
56 |
+ |
else if (et.startsWith("d")) |
57 |
+ |
eclass = java.lang.Double.class; |
58 |
|
} |
59 |
|
if (eclass == null) |
60 |
|
eclass = Object.class; |
61 |
|
|
62 |
< |
if (args.length > 2) |
62 |
> |
if (args.length > 2) |
63 |
|
numTests = Integer.parseInt(args[2]); |
64 |
|
|
65 |
< |
if (args.length > 3) |
65 |
> |
if (args.length > 3) |
66 |
|
size = Integer.parseInt(args[3]); |
67 |
|
|
68 |
|
boolean doSerializeTest = args.length > 4; |
99 |
|
|
100 |
|
static Map newMap() { |
101 |
|
try { |
102 |
< |
return (Map)mapClass.newInstance(); |
103 |
< |
} catch(Exception e) { |
102 |
> |
return (Map) mapClass.newInstance(); |
103 |
> |
} catch (Exception e) { |
104 |
|
throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); |
105 |
|
} |
106 |
|
} |
134 |
|
try { |
135 |
|
m.put(null, x); |
136 |
|
v = m.get(null); |
137 |
< |
} catch(NullPointerException npe) { |
137 |
> |
} catch (NullPointerException npe) { |
138 |
|
System.out.println("Map does not allow null keys"); |
139 |
|
return; |
140 |
|
} |
152 |
|
if (v != null && v.getClass() == eclass) |
153 |
|
++sum; |
154 |
|
} |
155 |
< |
timer.finish(); |
155 |
> |
timer.finish(); |
156 |
|
reallyAssert (sum == expect); |
157 |
|
checkSum += sum; |
158 |
|
} |
166 |
|
for (int i = 0; i < n; i++) { |
167 |
|
if ((Integer)(intMap.get(i)) != i) ++sum; |
168 |
|
} |
169 |
< |
timer.finish(); |
169 |
> |
timer.finish(); |
170 |
|
reallyAssert (sum == expect); |
171 |
|
} |
172 |
|
|
176 |
|
for (int i = 0; i < n; i++) { |
177 |
|
if (s.remove(key[i]) != null) ++sum; |
178 |
|
} |
179 |
< |
timer.finish(); |
179 |
> |
timer.finish(); |
180 |
|
reallyAssert (sum == expect); |
181 |
|
checkSum += sum; |
182 |
|
} |
185 |
|
String nm = "Remove Present "; |
186 |
|
timer.start(nm, n); |
187 |
|
s.clear(); |
188 |
< |
timer.finish(); |
188 |
> |
timer.finish(); |
189 |
|
reallyAssert (s.isEmpty()); |
190 |
|
} |
191 |
|
|
197 |
|
Object v = s.put(k, k); |
198 |
|
if (v == null) ++sum; |
199 |
|
} |
200 |
< |
timer.finish(); |
200 |
> |
timer.finish(); |
201 |
|
reallyAssert (sum == expect); |
202 |
|
checkSum += sum; |
203 |
|
} |
208 |
|
for (int i = 0; i < n; i++) { |
209 |
|
if (s.containsKey(key[i])) ++sum; |
210 |
|
} |
211 |
< |
timer.finish(); |
211 |
> |
timer.finish(); |
212 |
|
reallyAssert (sum == expect); |
213 |
|
checkSum += sum; |
214 |
|
} |
229 |
|
for (int i = n-2; i >= 0; i-=2) { |
230 |
|
if (s.remove(key[i]) != null) ++sum; |
231 |
|
} |
232 |
< |
timer.finish(); |
232 |
> |
timer.finish(); |
233 |
|
reallyAssert (sum == expect); |
234 |
|
checkSum += sum; |
235 |
|
} |
237 |
|
static void valTest(Map s, Object[] key) { |
238 |
|
int size = s.size(); |
239 |
|
int sum = 0; |
240 |
< |
timer.start("Traverse access key/val", size); |
240 |
> |
timer.start("Traverse key or value ", size); |
241 |
|
if (s.containsValue(MISSING)) ++sum; |
242 |
< |
timer.finish(); |
242 |
> |
timer.finish(); |
243 |
|
reallyAssert (sum == 0); |
244 |
|
checkSum += sum; |
245 |
|
} |
248 |
|
static Object kitTest(Map s, int size) { |
249 |
|
Object last = null; |
250 |
|
int sum = 0; |
251 |
< |
timer.start("Traverse access key/val", size); |
251 |
> |
timer.start("Traverse key or value ", size); |
252 |
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
253 |
|
Object x = it.next(); |
254 |
|
if (x != last && x != null && x.getClass() == eclass) |
255 |
|
++sum; |
256 |
|
last = x; |
257 |
|
} |
258 |
< |
timer.finish(); |
258 |
> |
timer.finish(); |
259 |
|
reallyAssert (sum == size); |
260 |
|
checkSum += sum; |
261 |
|
return last; |
264 |
|
static Object vitTest(Map s, int size) { |
265 |
|
Object last = null; |
266 |
|
int sum = 0; |
267 |
< |
timer.start("Traverse access key/val", size); |
267 |
> |
timer.start("Traverse key or value ", size); |
268 |
|
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
269 |
|
Object x = it.next(); |
270 |
|
if (x != last && x != null && x.getClass() == eclass) |
271 |
|
++sum; |
272 |
|
last = x; |
273 |
|
} |
274 |
< |
timer.finish(); |
274 |
> |
timer.finish(); |
275 |
|
reallyAssert (sum == size); |
276 |
|
checkSum += sum; |
277 |
|
return last; |
279 |
|
|
280 |
|
static void eitTest(Map s, int size) { |
281 |
|
int sum = 0; |
282 |
< |
timer.start("Traverse access entry ", size); |
282 |
> |
timer.start("Traverse entry ", size); |
283 |
|
for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { |
284 |
|
Map.Entry e = (Map.Entry)it.next(); |
285 |
|
Object k = e.getKey(); |
288 |
|
v != null && v.getClass() == eclass) |
289 |
|
++sum; |
290 |
|
} |
291 |
< |
timer.finish(); |
291 |
> |
timer.finish(); |
292 |
|
reallyAssert (sum == size); |
293 |
|
checkSum += sum; |
294 |
|
} |
303 |
|
it.remove(); |
304 |
|
++sum; |
305 |
|
} |
306 |
< |
timer.finish(); |
306 |
> |
timer.finish(); |
307 |
|
reallyAssert (sum == sz); |
308 |
|
checkSum += sum; |
309 |
|
} |
320 |
|
it.next(); |
321 |
|
++sum; |
322 |
|
} |
323 |
< |
timer.finish(); |
323 |
> |
timer.finish(); |
324 |
|
reallyAssert (sum == sz / 2); |
325 |
|
checkSum += sum; |
326 |
|
} |
328 |
|
static void putAllTest(String nm, int n, Map src, Map dst) { |
329 |
|
timer.start(nm, n); |
330 |
|
dst.putAll(src); |
331 |
< |
timer.finish(); |
331 |
> |
timer.finish(); |
332 |
|
reallyAssert (src.size() == dst.size()); |
333 |
|
} |
334 |
|
|
335 |
|
static void serTest(Map s, int size) throws Exception { |
336 |
< |
if (!(s instanceof Serializable)) |
336 |
> |
if (!(s instanceof Serializable)) |
337 |
|
return; |
338 |
|
System.out.print("Serialize : "); |
339 |
< |
|
339 |
> |
|
340 |
|
for (int i = 0; i < size; i++) { |
341 |
|
s.put(new Integer(i), Boolean.TRUE); |
342 |
|
} |
393 |
|
kitTest(s, size); |
394 |
|
vitTest(s, size); |
395 |
|
eitTest(s, size); |
396 |
< |
twoMapTest1(s, key, absent); |
396 |
> |
twoMapTest1(s, key, absent); |
397 |
|
twoMapTest2(s, key, absent); |
398 |
|
} |
399 |
|
|
410 |
|
putTest("Add Absent ", size, s2, key, size * 3 / 4); |
411 |
|
reallyAssert(s2.size() == size * 2); |
412 |
|
clrTest(size, s2); |
413 |
< |
} |
413 |
> |
} |
414 |
|
|
415 |
|
static void twoMapTest2(Map s, Object[] key, Object[] absent) { |
416 |
|
int size = key.length; |
423 |
|
Object hold = s2.get(lastkey); |
424 |
|
int sum = 0; |
425 |
|
|
426 |
< |
timer.start("Traverse access entry ", size * 12); // 12 until finish |
426 |
> |
timer.start("Traverse entry ", size * 12); // 12 until finish |
427 |
|
|
428 |
|
int sh1 = s.hashCode() - s2.hashCode(); |
429 |
|
reallyAssert (sh1 == 0); |
459 |
|
e.setValue(s.get(e.getKey())); |
460 |
|
} |
461 |
|
|
462 |
< |
timer.finish(); |
462 |
> |
timer.finish(); |
463 |
|
|
464 |
|
int rmiss = 0; |
465 |
|
timer.start("Remove Present ", size * 2); |
469 |
|
if (!es.remove(s2i.next())) |
470 |
|
++rmiss; |
471 |
|
} |
472 |
< |
timer.finish(); |
472 |
> |
timer.finish(); |
473 |
|
reallyAssert(rmiss == 0); |
474 |
|
|
475 |
|
clrTest(size, s2); |
481 |
|
reallyAssert (s.size() == size); |
482 |
|
int sum = 0; |
483 |
|
timer.start("Iter XEntry ", size); |
484 |
< |
Iterator it = s.entrySet().iterator(); |
484 |
> |
Iterator it = s.entrySet().iterator(); |
485 |
|
Object k = null; |
486 |
|
Object v = null; |
487 |
|
for (int i = 0; i < size-pos; ++i) { |
506 |
|
reallyAssert (s.size() == size-1); |
507 |
|
s.put(k, v); |
508 |
|
reallyAssert (seen.size() == size); |
509 |
< |
timer.finish(); |
509 |
> |
timer.finish(); |
510 |
|
reallyAssert (sum == size); |
511 |
|
reallyAssert (s.size() == size); |
512 |
|
} |
524 |
|
else if (eclass == Float.class) { |
525 |
|
initFloats(key, absent, size); |
526 |
|
} |
527 |
+ |
else if (eclass == Double.class) { |
528 |
+ |
initDoubles(key, absent, size); |
529 |
+ |
} |
530 |
|
else if (eclass == String.class) { |
531 |
|
initWords(size, key, absent); |
532 |
|
} |
533 |
< |
else |
533 |
> |
else |
534 |
|
throw new Error("unknown type"); |
535 |
|
} |
536 |
|
|
537 |
|
static void initInts(Object[] key, Object[] absent, int size) { |
538 |
< |
for (int i = 0; i < size; ++i) |
538 |
> |
for (int i = 0; i < size; ++i) |
539 |
|
key[i] = Integer.valueOf(i); |
540 |
|
Map m = newMap(); |
541 |
|
int k = 0; |
565 |
|
} |
566 |
|
} |
567 |
|
|
568 |
+ |
static void initDoubles(Object[] key, Object[] absent, int size) { |
569 |
+ |
Map m = newMap(); |
570 |
+ |
for (int i = 0; i < size; ++i) { |
571 |
+ |
double r = Double.valueOf(i); |
572 |
+ |
key[i] = r; |
573 |
+ |
m.put(r, r); |
574 |
+ |
} |
575 |
+ |
int k = 0; |
576 |
+ |
while (k < size) { |
577 |
+ |
double r = rng.nextDouble(); |
578 |
+ |
Double ir = Double.valueOf(r); |
579 |
+ |
if (m.put(ir, ir) == null) |
580 |
+ |
absent[k++] = ir; |
581 |
+ |
} |
582 |
+ |
} |
583 |
+ |
|
584 |
|
// Use as many real words as possible, then use fake random words |
585 |
|
|
586 |
|
static void initWords(int size, Object[] key, Object[] abs) { |
595 |
|
for (;;) { |
596 |
|
int c = in.read(); |
597 |
|
if (c < 0) { |
598 |
< |
if (ki < size) |
598 |
> |
if (ki < size) |
599 |
|
randomWords(key, ki, size); |
600 |
|
if (ai < size) |
601 |
|
randomWords(abs, ai, size); |
637 |
|
r >>>= 8; |
638 |
|
c[k++] = (char)(' ' + (r & 0x7f)); |
639 |
|
} |
640 |
< |
c[k++] = (char)((i & 31) | 1); // never == to any testword |
640 |
> |
c[k++] = (char)((i & 31) | 1); // never == to any testword |
641 |
|
ws[i] = new String(c); |
642 |
|
} |
643 |
|
} |
648 |
|
private long startTime; |
649 |
|
|
650 |
|
static final java.util.TreeMap accum = new java.util.TreeMap(); |
651 |
< |
|
651 |
> |
|
652 |
|
static void printStats() { |
653 |
|
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
654 |
|
Map.Entry e = (Map.Entry)(it.next()); |
660 |
|
n = stats.firstn; |
661 |
|
s = stats.first; |
662 |
|
} |
663 |
< |
else |
663 |
> |
else |
664 |
|
s = stats.sum; |
665 |
< |
|
665 |
> |
|
666 |
|
double t = ((double)s) / n; |
667 |
|
long nano = Math.round(t); |
668 |
|
System.out.printf("%6d", + nano); |
669 |
|
System.out.println(); |
670 |
|
} |
671 |
|
} |
672 |
< |
|
672 |
> |
|
673 |
|
void start(String name, long numOps) { |
674 |
|
this.name = name; |
675 |
|
this.numOps = numOps; |
681 |
|
Object st = accum.get(name); |
682 |
|
if (st == null) |
683 |
|
accum.put(name, new Stats(elapsed, numOps)); |
684 |
< |
else |
684 |
> |
else |
685 |
|
((Stats)st).addTime(elapsed, numOps); |
686 |
|
} |
687 |
|
|
692 |
|
long number; |
693 |
|
long first; |
694 |
|
long firstn; |
695 |
< |
Stats(long t, long n) { |
696 |
< |
first = t; |
695 |
> |
Stats(long t, long n) { |
696 |
> |
first = t; |
697 |
|
firstn = n; |
698 |
|
} |
699 |
|
void addTime(long t, long n) { |
724 |
|
} |
725 |
|
|
726 |
|
} |
706 |
– |
|