Skip to main content

assertables/assert_fn/
assert_fn_lt_x.rs

1//! Assert a function output is less than an expression.
2//!
3//! Pseudocode:<br>
4//! a_function(a) < b
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a: i8 = -1;
12//! let b: i8 = 2;
13//! assert_fn_lt_x!(i8::abs, a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_fn_lt_x`](macro@crate::assert_fn_lt_x)
19//! * [`assert_fn_lt_x_as_result`](macro@crate::assert_fn_lt_x_as_result)
20//! * [`debug_assert_fn_lt_x`](macro@crate::debug_assert_fn_lt_x)
21
22/// Assert a function output is less than an expression.
23///
24/// Pseudocode:<br>
25/// a_function(a) < b
26///
27/// * If true, return Result `Ok(a)`.
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_fn_lt_x`](macro@crate::assert_fn_lt_x)
37/// * [`assert_fn_lt_x_as_result`](macro@crate::assert_fn_lt_x_as_result)
38/// * [`debug_assert_fn_lt_x`](macro@crate::debug_assert_fn_lt_x)
39///
40#[macro_export]
41macro_rules! assert_fn_lt_x_as_result {
42    //// Arity 1
43    ($a_function:path, $a_param:expr, $b_expr:expr $(,)?) => {
44        match (&$a_function, &$a_param, &$b_expr) {
45            (_a_function, a_param, b_expr) => {
46                let a = $a_function($a_param);
47                if a < $b_expr {
48                    Ok(a)
49                } else {
50                    Err(format!(
51                        concat!(
52                            "assertion failed: `assert_fn_lt_x!(a_function, a_param, b_expr)`\n",
53                            "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
54                            " a_function label: `{}`,\n",
55                            "    a_param label: `{}`,\n",
56                            "    a_param debug: `{:?}`,\n",
57                            "     b_expr label: `{}`,\n",
58                            "     b_expr debug: `{:?}`,\n",
59                            "                a: `{:?}`,\n",
60                            "                b: `{:?}`"
61                        ),
62                        stringify!($a_function),
63                        stringify!($a_param),
64                        a_param,
65                        stringify!($b_expr),
66                        b_expr,
67                        a,
68                        b_expr
69                    ))
70                }
71            }
72        }
73    };
74
75    //// Arity 0
76    ($a_function:path, $b_expr:expr $(,)?) => {
77        match (&$a_function, &$b_expr) {
78            (_a_function, b_expr) => {
79                let a = $a_function();
80                if a < $b_expr {
81                    Ok(a)
82                } else {
83                    Err(format!(
84                        concat!(
85                            "assertion failed: `assert_fn_lt_x!(a_function, b_expr)`\n",
86                            "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
87                            " a_function label: `{}`,\n",
88                            "     b_expr label: `{}`,\n",
89                            "     b_expr debug: `{:?}`,\n",
90                            "                a: `{:?}`,\n",
91                            "                b: `{:?}`"
92                        ),
93                        stringify!($a_function),
94                        stringify!($b_expr),
95                        b_expr,
96                        a,
97                        b_expr
98                    ))
99                }
100            }
101        }
102    };
103}
104
105#[cfg(test)]
106mod test_assert_fn_lt_x_as_result {
107    // use std::sync::Once;
108
109    mod arity_1 {
110
111        fn f(i: i8) -> i8 {
112            return i;
113        }
114
115        #[test]
116        fn lt() {
117            let a: i8 = 1;
118            let b: i8 = 2;
119            for _ in 0..1 {
120                let actual = assert_fn_lt_x_as_result!(f, a, b);
121                assert_eq!(actual.unwrap(), 1);
122            }
123        }
124
125        #[test]
126        fn eq() {
127            let a: i8 = 1;
128            let b: i8 = 1;
129            let actual = assert_fn_lt_x_as_result!(f, a, b);
130            let message = concat!(
131                "assertion failed: `assert_fn_lt_x!(a_function, a_param, b_expr)`\n",
132                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
133                " a_function label: `f`,\n",
134                "    a_param label: `a`,\n",
135                "    a_param debug: `1`,\n",
136                "     b_expr label: `b`,\n",
137                "     b_expr debug: `1`,\n",
138                "                a: `1`,\n",
139                "                b: `1`"
140            );
141            assert_eq!(actual.unwrap_err(), message);
142        }
143
144        #[test]
145        fn gt() {
146            let a: i8 = 1;
147            let b: i8 = 0;
148            let actual = assert_fn_lt_x_as_result!(f, a, b);
149            let message = concat!(
150                "assertion failed: `assert_fn_lt_x!(a_function, a_param, b_expr)`\n",
151                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
152                " a_function label: `f`,\n",
153                "    a_param label: `a`,\n",
154                "    a_param debug: `1`,\n",
155                "     b_expr label: `b`,\n",
156                "     b_expr debug: `0`,\n",
157                "                a: `1`,\n",
158                "                b: `0`"
159            );
160            assert_eq!(actual.unwrap_err(), message);
161        }
162    }
163
164    mod arity_0 {
165
166        fn f() -> i8 {
167            return 1;
168        }
169
170        #[test]
171        fn lt() {
172            let b: i8 = 2;
173            for _ in 0..1 {
174                let actual = assert_fn_lt_x_as_result!(f, b);
175                assert_eq!(actual.unwrap(), 1);
176            }
177        }
178
179        #[test]
180        fn eq() {
181            let b: i8 = 1;
182            let actual = assert_fn_lt_x_as_result!(f, b);
183            let message = concat!(
184                "assertion failed: `assert_fn_lt_x!(a_function, b_expr)`\n",
185                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
186                " a_function label: `f`,\n",
187                "     b_expr label: `b`,\n",
188                "     b_expr debug: `1`,\n",
189                "                a: `1`,\n",
190                "                b: `1`"
191            );
192            assert_eq!(actual.unwrap_err(), message);
193        }
194
195        #[test]
196        fn gt() {
197            let b: i8 = 0;
198            let actual = assert_fn_lt_x_as_result!(f, b);
199            let message = concat!(
200                "assertion failed: `assert_fn_lt_x!(a_function, b_expr)`\n",
201                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
202                " a_function label: `f`,\n",
203                "     b_expr label: `b`,\n",
204                "     b_expr debug: `0`,\n",
205                "                a: `1`,\n",
206                "                b: `0`"
207            );
208            assert_eq!(actual.unwrap_err(), message);
209        }
210    }
211}
212
213/// Assert a function output is less than an expression.
214///
215/// Pseudocode:<br>
216/// a_function(a) < b
217///
218/// * If true, return `a`.
219///
220/// * Otherwise, call [`panic!`] with a message and the values of the
221///   expressions with their debug representations.
222///
223/// # Examples
224///
225/// ```rust
226/// use assertables::*;
227/// # use std::panic;
228///
229/// # fn main() {
230/// let a: i8 = -1;
231/// let b: i8 = 2;
232/// assert_fn_lt_x!(i8::abs, a, b);
233///
234/// # let result = panic::catch_unwind(|| {
235/// // This will panic
236/// let a: i8 = -2;
237/// let b: i8 = 1;
238/// assert_fn_lt_x!(i8::abs, a, b);
239/// # });
240/// // assertion failed: `assert_fn_lt_x!(a_function, a_param, b_expr)`
241/// // https://docs.rs/assertables/…/assertables/macro.assert_fn_lt_x.html
242/// //  a_function label: `i8::abs`,
243/// //     a_param label: `a`,
244/// //     a_param debug: `-2`,
245/// //      b_expr label: `b`,
246/// //      b_expr debug: `1`,
247/// //                 a: `2`,
248/// //                 b: `1`
249/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
250/// # let message = concat!(
251/// #     "assertion failed: `assert_fn_lt_x!(a_function, a_param, b_expr)`\n",
252/// #     "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
253/// #     " a_function label: `i8::abs`,\n",
254/// #     "    a_param label: `a`,\n",
255/// #     "    a_param debug: `-2`,\n",
256/// #     "     b_expr label: `b`,\n",
257/// #     "     b_expr debug: `1`,\n",
258/// #     "                a: `2`,\n",
259/// #     "                b: `1`"
260/// # );
261/// # assert_eq!(actual, message);
262/// # }
263/// ```
264///
265/// # Module macros
266///
267/// * [`assert_fn_lt_x`](macro@crate::assert_fn_lt_x)
268/// * [`assert_fn_lt_x_as_result`](macro@crate::assert_fn_lt_x_as_result)
269/// * [`debug_assert_fn_lt_x`](macro@crate::debug_assert_fn_lt_x)
270///
271#[macro_export]
272macro_rules! assert_fn_lt_x {
273
274    //// Arity 1
275
276    ($a_function:path, $a_param:expr, $b_expr:expr $(,)?) => {
277        match $crate::assert_fn_lt_x_as_result!($a_function, $a_param, $b_expr) {
278            Ok(x) => x,
279            Err(err) => panic!("{}", err),
280        }
281    };
282
283    ($a_function:path, $a_param:expr, $b_expr:expr, $($message:tt)+) => {
284        match $crate::assert_fn_lt_x_as_result!($a_function, $a_param, $b_expr) {
285            Ok(x) => x,
286            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
287        }
288    };
289
290    //// Arity 0
291
292    ($a_function:path, $b_expr:expr $(,)?) => {
293        match $crate::assert_fn_lt_x_as_result!($a_function, $b_expr) {
294            Ok(x) => x,
295            Err(err) => panic!("{}", err),
296        }
297    };
298
299    ($a_function:path, $b_expr:expr, $($message:tt)+) => {
300        match $crate::assert_fn_lt_x_as_result!($a_function, $b_expr) {
301            Ok(x) => x,
302            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
303        }
304    };
305
306}
307
308#[cfg(test)]
309mod test_assert_fn_lt_x {
310    use std::panic;
311
312    mod arity_1 {
313        use super::*;
314
315        fn f(i: i8) -> i8 {
316            return i;
317        }
318
319        #[test]
320        fn lt() {
321            let a: i8 = 1;
322            let b: i8 = 2;
323            for _ in 0..1 {
324                let actual = assert_fn_lt_x!(f, a, b);
325                assert_eq!(actual, 1);
326            }
327        }
328
329        #[test]
330        fn eq() {
331            let result = panic::catch_unwind(|| {
332                let a: i8 = 1;
333                let b: i8 = 1;
334                let _actual = assert_fn_lt_x!(f, a, b);
335            });
336            let message = concat!(
337                "assertion failed: `assert_fn_lt_x!(a_function, a_param, b_expr)`\n",
338                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
339                " a_function label: `f`,\n",
340                "    a_param label: `a`,\n",
341                "    a_param debug: `1`,\n",
342                "     b_expr label: `b`,\n",
343                "     b_expr debug: `1`,\n",
344                "                a: `1`,\n",
345                "                b: `1`"
346            );
347            assert_eq!(
348                result
349                    .unwrap_err()
350                    .downcast::<String>()
351                    .unwrap()
352                    .to_string(),
353                message
354            );
355        }
356
357        #[test]
358        fn gt() {
359            let result = panic::catch_unwind(|| {
360                let a: i8 = 1;
361                let b: i8 = 0;
362                let _actual = assert_fn_lt_x!(f, a, b);
363            });
364            let message = concat!(
365                "assertion failed: `assert_fn_lt_x!(a_function, a_param, b_expr)`\n",
366                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
367                " a_function label: `f`,\n",
368                "    a_param label: `a`,\n",
369                "    a_param debug: `1`,\n",
370                "     b_expr label: `b`,\n",
371                "     b_expr debug: `0`,\n",
372                "                a: `1`,\n",
373                "                b: `0`"
374            );
375            assert_eq!(
376                result
377                    .unwrap_err()
378                    .downcast::<String>()
379                    .unwrap()
380                    .to_string(),
381                message
382            );
383        }
384    }
385
386    mod arity_0 {
387        use super::*;
388
389        fn f() -> i8 {
390            return 1;
391        }
392
393        #[test]
394        fn lt() {
395            let b: i8 = 2;
396            for _ in 0..1 {
397                let actual = assert_fn_lt_x!(f, b);
398                assert_eq!(actual, 1);
399            }
400        }
401
402        #[test]
403        fn eq() {
404            let result = panic::catch_unwind(|| {
405                let b: i8 = 1;
406                let _actual = assert_fn_lt_x!(f, b);
407            });
408            let message = concat!(
409                "assertion failed: `assert_fn_lt_x!(a_function, b_expr)`\n",
410                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
411                " a_function label: `f`,\n",
412                "     b_expr label: `b`,\n",
413                "     b_expr debug: `1`,\n",
414                "                a: `1`,\n",
415                "                b: `1`"
416            );
417            assert_eq!(
418                result
419                    .unwrap_err()
420                    .downcast::<String>()
421                    .unwrap()
422                    .to_string(),
423                message
424            );
425        }
426
427        #[test]
428        fn gt() {
429            let result = panic::catch_unwind(|| {
430                let b: i8 = 0;
431                let _actual = assert_fn_lt_x!(f, b);
432            });
433            let message = concat!(
434                "assertion failed: `assert_fn_lt_x!(a_function, b_expr)`\n",
435                "https://docs.rs/assertables/9.8.5/assertables/macro.assert_fn_lt_x.html\n",
436                " a_function label: `f`,\n",
437                "     b_expr label: `b`,\n",
438                "     b_expr debug: `0`,\n",
439                "                a: `1`,\n",
440                "                b: `0`"
441            );
442            assert_eq!(
443                result
444                    .unwrap_err()
445                    .downcast::<String>()
446                    .unwrap()
447                    .to_string(),
448                message
449            );
450        }
451    }
452}
453
454/// Assert a function output is less than an expression.
455///
456/// Pseudocode:<br>
457/// a_function(a) < b
458///
459/// This macro provides the same statements as [`assert_fn_lt_x`](macro.assert_fn_lt_x.html),
460/// except this macro's statements are only enabled in non-optimized
461/// builds by default. An optimized build will not execute this macro's
462/// statements unless `-C debug-assertions` is passed to the compiler.
463///
464/// This macro is useful for checks that are too expensive to be present
465/// in a release build but may be helpful during development.
466///
467/// The result of expanding this macro is always type checked.
468///
469/// An unchecked assertion allows a program in an inconsistent state to
470/// keep running, which might have unexpected consequences but does not
471/// introduce unsafety as long as this only happens in safe code. The
472/// performance cost of assertions, however, is not measurable in general.
473/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
474/// after thorough profiling, and more importantly, only in safe code!
475///
476/// This macro is intended to work in a similar way to
477/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
478///
479/// # Module macros
480///
481/// * [`assert_fn_lt_x`](macro@crate::assert_fn_lt_x)
482/// * [`assert_fn_lt_x`](macro@crate::assert_fn_lt_x)
483/// * [`debug_assert_fn_lt_x`](macro@crate::debug_assert_fn_lt_x)
484///
485#[macro_export]
486macro_rules! debug_assert_fn_lt_x {
487    ($($arg:tt)*) => {
488        if cfg!(debug_assertions) {
489            $crate::assert_fn_lt_x!($($arg)*);
490        }
491    };
492}