assertables/assert_f64/
assert_f64_lt.rs

1//! Assert a floating point 64-bit number is less than 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_lt!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_f64_lt`](macro@crate::assert_f64_lt)
19//! * [`assert_f64_lt_as_result`](macro@crate::assert_f64_lt_as_result)
20//! * [`debug_assert_f64_lt`](macro@crate::debug_assert_f64_lt)
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_lt`](macro@crate::assert_f64_lt)
37/// * [`assert_f64_lt_as_result`](macro@crate::assert_f64_lt_as_result)
38/// * [`debug_assert_f64_lt`](macro@crate::debug_assert_f64_lt)
39///
40#[macro_export]
41macro_rules! assert_f64_lt_as_result {
42    ($a:expr, $b:expr $(,)?) => {
43        match (&$a, &$b) {
44            (a, b) => {
45                if a + f64::EPSILON < *b {
46                    Ok(())
47                } else {
48                    Err(format!(
49                        concat!(
50                            "assertion failed: `assert_f64_lt!(a, b)`\n",
51                            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_lt.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_lt_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_lt_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_lt_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 eq() {
119        let a: f64 = EQ;
120        let b: f64 = EQ;
121        let actual = assert_f64_lt_as_result!(a, b);
122        let message = concat!(
123            "assertion failed: `assert_f64_lt!(a, b)`\n",
124            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_lt.html\n",
125            " a label: `a`,\n",
126            " a debug: `0.3333333333333333`,\n",
127            " b label: `b`,\n",
128            " b debug: `0.3333333333333333`,\n",
129            "    diff: `0`,\n",
130            "       ε: `0.0000000000000002220446049250313`",
131        );
132        assert_eq!(actual.unwrap_err(), message);
133    }
134
135    #[test]
136    fn eq_lt() {
137        let a: f64 = EQ;
138        let b: f64 = EQ_GT;
139        let actual = assert_f64_lt_as_result!(a, b);
140        let message = concat!(
141            "assertion failed: `assert_f64_lt!(a, b)`\n",
142            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_lt.html\n",
143            " a label: `a`,\n",
144            " a debug: `0.3333333333333333`,\n",
145            " b label: `b`,\n",
146            " b debug: `0.3333333333333335`,\n",
147            "    diff: `-0.00000000000000016653345369377348`,\n",
148            "       ε: `0.0000000000000002220446049250313`",
149        );
150        assert_eq!(actual.unwrap_err(), message);
151    }
152
153    #[test]
154    fn eq_gt() {
155        let a: f64 = EQ;
156        let b: f64 = EQ_LT;
157        let actual = assert_f64_lt_as_result!(a, b);
158        let message = concat!(
159            "assertion failed: `assert_f64_lt!(a, b)`\n",
160            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_lt.html\n",
161            " a label: `a`,\n",
162            " a debug: `0.3333333333333333`,\n",
163            " b label: `b`,\n",
164            " b debug: `0.3333333333333332`,\n",
165            "    diff: `0.00000000000000011102230246251565`,\n",
166            "       ε: `0.0000000000000002220446049250313`",
167        );
168        assert_eq!(actual.unwrap_err(), message);
169    }
170
171    #[test]
172    fn gt() {
173        let a: f64 = EQ;
174        let b: f64 = LT;
175        let actual = assert_f64_lt_as_result!(a, b);
176        let message = concat!(
177            "assertion failed: `assert_f64_lt!(a, b)`\n",
178            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_lt.html\n",
179            " a label: `a`,\n",
180            " a debug: `0.3333333333333333`,\n",
181            " b label: `b`,\n",
182            " b debug: `0.3333333333333329`,\n",
183            "    diff: `0.0000000000000003885780586188048`,\n",
184            "       ε: `0.0000000000000002220446049250313`",
185        );
186        assert_eq!(actual.unwrap_err(), message);
187    }
188}
189
190/// Assert a floating point 64-bit number is less than another within f64::EPSILON.
191///
192/// Pseudocode:<br>
193/// a < b
194///
195/// * If true, return `()`.
196///
197/// * Otherwise, call [`panic!`] with a message and the values of the
198///   expressions with their debug representations.
199///
200/// # Examples
201///
202/// ```rust
203/// use assertables::*;
204/// # use std::panic;
205///
206/// # fn main() {
207/// let a: f64 = 1.0 / 3.0;
208/// let b: f64 = 0.3333333333333339;
209/// assert_f64_lt!(a, b);
210///
211/// # let result = panic::catch_unwind(|| {
212/// // This will panic
213/// let a: f64 = 1.0 / 3.0;
214/// let b: f64 = 0.3333333333333329;
215/// assert_f64_lt!(a, b);
216/// # });
217/// // assertion failed: `assert_f64_lt!(a, b)`
218/// // https://docs.rs/assertables/…/assertables/macro.assert_f64_lt.html
219/// //  a label: `a`,
220/// //  a debug: `0.3333333333333333`,
221/// //  b label: `b`,
222/// //  b debug: `0.3333333333333329`,`
223/// //     diff: `0.0000000000000003885780586188048`,
224/// //        ε: `0.0000000000000002220446049250313`
225/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
226/// # let message = concat!(
227/// #     "assertion failed: `assert_f64_lt!(a, b)`\n",
228/// #     "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_lt.html\n",
229/// #     " a label: `a`,\n",
230/// #     " a debug: `0.3333333333333333`,\n",
231/// #     " b label: `b`,\n",
232/// #     " b debug: `0.3333333333333329`,\n",
233/// #     "    diff: `0.0000000000000003885780586188048`,\n",
234/// #     "       ε: `0.0000000000000002220446049250313`",
235/// # );
236/// # assert_eq!(actual, message);
237/// # }
238/// ```
239///
240/// # Module macros
241///
242/// * [`assert_f64_lt`](macro@crate::assert_f64_lt)
243/// * [`assert_f64_lt_as_result`](macro@crate::assert_f64_lt_as_result)
244/// * [`debug_assert_f64_lt`](macro@crate::debug_assert_f64_lt)
245///
246#[macro_export]
247macro_rules! assert_f64_lt {
248    ($a:expr, $b:expr $(,)?) => {
249        match $crate::assert_f64_lt_as_result!($a, $b) {
250            Ok(()) => (),
251            Err(err) => panic!("{}", err),
252        }
253    };
254    ($a:expr, $b:expr, $($message:tt)+) => {
255        match $crate::assert_f64_lt_as_result!($a, $b) {
256            Ok(()) => (),
257            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
258        }
259    };
260}
261
262#[cfg(test)]
263mod test_assert_f64_lt {
264    use crate::assert_f64::{EQ, GT};
265    use std::panic;
266
267    #[test]
268    fn lt() {
269        let a: f64 = EQ;
270        let b: f64 = GT;
271        for _ in 0..1 {
272            let actual = assert_f64_lt!(a, b);
273            assert_eq!(actual, ());
274        }
275    }
276}
277
278/// Assert a floating point 64-bit number is less than another within f64::EPSILON.
279///
280/// Pseudocode:<br>
281/// a < b
282///
283/// This macro provides the same statements as [`assert_f64_lt`](macro.assert_f64_lt.html),
284/// except this macro's statements are only enabled in non-optimized
285/// builds by default. An optimized build will not execute this macro's
286/// statements unless `-C debug-assertions` is passed to the compiler.
287///
288/// This macro is useful for checks that are too expensive to be present
289/// in a release build but may be helpful during development.
290///
291/// The result of expanding this macro is always type checked.
292///
293/// An unchecked assertion allows a program in an inconsistent state to
294/// keep running, which might have unexpected consequences but does not
295/// introduce unsafety as long as this only happens in safe code. The
296/// performance cost of assertions, however, is not measurable in general.
297/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
298/// after thorough profiling, and more importantly, only in safe code!
299///
300/// This macro is intended to work in a similar way to
301/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
302///
303/// # Module macros
304///
305/// * [`assert_f64_lt`](macro@crate::assert_f64_lt)
306/// * [`assert_f64_lt`](macro@crate::assert_f64_lt)
307/// * [`debug_assert_f64_lt`](macro@crate::debug_assert_f64_lt)
308///
309#[macro_export]
310macro_rules! debug_assert_f64_lt {
311    ($($arg:tt)*) => {
312        if $crate::cfg!(debug_assertions) {
313            $crate::assert_f64_lt!($($arg)*);
314        }
315    };
316}