vendredi 12 août 2016

Whats the best way to count indirect function calls for tests in Rust?

When writing a test, I would like to know how many times a function is called, since bad logic may yield a correct result, (even when excessive & unnecessary function calls are performed).

(To give some context, this happens to be a tree-search functions, running a test on a fixed data-set, however that isn't important to the answer).

Currently I'm using a static mutable variable, however this means every access needs to be marked as unsafe.

#[cfg(test)]
static mut total_calls: usize = 0;

fn function_to_count() {

#[cfg(test)]
    unsafe {
        total_calls += 1;
    }

    // do stuff

}


#[test]
fn some_test() {
    // do stuff, indirectly call function_to_count().

    assert!(total_calls < 100);
}

It would be good to avoid having to put unsafe into the code.

Is there a better way to count indirect function calls in Rust?

Aucun commentaire:

Enregistrer un commentaire