assertables/assert_f64/
assert_f64_ne.rs

1//! Assert a floating point 64-bit number is not equal to another within f64::EPSILON.
2//!
3//! Pseudocode:<br>
4//! a ≠ b
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a: f64 = 1.0 / 3.0;
12//! let b: f64 = 0.3333333333333339;
13//! assert_f64_ne!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_f64_ne`](macro@crate::assert_f64_ne)
19//! * [`assert_f64_ne_as_result`](macro@crate::assert_f64_ne_as_result)
20//! * [`debug_assert_f64_ne`](macro@crate::debug_assert_f64_ne)
21
22/// Assert two floating point numbers are equal within f64::EPSILON.
23///
24/// Pseudocode:<br>
25/// a ≠ b
26///
27/// * If true, return Result `Ok(())`.
28///
29/// * Otherwise, return Result `Err(message)`.
30///
31/// This macro is useful for runtime checks, such as checking parameters,
32/// or sanitizing inputs, or handling different results in different ways.
33///
34/// # Module macros
35///
36/// * [`assert_f64_ne`](macro@crate::assert_f64_ne)
37/// * [`assert_f64_ne_as_result`](macro@crate::assert_f64_ne_as_result)
38/// * [`debug_assert_f64_ne`](macro@crate::debug_assert_f64_ne)
39///
40#[macro_export]
41macro_rules! assert_f64_ne_as_result {
42    ($a:expr, $b:expr $(,)?) => {
43        match (&$a, &$b) {
44            (a, b) => {
45                if !(a >= b && a - b < f64::EPSILON) && !(a <= b && b - a < f64::EPSILON) {
46                    Ok(())
47                } else {
48                    Err(format!(
49                        concat!(
50                            "assertion failed: `assert_f64_ne!(a, b)`\n",
51                            "https://docs.rs/assertables/9.8.1/assertables/macro.assert_f64_ne.html\n",
52                            " a label: `{}`,\n",
53                            " a debug: `{}`,\n",
54                            " b label: `{}`,\n",
55                            " b debug: `{:?}`,\n",
56                            "    diff: `{}`,\n",
57                            "       ε: `{}`",
58                        ),
59                        stringify!($a),
60                        a,
61                        stringify!($b),
62                        b,
63                        a - b,
64                        f64::EPSILON,
65                    ))
66                }
67            }
68        }
69    };
70}
71
72#[cfg(test)]
73mod test_assert_f64_ne_as_result {
74    use crate::assert_f64::{EQ, EQ_GT, EQ_LT, GT, LT};
75    use std::sync::Once;
76
77    #[test]
78    fn lt() {
79        let a: f64 = EQ;
80        let b: f64 = GT;
81        for _ in 0..1 {
82            let actual = assert_f64_ne_as_result!(a, b);
83            assert_eq!(actual.unwrap(), ());
84        }
85    }
86
87    #[test]
88    fn lt_once() {
89        static A: Once = Once::new();
90        fn a() -> f64 {
91            if A.is_completed() {
92                panic!("A.is_completed()")
93            } else {
94                A.call_once(|| {})
95            }
96            EQ
97        }
98
99        static B: Once = Once::new();
100        fn b() -> f64 {
101            if B.is_completed() {
102                panic!("B.is_completed()")
103            } else {
104                B.call_once(|| {})
105            }
106            GT
107        }
108
109        assert_eq!(A.is_completed(), false);
110        assert_eq!(B.is_completed(), false);
111        let result = assert_f64_ne_as_result!(a(), b());
112        assert!(result.is_ok());
113        assert_eq!(A.is_completed(), true);
114        assert_eq!(B.is_completed(), true);
115    }
116
117    #[test]
118    fn gt() {
119        let a: f64 = EQ;
120        let b: f64 = LT;
121        for _ in 0..1 {
122            let actual = assert_f64_ne_as_result!(a, b);
123            assert_eq!(actual.unwrap(), ());
124        }
125    }
126
127    #[test]
128    fn gt_once() {
129        static A: Once = Once::new();
130        fn a() -> f64 {
131            if A.is_completed() {
132                panic!("A.is_completed()")
133            } else {
134                A.call_once(|| {})
135            }
136            EQ
137        }
138
139        static B: Once = Once::new();
140        fn b() -> f64 {
141            if B.is_completed() {
142                panic!("B.is_completed()")
143            } else {
144                B.call_once(|| {})
145            }
146            GT
147        }
148
149        assert_eq!(A.is_completed(), false);
150        assert_eq!(B.is_completed(), false);
151        let result = assert_f64_ne_as_result!(a(), b());
152        assert!(result.is_ok());
153        assert_eq!(A.is_completed(), true);
154        assert_eq!(B.is_completed(), true);
155    }
156
157    #[test]
158    fn eq() {
159        let a: f64 = EQ;
160        let b: f64 = EQ;
161        let actual = assert_f64_ne_as_result!(a, b);
162        let message = concat!(
163            "assertion failed: `assert_f64_ne!(a, b)`\n",
164            "https://docs.rs/assertables/9.8.1/assertables/macro.assert_f64_ne.html\n",
165            " a label: `a`,\n",
166            " a debug: `0.3333333333333333`,\n",
167            " b label: `b`,\n",
168            " b debug: `0.3333333333333333`,\n",
169            "    diff: `0`,\n",
170            "       ε: `0.0000000000000002220446049250313`",
171        );
172        assert_eq!(actual.unwrap_err(), message);
173    }
174
175    #[test]
176    fn eq_lt() {
177        let a: f64 = EQ;
178        let b: f64 = EQ_GT;
179        let actual = assert_f64_ne_as_result!(a, b);
180        let message = concat!(
181            "assertion failed: `assert_f64_ne!(a, b)`\n",
182            "https://docs.rs/assertables/9.8.1/assertables/macro.assert_f64_ne.html\n",
183            " a label: `a`,\n",
184            " a debug: `0.3333333333333333`,\n",
185            " b label: `b`,\n",
186            " b debug: `0.3333333333333335`,\n",
187            "    diff: `-0.00000000000000016653345369377348`,\n",
188            "       ε: `0.0000000000000002220446049250313`",
189        );
190        assert_eq!(actual.unwrap_err(), message);
191    }
192
193    #[test]
194    fn eq_gt() {
195        let a: f64 = EQ;
196        let b: f64 = EQ_LT;
197        let actual = assert_f64_ne_as_result!(a, b);
198        let message = concat!(
199            "assertion failed: `assert_f64_ne!(a, b)`\n",
200            "https://docs.rs/assertables/9.8.1/assertables/macro.assert_f64_ne.html\n",
201            " a label: `a`,\n",
202            " a debug: `0.3333333333333333`,\n",
203            " b label: `b`,\n",
204            " b debug: `0.3333333333333332`,\n",
205            "    diff: `0.00000000000000011102230246251565`,\n",
206            "       ε: `0.0000000000000002220446049250313`",
207        );
208        assert_eq!(actual.unwrap_err(), message);
209    }
210}
211
212/// Assert a floating point 64-bit number is not equal to another within f64::EPSILON.
213///
214/// Pseudocode:<br>
215/// a ≠ b
216///
217/// * If true, return `()`.
218///
219/// * Otherwise, call [`panic!`] with a message and the values of the
220///   expressions with their debug representations.
221///
222/// # Examples
223///
224/// ```rust
225/// use assertables::*;
226/// # use std::panic;
227///
228/// # fn main() {
229/// let a: f64 = 1.0 / 3.0;
230/// let b: f64 = 0.3333333333333338;
231/// assert_f64_ne!(a, b);
232///
233/// # let result = panic::catch_unwind(|| {
234/// // This will panic
235/// let a: f64 = 1.0 / 3.0;
236/// let b: f64 = 0.3333333333333333;
237/// assert_f64_ne!(a, b);
238/// # });
239/// // assertion failed: `assert_f64_ne!(a, b)`
240/// // https://docs.rs/assertables/…/assertables/macro.assert_f64_ne.html
241/// //  a label: `a`,
242/// //  a debug: `0.3333333333333333`,
243/// //  b label: `b`,
244/// //  b debug: `0.3333333333333333`,`
245/// //     diff: `0`,
246/// //        ε: `0.0000000000000002220446049250313`
247/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
248/// # let message = concat!(
249/// #     "assertion failed: `assert_f64_ne!(a, b)`\n",
250/// #     "https://docs.rs/assertables/9.8.1/assertables/macro.assert_f64_ne.html\n",
251/// #     " a label: `a`,\n",
252/// #     " a debug: `0.3333333333333333`,\n",
253/// #     " b label: `b`,\n",
254/// #     " b debug: `0.3333333333333333`,\n",
255/// #     "    diff: `0`,\n",
256/// #     "       ε: `0.0000000000000002220446049250313`",
257/// # );
258/// # assert_eq!(actual, message);
259/// # }
260/// ```
261///
262/// # Module macros
263///
264/// * [`assert_f64_ne`](macro@crate::assert_f64_ne)
265/// * [`assert_f64_ne_as_result`](macro@crate::assert_f64_ne_as_result)
266/// * [`debug_assert_f64_ne`](macro@crate::debug_assert_f64_ne)
267///
268#[macro_export]
269macro_rules! assert_f64_ne {
270    ($a:expr, $b:expr $(,)?) => {
271        match $crate::assert_f64_ne_as_result!($a, $b) {
272            Ok(()) => (),
273            Err(err) => panic!("{}", err),
274        }
275    };
276    ($a:expr, $b:expr, $($message:tt)+) => {
277        match $crate::assert_f64_ne_as_result!($a, $b) {
278            Ok(()) => (),
279            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
280        }
281    };
282}
283
284#[cfg(test)]
285mod test_assert_f64_ne {
286    use crate::assert_f64::{EQ, GT};
287    use std::panic;
288
289    #[test]
290    fn ne() {
291        let a: f64 = EQ;
292        let b: f64 = GT;
293        for _ in 0..1 {
294            let actual = assert_f64_ne!(a, b);
295            assert_eq!(actual, ());
296        }
297    }
298}
299
300/// Assert a floating point 64-bit number is not equal to another within f64::EPSILON.
301///
302/// Pseudocode:<br>
303/// a ≠ b
304///
305/// This macro provides the same statements as [`assert_f64_ne`](macro.assert_f64_ne.html),
306/// except this macro's statements are only enabled in non-optimized
307/// builds by default. An optimized build will not execute this macro's
308/// statements unless `-C debug-assertions` is passed to the compiler.
309///
310/// This macro is useful for checks that are too expensive to be present
311/// in a release build but may be helpful during development.
312///
313/// The result of expanding this macro is always type checked.
314///
315/// An unchecked assertion allows a program in an inconsistent state to
316/// keep running, which might have unexpected consequences but does not
317/// introduce unsafety as long as this only happens in safe code. The
318/// performance cost of assertions, however, is not measurable in general.
319/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
320/// after thorough profiling, and more importantly, only in safe code!
321///
322/// This macro is intended to work in a similar way to
323/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
324///
325/// # Module macros
326///
327/// * [`assert_f64_ne`](macro@crate::assert_f64_ne)
328/// * [`assert_f64_ne`](macro@crate::assert_f64_ne)
329/// * [`debug_assert_f64_ne`](macro@crate::debug_assert_f64_ne)
330///
331#[macro_export]
332macro_rules! debug_assert_f64_ne {
333    ($($arg:tt)*) => {
334        if $crate::cfg!(debug_assertions) {
335            $crate::assert_f64_ne!($($arg)*);
336        }
337    };
338}