Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid div with zero in assert_approx_eq (backport #2101) #2113

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/std/src/testing/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ pub fn assert_approx_eq_impl<U: Into<Uint128>>(
) {
let left = left.into();
let right = right.into();

if left == right {
// If both values are equal, we don't need to check the relative difference.
// We check this first to avoid division by zero below.
return;
}

let max_rel_diff = Decimal::from_str(max_rel_diff).unwrap();

let largest = core::cmp::max(left, right);
Expand Down Expand Up @@ -126,6 +133,11 @@ mod tests {
10_000_000_000_000_000_000_000_000_000_000_000_000_u128,
"0.10"
);
assert_approx_eq!(0_u32, 0_u32, "0.12");
assert_approx_eq!(1_u64, 0_u64, "1");
assert_approx_eq!(0_u64, 1_u64, "1");
assert_approx_eq!(5_u64, 0_u64, "1");
assert_approx_eq!(0_u64, 5_u64, "1");
}

#[test]
Expand Down
Loading