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