I am trying to write a test which depends on non-empty vector of tuples as an argument but unsure of how to add such a constraint using quickcheck.
#[quickcheck]
fn graph_contains_vertex(vs: Vec<(u64, i64)>) {
// ...
let rand_index = rand::thread_rng().gen_range(0..vs.len());
let rand_vertex = vs.get(rand_index).unwrap().0;
// ...
}
What I am trying to do here is pick up an index from the vector passed as an argument and use first tuple value.
I ran a couple of tests and ended up with a use case where argument passed is []
.
Apparently, we can handle invalid arguments right inside our tests like so:
#[quickcheck]
fn graph_contains_vertex(vs: Vec<(u64, i64)>) -> TestResult {
if vs.is_empty() {
return TestResult::discard();
}
// ..
let rand_index = rand::thread_rng().gen_range(0..vs.len());
let rand_vertex = vs.get(rand_index).unwrap().0;
// ...
TestResult::from_bool(g.contains(rand_vertex))
}
In this particular example, since I was depending on a non-empty vector argument, I could choose to discard the test results when received an empty vector using TestResult::discard()
.