dicetest 0.4.0

Framework for writing tests with randomly generated test data
Documentation
use std::collections::HashSet;
use std::hash::{BuildHasher, Hash};

use crate::dice::{CollectionBuilder, LengthRange};
use crate::prelude::*;

/// [`HashSet`] builder for [`dice::collection`].
///
/// [`dice::collection`]: dice::collection()
pub struct HashSetBuilder<S>
where
    S: BuildHasher + Clone,
{
    hasher: S,
}

impl<S> HashSetBuilder<S>
where
    S: BuildHasher + Clone,
{
    /// Creates a builder that uses the given [`BuildHasher`] for constructing a [`HashSet`].
    pub fn with_hasher(build_hasher: S) -> Self {
        Self {
            hasher: build_hasher,
        }
    }
}

impl<T, S> CollectionBuilder<T, HashSet<T, S>> for HashSetBuilder<S>
where
    T: Eq + Hash,
    S: BuildHasher + Clone,
{
    fn build(&self, elems: impl ExactSizeIterator<Item = T>) -> HashSet<T, S> {
        let build_hasher = self.hasher.clone();
        let mut set = HashSet::with_capacity_and_hasher(elems.len(), build_hasher);
        set.extend(elems);
        set
    }
}

/// Generates a [`HashSet`] that uses a default pseudorandom [`BuildHasher`] and contains elements
/// of type `T`.
///
/// The range specifies the number of tries to generate distinct elements.
///
/// # Panics
///
/// Panics if the range is empty.
///
/// # Hasher
///
/// [`HashSet`] requires a [`BuildHasher`] which determines the order of elements.
/// The default is [`RandomState`] and it has no deterministic constructor.
///
/// If deterministic behavior is required, you can use [`Prng`] as [`BuildHasher`]. Otherwise,
/// you can construct [`RandomState`] yourself.
///
/// ```
/// use std::collections::hash_map::RandomState;
///
/// use dicetest::prelude::*;
/// use dicetest::{Prng, Limit};
///
/// let mut prng = Prng::from_seed(0x5EED.into());
/// let limit = Limit::default();
/// let mut fate = Fate::new(&mut prng, limit);
///
/// let elem_die = dice::u8(..);
///
/// let hasher = fate.fork_prng();
/// let deterministic_set_die = dice::hash_set(hasher, &elem_die, ..);
///
/// let hasher = RandomState::new(); // Warning: not deterministic!
/// let non_deterministic_set_die = dice::hash_set(hasher, &elem_die, ..);
/// ```
///
/// [`Prng`]: crate::Prng
/// [`RandomState`]: std::collections::hash_map::RandomState
///
/// # Examples
///
/// ```
/// use dicetest::prelude::*;
/// use dicetest::{Prng, Limit};
///
/// let mut prng = Prng::from_seed(0x5EED.into());
/// let limit = Limit::default();
/// let mut fate = Fate::new(&mut prng, limit);
///
/// let elem_die = dice::u8(..);
///
/// let hasher = fate.fork_prng();
/// let set = fate.with_limit(100.into()).roll(dice::hash_set(hasher, &elem_die, ..));
/// assert!(set.len() <= 100);
///
/// let hasher = fate.fork_prng();
/// let set = fate.roll(dice::hash_set(hasher, &elem_die, ..=73));
/// assert!(set.len() <= 73);
///
/// let hasher = fate.fork_prng();
/// let set = fate.roll(dice::hash_set(hasher, &elem_die, 17..));
/// assert!(set.len() >= 17);
///
/// let hasher = fate.fork_prng();
/// let set = fate.roll(dice::hash_set(hasher, &elem_die, 42));
/// assert!(set.len() <= 42);
/// ```
pub fn hash_set<T, S>(
    hasher: S,
    elem_die: impl Die<T>,
    tries_range: impl LengthRange,
) -> impl Die<HashSet<T, S>>
where
    T: Eq + Hash,
    S: BuildHasher + Clone,
{
    dice::collection(HashSetBuilder::with_hasher(hasher), elem_die, tries_range)
}

/// Similar to [`dice::hash_set`] but each element is generated using only a random part of
/// [`Limit`].
///
/// If you want to generate a [`HashSet`] that contains other collections, then you should
/// consider using this generator for the outer [`HashSet`]. That way the overall length is
/// bounded by [`Limit`] (and not the square of [`Limit`]).
///
/// [`Limit`]: crate::Limit
/// [`dice::hash_set`]: dice::hash_set()
///
/// # Panics
///
/// Panics if the range is empty.
///
/// # Examples
///
/// ```
/// use dicetest::prelude::*;
/// use dicetest::{Prng, Limit};
///
/// let mut prng = Prng::from_seed(0x5EED.into());
/// let limit = Limit::default();
/// let mut fate = Fate::new(&mut prng, limit);
///
/// let elem_die = dice::u8(..);
/// let vec_die = dice::vec(elem_die, ..);
/// let hasher = fate.fork_prng();
/// let set_of_vecs_die = dice::outer_hash_set(hasher, vec_die, ..);
///
/// let set_of_vecs = fate.roll(set_of_vecs_die);
/// assert!(set_of_vecs.iter().flatten().count() <= 100);
/// ```
pub fn outer_hash_set<T, S>(
    hasher: S,
    elem_die: impl Die<T>,
    tries_range: impl LengthRange,
) -> impl Die<HashSet<T, S>>
where
    T: Eq + Hash,
    S: BuildHasher + Clone,
{
    dice::outer_collection(HashSetBuilder::with_hasher(hasher), elem_die, tries_range)
}