Function contrafact::brute[][src]

pub fn brute<T, F, S>(reason: S, f: F) -> BruteFact<'static, T> where
    S: ToString,
    T: Bounds,
    F: 'static + Fn(&T) -> bool
Expand description

A constraint defined only by a predicate closure. Mutation occurs by brute force, randomly trying values until one matches the constraint.

This is appropriate to use when the space of possible values is small, and you can rely on randomness to eventually find a value that matches the constraint through sheer brute force, e.g. when requiring a particular enum variant.

NOTE: When doing mutation, this constraint can do no better than brute force when finding data that satisfies the constraint. Therefore, if the predicate is unlikely to return true given arbitrary data, this constraint is a bad choice!

ALSO NOTE: It is usually best to place this constraint at the beginning of a chain when doing mutation, because if the closure specifies a weak constraint, the mutation may drastically alter the data, potentially undoing constraints that were met by previous mutations.

There is a fixed iteration limit, beyond which this will panic.

use arbitrary::Unstructured;
use contrafact::*;

fn div_by(n: usize) -> Facts<'static, usize> {
    facts![brute(format!("Is divisible by {}", n), move |x| x % n == 0)]
}

let mut u = Unstructured::new(&[0; 9999]);
assert!(div_by(3).build(&mut u) % 3 == 0);