1#![no_std]
2#![cfg_attr(test, no_main)]
3#![feature(custom_test_frameworks)]
4#![test_runner(crate::test_runner)]
5#![reexport_test_harness_main = "test_main"]
6
7use core::panic::PanicInfo;
8
9pub mod serial;
10pub mod vga_buffer;
11
12pub trait Testable {
13 fn run(&self) -> ();
14}
15
16impl<T> Testable for T
17where
18 T: Fn(),
19{
20 fn run(&self) {
21 serial_print!("{}...\t", core::any::type_name::<T>());
22 self();
23 serial_println!("[ok]");
24 }
25}
26
27pub fn test_runner(tests: &[&dyn Testable]) {
28 serial_println!("Running {} tests", tests.len());
29
30 for test in tests {
31 test.run();
32 }
33
34 exit_qemu(QemuExitCode::Success);
35}
36
37pub fn test_panic_handler(info: &PanicInfo) -> ! {
38 serial_println!("[failed]\n");
39 serial_println!("Error: {}\n", info);
40 exit_qemu(QemuExitCode::Failed);
41 loop {}
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45#[repr(u32)]
46pub enum QemuExitCode {
47 Success = 0x10,
48 Failed = 0x11,
49}
50
51pub fn exit_qemu(exit_code: QemuExitCode) {
52 use x86_64::instructions::port::Port;
53
54 unsafe {
55 let mut port = Port::new(0xf4);
56 port.write(exit_code as u32);
57 }
58}
59
60#[cfg(test)]
62#[no_mangle]
63pub extern "C" fn _start() -> ! {
64 test_main();
65 loop {}
66}
67
68#[cfg(test)]
69#[panic_handler]
70fn panic(info: &PanicInfo) -> ! {
71 test_panic_handler(info);
72}