commonlibsse_ng\macros/
assert.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2// Provided by cxx crate
3// https://github.com/dtolnay/cxx/blob/master/src/macros/assert.rs
4
5/// Compile time assertion.
6#[macro_export]
7#[doc(hidden)]
8macro_rules! const_assert_eq {
9    ($left:expr, $right:expr $(,)?) => {
10        const _: [(); $left] = [(); $right];
11    };
12}
13
14/// Custom macro to assert that two floating-point values are nearly equal.
15/// - origin: https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison
16///
17/// # Examples
18///
19/// ```
20/// commonlibsse_ng::assert_nearly_eq!(4.9303807e-32, 4.930381e-32);
21/// ```
22///
23/// - panic code
24/// ```should_panic
25/// commonlibsse_ng::assert_nearly_eq!(4.930381e-32, 4.9309825e-32, epsilon = 1e-4);
26/// ```
27#[macro_export]
28macro_rules! assert_nearly_eq {
29    // This variant takes both comparison values and optional named parameters (epsilon and abs_th)
30    ($left:expr, $right:expr, $(epsilon = $epsilon:expr),* $(, abs_th = $abs_th:expr)?) => {{
31        #[allow(unused_mut)]
32        let mut epsilon = 128.0 * f32::EPSILON;
33        #[allow(unused_mut)]
34        let mut abs_th = f32::MIN;
35
36        $(
37            if stringify!($epsilon) == "epsilon" {
38                epsilon = $epsilon;
39            }
40        )*
41        $(
42            #[allow(clippy::multi_assignments)]
43            if stringify!($abs_th) == "abs_th" {
44                abs_th = $abs_th;
45            }
46        )*
47
48        if !$crate::re::NiMath::nearly_equal($left, $right, $crate::re::NiMath::ComparisonOptions::new(epsilon, abs_th)) {
49            panic!(
50                "Assertion failed: (left: {}, right: {}) are not nearly equal with epsilon: {}, abs_th: {}",
51                $left, $right, epsilon, abs_th
52            );
53        }
54    }};
55    ($left:expr, $right:expr) => {
56        $crate::assert_nearly_eq!($left, $right, epsilon = 128.0 * f32::EPSILON, abs_th = f32::MIN);
57    };
58}