-
Notifications
You must be signed in to change notification settings - Fork 132
assert_approx_eq
macro for nicer testing
#352
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1c01f7a
wasmvm: assert_approx_eq macro
uint c00dc5c
Refactor assert_approx_eq!
uint a2d0485
assert_approx_eq! - panic msgs closer to std
uint 80eb9eb
cargo fmt
uint 926d2ca
assert_approx_eq: require args to be of one type
uint 22c1cd4
assert_approx_eq: test more types
uint c883151
assert_approx_eq: make sure arg order doesn't matter
uint c276a8e
test refactor
uint 765b15e
assert_approx_eq! - use abs_diff
uint File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ mod iterator; | |
mod memory; | ||
mod querier; | ||
mod storage; | ||
mod test_utils; | ||
mod tests; | ||
mod version; | ||
|
||
|
This file contains hidden or 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,102 @@ | ||
#![cfg(test)] | ||
|
||
use cosmwasm_std::{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::test_utils::assert_approx_eq_impl($left, $right, $max_rel_diff, None); | ||
}}; | ||
($left:expr, $right:expr, $max_rel_diff:expr, $($args:tt)+) => {{ | ||
$crate::test_utils::assert_approx_eq_impl($left, $right, $max_rel_diff, Some(format!($($args)*))); | ||
}}; | ||
} | ||
|
||
#[track_caller] | ||
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 | ||
), | ||
} | ||
} | ||
} | ||
|
||
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" | ||
)] | ||
fn assert_approx_with_custom_panic_msg() { | ||
assert_approx_eq!( | ||
17_u32, | ||
20_u32, | ||
"0.12", | ||
"some extra {} about the error", | ||
"info" | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.