use assert_no_alloc::*;
use std::panic::catch_unwind;
#[global_allocator]
static A: AllocDisabler = AllocDisabler;
#[cfg(not(feature = "warn_debug"))]
compile_error!("The test suite requires the warn_debug feature to be enabled. Use `cargo test --features warn_debug`");
#[cfg(not(debug_assertions))]
compile_error!("The test suite only works in debug mode. Use `cargo test --features warn_debug`");
#[cfg(feature = "warn_debug")]
fn check_and_reset() -> bool {
let result = violation_count() > 0;
reset_violation_count();
result
}
#[cfg(not(feature = "warn_debug"))]
fn check_and_reset() -> bool { unreachable!() }
fn do_alloc() {
let _tmp: Box<u32> = Box::new(42);
}
#[test]
fn ok_noop() {
assert_eq!(check_and_reset(), false);
do_alloc();
assert_eq!(check_and_reset(), false);
}
#[test]
fn ok_simple() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
});
do_alloc();
assert_eq!(check_and_reset(), false);
}
#[test]
fn ok_nested() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
assert_no_alloc(|| {
});
});
do_alloc();
assert_eq!(check_and_reset(), false);
}
#[test]
fn forbidden_simple() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
do_alloc();
});
assert_eq!(check_and_reset(), true);
}
#[test]
fn forbidden_in_nested() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
assert_no_alloc(|| {
do_alloc();
});
});
assert_eq!(check_and_reset(), true);
}
#[test]
fn forbidden_after_nested() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
assert_no_alloc(|| {
});
do_alloc();
});
assert_eq!(check_and_reset(), true);
}
#[test]
fn unwind_ok() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
let r = catch_unwind(|| {
assert_no_alloc(|| {
panic!();
});
});
assert!(r.is_err());
});
check_and_reset(); do_alloc();
assert_eq!(check_and_reset(), false);
}
#[test]
fn unwind_nested() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
let r = catch_unwind(|| {
assert_no_alloc(|| {
panic!();
});
});
assert!(r.is_err());
check_and_reset(); do_alloc();
assert_eq!(check_and_reset(), true);
});
}
#[test]
fn unwind_nested2() {
assert_eq!(check_and_reset(), false);
assert_no_alloc(|| {
assert_no_alloc(|| {
let r = catch_unwind(|| {
assert_no_alloc(|| {
assert_no_alloc(|| {
panic!();
});
});
});
assert!(r.is_err());
check_and_reset(); do_alloc();
assert_eq!(check_and_reset(), true);
});
});
check_and_reset(); do_alloc();
assert_eq!(check_and_reset(), false);
}