Skip to content

Commit 26f6041

Browse files
committed
Update integration tests for choice of allocator
1 parent b401dd7 commit 26f6041

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ jobs:
4141
sudo apt update
4242
sudo apt install qemu-system-arm
4343
- run: qemu-system-arm --version
44-
- run: cargo run --target thumbv7m-none-eabi --example integration_test --all-features
44+
- run: cargo run --target thumbv7m-none-eabi --example llff_integration_test --all-features
45+
- run: cargo run --target thumbv7m-none-eabi --example tlsf_integration_test --all-features
4546

4647
clippy:
4748
name: Clippy

examples/allocator_api.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloc::vec::Vec;
88
use core::mem::MaybeUninit;
99
use core::panic::PanicInfo;
1010
use cortex_m_rt::entry;
11-
use embedded_alloc::Heap;
11+
use embedded_alloc::LlffHeap as Heap;
1212

1313
// This is not used, but as of 2023-10-29 allocator_api cannot be used without
1414
// a global heap

examples/integration_test.rs renamed to examples/llff_integration_test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! After toolchain installation this test can be run with:
88
//!
99
//! ```bash
10-
//! cargo +nightly run --target thumbv7m-none-eabi --example integration_test --all-features
10+
//! cargo +nightly run --target thumbv7m-none-eabi --example llff_integration_test --all-features
1111
//! ```
1212
//!
1313
//! [Embedded Rust Book]: https://docs.rust-embedded.org/book/intro/index.html
@@ -23,7 +23,7 @@ use alloc::vec::Vec;
2323
use core::mem::{size_of, MaybeUninit};
2424
use cortex_m_rt::entry;
2525
use cortex_m_semihosting::{debug, hprintln};
26-
use embedded_alloc::Heap;
26+
use embedded_alloc::LlffHeap as Heap;
2727

2828
#[global_allocator]
2929
static HEAP: Heap = Heap::empty();

examples/tlsf_integration_test.rs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)