--- jsr166/src/test/loops/MapCheck.java 2009/07/01 12:11:44 1.4 +++ jsr166/src/test/loops/MapCheck.java 2009/11/02 23:42:46 1.8 @@ -9,7 +9,7 @@ * * When run with "s" second arg, this requires file "testwords", which * is best used with real words. We can't check in this file, but you - * can create one from a real dictonary (1 line per word) and then run + * can create one from a real dictionary (1 line per word) and then run * linux "shuf" to randomize entries. */ import java.util.*; @@ -36,14 +36,14 @@ public class MapCheck { if (args.length == 0) System.out.println("Usage: MapCheck mapclass [int|float|string|object] [trials] [size] [serialtest]"); - + if (args.length > 0) { try { mapClass = Class.forName(args[0]); - } catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } - } + } if (args.length > 1) { String et = args[1].toLowerCase(); @@ -53,14 +53,16 @@ public class MapCheck { eclass = java.lang.Float.class; else if (et.startsWith("s")) eclass = java.lang.String.class; + else if (et.startsWith("d")) + eclass = java.lang.Double.class; } if (eclass == null) eclass = Object.class; - if (args.length > 2) + if (args.length > 2) numTests = Integer.parseInt(args[2]); - if (args.length > 3) + if (args.length > 3) size = Integer.parseInt(args[3]); boolean doSerializeTest = args.length > 4; @@ -97,8 +99,8 @@ public class MapCheck { static Map newMap() { try { - return (Map)mapClass.newInstance(); - } catch(Exception e) { + return (Map) mapClass.newInstance(); + } catch (Exception e) { throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); } } @@ -132,7 +134,7 @@ public class MapCheck { try { m.put(null, x); v = m.get(null); - } catch(NullPointerException npe) { + } catch (NullPointerException npe) { System.out.println("Map does not allow null keys"); return; } @@ -150,7 +152,7 @@ public class MapCheck { if (v != null && v.getClass() == eclass) ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == expect); checkSum += sum; } @@ -164,7 +166,7 @@ public class MapCheck { for (int i = 0; i < n; i++) { if ((Integer)(intMap.get(i)) != i) ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == expect); } @@ -174,7 +176,7 @@ public class MapCheck { for (int i = 0; i < n; i++) { if (s.remove(key[i]) != null) ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == expect); checkSum += sum; } @@ -183,7 +185,7 @@ public class MapCheck { String nm = "Remove Present "; timer.start(nm, n); s.clear(); - timer.finish(); + timer.finish(); reallyAssert (s.isEmpty()); } @@ -195,7 +197,7 @@ public class MapCheck { Object v = s.put(k, k); if (v == null) ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == expect); checkSum += sum; } @@ -206,7 +208,7 @@ public class MapCheck { for (int i = 0; i < n; i++) { if (s.containsKey(key[i])) ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == expect); checkSum += sum; } @@ -227,7 +229,7 @@ public class MapCheck { for (int i = n-2; i >= 0; i-=2) { if (s.remove(key[i]) != null) ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == expect); checkSum += sum; } @@ -235,9 +237,9 @@ public class MapCheck { static void valTest(Map s, Object[] key) { int size = s.size(); int sum = 0; - timer.start("Traverse access key/val", size); + timer.start("Traverse key or value ", size); if (s.containsValue(MISSING)) ++sum; - timer.finish(); + timer.finish(); reallyAssert (sum == 0); checkSum += sum; } @@ -246,14 +248,14 @@ public class MapCheck { static Object kitTest(Map s, int size) { Object last = null; int sum = 0; - timer.start("Traverse access key/val", size); + timer.start("Traverse key or value ", size); for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { Object x = it.next(); if (x != last && x != null && x.getClass() == eclass) ++sum; last = x; } - timer.finish(); + timer.finish(); reallyAssert (sum == size); checkSum += sum; return last; @@ -262,14 +264,14 @@ public class MapCheck { static Object vitTest(Map s, int size) { Object last = null; int sum = 0; - timer.start("Traverse access key/val", size); + timer.start("Traverse key or value ", size); for (Iterator it = s.values().iterator(); it.hasNext(); ) { Object x = it.next(); if (x != last && x != null && x.getClass() == eclass) ++sum; last = x; } - timer.finish(); + timer.finish(); reallyAssert (sum == size); checkSum += sum; return last; @@ -277,7 +279,7 @@ public class MapCheck { static void eitTest(Map s, int size) { int sum = 0; - timer.start("Traverse access entry ", size); + timer.start("Traverse entry ", size); for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { Map.Entry e = (Map.Entry)it.next(); Object k = e.getKey(); @@ -286,7 +288,7 @@ public class MapCheck { v != null && v.getClass() == eclass) ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == size); checkSum += sum; } @@ -301,7 +303,7 @@ public class MapCheck { it.remove(); ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == sz); checkSum += sum; } @@ -318,7 +320,7 @@ public class MapCheck { it.next(); ++sum; } - timer.finish(); + timer.finish(); reallyAssert (sum == sz / 2); checkSum += sum; } @@ -326,15 +328,15 @@ public class MapCheck { static void putAllTest(String nm, int n, Map src, Map dst) { timer.start(nm, n); dst.putAll(src); - timer.finish(); + timer.finish(); reallyAssert (src.size() == dst.size()); } static void serTest(Map s, int size) throws Exception { - if (!(s instanceof Serializable)) + if (!(s instanceof Serializable)) return; System.out.print("Serialize : "); - + for (int i = 0; i < size; i++) { s.put(new Integer(i), Boolean.TRUE); } @@ -391,7 +393,7 @@ public class MapCheck { kitTest(s, size); vitTest(s, size); eitTest(s, size); - twoMapTest1(s, key, absent); + twoMapTest1(s, key, absent); twoMapTest2(s, key, absent); } @@ -408,7 +410,7 @@ public class MapCheck { putTest("Add Absent ", size, s2, key, size * 3 / 4); reallyAssert(s2.size() == size * 2); clrTest(size, s2); - } + } static void twoMapTest2(Map s, Object[] key, Object[] absent) { int size = key.length; @@ -421,7 +423,7 @@ public class MapCheck { Object hold = s2.get(lastkey); int sum = 0; - timer.start("Traverse access entry ", size * 12); // 12 until finish + timer.start("Traverse entry ", size * 12); // 12 until finish int sh1 = s.hashCode() - s2.hashCode(); reallyAssert (sh1 == 0); @@ -457,7 +459,7 @@ public class MapCheck { e.setValue(s.get(e.getKey())); } - timer.finish(); + timer.finish(); int rmiss = 0; timer.start("Remove Present ", size * 2); @@ -467,7 +469,7 @@ public class MapCheck { if (!es.remove(s2i.next())) ++rmiss; } - timer.finish(); + timer.finish(); reallyAssert(rmiss == 0); clrTest(size, s2); @@ -479,7 +481,7 @@ public class MapCheck { reallyAssert (s.size() == size); int sum = 0; timer.start("Iter XEntry ", size); - Iterator it = s.entrySet().iterator(); + Iterator it = s.entrySet().iterator(); Object k = null; Object v = null; for (int i = 0; i < size-pos; ++i) { @@ -504,7 +506,7 @@ public class MapCheck { reallyAssert (s.size() == size-1); s.put(k, v); reallyAssert (seen.size() == size); - timer.finish(); + timer.finish(); reallyAssert (sum == size); reallyAssert (s.size() == size); } @@ -522,15 +524,18 @@ public class MapCheck { else if (eclass == Float.class) { initFloats(key, absent, size); } + else if (eclass == Double.class) { + initDoubles(key, absent, size); + } else if (eclass == String.class) { initWords(size, key, absent); } - else + else throw new Error("unknown type"); } static void initInts(Object[] key, Object[] absent, int size) { - for (int i = 0; i < size; ++i) + for (int i = 0; i < size; ++i) key[i] = Integer.valueOf(i); Map m = newMap(); int k = 0; @@ -560,6 +565,22 @@ public class MapCheck { } } + static void initDoubles(Object[] key, Object[] absent, int size) { + Map m = newMap(); + for (int i = 0; i < size; ++i) { + double r = Double.valueOf(i); + key[i] = r; + m.put(r, r); + } + int k = 0; + while (k < size) { + double r = rng.nextDouble(); + Double ir = Double.valueOf(r); + if (m.put(ir, ir) == null) + absent[k++] = ir; + } + } + // Use as many real words as possible, then use fake random words static void initWords(int size, Object[] key, Object[] abs) { @@ -574,7 +595,7 @@ public class MapCheck { for (;;) { int c = in.read(); if (c < 0) { - if (ki < size) + if (ki < size) randomWords(key, ki, size); if (ai < size) randomWords(abs, ai, size); @@ -616,7 +637,7 @@ public class MapCheck { r >>>= 8; c[k++] = (char)(' ' + (r & 0x7f)); } - c[k++] = (char)((i & 31) | 1); // never == to any testword + c[k++] = (char)((i & 31) | 1); // never == to any testword ws[i] = new String(c); } } @@ -627,7 +648,7 @@ public class MapCheck { private long startTime; static final java.util.TreeMap accum = new java.util.TreeMap(); - + static void printStats() { for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { Map.Entry e = (Map.Entry)(it.next()); @@ -639,16 +660,16 @@ public class MapCheck { n = stats.firstn; s = stats.first; } - else + else s = stats.sum; - + double t = ((double)s) / n; long nano = Math.round(t); System.out.printf("%6d", + nano); System.out.println(); } } - + void start(String name, long numOps) { this.name = name; this.numOps = numOps; @@ -660,7 +681,7 @@ public class MapCheck { Object st = accum.get(name); if (st == null) accum.put(name, new Stats(elapsed, numOps)); - else + else ((Stats)st).addTime(elapsed, numOps); } @@ -671,8 +692,8 @@ public class MapCheck { long number; long first; long firstn; - Stats(long t, long n) { - first = t; + Stats(long t, long n) { + first = t; firstn = n; } void addTime(long t, long n) { @@ -703,4 +724,3 @@ public class MapCheck { } } -