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

fix arbitrary generators #238

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions lib/bolero-generator/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ fn vec_type_test() {

#[test]
fn vec_with_len_test() {
let vec: Vec<u8> = generator_test!(gen::<Vec<u8>>().with().len(8usize)).unwrap();
assert_eq!(vec.len(), 8);
let results = generator_test!(gen::<Vec<u8>>().with().len(8usize));
assert!(results.into_iter().all(|v| v.len() == 8));
}

#[test]
Expand Down
7 changes: 3 additions & 4 deletions lib/bolero-generator/src/alloc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ fn string_type_test() {
#[test]
fn string_with_test() {
for _ in 0..100 {
if let Some(string) = generator_test!(gen::<String>().with().len(32usize)) {
assert_eq!(string.chars().count(), 32usize);
return;
}
let results = generator_test!(gen::<String>().with().len(32usize));
assert!(results.into_iter().any(|s| s.chars().count() == 32));
return;
}

panic!("failed to generate a valid string");
Expand Down
12 changes: 10 additions & 2 deletions lib/bolero-generator/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ mod tests {

#[test]
fn vec() {
let _ = generator_test!(gen_arbitrary::<Vec<usize>>());
let results = generator_test!(gen_arbitrary::<Vec<usize>>());
assert!(results.into_iter().any(|v| !v.is_empty()));
}

#[test]
fn string() {
let results = generator_test!(gen_arbitrary::<String>());
assert!(results.into_iter().any(|v| !v.is_empty()));
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -96,6 +103,7 @@ mod tests {

#[test]
fn randomly_valid() {
let _ = generator_test!(gen_arbitrary::<RandomlyValid>());
let results = generator_test!(gen_arbitrary::<RandomlyValid>());
assert!(!results.is_empty());
}
}
8 changes: 2 additions & 6 deletions lib/bolero-generator/src/driver/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,10 @@ impl<R: RngCore> Driver for Rng<R> {
{
let (min, max) = hint();

// the lower bound is bigger than what we have remaining
if min > self.remaining_len() {
return None;
}

let max = max
.unwrap_or(usize::MAX)
.min(min)
// make sure max is at least min
.max(min)
.min(self.remaining_len())
.min(Buffer::MAX_CAPACITY);

Expand Down
5 changes: 2 additions & 3 deletions lib/bolero-generator/src/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ fn hash_set_type_test() {

#[test]
fn hash_set_with_len_test() {
if let Some(set) = generator_test!(gen::<HashSet<u8>>().with().len(8usize)) {
assert_eq!(set.len(), 8);
}
let results = generator_test!(gen::<HashSet<u8>>().with().len(8usize));
assert!(results.into_iter().any(|r| r.len() == 8));
}

#[test]
Expand Down
30 changes: 8 additions & 22 deletions lib/bolero-generator/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ macro_rules! generator_test {

let options = Options::default();

let mut rng_driver = Rng::new(rand::thread_rng(), &options);
let mut rng_driver = Rng::new(
rand::thread_rng(),
&options.clone().with_max_len(8 * 1024 * 1024),
);

let mut results = vec![];

let inputs = $crate::gen::<Vec<_>>()
.with()
Expand All @@ -18,26 +23,6 @@ macro_rules! generator_test {
.generate(&mut rng_driver)
.unwrap();

{
for input in inputs.iter() {
if let Some(value) =
ValueGenerator::generate(&gen, &mut ByteSliceDriver::new(input, &options))
{
let mut mutated = value.clone();
ValueGenerator::mutate(
&gen,
&mut ByteSliceDriver::new(input, &options),
&mut mutated,
)
.expect("mutation with same driver should produce a value");
assert_eq!(
value, mutated,
"a mutation with the same input should produce the original"
);
}
}
}

// keep track of failed inputs and make sure they didn't all fail
let mut failed = 0;

Expand All @@ -56,14 +41,15 @@ macro_rules! generator_test {
value, mutated,
"a mutation with the same input should produce the original"
);
results.push(value);
} else {
failed += 1;
}
}

assert_ne!(failed, inputs.len(), "all the inputs failed");

ValueGenerator::generate(&gen, &mut rng_driver)
results
}};
}

Expand Down
Loading