Skip to content

Instantly share code, notes, and snippets.

@awwaiid awwaiid/quick.p6
Created Sep 12, 2016

Embed
What would you like to do?
module Test::QuickCheck {
multi gen(Int) { 1000000.rand.Int }
our sub verify-one( &f, &predicate ) {
my @params = &f.signature.params.map: -> $p {
# say "Param: {$p.gist}";
gen($p.type)
};
my $result = &f(|@params);
# say "Result: {$result.gist}";
my $is-ok = so &predicate($result);
($is-ok, $result);
}
our sub verify(&f, &predicate) {
for ^1000 {
# return False unless verify-one(&f, &predicate);
my ($is-ok, $result) = verify-one(&f, &predicate);
if !$is-ok {
return (False, $result);
}
}
True;
}
}
sub is-even($n) { $n %% 2 }
sub timestwo(Int $n) { $n * 2 }
sub timesthree(Int $n) { $n * 3 }
say Test::QuickCheck::verify(&timestwo, &is-even);
say Test::QuickCheck::verify(&timesthree, &is-even);
say Test::QuickCheck::verify(&timesthree, * %% 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.