claims/
assert_gt.rs

1/// Asserts that the first expression is greater than the second.
2///
3/// Requires that both expressions be comparable with `>`.
4///
5/// ## Uses
6///
7/// Assertions are always checked in both debug and release builds, and cannot be disabled.
8/// See [`debug_assert_gt!`] for assertions that are not enabled in release builds by default.
9///
10/// ## Custom messages
11///
12/// This macro has a second form, where a custom panic message can be provided with or without
13/// arguments for formatting. See [`std::fmt`] for syntax for this form.
14///
15/// ## Examples
16///
17/// ```rust
18/// # #[macro_use] extern crate claims;
19/// # fn main() {
20/// assert_gt!(2, 1);
21///
22/// // With a custom message
23/// assert_gt!(2, 1, "Expecting that {} is greater or equal than {}", 2, 1);
24/// # }
25/// ```
26///
27/// ```rust,should_panic
28/// # #[macro_use] extern crate claims;
29/// # fn main() {
30/// assert_gt!(5, 5);  // Will panic
31///
32/// // With a custom message
33/// assert_gt!(5, 6, "Not expecting {} to be greater than {}", 5, 6);
34/// # }
35/// ```
36///
37/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
38/// [`debug_assert_gt!`]: crate::debug_assert_gt!
39#[macro_export]
40macro_rules! assert_gt {
41    ($left:expr, $right:expr $(,)?) => {
42        match (&$left, &$right) {
43            (left_val, right_val) => {
44                if !(*left_val > *right_val) {
45                    // The reborrows below are intentional. Without them, the stack slot for the
46                    // borrow is initialized even before the values are compared, leading to a
47                    // noticeable slow down.
48                    ::core::panic!(r#"assertion failed: `(left > right)`
49    left: `{:?}`,
50    right: `{:?}`"#, &*left_val, &*right_val)
51                }
52            }
53        }
54    };
55    ($left:expr, $right:expr, $($arg:tt)+) => {
56        match (&$left, &$right) {
57            (left_val, right_val) => {
58                if !(*left_val > *right_val) {
59                    // The reborrows below are intentional. Without them, the stack slot for the
60                    // borrow is initialized even before the values are compared, leading to a
61                    // noticeable slow down.
62                    ::core::panic!(r#"assertion failed: `(left > right)`
63    left: `{:?}`,
64    right: `{:?}`: {}"#, &*left_val, &*right_val, ::core::format_args!($($arg)+))
65                }
66            }
67        }
68    };
69}
70
71/// Asserts that the first expression is greater than the second in debug builds.
72///
73/// This macro behaves the same as [`assert_gt!`] on debug builds. On release builds it is a no-op.
74#[macro_export]
75macro_rules! debug_assert_gt {
76    ($($arg:tt)*) => {
77        #[cfg(debug_assertions)]
78        $crate::assert_gt!($($arg)*);
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    #[test]
85    fn greater_than() {
86        assert_gt!(5, 3);
87    }
88
89    #[test]
90    #[should_panic(expected = "assertion failed: `(left > right)`\n    left: `3`,\n    right: `3`")]
91    fn equal() {
92        assert_gt!(3, 3);
93    }
94
95    #[test]
96    #[should_panic(expected = "assertion failed: `(left > right)`\n    left: `1`,\n    right: `3`")]
97    fn less_than() {
98        assert_gt!(1, 3);
99    }
100
101    #[test]
102    #[should_panic(
103        expected = "assertion failed: `(left > right)`\n    left: `3`,\n    right: `3`: foo"
104    )]
105    fn equal_custom_message() {
106        assert_gt!(3, 3, "foo");
107    }
108
109    #[test]
110    #[should_panic(
111        expected = "assertion failed: `(left > right)`\n    left: `1`,\n    right: `3`: foo"
112    )]
113    fn less_than_custom_message() {
114        assert_gt!(1, 3, "foo");
115    }
116
117    #[test]
118    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
119    fn debug_greater_than() {
120        debug_assert_gt!(5, 3);
121    }
122
123    #[test]
124    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
125    #[should_panic(expected = "assertion failed: `(left > right)`\n    left: `3`,\n    right: `3`")]
126    fn debug_equal() {
127        debug_assert_gt!(3, 3);
128    }
129
130    #[test]
131    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
132    #[should_panic(expected = "assertion failed: `(left > right)`\n    left: `1`,\n    right: `3`")]
133    fn debug_less_than() {
134        debug_assert_gt!(1, 3);
135    }
136
137    #[test]
138    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
139    #[should_panic(
140        expected = "assertion failed: `(left > right)`\n    left: `3`,\n    right: `3`: foo"
141    )]
142    fn debug_equal_custom_message() {
143        debug_assert_gt!(3, 3, "foo");
144    }
145
146    #[test]
147    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
148    #[should_panic(
149        expected = "assertion failed: `(left > right)`\n    left: `1`,\n    right: `3`: foo"
150    )]
151    fn debug_less_than_custom_message() {
152        debug_assert_gt!(1, 3, "foo");
153    }
154
155    #[test]
156    #[cfg_attr(debug_assertions, ignore = "only run in release mode")]
157    fn debug_release_equal() {
158        debug_assert_gt!(3, 3);
159    }
160
161    #[test]
162    #[cfg_attr(debug_assertions, ignore = "only run in release mode")]
163    fn debug_release_less_than() {
164        debug_assert_gt!(1, 3);
165    }
166}