Skip to content

Commit 11a5cc8

Browse files
authored
Merge pull request #79 from rust-embedded/integration-test
Add basic smoke test
2 parents ef48543 + a6e12c0 commit 11a5cc8

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

.cargo/config.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[target.thumbv7m-none-eabi]
2+
# used to run the qemu_test.rs example with QEMU
3+
runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
4+
rustflags = ["-C", "link-arg=-Tlink.x"]

.github/workflows/ci.yml

+15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ jobs:
2828
- if: ${{ matrix.toolchain == 'nightly' }}
2929
run: cargo check --target=${{ matrix.target }} --examples --all-features
3030

31+
test:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v4
35+
- uses: dtolnay/rust-toolchain@master
36+
with:
37+
targets: thumbv7m-none-eabi
38+
toolchain: nightly
39+
- name: Install QEMU
40+
run: |
41+
sudo apt update
42+
sudo apt install qemu-system-arm
43+
- run: qemu-system-arm --version
44+
- run: cargo run --target thumbv7m-none-eabi --example integration_test --all-features
45+
3146
clippy:
3247
name: Clippy
3348
runs-on: ubuntu-latest

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ version = "0.10.5"
3636
[dev-dependencies]
3737
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
3838
cortex-m-rt = "0.7"
39+
cortex-m-semihosting = "0.5"
40+
panic-semihosting = { version = "0.6", features = ["exit"] }

examples/integration_test.rs

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

memory.x

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MEMORY
2+
{
3+
/* These values correspond to the LM3S6965, one of the few devices QEMU can emulate */
4+
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
5+
RAM : ORIGIN = 0x20000000, LENGTH = 64K
6+
}

0 commit comments

Comments
 (0)