Module assert_set

Source
Expand description

Assert for comparing set collections.

These macros help with comparison of set parameters, such as two arrays or two vectors. where the item order does not matter, and the item count does not matter. These macros convert their inputs into HashSet iterators. See tutorial below.

For eq & ne:

For subset & superset:

For joint & disjoint:

§Example

use assertables::*;

let a = [1, 2];
let b = [2, 1];
assert_set_eq!(&a, &b);

§Tutorial

A set means a collection of elements, without any ordering, without duplicate elements.

A set is sometimes written by using mathematical notation, which looks like this:

set = {1, 2, 3}

The definition of a set includes never having duplicate elements:

set = {1, 2, 3, 2} // error because the element 2 is a duplicate

Sets are equal when they contain all the same elements in any order:

{1, 2, 3} = {1, 2, 3} (same order)
{1, 2, 3} = {3, 2, 1} (different order)

Sets are not equal when they contain any different elements:

{1, 2, 3} ≠ {1, 2}
{1, 2, 3} ≠ {1, 2, 3, 4}

To create a set using Rust, one way is to create an array or vector, then convert it into an iterator by using the method into_iter, then convert the elements into a set by using std::collections::BTreeSet:

let array = [1, 2, 3];
let set: BTreeSet<_> = array.into_iter().collect();

To compare two arrays as sets, one way is to convert each array to a set, then use assert_eq! to compare the sets:

let array1 = [1, 2, 3];
let array2 = [3, 2, 1];
let set1: BTreeSet<_> = array1.into_iter().collect();
let set2: BTreeSet<_> = array2.into_iter().collect();
assert_eq!(set1, set2);

The assertables crate provides macros that do the conversion for you:

let array1 = [1, 2, 3];
let array2 = [3, 2, 1];
assert_set_eq!(array1, array2);

Modules§

assert_set_disjoint
Assert a set is disjoint with another.
assert_set_eq
Assert a set is equal to another.
assert_set_joint
Assert a set is joint with another.
assert_set_ne
Assert a set is not equal to another.
assert_set_subset
Assert a set is a subset of another.
assert_set_superset
Assert a set is a superset of another.