Supposedly a property test library

By: on November 8, 2017

Over the past few weeks, I’ve been in­spired to create a new prop­erty testing lib­rary for rust, very much in­spired by the work in hy­po­thesis.

Why use sup­pos­i­tions over say, quickcheck? For one, this takes in­spir­a­tion from hy­po­thesis and theft. While it’s still in it’s early days, the gen­er­ator system (in­spired by hy­po­thesis’ gen­er­ators means that you don’t need a seperate shrinking mech­anism for each data­type.

To give an ex­ample, I ported my pre­vious die-hard puzzle to this lib­rary, and while it’s fairly sim­ilar, there are a few changes:

fn ops() -> Box<GeneratorObject<Item = Op>> {
    let g = one_of(consts(Op::FillSmallJug))
        .or(consts(Op::FillBigJug))
        .or(consts(Op::EmptySmallJug))
        .or(consts(Op::EmptyBigJug))
        .or(consts(Op::SmallToBig))
        .or(consts(Op::BigToSmall));
    Box::new(g)
}

fn main() {
    let gen = vecs(ops()).mean_length(1000);
    let config = CheckConfig::default().num_tests(10000);
    config.property(gen)
        .check(|xs| {
            debug!("Testing: {:?}", xs);
            let mut st = State::default();
            for o in xs.iter() {
                st.apply(o);
                st.assert_invariants();
                if st.finished() {
                    debug!("Success! {:?}", st);
                    return Err(st);
                }
            return Ok(());
        });
    panic!("No solution found")
}

The main changes are that we don’t need a custom Ar­bit­rary in­stance for the Op type, as we can build it using com­bin­at­ors, and that the test is ex­pressed as a rust clos­ure, rather than re­quiring a free func­tion.

In terms of what’s im­ple­men­ted, we have gen­er­ators for most of rust’s prim­itive types, com­bin­ators for same, and a fair few ex­amples. It’s far from com­plete though, so what’s offered is more in the spirit of a tech­nical pre­view to in­ter­ested parties than a full fledged re­lease. Feel free to have a look at sup­pos­i­tions on crates.io and poke about the sup­pos­i­tions re­pos­itory. But I’d wel­come input on the design and im­ple­ment­a­tion.

Share

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*