forked from rust-embedded/embedded-alloc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtlsf_integration_test.rs
More file actions
109 lines (88 loc) · 2.64 KB
/
tlsf_integration_test.rs
File metadata and controls
109 lines (88 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! This is a very basic smoke test that runs in QEMU
//! Reference the QEMU section of the [Embedded Rust Book] for more information
//!
//! This only tests integration of the allocator on an embedded target.
//! Comprehensive allocator tests are located in the allocator dependency.
//!
//! After toolchain installation this test can be run with:
//!
//! ```bash
//! cargo +nightly run --target thumbv7m-none-eabi --example tlsf_integration_test --all-features
//! ```
//!
//! [Embedded Rust Book]: https://docs.rust-embedded.org/book/intro/index.html
#![feature(allocator_api)]
#![no_main]
#![no_std]
extern crate alloc;
use defmt_semihosting as _;
use alloc::collections::LinkedList;
use core::{mem::MaybeUninit, panic::PanicInfo};
use cortex_m as _;
use cortex_m_rt::entry;
use embedded_alloc::TlsfHeap as Heap;
#[global_allocator]
static HEAP: Heap = Heap::empty();
const HEAP_SIZE: usize = 30 * 1024;
pub type TestTable<'a> = &'a [(fn() -> (), &'static str)];
fn test_global_heap() {
const ELEMS: usize = 250;
let mut allocated = LinkedList::new();
for _ in 0..ELEMS {
allocated.push_back(0);
}
for i in 0..ELEMS {
allocated.push_back(i as i32);
}
assert_eq!(allocated.len(), 2 * ELEMS);
for _ in 0..ELEMS {
allocated.pop_front();
}
for i in 0..ELEMS {
assert_eq!(allocated.pop_front().unwrap(), i as i32);
}
}
fn test_allocator_api() {
// small local heap
const HEAP_SIZE: usize = 256;
let mut heap_mem: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
let local_heap: Heap = Heap::empty();
unsafe { local_heap.init(heap_mem.as_mut_ptr() as usize, HEAP_SIZE) }
const ELEMS: usize = 2;
let mut allocated = LinkedList::new_in(local_heap);
for _ in 0..ELEMS {
allocated.push_back(0);
}
for i in 0..ELEMS {
allocated.push_back(i as i32);
}
assert_eq!(allocated.len(), 2 * ELEMS);
for _ in 0..ELEMS {
allocated.pop_front();
}
for i in 0..ELEMS {
assert_eq!(allocated.pop_front().unwrap(), i as i32);
}
}
#[entry]
fn main() -> ! {
unsafe {
embedded_alloc::init!(HEAP, HEAP_SIZE);
}
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);
}
// exit QEMU with a success status
semihosting::process::exit(0);
}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
defmt::error!("{}", info);
semihosting::process::exit(-1);
}