assertables/assert_set/
assert_set_disjoint.rs

1//! Assert a set is disjoint with another.
2//!
3//! Pseudocode:<br>
4//! (a_collection ⇒ a_set) ⨃ (b_collection ⇒ b_set)
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = [1, 2];
12//! let b = [3, 4];
13//! assert_set_disjoint!(&a, &b);
14//! ```
15//!
16//! This implementation uses [`::std::collections::BTreeSet`](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html) to count items and sort them.
17//!
18//! # Module macros
19//!
20//! * [`assert_set_disjoint`](macro@crate::assert_set_disjoint)
21//! * [`assert_set_disjoint_as_result`](macro@crate::assert_set_disjoint_as_result)
22//! * [`debug_assert_set_disjoint`](macro@crate::debug_assert_set_disjoint)
23
24/// Assert a set is disjoint with another.
25///
26/// Pseudocode:<br>
27/// (a_collection ⇒ a_set) ⨃ (b_collection ⇒ b_set)
28///
29/// * If true, return Result `Ok((a_set, b_set))`.
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_set_disjoint`](macro@crate::assert_set_disjoint)
39/// * [`assert_set_disjoint_as_result`](macro@crate::assert_set_disjoint_as_result)
40/// * [`debug_assert_set_disjoint`](macro@crate::debug_assert_set_disjoint)
41///
42#[macro_export]
43macro_rules! assert_set_disjoint_as_result {
44    ($a_collection:expr, $b_collection:expr $(,)?) => {{
45        match (&$a_collection, &$b_collection) {
46            (a_collection, b_collection) => {
47                let a: ::std::collections::BTreeSet<_> = assert_set_impl_prep!(a_collection);
48                let b: ::std::collections::BTreeSet<_> = assert_set_impl_prep!(b_collection);
49                if a.is_disjoint(&b) {
50                    Ok((a, b))
51                } else {
52                    Err(
53                        format!(
54                            concat!(
55                                "assertion failed: `assert_set_disjoint!(a_collection, b_collection)`\n",
56                                "https://docs.rs/assertables/9.5.3/assertables/macro.assert_set_disjoint.html\n",
57                                " a label: `{}`,\n",
58                                " a debug: `{:?}`,\n",
59                                " b label: `{}`,\n",
60                                " b debug: `{:?}`,\n",
61                                "       a: `{:?}`,\n",
62                                "       b: `{:?}`"
63                            ),
64                            stringify!($a_collection),
65                            a_collection,
66                            stringify!($b_collection),
67                            b_collection,
68                            a,
69                            b
70                        )
71                    )
72                }
73            }
74        }
75    }};
76}
77
78#[cfg(test)]
79mod test_assert_set_disjoint_as_result {
80    use std::collections::BTreeSet;
81
82    #[test]
83    fn success() {
84        let a = [1, 2];
85        let b = [3, 4];
86        let actual = assert_set_disjoint_as_result!(&a, &b);
87        assert_eq!(
88            actual.unwrap(),
89            (BTreeSet::from([&1, &2]), BTreeSet::from([&3, &4]))
90        );
91    }
92
93    #[test]
94    fn failure() {
95        let a = [1, 2];
96        let b = [2, 3];
97        let actual = assert_set_disjoint_as_result!(&a, &b);
98        let message = concat!(
99            "assertion failed: `assert_set_disjoint!(a_collection, b_collection)`\n",
100            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_set_disjoint.html\n",
101            " a label: `&a`,\n",
102            " a debug: `[1, 2]`,\n",
103            " b label: `&b`,\n",
104            " b debug: `[2, 3]`,\n",
105            "       a: `{1, 2}`,\n",
106            "       b: `{2, 3}`"
107        );
108        assert_eq!(actual.unwrap_err(), message);
109    }
110}
111
112/// Assert a set is disjoint with another.
113///
114/// Pseudocode:<br>
115/// (a_collection ⇒ a_set) ⨃ (b_collection ⇒ b_set)
116///
117/// * If true, return `(a_set, b_set)`.
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, 2];
130/// let b = [3, 4];
131/// assert_set_disjoint!(&a, &b);
132///
133/// # let result = panic::catch_unwind(|| {
134/// // This will panic
135/// let a = [1, 2];
136/// let b = [2, 3];
137/// assert_set_disjoint!(&a, &b);
138/// # });
139/// // assertion failed: `assert_set_disjoint!(a_collection, b_collection)`
140/// // https://docs.rs/assertables/9.5.3/assertables/macro.assert_set_disjoint.html
141/// //  a label: `&a`,
142/// //  a debug: `[1, 2]`,
143/// //  b label: `&b`,
144/// //  b debug: `[2, 3]`,
145/// //        a: `{1, 2}`,
146/// //        b: `{2, 3}`
147/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
148/// # let message = concat!(
149/// #     "assertion failed: `assert_set_disjoint!(a_collection, b_collection)`\n",
150/// #     "https://docs.rs/assertables/9.5.3/assertables/macro.assert_set_disjoint.html\n",
151/// #     " a label: `&a`,\n",
152/// #     " a debug: `[1, 2]`,\n",
153/// #     " b label: `&b`,\n",
154/// #     " b debug: `[2, 3]`,\n",
155/// #     "       a: `{1, 2}`,\n",
156/// #     "       b: `{2, 3}`"
157/// # );
158/// # assert_eq!(actual, message);
159/// # }
160/// ```
161///
162/// This implementation uses [`::std::collections::BTreeSet`](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html) to count items and sort them.
163///
164/// # Module macros
165///
166/// * [`assert_set_disjoint`](macro@crate::assert_set_disjoint)
167/// * [`assert_set_disjoint_as_result`](macro@crate::assert_set_disjoint_as_result)
168/// * [`debug_assert_set_disjoint`](macro@crate::debug_assert_set_disjoint)
169///
170#[macro_export]
171macro_rules! assert_set_disjoint {
172    ($a_collection:expr, $b_collection:expr $(,)?) => {{
173        match $crate::assert_set_disjoint_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_set_disjoint_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_set_disjoint {
188    use std::collections::BTreeSet;
189    use std::panic;
190
191    #[test]
192    fn success() {
193        let a = [1, 2];
194        let b = [3, 4];
195        let actual = assert_set_disjoint!(&a, &b);
196        assert_eq!(actual, (BTreeSet::from([&1, &2]), BTreeSet::from([&3, &4])));
197    }
198
199    #[test]
200    fn failure() {
201        let a = [1, 2];
202        let b = [2, 3];
203        let result = panic::catch_unwind(|| {
204            let _actual = assert_set_disjoint!(&a, &b);
205        });
206        let message = concat!(
207            "assertion failed: `assert_set_disjoint!(a_collection, b_collection)`\n",
208            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_set_disjoint.html\n",
209            " a label: `&a`,\n",
210            " a debug: `[1, 2]`,\n",
211            " b label: `&b`,\n",
212            " b debug: `[2, 3]`,\n",
213            "       a: `{1, 2}`,\n",
214            "       b: `{2, 3}`"
215        );
216        assert_eq!(
217            result
218                .unwrap_err()
219                .downcast::<String>()
220                .unwrap()
221                .to_string(),
222            message
223        );
224    }
225}
226
227/// Assert a set is disjoint with another.
228///
229/// Pseudocode:<br>
230/// (a_collection ⇒ a_set) ⨃ (b_collection ⇒ b_set)
231///
232/// This macro provides the same statements as [`assert_set_disjoint`](macro.assert_set_disjoint.html),
233/// except this macro's statements are only enabled in non-optimized
234/// builds by default. An optimized build will not execute this macro's
235/// statements unless `-C debug-assertions` is passed to the compiler.
236///
237/// This macro is useful for checks that are too expensive to be present
238/// in a release build but may be helpful during development.
239///
240/// The result of expanding this macro is always type checked.
241///
242/// An unchecked assertion allows a program in an inconsistent state to
243/// keep running, which might have unexpected consequences but does not
244/// introduce unsafety as long as this only happens in safe code. The
245/// performance cost of assertions, however, is not measurable in general.
246/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
247/// after thorough profiling, and more importantly, only in safe code!
248///
249/// This macro is intended to work in a similar way to
250/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
251///
252/// # Module macros
253///
254/// * [`assert_set_disjoint`](macro@crate::assert_set_disjoint)
255/// * [`assert_set_disjoint`](macro@crate::assert_set_disjoint)
256/// * [`debug_assert_set_disjoint`](macro@crate::debug_assert_set_disjoint)
257///
258#[macro_export]
259macro_rules! debug_assert_set_disjoint {
260    ($($arg:tt)*) => {
261        if $crate::cfg!(debug_assertions) {
262            $crate::assert_set_disjoint!($($arg)*);
263        }
264    };
265}