#![feature(allocator_api)]
#![no_main]
#![no_std]
extern crate alloc;
use alloc::vec::Vec;
use core::{
mem::{size_of, MaybeUninit},
panic::PanicInfo,
};
use cortex_m as _;
use cortex_m_rt::entry;
use defmt_semihosting as _;
use embedded_alloc::LlffHeap as Heap;
#[global_allocator]
static HEAP: Heap = Heap::empty();
fn test_global_heap() {
assert_eq!(HEAP.used(), 0);
let mut xs: Vec<i32> = alloc::vec![1];
xs.push(2);
xs.extend(&[3, 4]);
core::hint::black_box(&mut xs);
assert_eq!(xs.as_slice(), &[1, 2, 3, 4]);
assert_eq!(HEAP.used(), size_of::<i32>() * xs.len());
}
fn test_allocator_api() {
const HEAP_SIZE: usize = 16;
let mut heap_mem: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
let local_heap: Heap = Heap::empty();
unsafe { local_heap.init(&raw mut heap_mem as usize, HEAP_SIZE) }
assert_eq!(local_heap.used(), 0);
let mut v: Vec<u16, Heap> = Vec::new_in(local_heap);
v.push(0xCAFE);
v.extend(&[0xDEAD, 0xFEED]);
core::hint::black_box(&mut v);
assert_eq!(v.as_slice(), &[0xCAFE, 0xDEAD, 0xFEED]);
}
pub type TestTable<'a> = &'a [(fn() -> (), &'static str)];
#[entry]
fn main() -> ! {
unsafe {
embedded_alloc::init!(HEAP, 1024);
}
let tests: TestTable = &[
(test_global_heap, "test_global_heap"),
(test_allocator_api, "test_allocator_api"),
];
for (test_fn, test_name) in tests {
defmt::info!("{}: start", test_name);
test_fn();
defmt::info!("{}: pass", test_name);
}
semihosting::process::exit(0);
}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
defmt::error!("{}", info);
semihosting::process::exit(-1);
}