chore: add some tests

This commit is contained in:
Paul Pan 2024-01-26 15:42:58 +08:00
parent 1da0637dfd
commit 165717621a
2 changed files with 80 additions and 0 deletions

View File

@ -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<PhysAddr> = list.pop().map(|addr| addr.into());
let expected = PhysAddr((1023 - i) * PAGE_SIZE + KERNEL_MEM_START);
assert_eq!(addr, Some(expected));
}
let addr: Option<PhysAddr> = 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<PhysAddr> = 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<PhysAddr> = 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());
}
}
}

View File

@ -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("");
}
}