BurntSushi / quickcheck

Automated property based testing for Rust (with shrinking).
The Unlicense
2.4k stars 149 forks source link

Better way to implement arbitrary with floats #229

Closed JoshMcguigan closed 5 years ago

JoshMcguigan commented 5 years ago

I have a custom Arbitrary impl for a struct which is made up of four floats. Is there a reason Gen doesn't expose any methods for directly creating float values? I'd be happy to implement this if you'd be willing to add this.

    impl Arbitrary for Q {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            Q {
                q1: g.next_u32() as f32 / core::u32::MAX as f32 * 2. - 1.,
                q2: g.next_u32() as f32 / core::u32::MAX as f32 * 2. - 1.,
                q3: g.next_u32() as f32 / core::u32::MAX as f32 * 2. - 1.,
                q4: g.next_u32() as f32 / core::u32::MAX as f32 * 2. - 1.,
            }
        }
    }
BurntSushi commented 5 years ago

Anything that implements Gen also implements RngCore and, consequently, Rng, which has a gen method that should do what you want.

JoshMcguigan commented 5 years ago

Thanks for the quick response. For future readers, in order to use gen I needed to add use rand::Rng; and add rand as a dependency of my crate.