slowCheck: handle exceptions from generators

This commit is contained in:
peter
2017-06-30 09:29:42 +02:00
parent f4076b4ad7
commit 288e99245d
5 changed files with 70 additions and 11 deletions
@@ -0,0 +1,11 @@
package slowCheck;
/**
* @author peter
*/
public class GeneratorException extends RuntimeException {
GeneratorException(long seed, Throwable cause) {
super("Exception while generating data, seed=" + seed, cause);
}
}
+20 -2
View File
@@ -79,7 +79,13 @@ public class PropertyChecker<T> {
private CounterExampleImpl<T> findCounterExample(int sizeHint) {
for (int i = 0; i < 100; i++) {
StructureNode node = new StructureNode();
T value = generator.generateUnstructured(new GenerativeDataStructure(random, node, sizeHint));
T value;
try {
value = generator.generateUnstructured(new GenerativeDataStructure(random, node, sizeHint));
}
catch (Throwable e) {
throw new GeneratorException(seed, e);
}
if (!generatedHashes.add(node.hashCode())) continue;
return CounterExampleImpl.checkProperty(property, value, node);
@@ -92,12 +98,18 @@ public class PropertyChecker<T> {
private CounterExampleImpl<T> minimized;
private int totalSteps;
private int sizeHint;
private Throwable stoppingReason;
PropertyFailureImpl(@NotNull CounterExampleImpl<T> initial, int sizeHint) {
this.initial = initial;
this.minimized = initial;
this.sizeHint = sizeHint;
shrink();
try {
shrink();
}
catch (Throwable e) {
stoppingReason = e;
}
}
@NotNull
@@ -112,6 +124,12 @@ public class PropertyChecker<T> {
return minimized;
}
@Nullable
@Override
public Throwable getStoppingReason() {
return stoppingReason;
}
@Override
public int getTotalMinimizationStepCount() {
return totalSteps;
@@ -12,6 +12,9 @@ public interface PropertyFailure<T> {
@NotNull
CounterExample<T> getMinimalCounterexample();
@Nullable
Throwable getStoppingReason();
int getTotalMinimizationStepCount();
@@ -1,6 +1,8 @@
package slowCheck;
import static slowCheck.Generator.from;
import static slowCheck.Generator.integers;
import static slowCheck.Generator.listsOf;
/**
* @author peter
@@ -48,4 +50,38 @@ public class ExceptionTest extends PropertyCheckerTestCase {
}
}
public void testExceptionWhileGeneratingValue() {
try {
PropertyChecker.forAll(ourTestSettings, from(data -> {
throw new AssertionError("fail");
}), i -> true);
fail();
}
catch (GeneratorException ignore) {
}
}
public void testExceptionWhileShrinkingValue() {
try {
PropertyChecker.forAll(ourTestSettings, listsOf(integers()).suchThat(l -> {
if (l.size() == 1 && l.get(0) == 0) throw new RuntimeException("my exception");
return true;
}), l -> l.stream().allMatch(i -> i > 0));
fail();
}
catch (PropertyFalsified e) {
assertEquals("my exception", e.getFailure().getStoppingReason().getMessage());
}
}
public void testUnsatisfiableSuchThat() {
try {
PropertyChecker.forAll(integers(-1, 1).suchThat(i -> i > 2), i -> i == 0);
fail();
}
catch (GeneratorException e) {
assertTrue(e.getCause() instanceof CannotSatisfyCondition);
}
}
}
@@ -75,15 +75,6 @@ public class GeneratorTest extends PropertyCheckerTestCase {
PropertyChecker.forAll(integers().suchThat(i -> i < 0), i -> i < 0);
}
public void testUnsatisfiableSuchThat() {
try {
PropertyChecker.forAll(integers(-1, 1).suchThat(i -> i > 2), i -> i == 0);
fail();
}
catch (CannotSatisfyCondition ignored) {
}
}
public void testStringOfStringChecksAllChars() {
checkFalsified(stringsOf("abc "),
s -> !s.contains(" "),