feat: introduce test infrastructure

This commit is contained in:
Paul Pan 2023-12-29 17:02:01 +08:00
parent 03fa1b5f4c
commit f897f4e558
4 changed files with 36 additions and 3 deletions

View File

@ -21,7 +21,7 @@ impl LowLevel for Hardware {
#[cfg(feature = "board_virt")]
unsafe {
TEST_DEVICE.write_volatile(0x0042_3333)
TEST_DEVICE.write_volatile(0x0001_3333)
}
} else {
#[cfg(not(feature = "board_virt"))]
@ -29,7 +29,7 @@ impl LowLevel for Hardware {
#[cfg(feature = "board_virt")]
unsafe {
TEST_DEVICE.write_volatile(0x0042_5555)
TEST_DEVICE.write_volatile(0x0000_5555)
}
}

View File

@ -5,7 +5,8 @@ use crate::utils::lowlevel::{Hardware, LowLevel};
pub extern "C" fn rust_main(hart_id: usize, device_tree_addr: usize) -> ! {
crate::logging::init();
info!("[rust_main] Kernel Started");
#[cfg(test)]
crate::test_main(); // test_main will exit
info!("Hello World!");
info!("hart_id = {}", hart_id);

View File

@ -32,3 +32,6 @@ pub mod utils;
// logging
pub mod logging;
// test infrastructure
#[cfg(test)]
mod test_runner;

29
src/test_runner.rs Normal file
View File

@ -0,0 +1,29 @@
use log::info;
use crate::utils::lowlevel::{Hardware, LowLevel};
// Reference: https://os.phil-opp.com/testing/
#[cfg(test)]
pub fn runner(tests: &[&dyn Testable]) {
info!("[TEST] Running {} tests", tests.len());
for test in tests {
test.run();
}
Hardware::shutdown(false);
}
pub trait Testable {
fn run(&self);
}
impl<T> Testable for T
where
T: Fn(),
{
fn run(&self) {
info!("[TEST] [{}] Testing", core::any::type_name::<T>());
self();
info!("[TEST] [{}] Passed", core::any::type_name::<T>());
}
}