1
0
mirror of https://github.com/bspeice/qadapt synced 2024-11-21 13:28:11 -05:00
qadapt/tests/allocations.rs

106 lines
2.0 KiB
Rust
Raw Permalink Normal View History

use qadapt::enter_protected;
use qadapt::exit_protected;
use qadapt::protection_level;
2018-11-10 21:59:39 -05:00
use qadapt::QADAPT;
#[global_allocator]
static Q: QADAPT = QADAPT;
#[test]
fn test_copy() {
enter_protected();
2018-11-15 20:16:49 -05:00
let v = 0u8;
2018-11-15 23:08:02 -05:00
let _v2 = v;
exit_protected();
}
#[test]
2018-11-18 21:29:32 -05:00
#[cfg_attr(debug_assertions, should_panic)]
fn test_allocate() {
enter_protected();
let _x = Box::new(12);
exit_protected();
2018-11-06 20:51:44 -05:00
}
2018-11-15 23:08:02 -05:00
fn return_unit_result(b: bool) -> Result<(), ()> {
2018-11-06 20:51:44 -05:00
if b {
Ok(())
} else {
Err(())
}
}
#[test]
2018-11-15 23:08:02 -05:00
fn unit_result() {
enter_protected();
2018-11-15 23:08:02 -05:00
return_unit_result(true).unwrap();
return_unit_result(false).unwrap_err();
exit_protected();
2018-11-06 20:51:44 -05:00
}
#[test]
#[should_panic]
2018-11-15 23:08:02 -05:00
fn vec_push() {
2018-11-06 20:51:44 -05:00
let mut v = Vec::new();
enter_protected();
2018-11-06 20:51:44 -05:00
v.push(0);
// We don't make it here in debug mode, but in release mode,
// pushing one element doesn't trigger an allocation. Instead,
// we use a box to force it onto the heap
assert_eq!(protection_level(), 1);
let _b = Box::new(v);
2018-11-06 20:51:44 -05:00
}
#[test]
2018-11-15 23:08:02 -05:00
fn vec_push_capacity() {
2018-11-06 20:51:44 -05:00
let mut v = Vec::with_capacity(1);
enter_protected();
2018-11-06 20:51:44 -05:00
v.push(0);
v.pop();
v.push(0);
exit_protected();
2018-11-06 20:51:44 -05:00
}
#[test]
2018-11-15 23:08:02 -05:00
fn vec_with_zero() {
enter_protected();
2018-11-15 20:16:49 -05:00
let _v: Vec<u8> = Vec::with_capacity(0);
exit_protected();
2018-11-06 20:51:44 -05:00
}
#[test]
2018-11-15 23:08:02 -05:00
fn vec_new() {
enter_protected();
2018-11-15 20:16:49 -05:00
let _v: Vec<u8> = Vec::new();
exit_protected();
2018-11-06 20:51:44 -05:00
}
#[test]
#[should_panic]
2018-11-15 23:08:02 -05:00
fn vec_with_one() {
enter_protected();
let v: Vec<u8> = Vec::with_capacity(1);
// We don't make it here in debug mode, but in release mode,
// pushing one element doesn't trigger an allocation. Instead,
// we use a box to force it onto the heap
assert_eq!(protection_level(), 1);
let _b = Box::new(v);
2018-11-10 21:54:13 -05:00
}
#[test]
2018-11-18 21:29:32 -05:00
#[cfg_attr(debug_assertions, should_panic)]
2018-11-10 21:54:13 -05:00
fn exit_too_often() {
enter_protected();
exit_protected();
exit_protected();
2018-11-10 21:59:39 -05:00
}
2018-11-15 20:16:49 -05:00
#[test]
2018-11-18 21:29:32 -05:00
#[cfg_attr(debug_assertions, should_panic)]
2018-11-15 20:16:49 -05:00
fn intentional_drop() {
let v: Vec<()> = Vec::new();
let v = Box::new(v);
enter_protected();
drop(v);
}