Compare commits

...

2 commits

Author SHA1 Message Date
386522208e unit testing for vga 2025-08-31 20:22:13 +02:00
e009cd22a8 added serial ports communication + Testable trait 2025-08-31 13:07:59 +02:00
4 changed files with 102 additions and 7 deletions

View file

@ -19,6 +19,7 @@ bootloader = "0.9"
volatile = "0.2.6"
spin = "0.5.2"
x86_64 = "0.14.2"
uart_16550 = "0.2.0"
[dependencies.lazy_static]
version = "1.0"
@ -26,5 +27,13 @@ features = ["spin_no_std"]
# this enables exiting qemu from the guest system
[package.metadata.bootimage]
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04"]
test-args = [
"-device",
"isa-debug-exit,iobase=0xf4,iosize=0x04",
"-serial",
"stdio",
"-display",
"none",
]
test-success-exit-code = 33 # (0x10 << 1) | 1 = 33
test-timeout = "300" # in seconds

View file

@ -7,6 +7,22 @@
use core::panic::PanicInfo;
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";
@ -38,7 +54,8 @@ pub extern "C" fn _start() -> ! {
loop {}
}
// called on panic
// panic handler
#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
@ -46,12 +63,23 @@ fn panic(info: &PanicInfo) -> ! {
loop {}
}
// panic handler in test mode
#[cfg(test)]
pub fn test_runner(tests: &[&dyn Fn()]) {
println!("Running {} tests", tests.len());
#[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();
test.run();
}
exit_qemu(QemuExitCode::Success);
@ -59,7 +87,5 @@ pub fn test_runner(tests: &[&dyn Fn()]) {
#[test_case]
fn trivial_assertion() {
print!("asserzione triviale... ");
assert_eq!(1, 1);
println!("[ok]");
}

35
src/serial.rs Normal file
View file

@ -0,0 +1,35 @@
use uart_16550::SerialPort;
use spin::Mutex;
use lazy_static::lazy_static;
lazy_static! {
pub static ref SERIAL1: Mutex<SerialPort> = {
let mut serial_port = unsafe { SerialPort::new(0x3F8) };
serial_port.init();
Mutex::new(serial_port)
};
}
#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
SERIAL1.lock().write_fmt(args).expect("Printing to serial failed :(");
}
// prints to the host through the serial interface
#[macro_export]
macro_rules! serial_print {
($($arg:tt)*) => {
$crate::serial::_print(format_args!($($arg)*));
};
}
// prints to the host through the serial interface
// appending a newline
#[macro_export]
macro_rules! serial_println {
() => ($crate::serial_print!("\n"));
($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => ($crate::serial_print!(
concat!($fmt, "\n"), $($arg)*));
}

View file

@ -155,3 +155,28 @@ impl Writer {
}
}
#[test_case]
fn test_println_simple() {
println!("test_println_simple output");
}
#[test_case]
fn test_println_many() {
for _ in 0..200 {
println!("test_println_many output");
}
}
#[test_case]
fn test_println_output() {
let s = "loss and gain is the same";
println!("{}", s);
for (i, c) in s.chars().enumerate() {
let screen_char = WRITER.lock()
.buffer
.chars[BUFFER_HEIGHT - 2][i]
.read();
assert_eq!(char::from(screen_char.ascii_carachter), c);
}
}