From 165717621afd37375bd1e7f64bdbd3bf55832609 Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Fri, 26 Jan 2024 15:42:58 +0800 Subject: [PATCH] chore: add some tests --- src/mm/freelist.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils/io.rs | 15 +++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/mm/freelist.rs b/src/mm/freelist.rs index f92116f..d4eab1d 100644 --- a/src/mm/freelist.rs +++ b/src/mm/freelist.rs @@ -27,3 +27,68 @@ impl FreeList { popped } } + +#[cfg(test)] +mod tests { + use crate::arch::layout::{KERNEL_MEM_START, PAGE_SIZE}; + use crate::mm::addr::PhysAddr; + + use super::*; + + #[test_case] + fn test_freelist_serial() { + let mut list = FreeList::new(); + + for i in 0..1024 { + let addr = PhysAddr((i * PAGE_SIZE) + KERNEL_MEM_START); + list.push(addr.into()); + } + + for i in 0..1024 { + let addr: Option = list.pop().map(|addr| addr.into()); + let expected = PhysAddr((1023 - i) * PAGE_SIZE + KERNEL_MEM_START); + assert_eq!(addr, Some(expected)); + } + + let addr: Option = list.pop().map(|addr| addr.into()); + assert_eq!(addr, None); + + for i in 0..1024 { + let addr = PhysAddr((i * PAGE_SIZE) + KERNEL_MEM_START); + list.push(addr.into()); + } + } + + #[test_case] + fn test_freelist_complex() { + let mut list = FreeList::new(); + + for i in 0..1024 { + let addr = PhysAddr((i * PAGE_SIZE) + KERNEL_MEM_START); + list.push(addr.into()); + } + + // pop 2 with 1 push + let mut cnt = 0; + for i in 0..1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 + 1 { + let addr: Option = list.pop().map(|addr| addr.into()); + let expected = PhysAddr((1023 - cnt) * PAGE_SIZE + KERNEL_MEM_START); + assert_eq!(addr, Some(expected)); + + if i % 2 == 0 { + list.push(addr.unwrap().into()); + } else { + cnt += 1; + } + } + + let addr: Option = list.pop().map(|addr| addr.into()); + assert_eq!(addr, None); + assert_eq!(cnt, 1024); + + for i in 0..1024 { + let addr = PhysAddr((i * PAGE_SIZE) + KERNEL_MEM_START); + list.push(addr.into()); + } + } +} diff --git a/src/utils/io.rs b/src/utils/io.rs index f8e72ac..f6d069e 100644 --- a/src/utils/io.rs +++ b/src/utils/io.rs @@ -72,3 +72,18 @@ impl core::fmt::Write for RawConsole { pub trait Reader { fn get_char() -> char; } + +#[cfg(test)] +mod tests { + use super::*; + + #[test_case] + fn print_with_console() { + RawConsole::put_char('A'); + RawConsole::put_str("BC"); + RawConsole::put_line("DEF"); + RawConsole::put_num(123); + RawConsole::put_hex(0xdeadbeef); + RawConsole::put_line(""); + } +}