[][src]Attribute Macro checkers_macros::test

#[test]

Marks a function to be run as a test in a checkers test suite.

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 CHECKED: checkers::Allocator = checkers::Allocator;

#[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 CHECKED: checkers::Allocator = checkers::Allocator;

#[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 CHECKED: checkers::Allocator = checkers::Allocator;

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_dangling_region(|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());
}