1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// Compare floating-point values using an epsilon
pub trait NearlyEqual {
    fn nearly_equals(self, rhs: Self) -> bool;
}

impl NearlyEqual for f32 {
    fn nearly_equals(self, rhs: Self) -> bool {
        (self - rhs).abs() < std::f32::EPSILON
    }
}

impl<T> NearlyEqual for Option<T>
where
    T: NearlyEqual,
{
    fn nearly_equals(self, rhs: Self) -> bool {
        match (self, rhs) {
            (Some(a), Some(b)) => a.nearly_equals(b),
            (None, None) => true,
            _ => false,
        }
    }
}

/// Asserts that two expressions are nearly equal to each other (using [`NearlyEqual`]).
#[macro_export]
macro_rules! assert_nearly_eq {
    ($left:expr, $right:expr) => {{
        if !($left.nearly_equals($right)) {
            panic!(
                "assertion failed: `(left == right)`\nleft: `{:?}`,\nright: `{:?}`",
                $left, $right
            )
        }
    }};
}