diff --git a/README.md b/README.md index f5b0f57..6d7a85f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +quickcheck +========== QuickCheck is a way to do property based testing using randomly generated input. This crate comes with the ability to randomly generate and shrink integers, floats, tuples, booleans, lists, strings, options and results. @@ -359,6 +361,35 @@ and the whole idea of property checking is to take that burden off the programmer. Despite the theoretical discomfort, this approach can turn out to be practical. +### Generating Structs + +It is very simple to generate structs in QuickCheck. Consider the following +example, where the struct `Point` is defined: + +```rust +struct Point { + x: i32, + y: i32, +} +``` + +In order to generate a random `Point` instance, you need to implement +the trait `Arbitrary` for the struct `Point`: + +```rust +use quickcheck::{Arbitrary, Gen}; + +impl Arbitrary for Point { + fn arbitrary(g: &mut G) -> Point { + Point { + x: i32::arbitrary(g), + y: i32::arbitrary(g), + } + } +} +``` + + ### Case study: The Sieve of Eratosthenes The [Sieve of Eratosthenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)