From 7476ceec32b00085d9adcf0b8bea7d0166776b31 Mon Sep 17 00:00:00 2001 From: Vasant Karasulli Date: Sun, 2 Jun 2024 11:54:01 +0200 Subject: [PATCH] log_buffer: add unit tests to test the functionality Test the log_buffer by calling methods like write_log() and read_log(). Signed-off-by: Vasant Karasulli --- kernel/src/log_buffer/mod.rs | 92 ++++++++++++++++++++++++++++++++++++ kernel/src/types.rs | 5 ++ 2 files changed, 97 insertions(+) diff --git a/kernel/src/log_buffer/mod.rs b/kernel/src/log_buffer/mod.rs index 82dbefa3c..c4fe08534 100644 --- a/kernel/src/log_buffer/mod.rs +++ b/kernel/src/log_buffer/mod.rs @@ -10,6 +10,8 @@ use core::ptr::addr_of; use crate::locking::{LockGuard, SpinLock}; use crate::string::FixedString; + +#[cfg(not(test))] use crate::types::{LINE_BUFFER_SIZE, PAGE_SIZE}; use crate::utils::StringRingBuffer; @@ -17,6 +19,7 @@ use alloc::string::ToString; use alloc::vec; use alloc::vec::Vec; +#[cfg(not(test))] const BUF_SIZE: usize = PAGE_SIZE / core::mem::size_of::(); #[derive(Copy, Clone, Debug)] @@ -78,3 +81,92 @@ pub fn log_buffer() -> LockGuard<'static, LogBuffer> { pub fn get_lb() -> *const SpinLock { unsafe { addr_of!(LB) } } + +#[cfg(test)] +const BUF_SIZE: usize = 64; + +#[cfg(test)] +use crate::types::LINE_BUFFER_SIZE; + +#[test] +fn test_read_write_normal() { + let mut fs = FixedString::::new(); + for i in 1..=LINE_BUFFER_SIZE { + fs.push(char::from_u32(i as u32).unwrap()); + } + + log_buffer().write_log(&fs); + + let v = log_buffer().read_log(); + assert_eq!(v.len(), LINE_BUFFER_SIZE); + for i in 1..=v.len() { + assert_eq!(i as u8, v[i - 1]); + } +} + +#[test] +fn test_read_write_interleaved() { + let mut fs = FixedString::::new(); + for i in 1..=LINE_BUFFER_SIZE / 2 { + fs.push(char::from_u32(i as u32).unwrap()); + } + + log_buffer().write_log(&fs); + + let v = log_buffer().read_log(); + assert_eq!(v.len(), LINE_BUFFER_SIZE / 2); + for i in 1..=v.len() { + assert_eq!(i as u8, v[i - 1]); + } + + fs.clear(); + for i in LINE_BUFFER_SIZE / 2..LINE_BUFFER_SIZE { + fs.push(char::from_u32((i + 1) as u32).unwrap()); + } + + log_buffer().write_log(&fs); + + let v = log_buffer().read_log(); + assert_eq!(v.len(), LINE_BUFFER_SIZE / 2); + for i in 1..v.len() { + let val = (i + LINE_BUFFER_SIZE / 2) as u8; + assert_eq!(val, v[i - 1]); + } +} + +#[test] +fn test_write_wrap_around() { + let mut fs = FixedString::::new(); + for i in 1..=LINE_BUFFER_SIZE / 2 { + fs.push(char::from_u32(i as u32).unwrap()); + } + + log_buffer().write_log(&fs); + + let v = log_buffer().read_log(); + assert_eq!(v.len(), LINE_BUFFER_SIZE / 2); + for i in 1..=v.len() { + assert_eq!(i as u8, v[i - 1]); + } + + fs.clear(); + for i in 1..=LINE_BUFFER_SIZE { + let val = (i + LINE_BUFFER_SIZE / 2) as u32; + fs.push(char::from_u32(val).unwrap()); + } + + log_buffer().write_log(&fs); + + let v = log_buffer().read_log(); + assert_eq!(v.len(), LINE_BUFFER_SIZE); + for i in 1..v.len() { + let val = (i + LINE_BUFFER_SIZE / 2) as u8; + assert_eq!(val, v[i - 1]); + } +} + +#[test] +fn test_read_empty_buffer() { + let v = log_buffer().read_log(); + assert_eq!(v.len(), 0); +} diff --git a/kernel/src/types.rs b/kernel/src/types.rs index f85ff425d..dc07b166d 100644 --- a/kernel/src/types.rs +++ b/kernel/src/types.rs @@ -47,8 +47,13 @@ pub const GUEST_VMPL: usize = 2; const _: () = assert!(GUEST_VMPL > 0 && GUEST_VMPL < VMPL_MAX); pub const MAX_CPUS: usize = 512; + +#[cfg(not(test))] pub const LINE_BUFFER_SIZE: usize = 256; +#[cfg(test)] +pub const LINE_BUFFER_SIZE: usize = 64; + /// Length in byte which represents maximum 8 bytes(u64) #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)] pub enum Bytes {