camshaft / bolero

property testing and verification front-end for Rust
https://camshaft.github.io/bolero
MIT License
181 stars 14 forks source link

Uniform never generates upper value #199

Closed glebpom closed 8 months ago

glebpom commented 8 months ago

This also affects one_of, which never generates the last variant.

I think the bug is in this line:

https://github.com/camshaft/bolero/blob/ed2f9f5d49b277e14737aeef6c07af00118c43c3/lib/bolero-generator/src/uniform.rs#L96

let value = value % range;

should be replaced with

let value = value % range.saturating_add(1);
camshaft commented 8 months ago

That's a great catch! I will get a fix out for this shortly, along with a test showing the fix.

camshaft commented 8 months ago

For posterity: the bug only applies to Forced driver mode, which is used in the random engine. The Direct mode is still correct:

https://github.com/camshaft/bolero/blob/3d89deb8f221f6c4b56dead2e3c9e295dac7b2a9/lib/bolero-generator/src/uniform.rs#L45-L48

Also there's a special case for either mode that pulls bytes directly from the rng if the range is unbounded:

https://github.com/camshaft/bolero/blob/3d89deb8f221f6c4b56dead2e3c9e295dac7b2a9/lib/bolero-generator/src/uniform.rs#L27-L33