Skip to content

Commit

Permalink
Succeed early on unusual resize quickcheck case
Browse files Browse the repository at this point in the history
The quickcheck now exits early when checking resizing when it runs into
the case where we are limited by `u32::MAX`. These cases should still be
handled fine but in general should probably be covered by a separate
test. In particular we can no longer expect the new size to be exact in
the requested dimension.
  • Loading branch information
HeroicKatora committed Jan 31, 2022
1 parent 9a1e601 commit dee5590
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/math/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::cmp::max;
/// will either fill the dimensions to fit inside the smaller constraint
/// (will overflow the specified bounds on one axis to preserve
/// aspect ratio), or will shrink so that both dimensions are
/// completely contained with in the given `width` and `height`,
/// completely contained within the given `width` and `height`,
/// with empty space on one axis.
pub(crate) fn resize_dimensions(
width: u32,
Expand Down Expand Up @@ -44,16 +44,22 @@ mod test {
quickcheck! {
fn resize_bounds_correctly_width(old_w: u32, new_w: u32) -> bool {
if old_w == 0 || new_w == 0 { return true; }
if new_w as u64 * 400u64 >= old_w as u64 * u64::from(u32::MAX) { return true; }

let result = super::resize_dimensions(old_w, 400, new_w, ::std::u32::MAX, false);
result.0 == new_w && result.1 == (400 as f64 * new_w as f64 / old_w as f64).round() as u32
let exact = (400 as f64 * new_w as f64 / old_w as f64).round() as u32;
result.0 == new_w && result.1 == exact.max(1)
}
}

quickcheck! {
fn resize_bounds_correctly_height(old_h: u32, new_h: u32) -> bool {
if old_h == 0 || new_h == 0 { return true; }
if 400u64 * new_h as u64 >= old_h as u64 * u64::from(u32::MAX) { return true; }

let result = super::resize_dimensions(400, old_h, ::std::u32::MAX, new_h, false);
result.1 == new_h && result.0 == (400 as f64 * new_h as f64 / old_h as f64).round() as u32
let exact = (400 as f64 * new_h as f64 / old_h as f64).round() as u32;
result.1 == new_h && result.0 == exact.max(1)
}
}

Expand Down

0 comments on commit dee5590

Please sign in to comment.