I'm porting the QuickCheck <http://www.yellosoft.us/quickcheck> unit test
library to Rust, and I need to find a way to do Lisp's (apply) or (curry).
Rust has a fantastic type system, but I'm not sure how to use templates to
write quickcheck::for_all. In a dynamic language, I wouldn't have to worry
about the types.
Example:
use rustcheck;
import rustcheck::*;
use std;
fn prop_even(x : int) -> bool {
ret x % 2 == 0;
}
fn gen_even() -> int {
let i : int = rustcheck::gen_int();
if i % 2 == 0 {
ret i;
}
else {
ret i + 1;
}
}
fn main() {
for_all(prop_even, [gen_int]);
for_all(prop_even, [gen_even]);
}
Desired Output:
$ make
$ ./example
*** Failed!
37
+++ OK, passed 100 tests.
In other words, for_all uses the passed in generators to create 100 random
test cases for the prop_even function. Because gen_int returns an odd
number 50% of the time, prop_even will quickly fail, and for_all will print
the offending value (e.g. 37).
Assuming gen_even really returns a random even integer, when for_all calls
is 100 times, every value passed to prop_even will result in true, and so
for_all reports that it passes 100 test cases.
Again, the type system is the problem: it's clearly wonderful and
declarative, like Haskell and Erlang, where QuickCheck originates, but I'm
not sure how to properly wield Rust's type system to port over QuickCheck's
for_all function.
Cheers,
Andrew Pennebaker
www.yellosoft.us
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev