Skip to content

Commit

Permalink
Add book section on controlling the number of operations generated (#246
Browse files Browse the repository at this point in the history
)

* doc: add book section on controlling number of operations generated

* Update book/src/features/structured-testing.md

Co-authored-by: Cameron Bytheway <[email protected]>

---------

Co-authored-by: Cameron Bytheway <[email protected]>
  • Loading branch information
mkforsb and camshaft authored Sep 28, 2024
1 parent b3ca02f commit bdfd88c
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion book/src/features/structured-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,50 @@ fn main() {
}
```

This basic test will make sure we don't panic on any of the list of operations. We can take it to the next step by using a test oracle to make sure the behavior of `MySet` is actually correct. Here we'll use `HashSet` from the `std` library:
## Controlling The Number Of Operations Generated

Using `check!().with_type::<Vec<T>>()` will generate vectors with lengths in the range `0..=64`. If you need more control over the number of elements being generated you can instead use `.with_generator` and provide a customized generator:

```rust
# use bolero::{check, generator::*};
use bolero::gen;
# use my_set::MySet;

# #[derive(Debug, TypeGenerator)]
# enum Operation {
# Insert(u64),
# Remove(u64),
# Clear,
# }
#
fn main() {
check!()
// Generate 0 to 200 operations
.with_generator(gen::<Vec<Operation>>().with().len(0..=200))
.for_each(|operations| {
# let mut set = MySet::new();
#
# for operation in operations.iter() {
# match operation {
# Operation::Insert(value) => {
# set.insert(value);
# }
# Operation::Remove(value) => {
# set.remove(value);
# }
# Operation::Clear => {
# set.clear();
# }
# }
# }
// assertions go here
})
}
```

## Using Test Oracles

The basic test we constructed above will make sure we don't panic on any of the list of operations. We can take it to the next step by using a test oracle to make sure the behavior of `MySet` is actually correct. Here we'll use `HashSet` from the `std` library:

```rust
use bolero::{check, generator::*};
Expand Down

0 comments on commit bdfd88c

Please sign in to comment.