--- jsr166/src/test/loops/NavigableMapCheck.java 2006/04/19 15:10:47 1.3 +++ jsr166/src/test/loops/NavigableMapCheck.java 2015/08/10 03:13:33 1.15 @@ -1,14 +1,14 @@ /* * 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 + * http://creativecommons.org/publicdomain/zero/1.0/ */ /** * @test * @synopsis Times and checks basic map operations */ -import java.util.*; import java.io.*; +import java.util.*; public class NavigableMapCheck { @@ -25,59 +25,54 @@ public class NavigableMapCheck { } public static void main(String[] args) throws Exception { - Class mapClass = null; + Class mapClass = null; int numTests = 50; int size = 50000; 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); - absentSize = 8; while (absentSize < size) absentSize <<= 1; 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); - for (int rep = 0; rep < numTests; ++rep) { runTest(newMap(mapClass), key); } TestTimer.printStats(); - } - static NavigableMap newMap(Class cl) { + 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); } } - static void runTest(NavigableMap s, Integer[] key) { shuffle(key); int size = key.length; @@ -95,8 +90,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) { @@ -105,8 +100,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) { @@ -115,8 +110,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) { @@ -125,8 +120,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) { @@ -135,8 +130,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) { @@ -146,8 +141,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) { @@ -157,8 +152,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) { @@ -167,11 +162,10 @@ 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); } - static void t9(NavigableMap s) { int sum = 0; int iters = 20; @@ -179,8 +173,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) { @@ -192,8 +186,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) { @@ -205,8 +199,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) { @@ -219,8 +213,8 @@ public class NavigableMapCheck { if (e != null) ++sum; } - timer.finish(); - reallyAssert (sum == iters); + timer.finish(); + reallyAssert(sum == iters); } static void floorTest(NavigableMap s) { @@ -233,11 +227,10 @@ public class NavigableMapCheck { if (e != null) ++sum; } - timer.finish(); - reallyAssert (sum == iters-1); + timer.finish(); + reallyAssert(sum == iters-1); } - static void ktest(NavigableMap s, int size, Integer[] key) { timer.start("ContainsKey ", size); Set ks = s.keySet(); @@ -245,41 +238,40 @@ 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); } - static void ittest1(NavigableMap s, int size) { 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) { @@ -292,27 +284,25 @@ 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 rittest(NavigableMap s, int size) { rittest1(s, size); // rittest2(s, size); } - static void rtest(NavigableMap s, int size) { timer.start("Remove (iterator) ", size); for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { it.next(); it.remove(); } - timer.finish(); + timer.finish(); } static void rvtest(NavigableMap s, int size) { @@ -321,10 +311,9 @@ public class NavigableMapCheck { it.next(); it.remove(); } - timer.finish(); + timer.finish(); } - static void dtest(NavigableMap s, int size, Integer[] key) { timer.start("Put (putAll) ", size * 2); NavigableMap s2 = null; @@ -333,22 +322,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(); @@ -357,8 +346,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); @@ -366,14 +355,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); @@ -381,18 +370,16 @@ 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; @@ -431,14 +418,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; @@ -446,14 +433,13 @@ 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")) @@ -464,14 +450,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) @@ -494,9 +480,7 @@ public class NavigableMapCheck { if (timePerOp < stats.least) stats.least = timePerOp; } } - } - } static class Stats { @@ -506,11 +490,11 @@ 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; - for (int i=size; i>1; i--) { + for (int i = size; i > 1; i--) { int r = rng.nextInt(i); Integer t = keys[i-1]; keys[i-1] = keys[r]; @@ -519,4 +503,3 @@ public class NavigableMapCheck { } } -