|
| 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 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::vec::Vec; |
| 23 | +use core::mem::{size_of, MaybeUninit}; |
| 24 | +use cortex_m_rt::entry; |
| 25 | +use cortex_m_semihosting::{debug, hprintln}; |
| 26 | +use embedded_alloc::Heap; |
| 27 | + |
| 28 | +#[global_allocator] |
| 29 | +static HEAP: Heap = Heap::empty(); |
| 30 | + |
| 31 | +fn test_global_heap() { |
| 32 | + assert_eq!(HEAP.used(), 0); |
| 33 | + |
| 34 | + let mut xs: Vec<i32> = alloc::vec![1]; |
| 35 | + xs.push(2); |
| 36 | + xs.extend(&[3, 4]); |
| 37 | + |
| 38 | + // do not optimize xs |
| 39 | + core::hint::black_box(&mut xs); |
| 40 | + |
| 41 | + assert_eq!(xs.as_slice(), &[1, 2, 3, 4]); |
| 42 | + assert_eq!(HEAP.used(), size_of::<i32>() * xs.len()); |
| 43 | +} |
| 44 | + |
| 45 | +fn test_allocator_api() { |
| 46 | + // small local heap |
| 47 | + const HEAP_SIZE: usize = 16; |
| 48 | + let heap_mem: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; |
| 49 | + let local_heap: Heap = Heap::empty(); |
| 50 | + unsafe { local_heap.init(heap_mem.as_ptr() as usize, HEAP_SIZE) } |
| 51 | + |
| 52 | + assert_eq!(local_heap.used(), 0); |
| 53 | + |
| 54 | + let mut v: Vec<u16, Heap> = Vec::new_in(local_heap); |
| 55 | + v.push(0xCAFE); |
| 56 | + v.extend(&[0xDEAD, 0xFEED]); |
| 57 | + |
| 58 | + // do not optimize v |
| 59 | + core::hint::black_box(&mut v); |
| 60 | + |
| 61 | + assert_eq!(v.as_slice(), &[0xCAFE, 0xDEAD, 0xFEED]); |
| 62 | +} |
| 63 | + |
| 64 | +#[entry] |
| 65 | +fn main() -> ! { |
| 66 | + { |
| 67 | + const HEAP_SIZE: usize = 1024; |
| 68 | + static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; |
| 69 | + unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } |
| 70 | + } |
| 71 | + |
| 72 | + #[allow(clippy::type_complexity)] |
| 73 | + let tests: &[(fn() -> (), &'static str)] = &[ |
| 74 | + (test_global_heap, "test_global_heap"), |
| 75 | + (test_allocator_api, "test_allocator_api"), |
| 76 | + ]; |
| 77 | + |
| 78 | + for (test_fn, test_name) in tests { |
| 79 | + hprintln!("{}: start", test_name); |
| 80 | + test_fn(); |
| 81 | + hprintln!("{}: pass", test_name); |
| 82 | + } |
| 83 | + |
| 84 | + // exit QEMU with a success status |
| 85 | + debug::exit(debug::EXIT_SUCCESS); |
| 86 | + #[allow(clippy::empty_loop)] |
| 87 | + loop {} |
| 88 | +} |
0 commit comments