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 #2101

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Changes from 2 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
8 changes: 8 additions & 0 deletions packages/core/src/testing/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub fn assert_approx_eq_impl<U: Into<Uint128>>(
let max_rel_diff = Decimal::from_str(max_rel_diff).unwrap();

let largest = core::cmp::max(left, right);
if largest.is_zero() {
return;
}
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
let rel_diff = Decimal::from_ratio(left.abs_diff(right), largest);

if rel_diff > max_rel_diff {
Expand Down Expand Up @@ -128,6 +131,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