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