Skip to main content

assertables/assert_f32/
assert_f32_gt.rs

1//! Assert a floating point 32-bit number is greater than another within f32::EPSILON.
2//!
3//! Pseudocode:<br>
4//! a > b
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a: f32 = 1.0 / 3.0;
12//! let b: f32 = 0.3333331;
13//! assert_f32_gt!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_f32_gt`](macro@crate::assert_f32_gt)
19//! * [`assert_f32_gt_as_result`](macro@crate::assert_f32_gt_as_result)
20//! * [`debug_assert_f32_gt`](macro@crate::debug_assert_f32_gt)
21
22//! Assert a floating point 32-bit number is greater than another within f32::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_f32_gt`](macro@crate::assert_f32_gt)
37/// * [`assert_f32_gt_as_result`](macro@crate::assert_f32_gt_as_result)
38/// * [`debug_assert_f32_gt`](macro@crate::debug_assert_f32_gt)
39///
40#[macro_export]
41macro_rules! assert_f32_gt_as_result {
42    ($a:expr, $b:expr $(,)?) => {
43        match (&$a, &$b) {
44            (a, b) => {
45                if a - f32::EPSILON > *b {
46                    Ok(())
47                } else {
48                    Err(format!(
49                        concat!(
50                            "assertion failed: `assert_f32_gt!(a, b)`\n",
51                            "https://docs.rs/assertables/9.8.5/assertables/macro.assert_f32_gt.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                        f32::EPSILON,
65                    ))
66                }
67            }
68        }
69    };
70}
71
72#[cfg(test)]
73mod test_assert_f32_gt_as_result {
74    use crate::assert_f32::{EQ, EQ_GT, EQ_LT, GT, LT};
75    use std::sync::Once;
76
77    #[test]
78    fn gt() {
79        let a: f32 = EQ;
80        let b: f32 = LT;
81        for _ in 0..1 {
82            let actual = assert_f32_gt_as_result!(a, b);
83            assert_eq!(actual.unwrap(), ());
84        }
85    }
86
87    #[test]
88    fn gt_once() {
89        static A: Once = Once::new();
90        fn a() -> f32 {
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() -> f32 {
101            if B.is_completed() {
102                panic!("B.is_completed()")
103            } else {
104                B.call_once(|| {})
105            }
106            LT
107        }
108
109        assert_eq!(A.is_completed(), false);
110        assert_eq!(B.is_completed(), false);
111        let result = assert_f32_gt_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: f32 = EQ;
120        let b: f32 = EQ;
121        let actual = assert_f32_gt_as_result!(a, b);
122        let message = concat!(
123            "assertion failed: `assert_f32_gt!(a, b)`\n",
124            "https://docs.rs/assertables/9.8.5/assertables/macro.assert_f32_gt.html\n",
125            " a label: `a`,\n",
126            " a debug: `0.33333334`,\n",
127            " b label: `b`,\n",
128            " b debug: `0.33333334`,\n",
129            "    diff: `0`,\n",
130            "       ε: `0.00000011920929`",
131        );
132        assert_eq!(actual.unwrap_err(), message);
133    }
134
135    #[test]
136    fn eq_lt() {
137        let a: f32 = EQ;
138        let b: f32 = EQ_GT;
139        let actual = assert_f32_gt_as_result!(a, b);
140        let message = concat!(
141            "assertion failed: `assert_f32_gt!(a, b)`\n",
142            "https://docs.rs/assertables/9.8.5/assertables/macro.assert_f32_gt.html\n",
143            " a label: `a`,\n",
144            " a debug: `0.33333334`,\n",
145            " b label: `b`,\n",
146            " b debug: `0.3333334`,\n",
147            "    diff: `-0.000000059604645`,\n",
148            "       ε: `0.00000011920929`",
149        );
150        assert_eq!(actual.unwrap_err(), message);
151    }
152
153    #[test]
154    fn eq_gt() {
155        let a: f32 = EQ;
156        let b: f32 = EQ_LT;
157        let actual = assert_f32_gt_as_result!(a, b);
158        let message = concat!(
159            "assertion failed: `assert_f32_gt!(a, b)`\n",
160            "https://docs.rs/assertables/9.8.5/assertables/macro.assert_f32_gt.html\n",
161            " a label: `a`,\n",
162            " a debug: `0.33333334`,\n",
163            " b label: `b`,\n",
164            " b debug: `0.3333333`,\n",
165            "    diff: `0.000000029802322`,\n",
166            "       ε: `0.00000011920929`",
167        );
168        assert_eq!(actual.unwrap_err(), message);
169    }
170
171    #[test]
172    fn lt() {
173        let a: f32 = EQ;
174        let b: f32 = GT;
175        let actual = assert_f32_gt_as_result!(a, b);
176        let message = concat!(
177            "assertion failed: `assert_f32_gt!(a, b)`\n",
178            "https://docs.rs/assertables/9.8.5/assertables/macro.assert_f32_gt.html\n",
179            " a label: `a`,\n",
180            " a debug: `0.33333334`,\n",
181            " b label: `b`,\n",
182            " b debug: `0.3333336`,\n",
183            "    diff: `-0.0000002682209`,\n",
184            "       ε: `0.00000011920929`",
185        );
186        assert_eq!(actual.unwrap_err(), message);
187    }
188}
189
190/// Assert a floating point 32-bit number is greater than another within f32::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: f32 = 1.0 / 3.0;
208/// let b: f32 = 0.3333331;
209/// assert_f32_gt!(a, b);
210///
211/// # let result = panic::catch_unwind(|| {
212/// // This will panic
213/// let a: f32 = 1.0 / 3.0;
214/// let b: f32 = 0.3333336;
215/// assert_f32_gt!(a, b);
216/// # });
217/// // assertion failed: `assert_f32_gt!(a, b)`
218/// // https://docs.rs/assertables/…/assertables/macro.assert_f32_gt.html
219/// //  a label: `a`,
220/// //  a debug: `0.33333334`,
221/// //  b label: `b`,
222/// //  b debug: `0.3333336`,`
223/// //     diff: `-0.00000029802322`,
224/// //        ε: `0.00000011920929`
225/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
226/// # let message = concat!(
227/// #     "assertion failed: `assert_f32_gt!(a, b)`\n",
228/// #     "https://docs.rs/assertables/9.8.5/assertables/macro.assert_f32_gt.html\n",
229/// #     " a label: `a`,\n",
230/// #     " a debug: `0.33333334`,\n",
231/// #     " b label: `b`,\n",
232/// #     " b debug: `0.3333336`,\n",
233/// #     "    diff: `-0.0000002682209`,\n",
234/// #     "       ε: `0.00000011920929`",
235/// # );
236/// # assert_eq!(actual, message);
237/// # }
238/// ```
239///
240/// # Module macros
241///
242/// * [`assert_f32_gt`](macro@crate::assert_f32_gt)
243/// * [`assert_f32_gt_as_result`](macro@crate::assert_f32_gt_as_result)
244/// * [`debug_assert_f32_gt`](macro@crate::debug_assert_f32_gt)
245///
246#[macro_export]
247macro_rules! assert_f32_gt {
248    ($a:expr, $b:expr $(,)?) => {
249        match $crate::assert_f32_gt_as_result!($a, $b) {
250            Ok(()) => (),
251            Err(err) => panic!("{}", err),
252        }
253    };
254    ($a:expr, $b:expr, $($message:tt)+) => {
255        match $crate::assert_f32_gt_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_f32_gt {
264    use crate::assert_f32::{EQ, LT};
265    use std::panic;
266
267    #[test]
268    fn gt() {
269        let a: f32 = EQ;
270        let b: f32 = LT;
271        for _ in 0..1 {
272            let actual = assert_f32_gt!(a, b);
273            assert_eq!(actual, ());
274        }
275    }
276}
277
278/// Assert a floating point 32-bit number is greater than another within f32::EPSILON.
279///
280/// Pseudocode:<br>
281/// a > b
282///
283///
284/// This macro provides the same statements as [`assert_f32_gt`](macro.assert_f32_gt.html),
285/// except this macro's statements are only enabled in non-optimized
286/// builds by default. An optimized build will not execute this macro's
287/// statements unless `-C debug-assertions` is passed to the compiler.
288///
289/// This macro is useful for checks that are too expensive to be present
290/// in a release build but may be helpful during development.
291///
292/// The result of expanding this macro is always type checked.
293///
294/// An unchecked assertion allows a program in an inconsistent state to
295/// keep running, which might have unexpected consequences but does not
296/// introduce unsafety as long as this only happens in safe code. The
297/// performance cost of assertions, however, is not measurable in general.
298/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
299/// after thorough profiling, and more importantly, only in safe code!
300///
301/// This macro is intended to work in a similar way to
302/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
303///
304/// # Module macros
305///
306/// * [`assert_f32_gt`](macro@crate::assert_f32_gt)
307/// * [`assert_f32_gt`](macro@crate::assert_f32_gt)
308/// * [`debug_assert_f32_gt`](macro@crate::debug_assert_f32_gt)
309///
310#[macro_export]
311macro_rules! debug_assert_f32_gt {
312    ($($arg:tt)*) => {
313        if cfg!(debug_assertions) {
314            $crate::assert_f32_gt!($($arg)*);
315        }
316    };
317}