|
| 1 | +//! This is a very basic smoke test that runs in QEMU |
| 2 | +//! Reference the QEMU section of the [Embedded Rust Book] for more information |
| 3 | +//! |
| 4 | +//! This only tests integration of the allocator on an embedded target. |
| 5 | +//! Comprehensive allocator tests are located in the allocator dependency. |
| 6 | +//! |
| 7 | +//! After toolchain installation this test can be run with: |
| 8 | +//! |
| 9 | +//! ```bash |
| 10 | +//! cargo +nightly run --target thumbv7m-none-eabi --example tlsf_integration_test --all-features |
| 11 | +//! ``` |
| 12 | +//! |
| 13 | +//! [Embedded Rust Book]: https://docs.rust-embedded.org/book/intro/index.html |
| 14 | +
|
| 15 | +#![feature(allocator_api)] |
| 16 | +#![no_main] |
| 17 | +#![no_std] |
| 18 | + |
| 19 | +extern crate alloc; |
| 20 | +extern crate panic_semihosting; |
| 21 | + |
| 22 | +use alloc::collections::LinkedList; |
| 23 | +use core::mem::MaybeUninit; |
| 24 | +use cortex_m_rt::entry; |
| 25 | +use cortex_m_semihosting::{debug, hprintln}; |
| 26 | +use embedded_alloc::TlsfHeap as Heap; |
| 27 | + |
| 28 | +#[global_allocator] |
| 29 | +static HEAP: Heap = Heap::empty(); |
| 30 | +const HEAP_SIZE: usize = 30 * 1024; |
| 31 | + |
| 32 | +fn test_global_heap() { |
| 33 | + const ELEMS: usize = 250; |
| 34 | + |
| 35 | + let mut allocated = LinkedList::new(); |
| 36 | + for _ in 0..ELEMS { |
| 37 | + allocated.push_back(0); |
| 38 | + } |
| 39 | + for i in 0..ELEMS { |
| 40 | + allocated.push_back(i as i32); |
| 41 | + } |
| 42 | + |
| 43 | + assert_eq!(allocated.len(), 2 * ELEMS); |
| 44 | + |
| 45 | + for _ in 0..ELEMS { |
| 46 | + allocated.pop_front(); |
| 47 | + } |
| 48 | + |
| 49 | + for i in 0..ELEMS { |
| 50 | + assert_eq!(allocated.pop_front().unwrap(), i as i32); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn test_allocator_api() { |
| 55 | + // small local heap |
| 56 | + const HEAP_SIZE: usize = 256; |
| 57 | + let heap_mem: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; |
| 58 | + let local_heap: Heap = Heap::empty(); |
| 59 | + unsafe { local_heap.init(heap_mem.as_ptr() as usize, HEAP_SIZE) } |
| 60 | + |
| 61 | + const ELEMS: usize = 2; |
| 62 | + |
| 63 | + let mut allocated = LinkedList::new_in(local_heap); |
| 64 | + for _ in 0..ELEMS { |
| 65 | + allocated.push_back(0); |
| 66 | + } |
| 67 | + for i in 0..ELEMS { |
| 68 | + allocated.push_back(i as i32); |
| 69 | + } |
| 70 | + |
| 71 | + assert_eq!(allocated.len(), 2 * ELEMS); |
| 72 | + |
| 73 | + for _ in 0..ELEMS { |
| 74 | + allocated.pop_front(); |
| 75 | + } |
| 76 | + |
| 77 | + for i in 0..ELEMS { |
| 78 | + assert_eq!(allocated.pop_front().unwrap(), i as i32); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[entry] |
| 83 | +fn main() -> ! { |
| 84 | + { |
| 85 | + static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; |
| 86 | + unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } |
| 87 | + } |
| 88 | + |
| 89 | + #[allow(clippy::type_complexity)] |
| 90 | + let tests: &[(fn() -> (), &'static str)] = &[ |
| 91 | + (test_global_heap, "test_global_heap"), |
| 92 | + (test_allocator_api, "test_allocator_api"), |
| 93 | + ]; |
| 94 | + |
| 95 | + for (test_fn, test_name) in tests { |
| 96 | + hprintln!("{}: start", test_name); |
| 97 | + test_fn(); |
| 98 | + hprintln!("{}: pass", test_name); |
| 99 | + } |
| 100 | + |
| 101 | + // exit QEMU with a success status |
| 102 | + debug::exit(debug::EXIT_SUCCESS); |
| 103 | + #[allow(clippy::empty_loop)] |
| 104 | + loop {} |
| 105 | +} |
0 commit comments