Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add book section on controlling the number of operations generated #246

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading