Skip to content

Commit

Permalink
impl: make sure a float's shrinker does not yield the original value
Browse files Browse the repository at this point in the history
For floating point values, we use a shrinker for signed integers and
convert the output to the float's type. The underlying shrinker is
bounded and never includes the original value in the output, i.e. it
behaves correctly. However, when converting the value to the target type
we may loose some information and thus yield the original value, again.

If the test happens to fail for such a value, we may end up endlessly
repeating the test with a sequence of values including the original one
over and over again. This change avoids the issue by applying an
additional filter.
  • Loading branch information
neithernut committed Aug 26, 2021
1 parent a00228c commit 0c279d6
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ macro_rules! float_arbitrary {
fn shrink(&self) -> Box<dyn Iterator<Item = $t>> {
signed_shrinker!($shrinkable);
let it = shrinker::SignedShrinker::new(*self as $shrinkable);
Box::new(it.map(|x| x as $t))
let old = *self;
Box::new(it.map(|x| x as $t).filter(move |x| *x != old))
}
}
)*};
Expand Down

0 comments on commit 0c279d6

Please sign in to comment.