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