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_bag, b_bag))`.
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_bag = assert_bag_impl_prep!(a_collection);
48 let b_bag = assert_bag_impl_prep!(b_collection);
49 if a_bag != b_bag {
50 Ok((a_bag, b_bag))
51 } else {
52 Err(
53 format!(
54 concat!(
55 "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
56 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_bag_ne.html\n",
57 " a label: `{}`,\n",
58 " a debug: `{:?}`,\n",
59 " b label: `{}`,\n",
60 " b debug: `{:?}`,\n",
61 " a bag: `{:?}`,\n",
62 " b bag: `{:?}`"
63 ),
64 stringify!($a_collection),
65 a_collection,
66 stringify!($b_collection),
67 b_collection,
68 a_bag,
69 b_bag
70 )
71 )
72 }
73 }
74 }
75 };
76}
77
78#[cfg(test)]
79mod test_assert_bag_ne_as_result {
80 use std::collections::BTreeMap;
81
82 #[test]
83 fn success() {
84 let a = [1, 1];
85 let b = [1, 1, 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 #[test]
94 fn failure() {
95 let a = [1, 1];
96 let b = [1, 1];
97 let actual = assert_bag_ne_as_result!(&a, &b);
98 let message = concat!(
99 "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
100 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_bag_ne.html\n",
101 " a label: `&a`,\n",
102 " a debug: `[1, 1]`,\n",
103 " b label: `&b`,\n",
104 " b debug: `[1, 1]`,\n",
105 " a bag: `{1: 2}`,\n",
106 " b bag: `{1: 2}`"
107 );
108 assert_eq!(actual.unwrap_err(), message);
109 }
110}
111
112/// Assert a bag is not equal to another.
113///
114/// Pseudocode:<br>
115/// (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
116///
117/// * If true, return `(a_bag, b_bag)`.
118///
119/// * Otherwise, call [`panic!`] with a message and the values of the
120/// expressions with their debug representations.
121///
122/// # Examples
123///
124/// ```rust
125/// use assertables::*;
126/// # use std::panic;
127///
128/// # fn main() {
129/// let a = [1, 1];
130/// let b = [1, 1, 1];
131/// assert_bag_ne!(&a, &b);
132///
133/// # let result = panic::catch_unwind(|| {
134/// // This will panic
135/// let a = [1, 1];
136/// let b = [1, 1];
137/// assert_bag_ne!(&a, &b);
138/// # });
139/// // assertion failed: `assert_bag_ne!(a_collection, b_collection)`
140/// // https://docs.rs/assertables/9.5.5/assertables/macro.assert_bag_ne.html
141/// // a label: `&a`,
142/// // a debug: `[1, 1]`,
143/// // b label: `&b`,
144/// // b debug: `[1, 1]`,
145/// // a bag: `{1: 2}`,
146/// // b bag: `{1: 2}`
147/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
148/// # let message = concat!(
149/// # "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
150/// # "https://docs.rs/assertables/9.5.5/assertables/macro.assert_bag_ne.html\n",
151/// # " a label: `&a`,\n",
152/// # " a debug: `[1, 1]`,\n",
153/// # " b label: `&b`,\n",
154/// # " b debug: `[1, 1]`,\n",
155/// # " a bag: `{1: 2}`,\n",
156/// # " b bag: `{1: 2}`"
157/// # );
158/// # assert_eq!(actual, message);
159/// # }
160/// ```
161///
162/// This implementation uses [`::std::collections::BTreeMap`](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html) to count items and sort them.
163///
164/// # Module macros
165///
166/// * [`assert_bag_ne`](macro@crate::assert_bag_ne)
167/// * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
168/// * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
169///
170#[macro_export]
171macro_rules! assert_bag_ne {
172 ($a_collection:expr, $b_collection:expr $(,)?) => {{
173 match $crate::assert_bag_ne_as_result!($a_collection, $b_collection) {
174 Ok(x) => x,
175 Err(err) => panic!("{}", err),
176 }
177 }};
178 ($a_collection:expr, $b_collection:expr, $($message:tt)+) => {{
179 match $crate::assert_bag_ne_as_result!($a_collection, $b_collection) {
180 Ok(x) => x,
181 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
182 }
183 }};
184}
185
186#[cfg(test)]
187mod test_assert_bag_ne {
188 use std::collections::BTreeMap;
189 use std::panic;
190
191 #[test]
192 fn success() {
193 let a = [1, 1];
194 let b = [1, 1, 1];
195 let actual = assert_bag_ne!(&a, &b);
196 assert_eq!(
197 actual,
198 (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 3)]))
199 );
200 }
201
202 #[test]
203 fn failure() {
204 let result = panic::catch_unwind(|| {
205 let a = [1, 1];
206 let b = [1, 1];
207 let _actual = assert_bag_ne!(&a, &b);
208 });
209 let message = concat!(
210 "assertion failed: `assert_bag_ne!(a_collection, b_collection)`\n",
211 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_bag_ne.html\n",
212 " a label: `&a`,\n",
213 " a debug: `[1, 1]`,\n",
214 " b label: `&b`,\n",
215 " b debug: `[1, 1]`,\n",
216 " a bag: `{1: 2}`,\n",
217 " b bag: `{1: 2}`"
218 );
219 assert_eq!(
220 result
221 .unwrap_err()
222 .downcast::<String>()
223 .unwrap()
224 .to_string(),
225 message
226 );
227 }
228}
229
230/// Assert a bag is not equal to another.
231///
232/// Pseudocode:<br>
233/// (a_collection ⇒ a_bag) ≠ (b_collection ⇒ b_bag)
234///
235/// This macro provides the same statements as [`assert_bag_ne`](macro.assert_bag_ne.html),
236/// except this macro's statements are only enabled in non-optimized
237/// builds by default. An optimized build will not execute this macro's
238/// statements unless `-C debug-assertions` is passed to the compiler.
239///
240/// This macro is useful for checks that are too expensive to be present
241/// in a release build but may be helpful during development.
242///
243/// The result of expanding this macro is always type checked.
244///
245/// An unchecked assertion allows a program in an inconsistent state to
246/// keep running, which might have unexpected consequences but does not
247/// introduce unsafety as long as this only happens in safe code. The
248/// performance cost of assertions, however, is not measurable in general.
249/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
250/// after thorough profiling, and more importantly, only in safe code!
251///
252/// This macro is intended to work in a similar way to
253/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
254///
255/// # Module macros
256///
257/// * [`assert_bag_ne`](macro@crate::assert_bag_ne)
258/// * [`assert_bag_ne_as_result`](macro@crate::assert_bag_ne_as_result)
259/// * [`debug_assert_bag_ne`](macro@crate::debug_assert_bag_ne)
260///
261#[macro_export]
262macro_rules! debug_assert_bag_ne {
263 ($($arg:tt)*) => {
264 if $crate::cfg!(debug_assertions) {
265 $crate::assert_bag_ne!($($arg)*);
266 }
267 };
268}