Skip to main content

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.6/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            let expect = (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 3)]));
88            assert_eq!(actual.unwrap(), expect);
89        }
90    }
91
92    //TODO
93    // #[test]
94    // fn lt_once() {
95    //     static A: Once = Once::new();
96    //     fn a() -> [i32; 2] {
97    //         if A.is_completed() {
98    //             panic!("A.is_completed()")
99    //         } else {
100    //             A.call_once(|| {})
101    //         }
102    //         [1, 1]
103    //     }
104
105    //     static B: Once = Once::new();
106    //     fn b() -> [i32; 3] {
107    //         if B.is_completed() {
108    //             panic!("B.is_completed()")
109    //         } else {
110    //             B.call_once(|| {})
111    //         }
112    //         [1, 1, 1]
113    //     }
114
115    //     assert_eq!(A.is_completed(), false);
116    //     assert_eq!(B.is_completed(), false);
117    //     let result = assert_bag_ne_as_result!(a(), b());
118    //     assert!(result.is_ok());
119    //     assert_eq!(A.is_completed(), true);
120    //     assert_eq!(B.is_completed(), true);
121    // }
122
123    #[test]
124    fn gt() {
125        let a = [1, 1, 1];
126        let b = [1, 1];
127        for _ in 0..1 {
128            let actual = assert_bag_ne_as_result!(a, b);
129            let expect = (BTreeMap::from([(&1, 3)]), BTreeMap::from([(&1, 2)]));
130            assert_eq!(actual.unwrap(), expect);
131        }
132    }
133
134    //TODO
135    // #[test]
136    // fn gt_once() {
137    //     static A: Once = Once::new();
138    //     fn a() -> [i32; 3] {
139    //         if A.is_completed() {
140    //             panic!("A.is_completed()")
141    //         } else {
142    //             A.call_once(|| {})
143    //         }
144    //         [1, 1, 1]
145    //     }
146
147    //     static B: Once = Once::new();
148    //     fn b() -> [i32; 2] {
149    //         if B.is_completed() {
150    //             panic!("B.is_completed()")
151    //         } else {
152    //             B.call_once(|| {})
153    //         }
154    //         [1, 1]
155    //     }
156
157    //     assert_eq!(A.is_completed(), false);
158    //     assert_eq!(B.is_completed(), false);
159    //     let result = assert_bag_ne_as_result!(a(), b());
160    //     assert!(result.is_ok());
161    //     assert_eq!(A.is_completed(), true);
162    //     assert_eq!(B.is_completed(), true);
163    // }
164
165    #[test]
166    fn eq() {
167        let a = [1, 1];
168        let b = [1, 1];
169        let actual = assert_bag_ne_as_result!(a, b);
170        let message = concat!(
171            "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
172            "https://docs.rs/assertables/9.8.6/assertables/macro.assert_bag_ne.html\n",
173            " a label: `a`,\n",
174            " a debug: `[1, 1]`,\n",
175            " b label: `b`,\n",
176            " b debug: `[1, 1]`,\n",
177            "   a bag: `{1: 2}`,\n",
178            "   b bag: `{1: 2}`"
179        );
180        assert_eq!(actual.unwrap_err(), message);
181    }
182}
183
184/// Assert a bag is not equal to another.
185///
186/// Pseudocode:<br>
187/// (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
188///
189/// * If true, return `(a, b)`.
190///
191/// * Otherwise, call [`panic!`] with a message and the values of the
192///   expressions with their debug representations.
193///
194/// # Examples
195///
196/// ```rust
197/// use assertables::*;
198/// # use std::panic;
199///
200/// # fn main() {
201/// let a = [1, 1];
202/// let b = [1, 1, 1];
203/// assert_bag_ne!(a, b);
204///
205/// # let result = panic::catch_unwind(|| {
206/// // This will panic
207/// let a = [1, 1];
208/// let b = [1, 1];
209/// assert_bag_ne!(a, b);
210/// # });
211/// // assertion failed: `assert_bag_ne!(a_collection, b_collection)`
212/// // https://docs.rs/assertables/…/assertables/macro.assert_bag_ne.html
213/// //  a label: `a`,
214/// //  a debug: `[1, 1]`,
215/// //  b label: `b`,
216/// //  b debug: `[1, 1]`,
217/// //    a bag: `{1: 2}`,
218/// //    b bag: `{1: 2}`
219/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
220/// # let message = concat!(
221/// #     "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
222/// #     "https://docs.rs/assertables/9.8.6/assertables/macro.assert_bag_ne.html\n",
223/// #     " a label: `a`,\n",
224/// #     " a debug: `[1, 1]`,\n",
225/// #     " b label: `b`,\n",
226/// #     " b debug: `[1, 1]`,\n",
227/// #     "   a bag: `{1: 2}`,\n",
228/// #     "   b bag: `{1: 2}`"
229/// # );
230/// # assert_eq!(actual, message);
231/// # }
232/// ```
233///
234/// This implementation uses [`::std::collections::BTreeMap`](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html) to count items and sort them.
235///
236/// # Module macros
237///
238/// * [`assert_bag_ne`](macro@crate::assert_bag_ne)
239/// * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
240/// * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
241///
242#[macro_export]
243macro_rules! assert_bag_ne {
244    ($a_collection:expr, $b_collection:expr $(,)?) => {
245        match $crate::assert_bag_ne_as_result!($a_collection, $b_collection) {
246            Ok(x) => x,
247            Err(err) => panic!("{}", err),
248        }
249    };
250    ($a_collection:expr, $b_collection:expr, $($message:tt)+) => {
251        match $crate::assert_bag_ne_as_result!($a_collection, $b_collection) {
252            Ok(x) => x,
253            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
254        }
255    };
256}
257
258#[cfg(test)]
259mod test_assert_bag_ne {
260    use std::collections::BTreeMap;
261    use std::panic;
262
263    #[test]
264    fn success() {
265        let a = [1, 1];
266        let b = [1, 1, 1];
267        for _ in 0..1 {
268            let actual = assert_bag_ne!(a, b);
269            let expect = (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 3)]));
270            assert_eq!(actual, expect);
271        }
272    }
273
274    #[test]
275    fn failure() {
276        let result = panic::catch_unwind(|| {
277            let a = [1, 1];
278            let b = [1, 1];
279            let _actual = assert_bag_ne!(a, b);
280        });
281        let message = concat!(
282            "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
283            "https://docs.rs/assertables/9.8.6/assertables/macro.assert_bag_ne.html\n",
284            " a label: `a`,\n",
285            " a debug: `[1, 1]`,\n",
286            " b label: `b`,\n",
287            " b debug: `[1, 1]`,\n",
288            "   a bag: `{1: 2}`,\n",
289            "   b bag: `{1: 2}`"
290        );
291        assert_eq!(
292            result
293                .unwrap_err()
294                .downcast::<String>()
295                .unwrap()
296                .to_string(),
297            message
298        );
299    }
300}
301
302/// Assert a bag is not equal to another.
303///
304/// Pseudocode:<br>
305/// (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
306///
307/// This macro provides the same statements as [`assert_bag_ne`](macro.assert_bag_ne.html),
308/// except this macro's statements are only enabled in non-optimized
309/// builds by default. An optimized build will not execute this macro's
310/// statements unless `-C debug-assertions` is passed to the compiler.
311///
312/// This macro is useful for checks that are too expensive to be present
313/// in a release build but may be helpful during development.
314///
315/// The result of expanding this macro is always type checked.
316///
317/// An unchecked assertion allows a program in an inconsistent state to
318/// keep running, which might have unexpected consequences but does not
319/// introduce unsafety as long as this only happens in safe code. The
320/// performance cost of assertions, however, is not measurable in general.
321/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
322/// after thorough profiling, and more importantly, only in safe code!
323///
324/// This macro is intended to work in a similar way to
325/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
326///
327/// # Module macros
328///
329/// * [`assert_bag_ne`](macro@crate::assert_bag_ne)
330/// * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
331/// * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
332///
333#[macro_export]
334macro_rules! debug_assert_bag_ne {
335    ($($arg:tt)*) => {
336        if cfg!(debug_assertions) {
337            $crate::assert_bag_ne!($($arg)*);
338        }
339    };
340}
341
342#[cfg(test)]
343mod test_debug_assert_bag_ne {
344    use std::collections::BTreeMap;
345    use std::panic;
346
347    #[test]
348    fn success() {
349        let a = [1, 1];
350        let b = [1, 1, 1];
351        for _ in 0..1 {
352            let _actual = debug_assert_bag_ne!(a, b);
353            let _expect = (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 3)]));
354            // assert_eq!(actual, expect);
355        }
356    }
357
358    #[test]
359    fn failure() {
360        let result = panic::catch_unwind(|| {
361            let a = [1, 1];
362            let b = [1, 1];
363            let _actual = debug_assert_bag_ne!(a, b);
364        });
365        let message = concat!(
366            "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
367            "https://docs.rs/assertables/9.8.6/assertables/macro.assert_bag_ne.html\n",
368            " a label: `a`,\n",
369            " a debug: `[1, 1]`,\n",
370            " b label: `b`,\n",
371            " b debug: `[1, 1]`,\n",
372            "   a bag: `{1: 2}`,\n",
373            "   b bag: `{1: 2}`"
374        );
375        assert_eq!(
376            result
377                .unwrap_err()
378                .downcast::<String>()
379                .unwrap()
380                .to_string(),
381            message
382        );
383    }
384}