2022-08-05 11:22:23 +00:00
|
|
|
/*
|
2022-08-12 13:40:23 +00:00
|
|
|
* Copyright (c) 2022, able <abl3theabove@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
2022-08-05 11:22:23 +00:00
|
|
|
|
|
|
|
use core::panic::PanicInfo;
|
|
|
|
|
|
|
|
use kernel::arch::arch::sloop;
|
|
|
|
pub trait Testable {
|
|
|
|
fn run(&self) -> ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Testable for T
|
|
|
|
where
|
|
|
|
T: Fn(),
|
|
|
|
{
|
|
|
|
fn run(&self) {
|
|
|
|
serial_print!("{}...\t", core::any::type_name::<T>());
|
|
|
|
self();
|
|
|
|
serial_println!("[ok]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_runner(tests: &[&dyn Testable]) {
|
|
|
|
serial_println!("Running {} tests", tests.len());
|
|
|
|
for test in tests {
|
|
|
|
test.run();
|
|
|
|
}
|
|
|
|
exit_qemu(QemuExitCode::Success);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_panic_handler(info: &PanicInfo) -> ! {
|
|
|
|
serial_println!("[failed]\n");
|
|
|
|
serial_println!("Error: {}\n", info);
|
|
|
|
exit_qemu(QemuExitCode::Failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[panic_handler]
|
|
|
|
fn panic(info: &PanicInfo) -> ! {
|
|
|
|
test_panic_handler(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[repr(u32)]
|
|
|
|
pub enum QemuExitCode {
|
|
|
|
Success = 0x10,
|
|
|
|
Failed = 0x11,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_qemu(exit_code: QemuExitCode) -> ! {
|
|
|
|
use x86_64::instructions::port::Port;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut port = Port::new(0xf4);
|
|
|
|
port.write(exit_code as u32);
|
|
|
|
}
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry_point!(test_kernel_main);
|
|
|
|
/// Entry point for `cargo test`
|
|
|
|
pub fn test_kernel_main(boot_info: &'static BootInfo) -> ! {
|
|
|
|
// init(boot_info.physical_memory_offset);
|
|
|
|
test_main();
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
use bootloader::{entry_point, BootInfo};
|
|
|
|
|
2022-08-06 11:48:40 +00:00
|
|
|
use crate::test_main;
|