Attribute Macro checkers::test

source ·
#[test]
Expand description

Run a #[test] function in checkers.

At the end of the test case checkers checks memory sanitation and reports any errors encountered.

Attributes

The test macro has the following attributes:

  • capacity - Reserve capacity for the specified number of events beforehand. Checkers will otherwise grow it as necessary using the system allocator directly.
  • verify - Use a custom verification function (see below).

Examples

#[global_allocator]
static ALLOCATOR: checkers::Allocator = checkers::Allocator::system();

#[checkers::test]
fn test_leaky_box() {
    let _ = Box::into_raw(Box::new(42));
}

Reserve capacity for the specified number of events up front:

#[global_allocator]
static ALLOCATOR: checkers::Allocator = checkers::Allocator::system();

#[checkers::test(capacity = 10_000)]
fn test_custom_verify() {
    for i in 0..1000 {
        let v = Box::into_raw(vec![1, 2, 3, 4, 5].into_boxed_slice());
        let _ = unsafe { Box::from_raw(v) };
    }
}

Using a custom verifier:

#[global_allocator]
static ALLOCATOR: checkers::Allocator = checkers::Allocator::system();

fn verify_test_custom_verify(state: &mut checkers::State) {
   let mut violations = Vec::new();
   state.validate(&mut violations);
   assert_eq!(1, violations.len());
   assert!(violations[0].is_leaked_with(|region| region.size == 20 && region.align == 4));
}

#[checkers::test(verify = "verify_test_custom_verify")]
fn test_custom_verify() {
    let _ = Box::into_raw(vec![1, 2, 3, 4, 5].into_boxed_slice());
}