Skip to content

Commit

Permalink
docs: add quick tip for how to impl Arbitrary manually
Browse files Browse the repository at this point in the history
Closes #242
  • Loading branch information
Fabian Spillner authored and BurntSushi committed Jan 11, 2020
1 parent e048896 commit 77f9dc7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 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.
Expand Down Expand Up @@ -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: Gen>(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)
Expand Down

0 comments on commit 77f9dc7

Please sign in to comment.