Skip to content

Commit bdfd88c

Browse files
mkforsbcamshaft
andauthored
Add book section on controlling the number of operations generated (#246)
* 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]>
1 parent b3ca02f commit bdfd88c

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

book/src/features/structured-testing.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,50 @@ fn main() {
5959
}
6060
```
6161

62-
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:
62+
## Controlling The Number Of Operations Generated
63+
64+
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:
65+
66+
```rust
67+
# use bolero::{check, generator::*};
68+
use bolero::gen;
69+
# use my_set::MySet;
70+
71+
# #[derive(Debug, TypeGenerator)]
72+
# enum Operation {
73+
# Insert(u64),
74+
# Remove(u64),
75+
# Clear,
76+
# }
77+
#
78+
fn main() {
79+
check!()
80+
// Generate 0 to 200 operations
81+
.with_generator(gen::<Vec<Operation>>().with().len(0..=200))
82+
.for_each(|operations| {
83+
# let mut set = MySet::new();
84+
#
85+
# for operation in operations.iter() {
86+
# match operation {
87+
# Operation::Insert(value) => {
88+
# set.insert(value);
89+
# }
90+
# Operation::Remove(value) => {
91+
# set.remove(value);
92+
# }
93+
# Operation::Clear => {
94+
# set.clear();
95+
# }
96+
# }
97+
# }
98+
// assertions go here
99+
})
100+
}
101+
```
102+
103+
## Using Test Oracles
104+
105+
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:
63106

64107
```rust
65108
use bolero::{check, generator::*};

0 commit comments

Comments
 (0)