Skip to main content

assertables/assert_fn/
assert_fn_lt.rs

1//! Assert a function output is less than another.
2//!
3//! Pseudocode:<br>
4//! a_function(a) < b_function(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!(i8::abs, a, i8::abs, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_fn_lt`](macro@crate::assert_fn_lt)
19//! * [`assert_fn_lt_as_result`](macro@crate::assert_fn_lt_as_result)
20//! * [`debug_assert_fn_lt`](macro@crate::debug_assert_fn_lt)
21
22/// Assert a function output is less than another.
23///
24/// Pseudocode:<br>
25/// a_function(a) < b_function(b)
26///
27/// * If true, return Result `Ok(a, b)`.
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`](macro@crate::assert_fn_lt)
37/// * [`assert_fn_lt_as_result`](macro@crate::assert_fn_lt_as_result)
38/// * [`debug_assert_fn_lt`](macro@crate::debug_assert_fn_lt)
39///
40#[macro_export]
41macro_rules! assert_fn_lt_as_result {
42
43    //// Arity 1
44
45    ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr $(,)?) => {
46        match (&$a_function, &$a_param, &$b_function, &$b_param) {
47            (_a_function, a_param, _b_function, b_param) => {
48                let a = $a_function($a_param);
49                let b = $b_function($b_param);
50                if a < b {
51                    Ok((a, b))
52                } else {
53                    Err(
54                        format!(
55                            concat!(
56                                "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
57                                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
58                                " a_function label: `{}`,\n",
59                                "    a_param label: `{}`,\n",
60                                "    a_param debug: `{:?}`,\n",
61                                " b_function label: `{}`,\n",
62                                "    b_param label: `{}`,\n",
63                                "    b_param debug: `{:?}`,\n",
64                                "                a: `{:?}`,\n",
65                                "                b: `{:?}`"
66                            ),
67                            stringify!($a_function),
68                            stringify!($a_param),
69                            a_param,
70                            stringify!($b_function),
71                            stringify!($b_param),
72                            b_param,
73                            a,
74                            b
75                        )
76                    )
77                }
78            }
79        }
80    };
81
82    //// Arity 0
83
84    ($a_function:path, $b_function:path) => {
85        match (&$a_function, &$b_function) {
86            (_a_function, _b_function) => {
87                let a = $a_function();
88                let b = $b_function();
89                if a < b {
90                    Ok((a, b))
91                } else {
92                    Err(
93                        format!(
94                            concat!(
95                                "assertion failed: `assert_fn_lt!(a_function, b_function)`\n",
96                                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
97                                " a_function label: `{}`,\n",
98                                " b_function label: `{}`,\n",
99                                "                a: `{:?}`,\n",
100                                "                b: `{:?}`"
101                            ),
102                            stringify!($a_function),
103                            stringify!($b_function),
104                            a,
105                            b
106                        )
107                    )
108                }
109            }
110        }
111    };
112
113}
114
115#[cfg(test)]
116mod test_assert_fn_lt_as_result {
117    // use std::sync::Once;
118
119    mod arity_1 {
120
121        fn f(i: i8) -> i8 {
122            return i;
123        }
124
125        fn g(i: i8) -> i8 {
126            return i;
127        }
128
129        #[test]
130        fn lt() {
131            let a: i8 = 1;
132            let b: i8 = 2;
133            for _ in 0..1 {
134                let actual = assert_fn_lt_as_result!(f, a, g, b);
135                assert_eq!(actual.unwrap(), (1, 2));
136            }
137        }
138
139        #[test]
140        fn eq() {
141            let a: i8 = 1;
142            let b: i8 = 1;
143            let actual = assert_fn_lt_as_result!(f, a, g, b);
144            let message = concat!(
145                "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
146                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
147                " a_function label: `f`,\n",
148                "    a_param label: `a`,\n",
149                "    a_param debug: `1`,\n",
150                " b_function label: `g`,\n",
151                "    b_param label: `b`,\n",
152                "    b_param debug: `1`,\n",
153                "                a: `1`,\n",
154                "                b: `1`"
155            );
156            assert_eq!(actual.unwrap_err(), message);
157        }
158
159        #[test]
160        fn gt() {
161            let a: i8 = 2;
162            let b: i8 = 1;
163            let actual = assert_fn_lt_as_result!(f, a, g, b);
164            let message = concat!(
165                "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
166                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
167                " a_function label: `f`,\n",
168                "    a_param label: `a`,\n",
169                "    a_param debug: `2`,\n",
170                " b_function label: `g`,\n",
171                "    b_param label: `b`,\n",
172                "    b_param debug: `1`,\n",
173                "                a: `2`,\n",
174                "                b: `1`"
175            );
176            assert_eq!(actual.unwrap_err(), message);
177        }
178    }
179
180    mod arity_0 {
181
182        fn f() -> i8 {
183            return 1;
184        }
185
186        fn g() -> i8 {
187            return 2;
188        }
189
190        #[test]
191        fn lt() {
192            for _ in 0..1 {
193                let actual = assert_fn_lt_as_result!(f, g);
194                assert_eq!(actual.unwrap(), (1, 2));
195            }
196        }
197
198        #[test]
199        fn eq() {
200            let actual = assert_fn_lt_as_result!(f, f);
201            let message = concat!(
202                "assertion failed: `assert_fn_lt!(a_function, b_function)`\n",
203                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
204                " a_function label: `f`,\n",
205                " b_function label: `f`,\n",
206                "                a: `1`,\n",
207                "                b: `1`"
208            );
209            assert_eq!(actual.unwrap_err(), message);
210        }
211
212        #[test]
213        fn gt() {
214            let actual = assert_fn_lt_as_result!(g, f);
215            let message = concat!(
216                "assertion failed: `assert_fn_lt!(a_function, b_function)`\n",
217                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
218                " a_function label: `g`,\n",
219                " b_function label: `f`,\n",
220                "                a: `2`,\n",
221                "                b: `1`"
222            );
223            assert_eq!(actual.unwrap_err(), message);
224        }
225    }
226}
227
228/// Assert a function output is less than another.
229///
230/// Pseudocode:<br>
231/// a_function(a) < b_function(b)
232///
233/// * If true, return `(a, b)`.
234///
235/// * Otherwise, call [`panic!`] with a message and the values of the
236///   expressions with their debug representations.
237///
238/// # Examples
239///
240/// ```rust
241/// use assertables::*;
242/// # use std::panic;
243///
244/// # fn main() {
245/// let a: i8 = 1;
246/// let b: i8 = -2;
247/// assert_fn_lt!(i8::abs, a, i8::abs, b);
248///
249/// # let result = panic::catch_unwind(|| {
250/// // This will panic
251/// let a: i8 = -2;
252/// let b: i8 = 1;
253/// assert_fn_lt!(i8::abs, a, i8::abs, b);
254/// # });
255/// // assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`
256/// // https://docs.rs/assertables/…/assertables/macro.assert_fn_lt.html
257/// //  a_function label: `i8::abs`,
258/// //     a_param label: `a`,
259/// //     a_param debug: `-2`,
260/// //  b_function label: `i8::abs`,
261/// //     b_param label: `b`,
262/// //     b_param debug: `1`,
263/// //                 a: `2`,
264/// //                 b: `1`
265/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
266/// # let message = concat!(
267/// #     "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
268/// #     "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
269/// #     " a_function label: `i8::abs`,\n",
270/// #     "    a_param label: `a`,\n",
271/// #     "    a_param debug: `-2`,\n",
272/// #     " b_function label: `i8::abs`,\n",
273/// #     "    b_param label: `b`,\n",
274/// #     "    b_param debug: `1`,\n",
275/// #     "                a: `2`,\n",
276/// #     "                b: `1`"
277/// # );
278/// # assert_eq!(actual, message);
279/// # }
280/// ```
281///
282/// # Module macros
283///
284/// * [`assert_fn_lt`](macro@crate::assert_fn_lt)
285/// * [`assert_fn_lt_as_result`](macro@crate::assert_fn_lt_as_result)
286/// * [`debug_assert_fn_lt`](macro@crate::debug_assert_fn_lt)
287///
288#[macro_export]
289macro_rules! assert_fn_lt {
290
291    //// Arity 1
292
293    ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr $(,)?) => {
294        match $crate::assert_fn_lt_as_result!($a_function, $a_param, $b_function, $b_param) {
295            Ok(x) => x,
296            Err(err) => panic!("{}", err),
297        }
298    };
299
300    ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr, $($message:tt)+) => {
301        match $crate::assert_fn_lt_as_result!($a_function, $a_param, $b_function, $b_param) {
302            Ok(x) => x,
303            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
304        }
305    };
306
307    //// Arity 0
308
309    ($a_function:path, $b_function:path) => {
310        match $crate::assert_fn_lt_as_result!($a_function, $b_function) {
311            Ok(x) => x,
312            Err(err) => panic!("{}", err),
313        }
314    };
315
316    ($a_function:path, $b_function:path, $($message:tt)+) => {
317        match $crate::assert_fn_lt_as_result!($a_function, $b_function) {
318            Ok(x) => x,
319            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
320        }
321    };
322
323}
324
325#[cfg(test)]
326mod test_assert_fn_lt {
327    use std::panic;
328
329    mod arity_1 {
330        use super::*;
331
332        fn f(i: i8) -> i8 {
333            return i;
334        }
335
336        fn g(i: i8) -> i8 {
337            return i;
338        }
339
340        #[test]
341        fn lt() {
342            let a: i8 = 1;
343            let b: i8 = 2;
344            for _ in 0..1 {
345                let actual = assert_fn_lt!(f, a, g, b);
346                let expect = (1, 2);
347                assert_eq!(actual, expect);
348            }
349        }
350
351        #[test]
352        fn eq() {
353            let result = panic::catch_unwind(|| {
354                let a: i8 = 1;
355                let b: i8 = 1;
356                let _actual = assert_fn_lt!(f, a, g, b);
357            });
358            let message = concat!(
359                "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
360                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
361                " a_function label: `f`,\n",
362                "    a_param label: `a`,\n",
363                "    a_param debug: `1`,\n",
364                " b_function label: `g`,\n",
365                "    b_param label: `b`,\n",
366                "    b_param debug: `1`,\n",
367                "                a: `1`,\n",
368                "                b: `1`"
369            );
370            assert_eq!(
371                result
372                    .unwrap_err()
373                    .downcast::<String>()
374                    .unwrap()
375                    .to_string(),
376                message
377            );
378        }
379
380        #[test]
381        fn gt() {
382            let result = panic::catch_unwind(|| {
383                let a: i8 = 2;
384                let b: i8 = 1;
385                let _actual = assert_fn_lt!(f, a, g, b);
386            });
387            let message = concat!(
388                "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
389                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
390                " a_function label: `f`,\n",
391                "    a_param label: `a`,\n",
392                "    a_param debug: `2`,\n",
393                " b_function label: `g`,\n",
394                "    b_param label: `b`,\n",
395                "    b_param debug: `1`,\n",
396                "                a: `2`,\n",
397                "                b: `1`"
398            );
399            assert_eq!(
400                result
401                    .unwrap_err()
402                    .downcast::<String>()
403                    .unwrap()
404                    .to_string(),
405                message
406            );
407        }
408    }
409
410    mod arity_0 {
411        use super::*;
412
413        fn f() -> i8 {
414            return 1;
415        }
416
417        fn g() -> i8 {
418            return 2;
419        }
420
421        #[test]
422        fn lt() {
423            for _ in 0..1 {
424                let actual = assert_fn_lt!(f, g);
425                let expect = (1, 2);
426                assert_eq!(actual, expect);
427            }
428        }
429
430        #[test]
431        fn eq() {
432            let result = panic::catch_unwind(|| {
433                let _actual = assert_fn_lt!(f, f);
434            });
435            let message = concat!(
436                "assertion failed: `assert_fn_lt!(a_function, b_function)`\n",
437                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
438                " a_function label: `f`,\n",
439                " b_function label: `f`,\n",
440                "                a: `1`,\n",
441                "                b: `1`"
442            );
443            assert_eq!(
444                result
445                    .unwrap_err()
446                    .downcast::<String>()
447                    .unwrap()
448                    .to_string(),
449                message
450            );
451        }
452
453        #[test]
454        fn gt() {
455            let result = panic::catch_unwind(|| {
456                let _actual = assert_fn_lt!(g, f);
457            });
458            let message = concat!(
459                "assertion failed: `assert_fn_lt!(a_function, b_function)`\n",
460                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
461                " a_function label: `g`,\n",
462                " b_function label: `f`,\n",
463                "                a: `2`,\n",
464                "                b: `1`"
465            );
466            assert_eq!(
467                result
468                    .unwrap_err()
469                    .downcast::<String>()
470                    .unwrap()
471                    .to_string(),
472                message
473            );
474        }
475    }
476}
477
478/// Assert a function output is less than another.
479///
480/// Pseudocode:<br>
481/// a_function(a) < b_function(b)
482///
483/// This macro provides the same statements as [`assert_fn_lt`](macro.assert_fn_lt.html),
484/// except this macro's statements are only enabled in non-optimized
485/// builds by default. An optimized build will not execute this macro's
486/// statements unless `-C debug-assertions` is passed to the compiler.
487///
488/// This macro is useful for checks that are too expensive to be present
489/// in a release build but may be helpful during development.
490///
491/// The result of expanding this macro is always type checked.
492///
493/// An unchecked assertion allows a program in an inconsistent state to
494/// keep running, which might have unexpected consequences but does not
495/// introduce unsafety as long as this only happens in safe code. The
496/// performance cost of assertions, however, is not measurable in general.
497/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
498/// after thorough profiling, and more importantly, only in safe code!
499///
500/// This macro is intended to work in a similar way to
501/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
502///
503/// # Module macros
504///
505/// * [`assert_fn_lt`](macro@crate::assert_fn_lt)
506/// * [`assert_fn_lt`](macro@crate::assert_fn_lt)
507/// * [`debug_assert_fn_lt`](macro@crate::debug_assert_fn_lt)
508///
509#[macro_export]
510macro_rules! debug_assert_fn_lt {
511    ($($arg:tt)*) => {
512        if cfg!(debug_assertions) {
513            $crate::assert_fn_lt!($($arg)*);
514        }
515    };
516}
517
518#[cfg(test)]
519mod test_debug_assert_fn_lt {
520    use std::panic;
521
522    mod arity_1 {
523        use super::*;
524
525        fn f(i: i8) -> i8 {
526            return i;
527        }
528
529        fn g(i: i8) -> i8 {
530            return i;
531        }
532
533        #[test]
534        fn lt() {
535            let a: i8 = 1;
536            let b: i8 = 2;
537            for _ in 0..1 {
538                let _actual = debug_assert_fn_lt!(f, a, g, b);
539                let _expect = (1, 2);
540                // assert_eq!(actual, expect);
541            }
542        }
543
544        #[test]
545        fn eq() {
546            let result = panic::catch_unwind(|| {
547                let a: i8 = 1;
548                let b: i8 = 1;
549                let _actual = debug_assert_fn_lt!(f, a, g, b);
550            });
551            let message = concat!(
552                "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
553                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
554                " a_function label: `f`,\n",
555                "    a_param label: `a`,\n",
556                "    a_param debug: `1`,\n",
557                " b_function label: `g`,\n",
558                "    b_param label: `b`,\n",
559                "    b_param debug: `1`,\n",
560                "                a: `1`,\n",
561                "                b: `1`"
562            );
563            assert_eq!(
564                result
565                    .unwrap_err()
566                    .downcast::<String>()
567                    .unwrap()
568                    .to_string(),
569                message
570            );
571        }
572
573        #[test]
574        fn gt() {
575            let result = panic::catch_unwind(|| {
576                let a: i8 = 2;
577                let b: i8 = 1;
578                let _actual = debug_assert_fn_lt!(f, a, g, b);
579            });
580            let message = concat!(
581                "assertion failed: `assert_fn_lt!(a_function, a_param, b_function, b_param)`\n",
582                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
583                " a_function label: `f`,\n",
584                "    a_param label: `a`,\n",
585                "    a_param debug: `2`,\n",
586                " b_function label: `g`,\n",
587                "    b_param label: `b`,\n",
588                "    b_param debug: `1`,\n",
589                "                a: `2`,\n",
590                "                b: `1`"
591            );
592            assert_eq!(
593                result
594                    .unwrap_err()
595                    .downcast::<String>()
596                    .unwrap()
597                    .to_string(),
598                message
599            );
600        }
601    }
602
603    mod arity_0 {
604        use super::*;
605
606        fn f() -> i8 {
607            return 1;
608        }
609
610        fn g() -> i8 {
611            return 2;
612        }
613
614        #[test]
615        fn lt() {
616            for _ in 0..1 {
617                let _actual = debug_assert_fn_lt!(f, g);
618                let _expect = (1, 2);
619                // assert_eq!(actual, expect);
620            }
621        }
622
623        #[test]
624        fn eq() {
625            let result = panic::catch_unwind(|| {
626                let _actual = debug_assert_fn_lt!(f, f);
627            });
628            let message = concat!(
629                "assertion failed: `assert_fn_lt!(a_function, b_function)`\n",
630                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
631                " a_function label: `f`,\n",
632                " b_function label: `f`,\n",
633                "                a: `1`,\n",
634                "                b: `1`"
635            );
636            assert_eq!(
637                result
638                    .unwrap_err()
639                    .downcast::<String>()
640                    .unwrap()
641                    .to_string(),
642                message
643            );
644        }
645
646        #[test]
647        fn gt() {
648            let result = panic::catch_unwind(|| {
649                let _actual = debug_assert_fn_lt!(g, f);
650            });
651            let message = concat!(
652                "assertion failed: `assert_fn_lt!(a_function, b_function)`\n",
653                "https://docs.rs/assertables/9.8.6/assertables/macro.assert_fn_lt.html\n",
654                " a_function label: `g`,\n",
655                " b_function label: `f`,\n",
656                "                a: `2`,\n",
657                "                b: `1`"
658            );
659            assert_eq!(
660                result
661                    .unwrap_err()
662                    .downcast::<String>()
663                    .unwrap()
664                    .to_string(),
665                message
666            );
667        }
668    }
669}