1
0
mirror of https://github.com/bspeice/qadapt synced 2024-11-14 18:18:08 -05:00
qadapt/tests/allocations.rs

113 lines
2.2 KiB
Rust
Raw Normal View History

#![feature(asm)]
extern crate qadapt;
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;
pub fn black_box<T>(dummy: T) -> T {
// Taken from test lib, need to mark the arg as non-introspectable
unsafe { asm!("" : : "r"(&dummy)) }
dummy
}
#[test]
fn test_copy() {
enter_protected();
black_box(0u8);
exit_protected();
}
#[test]
#[should_panic]
fn test_allocate() {
enter_protected();
let _x = Box::new(12);
exit_protected();
2018-11-06 20:51:44 -05:00
}
fn unit_result(b: bool) -> Result<(), ()> {
if b {
Ok(())
} else {
Err(())
}
}
#[test]
fn test_unit_result() {
enter_protected();
#[allow(unused)]
{
black_box(unit_result(true));
}
black_box(unit_result(true)).unwrap();
#[allow(unused)]
{
black_box(unit_result(false));
}
black_box(unit_result(false)).unwrap_err();
exit_protected();
2018-11-06 20:51:44 -05:00
}
#[test]
#[should_panic]
fn test_vec_push() {
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]
fn test_vec_push_capacity() {
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]
fn test_vec_with_zero() {
enter_protected();
let _v: Vec<u8> = black_box(Vec::with_capacity(0));
exit_protected();
2018-11-06 20:51:44 -05:00
}
#[test]
fn test_vec_new() {
enter_protected();
let _v: Vec<u8> = black_box(Vec::new());
exit_protected();
2018-11-06 20:51:44 -05:00
}
#[test]
#[should_panic]
fn test_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]
#[should_panic]
fn exit_too_often() {
enter_protected();
exit_protected();
exit_protected();
2018-11-10 21:59:39 -05:00
}