@@ -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]
1717macro_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]
2727fn 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)`\n left: {}\n right: {}\n ratio applied to right : {}\n acceptable range: {} - {}: {}" ,
45- left, right, ratio , lower_bound , upper_bound , panic_msg
44+ "assertion failed: `(left ≈ right)`\n left: {}\n right: {}\n relative difference : {}\n max allowed relative difference: {}\n : {}" ,
45+ left, right, rel_diff , max_rel_diff , panic_msg
4646 ) ,
4747 None => panic ! (
48- "assertion failed: `(left ~= right)`\n left: {}\n right: {}\n ratio applied to right : {}\n acceptable range: {} - {}" ,
49- left, right, ratio , lower_bound , upper_bound
48+ "assertion failed: `(left ≈ right)`\n left: {}\n right: {}\n relative difference : {}\n max 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)`\n left: 8\n right: 10\n ratio applied to right : 0.12 \n acceptable range: 9 - 11 "
84+ expected = "assertion failed: `(left ≈ right)`\n left: 8\n right: 10\n relative difference : 0.2 \n max 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)`\n left: 8 \n right: 10 \n ratio applied to right : 0.12 \n acceptable range: 9 - 11 : some extra info about the error"
92+ expected = "assertion failed: `(left ≈ right)`\n left: 17 \n right: 20 \n relative difference : 0.15 \n max 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