Compare commits
3 commits
386522208e
...
caa6e8e951
Author | SHA1 | Date | |
---|---|---|---|
caa6e8e951 | |||
48f84b895b | |||
a3005ff146 |
4 changed files with 105 additions and 54 deletions
|
@ -36,4 +36,3 @@ test-args = [
|
|||
"none",
|
||||
]
|
||||
test-success-exit-code = 33 # (0x10 << 1) | 1 = 33
|
||||
test-timeout = "300" # in seconds
|
||||
|
|
75
src/lib.rs
Normal file
75
src/lib.rs
Normal file
|
@ -0,0 +1,75 @@
|
|||
#![no_std]
|
||||
#![cfg_attr(test, no_main)]
|
||||
#![feature(custom_test_frameworks)]
|
||||
#![test_runner(crate::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
pub mod vga_buffer;
|
||||
pub mod serial;
|
||||
|
||||
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);
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
/// entry point for `cargo test`
|
||||
#[cfg(test)]
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
test_main();
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
58
src/main.rs
58
src/main.rs
|
@ -1,47 +1,15 @@
|
|||
#![no_std] // dont link the standard library
|
||||
#![no_main] // disable all rust-level entry points
|
||||
#![feature(custom_test_frameworks)]
|
||||
#![test_runner(crate::test_runner)]
|
||||
#![test_runner(totos::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use totos::println;
|
||||
|
||||
mod vga_buffer;
|
||||
mod serial;
|
||||
|
||||
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]");
|
||||
}
|
||||
}
|
||||
|
||||
// this is here because i dont want to remove it :>
|
||||
static HELLO: &[u8] = b"Hello toto :3";
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
||||
// this function is the entry point since the linker
|
||||
// looks for a function named `_start` by default
|
||||
#[unsafe(no_mangle)]
|
||||
|
@ -54,7 +22,7 @@ pub extern "C" fn _start() -> ! {
|
|||
loop {}
|
||||
}
|
||||
|
||||
// panic handler
|
||||
/// this function is called on panic
|
||||
#[cfg(not(test))]
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
|
@ -63,26 +31,10 @@ fn panic(info: &PanicInfo) -> ! {
|
|||
loop {}
|
||||
}
|
||||
|
||||
// panic handler in test mode
|
||||
#[cfg(test)]
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
serial_println!("[failed]\n");
|
||||
serial_println!("Error: {}\n", info);
|
||||
exit_qemu(QemuExitCode::Failed);
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn test_runner(tests: &[&dyn Testable]) {
|
||||
serial_println!("Running {} tests", tests.len());
|
||||
|
||||
for test in tests {
|
||||
test.run();
|
||||
}
|
||||
|
||||
exit_qemu(QemuExitCode::Success);
|
||||
totos::test_panic_handler(info)
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
|
|
25
tests/basic_boot.rs
Normal file
25
tests/basic_boot.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(custom_test_frameworks)]
|
||||
#![test_runner(totos::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use totos::println;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
test_main();
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
totos::test_panic_handler(info)
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println() {
|
||||
println!("test_println_output");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue