assertables/assert_bag/
assert_bag_ne.rs

1//! Assert a bag is not equal to another.
2//!
3//! Pseudocode:<br>
4//! (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = [1, 1];
12//! let b = [1, 1, 1];
13//! assert_bag_ne!(a, b);
14//! ```
15//!
16//! This implementation uses [`::std::collections::BTreeMap`](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html) to count items and sort them.
17//!
18//! # Module macros
19//!
20//! * [`assert_bag_ne`](macro@crate::assert_bag_ne)
21//! * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
22//! * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
23
24/// Assert a bag is not equal to another.
25///
26/// Pseudocode:<br>
27/// (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
28///
29/// * If true, return Result `Ok((a, b))`.
30///
31/// * Otherwise, return Result `Err(message)`.
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_bag_ne`](macro@crate::assert_bag_ne)
39/// * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
40/// * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
41///
42#[macro_export]
43macro_rules! assert_bag_ne_as_result {
44    ($a_collection:expr, $b_collection:expr $(,)?) => {
45        match (&$a_collection, &$b_collection) {
46            (a_collection, b_collection) => {
47                let a = $crate::assert_bag_impl_prep!(a_collection);
48                let b = $crate::assert_bag_impl_prep!(b_collection);
49                if a != b {
50                    Ok((a, b))
51                } else {
52                    Err(format!(
53                        concat!(
54                            "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
55                            "https://docs.rs/assertables/9.8.4/assertables/macro.assert_bag_ne.html\n",
56                            " a label: `{}`,\n",
57                            " a debug: `{:?}`,\n",
58                            " b label: `{}`,\n",
59                            " b debug: `{:?}`,\n",
60                            "   a bag: `{:?}`,\n",
61                            "   b bag: `{:?}`"
62                        ),
63                        stringify!($a_collection),
64                        a_collection,
65                        stringify!($b_collection),
66                        b_collection,
67                        a,
68                        b
69                    ))
70                }
71            }
72        }
73    };
74}
75
76#[cfg(test)]
77mod test_assert_bag_ne_as_result {
78    use std::collections::BTreeMap;
79    // use std::sync::Once;
80
81    #[test]
82    fn lt() {
83        let a = [1, 1];
84        let b = [1, 1, 1];
85        for _ in 0..1 {
86            let actual = assert_bag_ne_as_result!(a, b);
87            assert_eq!(
88                actual.unwrap(),
89                (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 3)]))
90            );
91        }
92    }
93
94    //TODO
95    // #[test]
96    // fn lt_once() {
97    //     static A: Once = Once::new();
98    //     fn a() -> [i32; 2] {
99    //         if A.is_completed() {
100    //             panic!("A.is_completed()")
101    //         } else {
102    //             A.call_once(|| {})
103    //         }
104    //         [1, 1]
105    //     }
106
107    //     static B: Once = Once::new();
108    //     fn b() -> [i32; 3] {
109    //         if B.is_completed() {
110    //             panic!("B.is_completed()")
111    //         } else {
112    //             B.call_once(|| {})
113    //         }
114    //         [1, 1, 1]
115    //     }
116
117    //     assert_eq!(A.is_completed(), false);
118    //     assert_eq!(B.is_completed(), false);
119    //     let result = assert_bag_ne_as_result!(a(), b());
120    //     assert!(result.is_ok());
121    //     assert_eq!(A.is_completed(), true);
122    //     assert_eq!(B.is_completed(), true);
123    // }
124
125    #[test]
126    fn gt() {
127        let a = [1, 1, 1];
128        let b = [1, 1];
129        for _ in 0..1 {
130            let actual = assert_bag_ne_as_result!(a, b);
131            assert_eq!(
132                actual.unwrap(),
133                (BTreeMap::from([(&1, 3)]), BTreeMap::from([(&1, 2)]))
134            );
135        }
136    }
137
138    //TODO
139    // #[test]
140    // fn gt_once() {
141    //     static A: Once = Once::new();
142    //     fn a() -> [i32; 3] {
143    //         if A.is_completed() {
144    //             panic!("A.is_completed()")
145    //         } else {
146    //             A.call_once(|| {})
147    //         }
148    //         [1, 1, 1]
149    //     }
150
151    //     static B: Once = Once::new();
152    //     fn b() -> [i32; 2] {
153    //         if B.is_completed() {
154    //             panic!("B.is_completed()")
155    //         } else {
156    //             B.call_once(|| {})
157    //         }
158    //         [1, 1]
159    //     }
160
161    //     assert_eq!(A.is_completed(), false);
162    //     assert_eq!(B.is_completed(), false);
163    //     let result = assert_bag_ne_as_result!(a(), b());
164    //     assert!(result.is_ok());
165    //     assert_eq!(A.is_completed(), true);
166    //     assert_eq!(B.is_completed(), true);
167    // }
168
169    #[test]
170    fn eq() {
171        let a = [1, 1];
172        let b = [1, 1];
173        let actual = assert_bag_ne_as_result!(a, b);
174        let message = concat!(
175            "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
176            "https://docs.rs/assertables/9.8.4/assertables/macro.assert_bag_ne.html\n",
177            " a label: `a`,\n",
178            " a debug: `[1, 1]`,\n",
179            " b label: `b`,\n",
180            " b debug: `[1, 1]`,\n",
181            "   a bag: `{1: 2}`,\n",
182            "   b bag: `{1: 2}`"
183        );
184        assert_eq!(actual.unwrap_err(), message);
185    }
186}
187
188/// Assert a bag is not equal to another.
189///
190/// Pseudocode:<br>
191/// (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
192///
193/// * If true, return `(a, b)`.
194///
195/// * Otherwise, call [`panic!`] with a message and the values of the
196///   expressions with their debug representations.
197///
198/// # Examples
199///
200/// ```rust
201/// use assertables::*;
202/// # use std::panic;
203///
204/// # fn main() {
205/// let a = [1, 1];
206/// let b = [1, 1, 1];
207/// assert_bag_ne!(a, b);
208///
209/// # let result = panic::catch_unwind(|| {
210/// // This will panic
211/// let a = [1, 1];
212/// let b = [1, 1];
213/// assert_bag_ne!(a, b);
214/// # });
215/// // assertion failed: `assert_bag_ne!(a_collection, b_collection)`
216/// // https://docs.rs/assertables/…/assertables/macro.assert_bag_ne.html
217/// //  a label: `a`,
218/// //  a debug: `[1, 1]`,
219/// //  b label: `b`,
220/// //  b debug: `[1, 1]`,
221/// //    a bag: `{1: 2}`,
222/// //    b bag: `{1: 2}`
223/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
224/// # let message = concat!(
225/// #     "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
226/// #     "https://docs.rs/assertables/9.8.4/assertables/macro.assert_bag_ne.html\n",
227/// #     " a label: `a`,\n",
228/// #     " a debug: `[1, 1]`,\n",
229/// #     " b label: `b`,\n",
230/// #     " b debug: `[1, 1]`,\n",
231/// #     "   a bag: `{1: 2}`,\n",
232/// #     "   b bag: `{1: 2}`"
233/// # );
234/// # assert_eq!(actual, message);
235/// # }
236/// ```
237///
238/// This implementation uses [`::std::collections::BTreeMap`](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html) to count items and sort them.
239///
240/// # Module macros
241///
242/// * [`assert_bag_ne`](macro@crate::assert_bag_ne)
243/// * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
244/// * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
245///
246#[macro_export]
247macro_rules! assert_bag_ne {
248    ($a_collection:expr, $b_collection:expr $(,)?) => {
249        match $crate::assert_bag_ne_as_result!($a_collection, $b_collection) {
250            Ok(x) => x,
251            Err(err) => panic!("{}", err),
252        }
253    };
254    ($a_collection:expr, $b_collection:expr, $($message:tt)+) => {
255        match $crate::assert_bag_ne_as_result!($a_collection, $b_collection) {
256            Ok(x) => x,
257            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
258        }
259    };
260}
261
262#[cfg(test)]
263mod test_assert_bag_ne {
264    use std::collections::BTreeMap;
265    use std::panic;
266
267    #[test]
268    fn success() {
269        let a = [1, 1];
270        let b = [1, 1, 1];
271        for _ in 0..1 {
272            let actual = assert_bag_ne!(a, b);
273            assert_eq!(
274                actual,
275                (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 3)]))
276            );
277        }
278    }
279
280    #[test]
281    fn failure() {
282        let result = panic::catch_unwind(|| {
283            let a = [1, 1];
284            let b = [1, 1];
285            let _actual = assert_bag_ne!(a, b);
286        });
287        let message = concat!(
288            "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
289            "https://docs.rs/assertables/9.8.4/assertables/macro.assert_bag_ne.html\n",
290            " a label: `a`,\n",
291            " a debug: `[1, 1]`,\n",
292            " b label: `b`,\n",
293            " b debug: `[1, 1]`,\n",
294            "   a bag: `{1: 2}`,\n",
295            "   b bag: `{1: 2}`"
296        );
297        assert_eq!(
298            result
299                .unwrap_err()
300                .downcast::<String>()
301                .unwrap()
302                .to_string(),
303            message
304        );
305    }
306}
307
308/// Assert a bag is not equal to another.
309///
310/// Pseudocode:<br>
311/// (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
312///
313/// This macro provides the same statements as [`assert_bag_ne`](macro.assert_bag_ne.html),
314/// except this macro's statements are only enabled in non-optimized
315/// builds by default. An optimized build will not execute this macro's
316/// statements unless `-C debug-assertions` is passed to the compiler.
317///
318/// This macro is useful for checks that are too expensive to be present
319/// in a release build but may be helpful during development.
320///
321/// The result of expanding this macro is always type checked.
322///
323/// An unchecked assertion allows a program in an inconsistent state to
324/// keep running, which might have unexpected consequences but does not
325/// introduce unsafety as long as this only happens in safe code. The
326/// performance cost of assertions, however, is not measurable in general.
327/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
328/// after thorough profiling, and more importantly, only in safe code!
329///
330/// This macro is intended to work in a similar way to
331/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
332///
333/// # Module macros
334///
335/// * [`assert_bag_ne`](macro@crate::assert_bag_ne)
336/// * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
337/// * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
338///
339#[macro_export]
340macro_rules! debug_assert_bag_ne {
341    ($($arg:tt)*) => {
342        if $crate::cfg!(debug_assertions) {
343            $crate::assert_bag_ne!($($arg)*);
344        }
345    };
346}