Strucutring unit-tests

Signed-off-by: Noah Knegt <git@noahknegt.com>
This commit is contained in:
2025-07-22 22:19:20 +02:00
parent bd035f1c9a
commit 1a41d087a9
3 changed files with 17 additions and 27 deletions

13
kernel/src/adder.rs Normal file
View File

@@ -0,0 +1,13 @@
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn add_test() {
assert_eq!(add(1, 2), 3);
}
}

View File

@@ -1,8 +1,9 @@
#![no_std]
#![cfg_attr(not(test), no_std)]
use bootloader_api::BootInfo;
mod logging;
mod adder;
pub fn main(boot_info: &'static mut BootInfo) -> ! {
let info = boot_info.framebuffer.as_ref().unwrap().info();
@@ -11,15 +12,15 @@ pub fn main(boot_info: &'static mut BootInfo) -> ! {
logging::init_logger(buffer, info);
log::info!("Hello World from KERNEL");
log::debug!("ADDING 1 & 6, result == {}", add(1, 6));
log::debug!("ADDING 1 & 6, result == {}", adder::add(1, 6));
// Endless loop as the kernel must stay running
#[allow(clippy::empty_loop)]
loop {}
}
#[panic_handler]
#[cfg(not(test))]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
logging::force_unlock();
log::error!("{info}");
@@ -27,12 +28,3 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
#[allow(clippy::empty_loop)]
loop {}
}
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[test]
fn add_test() {
assert_eq!(add(1, 2), 3);
}

View File

@@ -1,15 +0,0 @@
#[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);
}
}