Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do recursive shrinking without recursive function calls #294

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,18 @@ impl<T: Testable,
$($name: Arbitrary + Debug),*> Testable for fn($($name),*) -> T {
#[allow(non_snake_case)]
fn result(&self, g: &mut Gen) -> TestResult {
fn shrink_failure<T: Testable, $($name: Arbitrary + Debug),*>(
g: &mut Gen,
self_: fn($($name),*) -> T,
a: ($($name,)*),
) -> Option<TestResult> {
for t in a.shrink() {
let self_ = *self;
let a: ($($name,)*) = Arbitrary::arbitrary(g);
let ( $($name,)* ) = a.clone();
let mut r = safe(move || {self_($($name),*)}).result(g);

{
let ( $(ref $name,)* ) = a;
r.arguments = debug_reprs(&[$($name),*]);
}
if r.is_failure() {
let mut a = a.shrink();
while let Some(t) = a.next() {
let ($($name,)*) = t.clone();
let mut r_new = safe(move || {self_($($name),*)}).result(g);
if r_new.is_failure() {
Expand All @@ -353,33 +359,18 @@ impl<T: Testable,
r_new.arguments = debug_reprs(&[$($name),*]);
}

// The shrunk value *does* witness a failure, so keep
// trying to shrink it.
let shrunk = shrink_failure(g, self_, t);
// The shrunk value *does* witness a failure, so remember
// it for now
r = r_new;

// If we couldn't witness a failure on any shrunk value,
// then return the failure we already have.
return Some(shrunk.unwrap_or(r_new))
// ... and switch over to that value, i.e. try to shrink
// it further.
a = t.shrink()
}
}
None
}

let self_ = *self;
let a: ($($name,)*) = Arbitrary::arbitrary(g);
let ( $($name,)* ) = a.clone();
let mut r = safe(move || {self_($($name),*)}).result(g);

{
let ( $(ref $name,)* ) = a;
r.arguments = debug_reprs(&[$($name),*]);
}
match r.status {
Pass|Discard => r,
Fail => {
shrink_failure(g, self_, a).unwrap_or(r)
}
}
r
}
}}}

Expand Down