Skip to content

Generated Vecs are really small #8

@jakoschiko

Description

@jakoschiko

To my surprise this test doesn't fail (I ran it ~10 times):

#[test]
fn my_test() {
    arbtest::arbtest(|u| {
        let x: Vec<u8> = u.arbitrary()?;
        assert!(x.len() < 10);
        Ok(())
    })
    .budget_ms(10000) // Try really hard
    .size_min(100000) // Provide a lot of bytes 
    .size_max(100000000);
}

I know, property-based testing is very limited. But this limit seems to be quite small (in comparison to something like quickcheck). And the limit can't be extended easily by increasing the budget or size parameters.

I checked the implementation of Arbitrary for Vec in the arbitrary crate:

impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        u.arbitrary_iter()?.collect()
    }
    ...
}

And here the implementation of the Iterator returned by arbitrary_iter:

impl<'a, 'b, ElementType: Arbitrary<'a>> Iterator for ArbitraryIter<'a, 'b, ElementType> {
    type Item = Result<ElementType>;
    fn next(&mut self) -> Option<Result<ElementType>> {
        let keep_going = self.u.arbitrary().unwrap_or(false);
        if keep_going {
            Some(Arbitrary::arbitrary(self.u))
        } else {
            None
        }
    }
}

As far as I know arbtest is using random bytes for Unstructured (instead of using a feedback loop like a fuzzer). Based on the implementation above I can understand why the generated Vecs are so small. But still, it doesn't feel right. Did I do something wrong?

Tested with arbtest 0.3.1 and arbitrary 1.3.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions