--- jsr166/src/test/loops/NavigableSetCheck.java 2009/10/29 23:09:07 1.3 +++ jsr166/src/test/loops/NavigableSetCheck.java 2015/08/10 03:13:33 1.13 @@ -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 set operations */ -import java.util.*; import java.io.*; +import java.util.*; public class NavigableSetCheck { @@ -25,19 +25,18 @@ public class NavigableSetCheck { } public static void main(String[] args) throws Exception { - Class setClass = null; + Class setClass = null; int numTests = 50; int size = 50000; if (args.length > 0) { try { setClass = 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 NavigableSetCheck { System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size); - absentSize = 8; while (absentSize < size) absentSize <<= 1; absentMask = absentSize - 1; @@ -59,25 +57,22 @@ public class NavigableSetCheck { for (int i = 0; i < size; ++i) key[i] = new Integer(i * 2 + 1); - for (int rep = 0; rep < numTests; ++rep) { runTest(newSet(setClass), key); } TestTimer.printStats(); - } - static NavigableSet newSet(Class cl) { + static NavigableSet newSet(Class cl) { try { - NavigableSet m = (NavigableSet)cl.newInstance(); + NavigableSet m = (NavigableSet) cl.newInstance(); return m; - } catch(Exception e) { + } catch (Exception e) { throw new RuntimeException("Can't instantiate " + cl + ": " + e); } } - static void runTest(NavigableSet s, Integer[] key) { shuffle(key); int size = key.length; @@ -96,7 +91,7 @@ public class NavigableSetCheck { } } timer.finish(); - reallyAssert (sum == expect * iters); + reallyAssert(sum == expect * iters); } static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) { @@ -106,7 +101,7 @@ public class NavigableSetCheck { if (s.remove(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) { @@ -116,7 +111,7 @@ public class NavigableSetCheck { if (s.add(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) { @@ -126,7 +121,7 @@ public class NavigableSetCheck { if (s.contains(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) { @@ -136,7 +131,7 @@ public class NavigableSetCheck { if (s.remove(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { @@ -147,7 +142,7 @@ public class NavigableSetCheck { if (s.contains(k2[i & absentMask])) ++sum; } timer.finish(); - reallyAssert (sum == n); + reallyAssert(sum == n); } static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { @@ -158,7 +153,7 @@ public class NavigableSetCheck { if (s.contains(k2[i & absentMask])) ++sum; } timer.finish(); - reallyAssert (sum == n); + reallyAssert(sum == n); } static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) { @@ -168,10 +163,9 @@ public class NavigableSetCheck { if (s.contains(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } - static void higherTest(NavigableSet s) { int sum = 0; int iters = s.size(); @@ -182,7 +176,7 @@ public class NavigableSetCheck { e = s.higher(e); } timer.finish(); - reallyAssert (sum == iters); + reallyAssert(sum == iters); } static void lowerTest(NavigableSet s) { @@ -195,7 +189,7 @@ public class NavigableSetCheck { e = s.higher(e); } timer.finish(); - reallyAssert (sum == iters); + reallyAssert(sum == iters); } static void ceilingTest(NavigableSet s) { @@ -209,7 +203,7 @@ public class NavigableSetCheck { ++sum; } timer.finish(); - reallyAssert (sum == iters); + reallyAssert(sum == iters); } static void floorTest(NavigableSet s) { @@ -223,10 +217,9 @@ public class NavigableSetCheck { ++sum; } timer.finish(); - reallyAssert (sum == iters-1); + reallyAssert(sum == iters-1); } - static void ktest(NavigableSet s, int size, Integer[] key) { timer.start("Contains ", size); int sum = 0; @@ -234,19 +227,18 @@ public class NavigableSetCheck { if (s.contains(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == size); + reallyAssert(sum == size); } - static void ittest1(NavigableSet s, int size) { int sum = 0; timer.start("Iter Key ", size); for (Iterator it = s.iterator(); it.hasNext(); ) { - if(it.next() != MISSING) + if (it.next() != MISSING) ++sum; } timer.finish(); - reallyAssert (sum == size); + reallyAssert(sum == size); } static void ittest(NavigableSet s, int size) { @@ -257,18 +249,17 @@ public class NavigableSetCheck { int sum = 0; timer.start("Desc Iter Key ", size); for (Iterator it = s.descendingIterator(); it.hasNext(); ) { - if(it.next() != MISSING) + if (it.next() != MISSING) ++sum; } timer.finish(); - reallyAssert (sum == size); + reallyAssert(sum == size); } static void rittest(NavigableSet s, int size) { rittest1(s, size); } - static void rtest(NavigableSet s, int size) { timer.start("Remove (iterator) ", size); for (Iterator it = s.iterator(); it.hasNext(); ) { @@ -278,8 +269,6 @@ public class NavigableSetCheck { timer.finish(); } - - static void dtest(NavigableSet s, int size, Integer[] key) { timer.start("Add (addAll) ", size * 2); NavigableSet s2 = null; @@ -292,13 +281,13 @@ public class NavigableSetCheck { 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("Add (present) ", size); @@ -312,25 +301,23 @@ public class NavigableSetCheck { timer.start("Iter Equals ", size * 2); eqt = s2.equals(s) && s.equals(s2); if (as2) - reallyAssert (!eqt); + reallyAssert(!eqt); timer.finish(); timer.start("Iter HashCode ", size * 2); int s1h = s.hashCode(); int s2h = s2.hashCode(); if (as2) - reallyAssert (s1h != s2h); + reallyAssert(s1h != s2h); timer.finish(); timer.start("Clear ", size); s.clear(); s2.clear(); timer.finish(); - reallyAssert (s2.isEmpty() && s.isEmpty()); + reallyAssert(s2.isEmpty() && s.isEmpty()); } - - static void test(NavigableSet s, Integer[] key) { int size = key.length; @@ -371,8 +358,8 @@ public class NavigableSetCheck { 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) @@ -391,7 +378,6 @@ public class NavigableSetCheck { startTime = System.currentTimeMillis(); } - String classify() { if (name.startsWith("Contains")) return "Contains "; @@ -408,7 +394,7 @@ public class NavigableSetCheck { 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) @@ -447,7 +433,7 @@ public class NavigableSetCheck { 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];