claims/
assert_le.rs

1/// Asserts that the first expression is less or equal 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_le!`] 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_le!(1, 2);
21///
22/// // With a custom message
23/// assert_le!(5, 5, "Expecting that {} is less or equal than {}", 5, 5);
24/// # }
25/// ```
26///
27/// ```rust,should_panic
28/// # #[macro_use] extern crate claims;
29/// # fn main() {
30/// assert_le!(6, 5);  // Will panic
31///
32/// // With a custom message
33/// assert_le!(6, 5, "Not expecting {} to be less or equal than {}", 6, 5);
34/// # }
35/// ```
36///
37/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
38/// [`debug_assert_le!`]: crate::debug_assert_le!
39#[macro_export]
40macro_rules! assert_le {
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 less or equal than the second on debug builds.
72///
73/// This macro behaves the same as [`assert_le!`] on debug builds. On release builds it is a no-op.
74#[macro_export]
75macro_rules! debug_assert_le {
76    ($($arg:tt)*) => {
77        #[cfg(debug_assertions)]
78        $crate::assert_le!($($arg)*);
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    #[test]
85    #[should_panic(
86        expected = "assertion failed: `(left <= right)`\n    left: `5`,\n    right: `3`"
87    )]
88    fn greater_than() {
89        assert_le!(5, 3);
90    }
91
92    #[test]
93    fn equal() {
94        assert_le!(3, 3);
95    }
96
97    #[test]
98    fn less_than() {
99        assert_le!(1, 3);
100    }
101
102    #[test]
103    #[should_panic(
104        expected = "assertion failed: `(left <= right)`\n    left: `5`,\n    right: `3`: foo"
105    )]
106    fn greater_than_custom_message() {
107        assert_le!(5, 3, "foo");
108    }
109
110    #[test]
111    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
112    #[should_panic(
113        expected = "assertion failed: `(left <= right)`\n    left: `5`,\n    right: `3`"
114    )]
115    fn debug_greater_than() {
116        debug_assert_le!(5, 3);
117    }
118
119    #[test]
120    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
121    fn debug_equal() {
122        debug_assert_le!(3, 3);
123    }
124
125    #[test]
126    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
127    fn debug_less_than() {
128        debug_assert_le!(1, 3);
129    }
130
131    #[test]
132    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
133    #[should_panic(
134        expected = "assertion failed: `(left <= right)`\n    left: `5`,\n    right: `3`: foo"
135    )]
136    fn debug_greater_than_custom_message() {
137        debug_assert_le!(5, 3, "foo");
138    }
139
140    #[test]
141    #[cfg_attr(debug_assertions, ignore = "only run in release mode")]
142    fn debug_release_greater_than() {
143        debug_assert_le!(5, 3);
144    }
145}