assertables/assert_diff/assert_diff_gt_x.rs
1//! Assert a difference is greater than 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 = 2;
14//! assert_diff_gt_x!(a, b, x);
15//! ```
16//!
17//! # Module macros
18//!
19//! * [`assert_diff_gt_x`](macro@crate::assert_diff_gt_x)
20//! * [`assert_diff_gt_x_as_result`](macro@crate::assert_diff_gt_x_as_result)
21//! * [`debug_assert_diff_gt_x`](macro@crate::debug_assert_diff_gt_x)
22
23/// Assert a difference is greater than 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_gt_x`](macro@crate::assert_diff_gt_x)
42/// * [`assert_diff_gt_x_as_result`](macro@crate::assert_diff_gt_x_as_result)
43/// * [`debug_assert_diff_gt_x`](macro@crate::debug_assert_diff_gt_x)
44///
45#[macro_export]
46macro_rules! assert_diff_gt_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_gt_x!(a, b, x)`\n",
59 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_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_gt_x!(a, b, x)`\n",
86 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_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_gt_x_as_result {
112
113 #[test]
114 fn gt() {
115 let a: i8 = 10;
116 let b: i8 = 13;
117 let x: i8 = 2;
118 let actual = assert_diff_gt_x_as_result!(a, b, x);
119 assert_eq!(actual.unwrap(), (3 as i8, 2 as i8));
120 }
121
122 #[test]
123 fn eq() {
124 let a: i8 = 10;
125 let b: i8 = 13;
126 let x: i8 = 3;
127 let actual = assert_diff_gt_x_as_result!(a, b, x);
128 let message = concat!(
129 "assertion failed: `assert_diff_gt_x!(a, b, x)`\n",
130 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html\n",
131 " a label: `a`,\n",
132 " a debug: `10`,\n",
133 " b label: `b`,\n",
134 " b debug: `13`,\n",
135 " x label: `x`,\n",
136 " x debug: `3`,\n",
137 " Δ: `3`,\n",
138 " Δ > x: false"
139 );
140 assert_eq!(actual.unwrap_err(), message);
141 }
142
143 #[test]
144 fn lt() {
145 let a: i8 = 10;
146 let b: i8 = 13;
147 let x: i8 = 4;
148 let actual = assert_diff_gt_x_as_result!(a, b, x);
149 let message = concat!(
150 "assertion failed: `assert_diff_gt_x!(a, b, x)`\n",
151 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html\n",
152 " a label: `a`,\n",
153 " a debug: `10`,\n",
154 " b label: `b`,\n",
155 " b debug: `13`,\n",
156 " x label: `x`,\n",
157 " x debug: `4`,\n",
158 " Δ: `3`,\n",
159 " Δ > x: false"
160 );
161 assert_eq!(actual.unwrap_err(), message);
162 }
163
164 #[test]
165 fn overflow() {
166 let a: i8 = i8::MAX;
167 let b: i8 = i8::MIN;
168 let x: i8 = 0;
169 let actual = assert_diff_gt_x_as_result!(a, b, x);
170 let message = format!(
171 concat!(
172 "assertion failed: `assert_diff_gt_x!(a, b, x)`\n",
173 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html\n",
174 " a label: `a`,\n",
175 " a debug: `{}`,\n",
176 " b label: `b`,\n",
177 " b debug: `{}`,\n",
178 " x label: `x`,\n",
179 " x debug: `{}`,\n",
180 " Δ: panic"
181 ),
182 a, b, x
183 );
184 assert_eq!(actual.unwrap_err(), message);
185 }
186
187 use std::sync::Once;
188 #[test]
189 fn once() {
190
191 static A: Once = Once::new();
192 fn a() -> i32 {
193 if A.is_completed() { panic!("A.is_completed()") } else { A.call_once(|| {}) }
194 10
195 }
196
197 static B: Once = Once::new();
198 fn b() -> i32 {
199 if B.is_completed() { panic!("B.is_completed()") } else { B.call_once(|| {}) }
200 13
201 }
202
203 static X: Once = Once::new();
204 fn x() -> i32 {
205 if X.is_completed() { panic!("X.is_completed()") } else { X.call_once(|| {}) }
206 2
207 }
208
209 assert_eq!(A.is_completed(), false);
210 assert_eq!(B.is_completed(), false);
211 assert_eq!(X.is_completed(), false);
212 let result = assert_diff_gt_x_as_result!(a(), b(), x());
213 assert!(result.is_ok());
214 assert_eq!(A.is_completed(), true);
215 assert_eq!(B.is_completed(), true);
216 assert_eq!(X.is_completed(), true);
217
218 }
219
220}
221
222/// Assert a difference is greater than an expression.
223///
224/// Pseudocode:<br>
225/// Δ > x
226///
227/// * If true, return `(lhs, rhs)`.
228///
229/// * Otherwise, call [`panic!`] with a message and the values of the
230/// expressions with their debug representations.
231///
232/// # Examples
233///
234/// ```rust
235/// use assertables::*;
236/// # use std::panic;
237///
238/// # fn main() {
239/// let a: i8 = 10;
240/// let b: i8 = 13;
241/// let x: i8 = 2;
242/// assert_diff_gt_x!(a, b, x);
243///
244/// # let result = panic::catch_unwind(|| {
245/// // This will panic
246/// let a: i8 = 10;
247/// let b: i8 = 13;
248/// let x: i8 = 4;
249/// assert_diff_gt_x!(a, b, x);
250/// # });
251/// // assertion failed: `assert_diff_gt_x!(a, b, x)`
252/// // https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html
253/// // a label: `a`,
254/// // a debug: `10`,
255/// // b label: `b`,
256/// // b debug: `13`,
257/// // x label: `x`,
258/// // x debug: `4`,
259/// // Δ: `3`,
260/// // Δ > x: false
261/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
262/// # let message = concat!(
263/// # "assertion failed: `assert_diff_gt_x!(a, b, x)`\n",
264/// # "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html\n",
265/// # " a label: `a`,\n",
266/// # " a debug: `10`,\n",
267/// # " b label: `b`,\n",
268/// # " b debug: `13`,\n",
269/// # " x label: `x`,\n",
270/// # " x debug: `4`,\n",
271/// # " Δ: `3`,\n",
272/// # " Δ > x: false",
273/// # );
274/// # assert_eq!(actual, message);
275/// # }
276/// ```
277///
278/// # Module macros
279///
280/// * [`assert_diff_gt_x`](macro@crate::assert_diff_gt_x)
281/// * [`assert_diff_gt_x_as_result`](macro@crate::assert_diff_gt_x_as_result)
282/// * [`debug_assert_diff_gt_x`](macro@crate::debug_assert_diff_gt_x)
283///
284#[macro_export]
285macro_rules! assert_diff_gt_x {
286 ($a:expr, $b:expr, $x:expr $(,)?) => {{
287 match $crate::assert_diff_gt_x_as_result!($a, $b, $x) {
288 Ok(x) => x,
289 Err(err) => panic!("{}", err),
290 }
291 }};
292 ($a:expr, $b:expr, $x:expr, $($message:tt)+) => {{
293 match $crate::assert_diff_gt_x_as_result!($a, $b, $x) {
294 Ok(x) => x,
295 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
296 }
297 }};
298}
299
300#[cfg(test)]
301mod test_assert_diff_gt_x {
302 use std::panic;
303
304 #[test]
305 fn gt() {
306 let a: i8 = 10;
307 let b: i8 = 13;
308 let x: i8 = 2;
309 let actual = assert_diff_gt_x!(a, b, x);
310 assert_eq!(actual, (3 as i8, 2 as i8));
311 }
312
313 #[test]
314 fn eq() {
315 let a: i8 = 10;
316 let b: i8 = 13;
317 let x: i8 = 3;
318 let result = panic::catch_unwind(|| {
319 let _actual = assert_diff_gt_x!(a, b, x);
320 });
321 let message = concat!(
322 "assertion failed: `assert_diff_gt_x!(a, b, x)`\n",
323 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html\n",
324 " a label: `a`,\n",
325 " a debug: `10`,\n",
326 " b label: `b`,\n",
327 " b debug: `13`,\n",
328 " x label: `x`,\n",
329 " x debug: `3`,\n",
330 " Δ: `3`,\n",
331 " Δ > x: false"
332 );
333 assert_eq!(
334 result
335 .unwrap_err()
336 .downcast::<String>()
337 .unwrap()
338 .to_string(),
339 message
340 );
341 }
342
343 #[test]
344 fn lt() {
345 let a: i8 = 10;
346 let b: i8 = 13;
347 let x: i8 = 4;
348 let result = panic::catch_unwind(|| {
349 let _actual = assert_diff_gt_x!(a, b, x);
350 });
351 let message = concat!(
352 "assertion failed: `assert_diff_gt_x!(a, b, x)`\n",
353 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html\n",
354 " a label: `a`,\n",
355 " a debug: `10`,\n",
356 " b label: `b`,\n",
357 " b debug: `13`,\n",
358 " x label: `x`,\n",
359 " x debug: `4`,\n",
360 " Δ: `3`,\n",
361 " Δ > x: false"
362 );
363 assert_eq!(
364 result
365 .unwrap_err()
366 .downcast::<String>()
367 .unwrap()
368 .to_string(),
369 message
370 );
371 }
372
373 #[test]
374 fn overflow() {
375 let a: i8 = i8::MAX;
376 let b: i8 = i8::MIN;
377 let x: i8 = 0;
378 let result = panic::catch_unwind(|| {
379 let _actual = assert_diff_gt_x!(a, b, x);
380 });
381 let message = format!(
382 concat!(
383 "assertion failed: `assert_diff_gt_x!(a, b, x)`\n",
384 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_diff_gt_x.html\n",
385 " a label: `a`,\n",
386 " a debug: `{}`,\n",
387 " b label: `b`,\n",
388 " b debug: `{}`,\n",
389 " x label: `x`,\n",
390 " x debug: `{}`,\n",
391 " Δ: panic"
392 ),
393 a, b, x
394 );
395 assert_eq!(
396 result
397 .unwrap_err()
398 .downcast::<String>()
399 .unwrap()
400 .to_string(),
401 message
402 );
403 }
404}
405
406/// Assert a difference is greater than an expression.
407///
408/// Pseudocode:<br>
409/// Δ > x
410///
411/// This macro provides the same statements as [`assert_diff_gt_x`](macro.assert_diff_gt_x.html),
412/// except this macro's statements are only enabled in non-optimized
413/// builds by default. An optimized build will not execute this macro's
414/// statements unless `-C debug-assertions` is passed to the compiler.
415///
416/// This macro is useful for checks that are too expensive to be present
417/// in a release build but may be helpful during development.
418///
419/// The result of expanding this macro is always type checked.
420///
421/// An unchecked assertion allows a program in an inconsistent state to
422/// keep running, which might have unexpected consequences but does not
423/// introduce unsafety as long as this only happens in safe code. The
424/// performance cost of assertions, however, is not measurable in general.
425/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
426/// after thorough profiling, and more importantly, only in safe code!
427///
428/// This macro is intended to work in a similar way to
429/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
430///
431/// # Module macros
432///
433/// * [`assert_diff_gt_x`](macro@crate::assert_diff_gt_x)
434/// * [`assert_diff_gt_x_as_result`](macro@crate::assert_diff_gt_x_as_result)
435/// * [`debug_assert_diff_gt_x`](macro@crate::debug_assert_diff_gt_x)
436///
437#[macro_export]
438macro_rules! debug_assert_diff_gt_x {
439 ($($arg:tt)*) => {
440 if $crate::cfg!(debug_assertions) {
441 $crate::assert_diff_gt_x!($($arg)*);
442 }
443 };
444}