#testing #minimalist #pub #fn #status #j #property-based

arbtest

A minimalist property-based testing library based on arbitrary

3 releases

Uses new Rust 2021

0.1.2 Apr 10, 2022
0.1.1 Apr 10, 2022
0.1.0 Apr 10, 2022

MIT/Apache

10KB
201 lines

arbtest

A minimalist property-based testing library based on the arbitrary crate.

Maintainance Status

Arbitrary.

Example

pub fn buggy_sort(xs: &mut [u8]) {
    for i in 0..xs.len() {
        for j in 0..i {
            if xs[i] == xs[j] {
                panic!("BUG")
            }
        }
    }
    xs.sort()
}

#[cfg(test)]
mod tests {
    use super::*;

    use arbtest::arbitrary::{self, Unstructured};

    fn prop(u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
        let mut xs = u.arbitrary::<Vec<u8>>()?;
        buggy_sort(&mut xs);
        Ok(())
    }

    #[test]
    fn test() {
        arbtest::builder().budget_ms(50_000)
            .run(|u| prop(u))
    }

    #[test]
    fn reproduce() {
        arbtest::builder().seed(0xde0ad94600000001)
            .run(|u| prop(u))
    }

    #[test]
    fn minimize() {
        arbtest::builder().seed(0x2d5a75df00003e9a).minimize()
            .run(|u| prop(u))
    }
}

lib.rs:

arbtest is a minimalist property-based testing library, waiting for me to write proper docs.

In the meantime, take a look at the following example:

fn buggy_sort(xs: &mut [u8]) {
    for i in 0..xs.len() {
        for j in 0..i {
            if xs[i] == xs[j] {
                panic!("BUG")
            }
        }
    }
    xs.sort()
}

#[cfg(test)]
mod tests {
    use super::*;

    use arbtest::arbitrary::{self, Unstructured};

    fn prop(u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
        let mut xs = u.arbitrary::<Vec<u8>>()?;
        buggy_sort(&mut xs);
        Ok(())
    }

    #[test]
    fn test() {
        arbtest::builder().budget_ms(50_000)
            .run(|u| prop(u))
    }

    #[test]
    fn reproduce() {
        arbtest::builder().seed(0xde0ad94600000001)
            .run(|u| prop(u))
    }

    #[test]
    fn minimize() {
        arbtest::builder().seed(0x2d5a75df00003e9a).minimize()
            .run(|u| prop(u))
    }
}

Notes:

  • You can use ARBTEST_BUDGET_MS to adjust time budget without recompilation.
  • While we are waiting for the docs, studying the source might be helpful, it's short!
  • If you like this crate, you might enjoy https://github.com/graydon/exhaustigen-rs as well.

Dependencies

~180KB