Compare commits
No commits in common. "caa6e8e9513bfa3af6132571639e0f0cf95a12b5" and "386522208e62769bfc22ce1038b49d6920d26010" have entirely different histories.
caa6e8e951
...
386522208e
4 changed files with 54 additions and 105 deletions
|
@ -36,3 +36,4 @@ test-args = [
|
|||
"none",
|
||||
]
|
||||
test-success-exit-code = 33 # (0x10 << 1) | 1 = 33
|
||||
test-timeout = "300" # in seconds
|
||||
|
|
75
src/lib.rs
75
src/lib.rs
|
@ -1,75 +0,0 @@
|
|||
#![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,15 +1,47 @@
|
|||
#![no_std] // dont link the standard library
|
||||
#![no_main] // disable all rust-level entry points
|
||||
#![feature(custom_test_frameworks)]
|
||||
#![test_runner(totos::test_runner)]
|
||||
#![test_runner(crate::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use totos::println;
|
||||
|
||||
// this is here because i dont want to remove it :>
|
||||
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]");
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
|
@ -22,7 +54,7 @@ pub extern "C" fn _start() -> ! {
|
|||
loop {}
|
||||
}
|
||||
|
||||
/// this function is called on panic
|
||||
// panic handler
|
||||
#[cfg(not(test))]
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
|
@ -31,10 +63,26 @@ fn panic(info: &PanicInfo) -> ! {
|
|||
loop {}
|
||||
}
|
||||
|
||||
// panic handler in test mode
|
||||
#[cfg(test)]
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
totos::test_panic_handler(info)
|
||||
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);
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
#![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