-
Notifications
You must be signed in to change notification settings - Fork 2
/
leaky_tests.rs
34 lines (30 loc) · 953 Bytes
/
leaky_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#[global_allocator]
static CHECKED: checkers::Allocator = checkers::Allocator::system();
#[checkers::test]
#[should_panic]
fn test_leak_box() {
let x = Box::into_raw(Box::new(0u128));
// Prevent optimization in `--release`
unsafe {
std::ptr::write_volatile(x, 1u128);
}
}
#[checkers::test]
fn test_non_leak_box() {
let b = Box::into_raw(Box::new(0u128));
let _ = unsafe { Box::from_raw(b) };
}
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 x = Box::into_raw(vec![1, 2, 3, 4, 5].into_boxed_slice());
// Prevent optimization in `--release`
unsafe {
std::ptr::write_volatile(x as _, 6);
}
}