Repository: camshaft/bolero (c)

Issue:
Integration with arbitrary is broken on bolero 0.11.x
On bolero 0.11:

This test:
```
        bolero::check!()
            .with_arbitrary::<String>()
            .cloned()
            .for_each(|input| {
                println!("{:?}", input);
                true
            });
```

gives only empty strings, but if changing `with_arbitrary` to `with_type`, it gives random strings.

Pull request:
fix arbitrary generators
Fixes #237

Patch:
diff --git a/lib/bolero-generator/src/alloc/mod.rs b/lib/bolero-generator/src/alloc/mod.rs
index 88b46c7..382b717 100644
--- a/lib/bolero-generator/src/alloc/mod.rs
+++ b/lib/bolero-generator/src/alloc/mod.rs
@@ -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]
diff --git a/lib/bolero-generator/src/alloc/string.rs b/lib/bolero-generator/src/alloc/string.rs
index 67702fc..6d0d5c7 100644
--- a/lib/bolero-generator/src/alloc/string.rs
+++ b/lib/bolero-generator/src/alloc/string.rs
@@ -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");
diff --git a/lib/bolero-generator/src/arbitrary.rs b/lib/bolero-generator/src/arbitrary.rs
index 17b7a28..7f75043 100644
--- a/lib/bolero-generator/src/arbitrary.rs
+++ b/lib/bolero-generator/src/arbitrary.rs
@@ -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)]
@@ -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());
     }
 }
diff --git a/lib/bolero-generator/src/driver/rng.rs b/lib/bolero-generator/src/driver/rng.rs
index 6b9fd29..3512dfc 100644
--- a/lib/bolero-generator/src/driver/rng.rs
+++ b/lib/bolero-generator/src/driver/rng.rs
@@ -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);
 
diff --git a/lib/bolero-generator/src/std/mod.rs b/lib/bolero-generator/src/std/mod.rs
index 906dd1b..ea61918 100644
--- a/lib/bolero-generator/src/std/mod.rs
+++ b/lib/bolero-generator/src/std/mod.rs
@@ -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]


Test patch:
diff --git a/lib/bolero-generator/src/testing.rs b/lib/bolero-generator/src/testing.rs
index 3bf41e3..8235bae 100644
--- a/lib/bolero-generator/src/testing.rs
+++ b/lib/bolero-generator/src/testing.rs
@@ -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()
@@ -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;
 
@@ -56,6 +41,7 @@ macro_rules! generator_test {
                     value, mutated,
                     "a mutation with the same input should produce the original"
                 );
+                results.push(value);
             } else {
                 failed += 1;
             }
@@ -63,7 +49,7 @@ macro_rules! generator_test {
 
         assert_ne!(failed, inputs.len(), "all the inputs failed");
 
-        ValueGenerator::generate(&gen, &mut rng_driver)
+        results
     }};
 }