Skip to main content

assertables/
assert_gt.rs

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