monkey_test 0.9.2

A property based testing (PBT) tool like QuickCheck, ScalaCheck and similar libraries, for the Rust programming language.
Documentation
//! Generators for boolean type.

use crate::BoxGen;

/// Generator of boolean values where ratio can be scewed according to given
/// ratios.
pub fn with_ratio(ratio_false: u8, ratio_true: u8) -> BoxGen<bool> {
    crate::gens::pick_with_ratio(&[(ratio_false, false), (ratio_true, true)])
        .with_shrinker(crate::shrinks::bool())
}

/// Uniformly distributed generator of `true` and `false`.
pub fn any() -> BoxGen<bool> {
    with_ratio(1, 1)
}

#[cfg(test)]
mod tests {
    use crate::testing::assert_iter_eq;
    use crate::testing::distribution::assert_generator_has_distribution_within_percent;
    use crate::testing::distribution::distribution_from_pairs;
    use crate::testing::distribution::even_distribution_of;

    #[test]
    fn any_has_uniform_distribution() {
        let bools = super::any();

        let expected = even_distribution_of(&[false, true]);

        assert_generator_has_distribution_within_percent(bools, expected, 1.0)
    }

    #[test]
    fn with_ratio_has_distribution_as_specified() {
        let bools = super::with_ratio(9, 1);

        let expected = distribution_from_pairs(&[(9, false), (1, true)]);

        assert_generator_has_distribution_within_percent(bools, expected, 1.0)
    }

    #[test]
    fn has_shrinker_that_shrinks_to_false() {
        let shrinker = super::any().shrinker();

        assert_iter_eq(
            shrinker.candidates(true),
            vec![false],
            "shrinking from 'true' to 'false'",
        );

        assert_iter_eq(
            shrinker.candidates(false),
            vec![],
            "no more to shrink to after 'false'",
        );
    }
}