Compare commits

...

2 Commits

Author SHA1 Message Date
a8d3dee80d feat: introduce test infrastructure 2023-12-29 17:02:01 +08:00
9b73d703b4 chore: no lib 2023-12-29 17:00:48 +08:00
5 changed files with 67 additions and 31 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

@ -1,25 +0,0 @@
#![no_std]
#![feature(asm_const)]
#![feature(naked_functions)]
#![feature(panic_info_message)]
#![feature(stmt_expr_attributes)]
extern crate static_assertions;
// arch
pub mod arch;
// rust language runtime
pub mod lang;
// entrypoint
pub mod entry;
// page table
pub mod mm;
// utils
pub mod utils;
// logging
pub mod logging;

View File

@ -1,6 +1,37 @@
// We handle entrypoint in arch
#![no_std]
#![no_main]
// Features
#![feature(asm_const)]
#![feature(naked_functions)]
#![feature(panic_info_message)]
#![feature(stmt_expr_attributes)]
// Test Infrastructure
#![feature(custom_test_frameworks)]
#![test_runner(test_runner::runner)]
#![reexport_test_harness_main = "test_main"]
#![cfg_attr(test, allow(dead_code))]
#[allow(unused_imports)]
#[allow(clippy::single_component_path_imports)]
use tiny_os;
extern crate static_assertions;
// arch
pub mod arch;
// rust language runtime
pub mod lang;
// entrypoint
pub mod entry;
// page table
pub mod mm;
// utils
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>());
}
}