Trait qcheck::Arbitrary

source ·
pub trait Arbitrary: Clone + 'static {
    fn arbitrary(g: &mut Gen) -> Self;

    fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { ... }
}
Expand description

Arbitrary describes types whose values can be randomly generated and shrunk.

Aside from shrinking, Arbitrary is different from typical RNGs in that it respects Gen::size() for controlling how much memory a particular value uses, for practical purposes. For example, Vec::arbitrary() respects Gen::size() to decide the maximum len() of the vector. This behavior is necessary due to practical speed and size limitations. Conversely, i32::arbitrary() ignores size() since all i32 values require O(1) memory and operations between i32s require O(1) time (with the exception of exponentiation).

Additionally, all types that implement Arbitrary must also implement Clone.

Required Methods§

Return an arbitrary value.

Implementations should respect Gen::size() when decisions about how big a particular value should be. Implementations should generally defer to other Arbitrary implementations to generate other random values when necessary. The Gen type also offers a few RNG helper routines.

Provided Methods§

Return an iterator of values that are smaller than itself.

The way in which a value is “smaller” is implementation defined. In some cases, the interpretation is obvious: shrinking an integer should produce integers smaller than itself. Others are more complex, for example, shrinking a Vec should both shrink its size and shrink its component values.

The iterator returned should be bounded to some reasonable size.

It is always correct to return an empty iterator, and indeed, this is the default implementation. The downside of this approach is that witnesses to failures in properties will be more inscrutable.

Implementations on Foreign Types§

Implementors§