--- jsr166/src/test/loops/NavigableMapCheck.java 2005/05/02 19:19:38 1.1 +++ jsr166/src/test/loops/NavigableMapCheck.java 2010/09/01 07:47:27 1.9 @@ -1,3 +1,8 @@ +/* + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + */ /** * @test * @synopsis Times and checks basic map operations @@ -27,16 +32,16 @@ public class NavigableMapCheck { 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) + if (args.length > 1) numTests = Integer.parseInt(args[1]); - if (args.length > 2) + if (args.length > 2) size = Integer.parseInt(args[2]); System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size); @@ -47,11 +52,11 @@ public class NavigableMapCheck { absentMask = absentSize - 1; absent = new Integer[absentSize]; - for (int i = 0; i < absentSize; ++i) + for (int i = 0; i < absentSize; ++i) absent[i] = new Integer(i * 2); Integer[] key = new Integer[size]; - for (int i = 0; i < size; ++i) + for (int i = 0; i < size; ++i) key[i] = new Integer(i * 2 + 1); @@ -65,9 +70,9 @@ public class NavigableMapCheck { static NavigableMap newMap(Class cl) { try { - NavigableMap m = (NavigableMap)cl.newInstance(); + NavigableMap m = (NavigableMap) cl.newInstance(); return m; - } catch(Exception e) { + } catch (Exception e) { throw new RuntimeException("Can't instantiate " + cl + ": " + e); } } @@ -90,8 +95,8 @@ public class NavigableMapCheck { if (s.get(key[i]) != null) ++sum; } } - timer.finish(); - reallyAssert (sum == expect * iters); + timer.finish(); + reallyAssert(sum == expect * iters); } static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -100,8 +105,8 @@ public class NavigableMapCheck { for (int i = 0; i < n; i++) { if (s.remove(key[i]) != null) ++sum; } - timer.finish(); - reallyAssert (sum == expect); + timer.finish(); + reallyAssert(sum == expect); } static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -110,8 +115,8 @@ public class NavigableMapCheck { for (int i = 0; i < n; i++) { if (s.put(key[i], absent[i & absentMask]) == null) ++sum; } - timer.finish(); - reallyAssert (sum == expect); + timer.finish(); + reallyAssert(sum == expect); } static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -120,8 +125,8 @@ public class NavigableMapCheck { for (int i = 0; i < n; i++) { if (s.containsKey(key[i])) ++sum; } - timer.finish(); - reallyAssert (sum == expect); + timer.finish(); + reallyAssert(sum == expect); } static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -130,8 +135,8 @@ public class NavigableMapCheck { for (int i = n-2; i >= 0; i-=2) { if (s.remove(key[i]) != null) ++sum; } - timer.finish(); - reallyAssert (sum == expect); + timer.finish(); + reallyAssert(sum == expect); } static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { @@ -141,8 +146,8 @@ public class NavigableMapCheck { if (s.get(k1[i]) != null) ++sum; if (s.get(k2[i & absentMask]) != null) ++sum; } - timer.finish(); - reallyAssert (sum == n); + timer.finish(); + reallyAssert(sum == n); } static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { @@ -152,8 +157,8 @@ public class NavigableMapCheck { if (s.containsKey(k1[i])) ++sum; if (s.containsKey(k2[i & absentMask])) ++sum; } - timer.finish(); - reallyAssert (sum == n); + timer.finish(); + reallyAssert(sum == n); } static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -162,8 +167,8 @@ public class NavigableMapCheck { for (int i = 0; i < n; i++) { if (s.get(key[i]) != null) ++sum; } - timer.finish(); - reallyAssert (sum == expect); + timer.finish(); + reallyAssert(sum == expect); } @@ -174,8 +179,8 @@ public class NavigableMapCheck { int step = absentSize / iters; for (int i = 0; i < absentSize; i += step) if (s.containsValue(absent[i])) ++sum; - timer.finish(); - reallyAssert (sum != 0); + timer.finish(); + reallyAssert(sum != 0); } static void higherTest(NavigableMap s) { @@ -187,8 +192,8 @@ public class NavigableMapCheck { ++sum; e = s.higherEntry(e.getKey()); } - timer.finish(); - reallyAssert (sum == iters); + timer.finish(); + reallyAssert(sum == iters); } static void lowerTest(NavigableMap s) { @@ -200,8 +205,8 @@ public class NavigableMapCheck { ++sum; e = s.higherEntry(e.getKey()); } - timer.finish(); - reallyAssert (sum == iters); + timer.finish(); + reallyAssert(sum == iters); } static void ceilingTest(NavigableMap s) { @@ -214,8 +219,8 @@ public class NavigableMapCheck { if (e != null) ++sum; } - timer.finish(); - reallyAssert (sum == iters); + timer.finish(); + reallyAssert(sum == iters); } static void floorTest(NavigableMap s) { @@ -228,8 +233,8 @@ public class NavigableMapCheck { if (e != null) ++sum; } - timer.finish(); - reallyAssert (sum == iters-1); + timer.finish(); + reallyAssert(sum == iters-1); } @@ -240,8 +245,8 @@ public class NavigableMapCheck { for (int i = 0; i < size; i++) { if (ks.contains(key[i])) ++sum; } - timer.finish(); - reallyAssert (sum == size); + timer.finish(); + reallyAssert(sum == size); } @@ -249,32 +254,32 @@ public class NavigableMapCheck { int sum = 0; timer.start("Iter Key ", size); for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { - if(it.next() != MISSING) + if (it.next() != MISSING) ++sum; } - timer.finish(); - reallyAssert (sum == size); + timer.finish(); + reallyAssert(sum == size); } static void ittest2(NavigableMap s, int size) { int sum = 0; timer.start("Iter Value ", size); for (Iterator it = s.values().iterator(); it.hasNext(); ) { - if(it.next() != MISSING) + if (it.next() != MISSING) ++sum; } - timer.finish(); - reallyAssert (sum == size); + timer.finish(); + reallyAssert(sum == size); } static void ittest3(NavigableMap s, int size) { int sum = 0; timer.start("Iter Entry ", size); for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { - if(it.next() != MISSING) + if (it.next() != MISSING) ++sum; } - timer.finish(); - reallyAssert (sum == size); + timer.finish(); + reallyAssert(sum == size); } static void ittest(NavigableMap s, int size) { @@ -287,27 +292,17 @@ public class NavigableMapCheck { int sum = 0; timer.start("Desc Iter Key ", size); for (Iterator it = s.descendingKeySet().iterator(); it.hasNext(); ) { - if(it.next() != MISSING) + if (it.next() != MISSING) ++sum; } - timer.finish(); - reallyAssert (sum == size); + timer.finish(); + reallyAssert(sum == size); } - static void rittest2(NavigableMap s, int size) { - int sum = 0; - timer.start("Desc Iter Entry ", size); - for (Iterator it = s.descendingEntrySet().iterator(); it.hasNext(); ) { - if(it.next() != MISSING) - ++sum; - } - timer.finish(); - reallyAssert (sum == size); - } static void rittest(NavigableMap s, int size) { rittest1(s, size); - rittest2(s, size); + // rittest2(s, size); } @@ -317,7 +312,7 @@ public class NavigableMapCheck { it.next(); it.remove(); } - timer.finish(); + timer.finish(); } static void rvtest(NavigableMap s, int size) { @@ -326,7 +321,7 @@ public class NavigableMapCheck { it.next(); it.remove(); } - timer.finish(); + timer.finish(); } @@ -338,22 +333,22 @@ public class NavigableMapCheck { s2.putAll(s); } catch (Exception e) { e.printStackTrace(); return; } - timer.finish(); - + timer.finish(); + timer.start("Iter Equals ", size * 2); boolean eqt = s2.equals(s) && s.equals(s2); - reallyAssert (eqt); - timer.finish(); + reallyAssert(eqt); + timer.finish(); timer.start("Iter HashCode ", size * 2); int shc = s.hashCode(); int s2hc = s2.hashCode(); - reallyAssert (shc == s2hc); - timer.finish(); + reallyAssert(shc == s2hc); + timer.finish(); timer.start("Put (present) ", size); s2.putAll(s); - timer.finish(); + timer.finish(); timer.start("Iter EntrySet contains ", size * 2); Set es2 = s2.entrySet(); @@ -362,8 +357,8 @@ public class NavigableMapCheck { Object entry = i1.next(); if (es2.contains(entry)) ++sum; } - timer.finish(); - reallyAssert (sum == size); + timer.finish(); + reallyAssert(sum == size); t6("Get ", size, s2, key, absent); @@ -371,14 +366,14 @@ public class NavigableMapCheck { s2.put(key[size-1], absent[0]); timer.start("Iter Equals ", size * 2); eqt = s2.equals(s) && s.equals(s2); - reallyAssert (!eqt); - timer.finish(); + reallyAssert(!eqt); + timer.finish(); timer.start("Iter HashCode ", size * 2); int s1h = s.hashCode(); int s2h = s2.hashCode(); - reallyAssert (s1h != s2h); - timer.finish(); + reallyAssert(s1h != s2h); + timer.finish(); s2.put(key[size-1], hold); timer.start("Remove (iterator) ", size * 2); @@ -386,18 +381,18 @@ public class NavigableMapCheck { Set es = s.entrySet(); while (s2i.hasNext()) es.remove(s2i.next()); - timer.finish(); + timer.finish(); - reallyAssert (s.isEmpty()); + reallyAssert(s.isEmpty()); timer.start("Clear ", size); s2.clear(); - timer.finish(); - reallyAssert (s2.isEmpty() && s.isEmpty()); + timer.finish(); + reallyAssert(s2.isEmpty() && s.isEmpty()); } - + static void test(NavigableMap s, Integer[] key) { int size = key.length; @@ -436,14 +431,14 @@ public class NavigableMapCheck { private String cname; 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()); - Stats stats = ((Stats)(e.getValue())); + Map.Entry e = (Map.Entry) it.next(); + Stats stats = (Stats) e.getValue(); int n = stats.number; double t; - if (n > 0) + if (n > 0) t = stats.sum / n; else t = stats.least; @@ -451,14 +446,14 @@ public class NavigableMapCheck { System.out.println(e.getKey() + ": " + nano); } } - + void start(String name, long numOps) { this.name = name; this.cname = classify(); this.numOps = numOps; startTime = System.currentTimeMillis(); } - + String classify() { if (name.startsWith("Get")) @@ -469,14 +464,14 @@ public class NavigableMapCheck { return "Remove "; else if (name.startsWith("Iter")) return "Iter "; - else + else return null; } void finish() { long endTime = System.currentTimeMillis(); long time = endTime - startTime; - double timePerOp = ((double)time)/numOps; + double timePerOp = (double) time /numOps; Object st = accum.get(name); if (st == null) @@ -511,7 +506,7 @@ public class NavigableMapCheck { Stats(double t) { least = t; } } - static Random rng = new Random(); + static Random rng = new Random(111); static void shuffle(Integer[] keys) { int size = keys.length; @@ -524,4 +519,3 @@ public class NavigableMapCheck { } } -