mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
feat: introduce test infrastructure
This commit is contained in:
parent
9b73d703b4
commit
a8d3dee80d
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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
29
src/test_runner.rs
Normal 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>());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user