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

assert_approx_eq macro for nicer testing #352

Merged
merged 9 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 6 additions & 7 deletions libwasmvm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ pub extern "C" fn release_cache(cache: *mut cache_t) {

#[cfg(test)]
mod tests {
use crate::assert_approx_eq;

use super::*;
use std::iter::FromIterator;
use tempfile::TempDir;
Expand Down Expand Up @@ -711,13 +713,10 @@ mod tests {
assert_eq!(misses, 0);
assert_eq!(elements_pinned_memory_cache, 1);
assert_eq!(elements_memory_cache, 0);
let expected = 5602873; // +/- 20%
assert!(
size_pinned_memory_cache > expected * 80 / 100,
"size_pinned_memory_cache: {size_pinned_memory_cache}"
);
assert!(
size_pinned_memory_cache < expected * 120 / 100,
assert_approx_eq!(
size_pinned_memory_cache,
5602873,
"0.2",
"size_pinned_memory_cache: {size_pinned_memory_cache}"
);
assert_eq!(size_memory_cache, 0);
Expand Down
1 change: 1 addition & 0 deletions libwasmvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod iterator;
mod memory;
mod querier;
mod storage;
mod test_utils;
mod tests;
mod version;

Expand Down
102 changes: 102 additions & 0 deletions libwasmvm/src/test_utils.rs
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");
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
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"
);
}
}