Created
September 12, 2016 01:16
-
-
Save awwaiid/d55434447e5503d48a252bf2eff53313 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(×two, &is-even); | |
say Test::QuickCheck::verify(×three, &is-even); | |
say Test::QuickCheck::verify(×three, * %% 2); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment