--- jsr166/src/test/loops/NavigableMapCheck.java 2009/10/29 23:09:07 1.5 +++ jsr166/src/test/loops/NavigableMapCheck.java 2016/10/23 03:03:23 1.16 @@ -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,19 +25,18 @@ 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) numTests = Integer.parseInt(args[1]); @@ -46,7 +45,6 @@ public class NavigableMapCheck { System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size); - absentSize = 8; while (absentSize < size) absentSize <<= 1; absentMask = absentSize - 1; @@ -59,25 +57,21 @@ public class NavigableMapCheck { 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(); - return m; - } catch(Exception e) { + return (NavigableMap) cl.getConstructor().newInstance(); + } catch (Exception e) { throw new RuntimeException("Can't instantiate " + cl + ": " + e); } } - static void runTest(NavigableMap s, Integer[] key) { shuffle(key); int size = key.length; @@ -96,7 +90,7 @@ public class NavigableMapCheck { } } timer.finish(); - reallyAssert (sum == expect * iters); + reallyAssert(sum == expect * iters); } static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -106,7 +100,7 @@ public class NavigableMapCheck { if (s.remove(key[i]) != null) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -116,7 +110,7 @@ public class NavigableMapCheck { if (s.put(key[i], absent[i & absentMask]) == null) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -126,7 +120,7 @@ public class NavigableMapCheck { if (s.containsKey(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -136,7 +130,7 @@ public class NavigableMapCheck { if (s.remove(key[i]) != null) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { @@ -147,7 +141,7 @@ public class NavigableMapCheck { if (s.get(k2[i & absentMask]) != null) ++sum; } timer.finish(); - reallyAssert (sum == n); + reallyAssert(sum == n); } static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { @@ -158,7 +152,7 @@ public class NavigableMapCheck { if (s.containsKey(k2[i & absentMask])) ++sum; } timer.finish(); - reallyAssert (sum == n); + reallyAssert(sum == n); } static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) { @@ -168,10 +162,9 @@ public class NavigableMapCheck { if (s.get(key[i]) != null) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } - static void t9(NavigableMap s) { int sum = 0; int iters = 20; @@ -180,7 +173,7 @@ public class NavigableMapCheck { for (int i = 0; i < absentSize; i += step) if (s.containsValue(absent[i])) ++sum; timer.finish(); - reallyAssert (sum != 0); + reallyAssert(sum != 0); } static void higherTest(NavigableMap s) { @@ -193,7 +186,7 @@ public class NavigableMapCheck { e = s.higherEntry(e.getKey()); } timer.finish(); - reallyAssert (sum == iters); + reallyAssert(sum == iters); } static void lowerTest(NavigableMap s) { @@ -206,7 +199,7 @@ public class NavigableMapCheck { e = s.higherEntry(e.getKey()); } timer.finish(); - reallyAssert (sum == iters); + reallyAssert(sum == iters); } static void ceilingTest(NavigableMap s) { @@ -220,7 +213,7 @@ public class NavigableMapCheck { ++sum; } timer.finish(); - reallyAssert (sum == iters); + reallyAssert(sum == iters); } static void floorTest(NavigableMap s) { @@ -234,10 +227,9 @@ public class NavigableMapCheck { ++sum; } timer.finish(); - reallyAssert (sum == iters-1); + reallyAssert(sum == iters-1); } - static void ktest(NavigableMap s, int size, Integer[] key) { timer.start("ContainsKey ", size); Set ks = s.keySet(); @@ -246,40 +238,39 @@ public class NavigableMapCheck { if (ks.contains(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == size); + 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); + 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); + 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); + reallyAssert(sum == size); } static void ittest(NavigableMap s, int size) { @@ -292,20 +283,18 @@ 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); + 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(); ) { @@ -324,12 +313,11 @@ public class NavigableMapCheck { timer.finish(); } - static void dtest(NavigableMap s, int size, Integer[] key) { timer.start("Put (putAll) ", size * 2); - NavigableMap s2 = null; + final NavigableMap s2; try { - s2 = (NavigableMap) (s.getClass().newInstance()); + s2 = (NavigableMap) s.getClass().getConstructor().newInstance(); s2.putAll(s); } catch (Exception e) { e.printStackTrace(); return; } @@ -337,13 +325,13 @@ public class NavigableMapCheck { timer.start("Iter Equals ", size * 2); boolean eqt = s2.equals(s) && s.equals(s2); - reallyAssert (eqt); + reallyAssert(eqt); timer.finish(); timer.start("Iter HashCode ", size * 2); int shc = s.hashCode(); int s2hc = s2.hashCode(); - reallyAssert (shc == s2hc); + reallyAssert(shc == s2hc); timer.finish(); timer.start("Put (present) ", size); @@ -358,7 +346,7 @@ public class NavigableMapCheck { if (es2.contains(entry)) ++sum; } timer.finish(); - reallyAssert (sum == size); + reallyAssert(sum == size); t6("Get ", size, s2, key, absent); @@ -366,13 +354,13 @@ 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); + reallyAssert(!eqt); timer.finish(); timer.start("Iter HashCode ", size * 2); int s1h = s.hashCode(); int s2h = s2.hashCode(); - reallyAssert (s1h != s2h); + reallyAssert(s1h != s2h); timer.finish(); s2.put(key[size-1], hold); @@ -383,16 +371,14 @@ public class NavigableMapCheck { es.remove(s2i.next()); timer.finish(); - reallyAssert (s.isEmpty()); + reallyAssert(s.isEmpty()); timer.start("Clear ", size); s2.clear(); timer.finish(); - reallyAssert (s2.isEmpty() && s.isEmpty()); + reallyAssert(s2.isEmpty() && s.isEmpty()); } - - static void test(NavigableMap s, Integer[] key) { int size = key.length; @@ -434,8 +420,8 @@ public class NavigableMapCheck { 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) @@ -454,7 +440,6 @@ public class NavigableMapCheck { startTime = System.currentTimeMillis(); } - String classify() { if (name.startsWith("Get")) return "Get "; @@ -471,7 +456,7 @@ public class NavigableMapCheck { 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 +479,7 @@ public class NavigableMapCheck { if (timePerOp < stats.least) stats.least = timePerOp; } } - } - } static class Stats { @@ -510,7 +493,7 @@ public class NavigableMapCheck { 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];