-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1423 from CosmWasm/assert_approx_eq
Add macro cosmwasm_std::assert_approx_eq!
- v2.2.1
- v2.2.0
- v2.2.0-rc.3
- v2.2.0-rc.2
- v2.2.0-rc.1
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.1.0-rc.1
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-rc.1
- v2.0.0-rc.0
- v2.0.0-beta.1
- v2.0.0-beta.0
- v1.5.10
- v1.5.9
- v1.5.8
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.5.0-rc.0
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.4.0-rc.1
- v1.4.0-beta.1
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.3.0-rc.0
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.2.0-rc.1
- v1.2.0-beta.1
- v1.2.0-beta.0
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
Showing
8 changed files
with
145 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::{Decimal, Uint128}; | ||
use std::str::FromStr as _; | ||
|
||
/// Asserts that two expressions are approximately equal to each other. | ||
/// | ||
/// The `max_rel_diff` argument defines the maximum relative difference | ||
/// of the `left` and `right` values. | ||
/// | ||
/// On panic, this macro will print the values of the arguments and | ||
/// the actual relative difference. | ||
/// | ||
/// Like [`assert_eq!`], this macro has a second form, where a custom | ||
/// panic message can be provided. | ||
#[macro_export] | ||
macro_rules! assert_approx_eq { | ||
($left:expr, $right:expr, $max_rel_diff:expr $(,)?) => {{ | ||
$crate::testing::assert_approx_eq_impl($left, $right, $max_rel_diff, None); | ||
}}; | ||
($left:expr, $right:expr, $max_rel_diff:expr, $($args:tt)+) => {{ | ||
$crate::testing::assert_approx_eq_impl($left, $right, $max_rel_diff, Some(format!($($args)*))); | ||
}}; | ||
} | ||
|
||
/// Implementation for the [`cosmwasm_std::assert_approx_eq`] macro. This does not provide any | ||
/// stability guarantees and may change any time. | ||
#[track_caller] | ||
#[doc(hidden)] | ||
pub fn assert_approx_eq_impl<U: Into<Uint128>>( | ||
left: U, | ||
right: U, | ||
max_rel_diff: &str, | ||
panic_msg: Option<String>, | ||
) { | ||
let left = left.into(); | ||
let right = right.into(); | ||
let max_rel_diff = Decimal::from_str(max_rel_diff).unwrap(); | ||
|
||
let largest = std::cmp::max(left, right); | ||
let rel_diff = Decimal::from_ratio(left.abs_diff(right), largest); | ||
|
||
if rel_diff > max_rel_diff { | ||
match panic_msg { | ||
Some(panic_msg) => panic!( | ||
"assertion failed: `(left ≈ right)`\nleft: {}\nright: {}\nrelative difference: {}\nmax allowed relative difference: {}\n: {}", | ||
left, right, rel_diff, max_rel_diff, panic_msg | ||
), | ||
None => panic!( | ||
"assertion failed: `(left ≈ right)`\nleft: {}\nright: {}\nrelative difference: {}\nmax allowed relative difference: {}\n", | ||
left, right, rel_diff, max_rel_diff | ||
), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn assert_approx() { | ||
assert_approx_eq!(9_u32, 10_u32, "0.12"); | ||
assert_approx_eq!(9_u64, 10_u64, "0.12"); | ||
assert_approx_eq!( | ||
9_000_000_000_000_000_000_000_000_000_000_000_000_u128, | ||
10_000_000_000_000_000_000_000_000_000_000_000_000_u128, | ||
"0.10" | ||
); | ||
} | ||
|
||
#[test] | ||
fn assert_approx_with_vars() { | ||
let a = 66_u32; | ||
let b = 67_u32; | ||
assert_approx_eq!(a, b, "0.02"); | ||
|
||
let a = 66_u64; | ||
let b = 67_u64; | ||
assert_approx_eq!(a, b, "0.02"); | ||
|
||
let a = 66_u128; | ||
let b = 67_u128; | ||
assert_approx_eq!(a, b, "0.02"); | ||
} | ||
|
||
#[test] | ||
#[should_panic( | ||
expected = "assertion failed: `(left ≈ right)`\nleft: 8\nright: 10\nrelative difference: 0.2\nmax allowed relative difference: 0.12\n" | ||
)] | ||
fn assert_approx_fail() { | ||
assert_approx_eq!(8_u32, 10_u32, "0.12"); | ||
} | ||
|
||
#[test] | ||
#[should_panic( | ||
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: Foo(8)" | ||
)] | ||
fn assert_approx_with_custom_panic_msg() { | ||
let adjective = "extra"; | ||
#[derive(Debug)] | ||
struct Foo(u32); | ||
assert_approx_eq!( | ||
17_u32, | ||
20_u32, | ||
"0.12", | ||
"some {adjective} {} about the error: {:?}", | ||
"info", | ||
Foo(8), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![cfg(not(target_arch = "wasm32"))] | ||
|
||
// Exposed for testing only | ||
// Both unit tests and integration tests are compiled to native code, so everything in here does not need to compile to Wasm. | ||
|
||
mod assertions; | ||
mod mock; | ||
|
||
pub use assertions::assert_approx_eq_impl; | ||
|
||
#[cfg(feature = "staking")] | ||
pub use mock::StakingQuerier; | ||
pub use mock::{ | ||
digit_sum, mock_dependencies, mock_dependencies_with_balance, mock_dependencies_with_balances, | ||
mock_env, mock_info, mock_wasmd_attr, riffle_shuffle, BankQuerier, MockApi, MockQuerier, | ||
MockQuerierCustomHandlerResult, MockStorage, MOCK_CONTRACT_ADDR, | ||
}; | ||
#[cfg(feature = "stargate")] | ||
pub use mock::{ | ||
mock_ibc_channel, mock_ibc_channel_close_confirm, mock_ibc_channel_close_init, | ||
mock_ibc_channel_connect_ack, mock_ibc_channel_connect_confirm, mock_ibc_channel_open_init, | ||
mock_ibc_channel_open_try, mock_ibc_packet_ack, mock_ibc_packet_recv, mock_ibc_packet_timeout, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters