#![cfg(ra_small_profile)]
use rusty_alloc::prim::fixed::{
FERR_REGISTERED, MIN_REGION, REGION_ALIGN, Region, good_region_size, usable_bytes,
};
const N: usize = good_region_size(220 * 1024);
#[cfg(not(ra_aligned_region))]
static HEAP: Region<N> = Region::new();
#[cfg(not(ra_aligned_region))]
static OTHER: Region<MIN_REGION> = Region::new();
#[test]
fn a_region_given_once_serves_exactly_what_its_size_says() {
assert_eq!(N, 196_608);
assert_eq!(Region::<N>::USABLE, 196_608);
assert_eq!(
core::mem::size_of::<Region<N>>(),
N,
"no padding: whole segments"
);
assert_eq!(
core::mem::align_of::<Region<N>>(),
REGION_ALIGN,
"16 bytes, not a segment: segments stride from the base (a segment under ra_aligned_region)"
);
#[cfg(not(ra_aligned_region))]
let (heap, other): (&'static Region<N>, &'static Region<MIN_REGION>) = (&HEAP, &OTHER);
#[cfg(ra_aligned_region)]
let (heap, other): (&'static Region<N>, &'static Region<MIN_REGION>) = unsafe {
(
Box::leak(Box::<Region<N>>::new_zeroed().assume_init()),
Box::leak(Box::<Region<MIN_REGION>>::new_zeroed().assume_init()),
)
};
let base = core::ptr::from_ref(heap).addr();
assert_eq!(base % REGION_ALIGN, 0);
assert_eq!(heap.usable(), Region::<N>::USABLE);
assert_eq!(heap.usable(), N, "nothing stranded, nothing reserved");
assert_eq!(heap.usable(), usable_bytes(0, N));
assert_eq!(usable_bytes(base, N), N, "the real base serves every byte");
let usable = heap.give().expect("first give of an exact region");
assert_eq!(usable, 196_608);
assert_eq!(usable, Region::<N>::USABLE);
assert_eq!(heap.give(), Err(FERR_REGISTERED));
assert_eq!(other.give(), Err(FERR_REGISTERED));
}