diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-04 13:34:56 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-04 13:34:56 +1000 |
commit | 6781ae52a41188e82a0354d4725a7c2718830e45 (patch) | |
tree | fbb6fd4ff14a86c7a730e8c2f275c0bbecafc328 | |
parent | 8b09a3f3d46a1c54e1a5a9713cf6868c074e0f91 (diff) |
Don't convert to/from arrays/lists unnecessarily while running tests
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java | 7 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/junit/Properties.java | 28 |
2 files changed, 16 insertions, 19 deletions
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<Object> args; public final Throwable thrown; - public ShrinkResult(Object[] args, Throwable thrown) { - this.args = Arrays.copyOf(args, args.length); + public ShrinkResult(List<Object> 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<Object[]> generator = Generator.tuple((Generator<Object>[]) generators).map(List::toArray); + Generator<?>[] generators = Arrays.stream(method.getGenericParameterTypes()) + .map(this.generators::get) + .toArray(Generator[]::new); + Generator<List<Object>> 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<Object[]> tree = generator.generate(random, size); + ShrinkTree<List<Object>> 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<Object[]> failed, Throwable originalEx) { + private ShrinkResult shrink(ShrinkTree<List<Object>> 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<ShrinkTree<Object[]>> trees = failed.getChildren(); + Iterator<ShrinkTree<List<Object>>> trees = failed.getChildren(); Set<List<Object>> seenArgs = new HashSet<>(); while (trees.hasNext()) { - ShrinkTree<Object[]> tree = trees.next(); + ShrinkTree<List<Object>> 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<ShrinkTree<Object[]>> children = tree.getChildren(); + Iterator<ShrinkTree<List<Object>>> 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); }); |