From 6781ae52a41188e82a0354d4725a7c2718830e45 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 4 Jun 2015 13:34:56 +1000 Subject: Don't convert to/from arrays/lists unnecessarily while running tests --- .../au/id/zancanaro/javacheck/ShrinkResult.java | 7 +++--- .../id/zancanaro/javacheck/junit/Properties.java | 28 ++++++++++------------ 2 files changed, 16 insertions(+), 19 deletions(-) (limited to 'src/main/java/au/id/zancanaro/javacheck') diff --git a/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java b/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java index 8776f4e..a28c2a5 100644 --- a/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java +++ b/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java @@ -1,13 +1,14 @@ package au.id.zancanaro.javacheck; import java.util.Arrays; +import java.util.List; public class ShrinkResult { - public final Object[] args; + public final List args; public final Throwable thrown; - public ShrinkResult(Object[] args, Throwable thrown) { - this.args = Arrays.copyOf(args, args.length); + public ShrinkResult(List args, Throwable thrown) { + this.args = args; this.thrown = thrown; } } diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java index ab56374..b0dd2cd 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -190,14 +190,10 @@ public class Properties extends BlockJUnit4ClassRunner { if (method.getParameterCount() == 0) { runTest(new Object[0]); } else { - @SuppressWarnings("unchecked") - Generator[] generators = (Generator[]) new Generator[method.getParameterCount()]; - int index = 0; - for (Type type : method.getGenericParameterTypes()) { - generators[index++] = this.generators.get(type); - } - @SuppressWarnings("unchecked") - Generator generator = Generator.tuple((Generator[]) generators).map(List::toArray); + Generator[] generators = Arrays.stream(method.getGenericParameterTypes()) + .map(this.generators::get) + .toArray(Generator[]::new); + Generator> generator = Generator.tuple(generators); long seed = getSeed(method); Random random = new Random(seed); @@ -208,9 +204,9 @@ public class Properties extends BlockJUnit4ClassRunner { int numTests = property.runs(); for (int i = 0; i < numTests; ++i) { int size = Math.min(i + 1, maxSize); - ShrinkTree tree = generator.generate(random, size); + ShrinkTree> tree = generator.generate(random, size); try { - runTest(tree.getValue()); + runTest(tree.getValue().toArray()); assumptionsViolated = 0; } catch (AssumptionViolatedException ex) { numTests++; @@ -225,7 +221,7 @@ public class Properties extends BlockJUnit4ClassRunner { } } - private ShrinkResult shrink(ShrinkTree failed, Throwable originalEx) { + private ShrinkResult shrink(ShrinkTree> failed, Throwable originalEx) { // this array is a mutable container so the shutdown handler can see the new version ShrinkResult[] smallest = new ShrinkResult[]{ new ShrinkResult(failed.getValue(), originalEx)}; @@ -233,18 +229,18 @@ public class Properties extends BlockJUnit4ClassRunner { Thread shutdownHandler = makeShutdownHandler(smallest, originalEx); Runtime.getRuntime().addShutdownHook(shutdownHandler); - Iterator> trees = failed.getChildren(); + Iterator>> trees = failed.getChildren(); Set> seenArgs = new HashSet<>(); while (trees.hasNext()) { - ShrinkTree tree = trees.next(); + ShrinkTree> tree = trees.next(); if (seenArgs.add(Arrays.asList(tree.getValue()))) { try { - runTest(tree.getValue()); + runTest(tree.getValue().toArray()); } catch (AssumptionViolatedException ex) { // ignore, because it's not useful } catch (Throwable ex) { smallest[0] = new ShrinkResult(tree.getValue(), ex); - Iterator> children = tree.getChildren(); + Iterator>> children = tree.getChildren(); if (children.hasNext()) { trees = children; } else { @@ -261,7 +257,7 @@ public class Properties extends BlockJUnit4ClassRunner { private Thread makeShutdownHandler(ShrinkResult[] smallest, Throwable originalException) { return new Thread(() -> { System.err.println("Signal received while shrinking.\n" + - "Current best shrink is: " + Arrays.toString(smallest[0].args) + "\n" + + "Current best shrink is: " + smallest[0].args + "\n" + "Shrinking exception: " + smallest[0].thrown + "\n" + "Originally was: " + originalException); }); -- cgit v1.2.3