Skip to content

Commit e823043

Browse files
committed
assert_approx_eq: make sure arg order doesn't matter
1 parent dace49d commit e823043

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

libwasmvm/src/test_utils.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,48 @@ use std::str::FromStr as _;
55

66
/// Asserts that two expressions are approximately equal to each other.
77
///
8-
/// The ratio argument defines how wide a range of values we accept, and is applied
9-
/// to the **second** (`right`) argument.
8+
/// The `max_rel_diff` argument defines the maximum relative difference
9+
/// of the `left` and `right` values.
1010
///
11-
/// On panic, this macro will print the values of the expressions with their
12-
/// debug representations, and info about the acceptable range.
11+
/// On panic, this macro will print the values of the arguments and
12+
/// the actual relative difference.
1313
///
1414
/// Like [`assert_eq!`], this macro has a second form, where a custom
1515
/// panic message can be provided.
1616
#[macro_export]
1717
macro_rules! assert_approx_eq {
18-
($left:expr, $right:expr, $ratio:expr $(,)?) => {{
19-
$crate::test_utils::assert_approx_eq_impl($left, $right, $ratio, None);
18+
($left:expr, $right:expr, $max_rel_diff:expr $(,)?) => {{
19+
$crate::test_utils::assert_approx_eq_impl($left, $right, $max_rel_diff, None);
2020
}};
21-
($left:expr, $right:expr, $ratio:expr, $($args:tt)+) => {{
22-
$crate::test_utils::assert_approx_eq_impl($left, $right, $ratio, Some(format!($($args)*)));
21+
($left:expr, $right:expr, $max_rel_diff:expr, $($args:tt)+) => {{
22+
$crate::test_utils::assert_approx_eq_impl($left, $right, $max_rel_diff, Some(format!($($args)*)));
2323
}};
2424
}
2525

2626
#[track_caller]
2727
fn assert_approx_eq_impl<U: Into<Uint128>>(
2828
left: U,
2929
right: U,
30-
ratio: &str,
30+
max_rel_diff: &str,
3131
panic_msg: Option<String>,
3232
) {
3333
let left = left.into();
3434
let right = right.into();
35-
let ratio = Decimal::from_str(ratio).unwrap();
35+
let max_rel_diff = Decimal::from_str(max_rel_diff).unwrap();
3636

37-
let delta = right * ratio;
38-
let lower_bound = right - delta;
39-
let upper_bound = right + delta;
37+
let largest = std::cmp::max(left, right);
38+
let smallest = std::cmp::min(left, right);
39+
let rel_diff = Decimal::from_ratio(largest - smallest, largest);
4040

41-
if !(left >= lower_bound && left <= upper_bound) {
41+
if rel_diff > max_rel_diff {
4242
match panic_msg {
4343
Some(panic_msg) => panic!(
44-
"assertion failed: `(left ~= right)`\nleft: {}\nright: {}\nratio applied to right: {}\nacceptable range: {} - {}: {}",
45-
left, right, ratio, lower_bound, upper_bound, panic_msg
44+
"assertion failed: `(left right)`\nleft: {}\nright: {}\nrelative difference: {}\nmax allowed relative difference: {}\n: {}",
45+
left, right, rel_diff, max_rel_diff, panic_msg
4646
),
4747
None => panic!(
48-
"assertion failed: `(left ~= right)`\nleft: {}\nright: {}\nratio applied to right: {}\nacceptable range: {} - {}",
49-
left, right, ratio, lower_bound, upper_bound
48+
"assertion failed: `(left right)`\nleft: {}\nright: {}\nrelative difference: {}\nmax allowed relative difference: {}\n",
49+
left, right, rel_diff, max_rel_diff
5050
),
5151
}
5252
}
@@ -81,20 +81,20 @@ mod tests {
8181

8282
#[test]
8383
#[should_panic(
84-
expected = "assertion failed: `(left ~= right)`\nleft: 8\nright: 10\nratio applied to right: 0.12\nacceptable range: 9 - 11"
84+
expected = "assertion failed: `(left right)`\nleft: 8\nright: 10\nrelative difference: 0.2\nmax allowed relative difference: 0.12\n"
8585
)]
8686
fn assert_approx_fail() {
8787
assert_approx_eq!(8_u32, 10_u32, "0.12");
8888
}
8989

9090
#[test]
9191
#[should_panic(
92-
expected = "assertion failed: `(left ~= right)`\nleft: 8\nright: 10\nratio applied to right: 0.12\nacceptable range: 9 - 11: some extra info about the error"
92+
expected = "assertion failed: `(left right)`\nleft: 17\nright: 20\nrelative difference: 0.15\nmax allowed relative difference: 0.12\n: some extra info about the error"
9393
)]
9494
fn assert_approx_with_custom_panic_msg() {
9595
assert_approx_eq!(
96-
8_u32,
97-
10_u32,
96+
17_u32,
97+
20_u32,
9898
"0.12",
9999
"some extra {} about the error",
100100
"info"

0 commit comments

Comments
 (0)