From 0a410ddbda2e679ec7276588773536852495a99f Mon Sep 17 00:00:00 2001 From: Vasant Karasulli Date: Wed, 19 Jun 2024 10:34:34 +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 | 106 ++++++++++++++++++++++++++++++++++- kernel/src/types.rs | 5 ++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/kernel/src/log_buffer/mod.rs b/kernel/src/log_buffer/mod.rs index b649e6055..9758afbcf 100644 --- a/kernel/src/log_buffer/mod.rs +++ b/kernel/src/log_buffer/mod.rs @@ -4,18 +4,27 @@ // // Author: Vasant Karasulli +#[cfg(test)] extern crate alloc; use core::fmt::Debug; use crate::locking::{LockGuard, SpinLock}; use crate::string::FixedString; -use crate::types::{LINE_BUFFER_SIZE, PAGE_SIZE}; + +use crate::types::LINE_BUFFER_SIZE; +#[cfg(not(test))] +use crate::types::PAGE_SIZE; use crate::utils::StringRingBuffer; +#[cfg(test)] use alloc::vec; +#[cfg(test)] use alloc::vec::Vec; +#[cfg(not(test))] const BUF_SIZE: usize = PAGE_SIZE / core::mem::size_of::(); +#[cfg(test)] +const BUF_SIZE: usize = 64; #[derive(Copy, Clone, Debug)] pub struct LogBuffer { @@ -37,7 +46,7 @@ impl LogBuffer { self.buf.write(s.iter()); } - // A method used for testing + #[cfg(test)] pub fn read_log(&mut self) -> Vec { if let Some(str) = self.buf.read() { str.as_bytes().to_vec() @@ -59,3 +68,96 @@ pub fn log_buffer() -> LockGuard<'static, LogBuffer> { pub fn get_lb() -> &'static SpinLock { &LB } + +#[cfg(test)] +mod tests { + use super::*; + 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()); + } + + let mut lb = LogBuffer::new(); + lb.write_log(&fs); + + let v = lb.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()); + } + + let mut lb = LogBuffer::new(); + lb.write_log(&fs); + + let v = lb.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()); + } + + lb.write_log(&fs); + + let v = lb.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()); + } + + let mut lb = LogBuffer::new(); + lb.write_log(&fs); + + let v = lb.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()); + } + + lb.write_log(&fs); + + let v = lb.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 mut lb = LogBuffer::new(); + let v = lb.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 {