Skip to content

Commit

Permalink
log_buffer: add unit tests to test the functionality
Browse files Browse the repository at this point in the history
Test the log_buffer by calling methods like write_log()
and read_log().

Signed-off-by: Vasant Karasulli <[email protected]>
  • Loading branch information
vsntk18 committed Oct 18, 2024
1 parent 971a443 commit 45c52f8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
106 changes: 104 additions & 2 deletions kernel/src/log_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
//
// Author: Vasant Karasulli <[email protected]>

#[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::<char>();
#[cfg(test)]
const BUF_SIZE: usize = 64;

#[derive(Copy, Clone, Debug)]
pub struct LogBuffer {
Expand All @@ -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<u8> {
if let Some(str) = self.buf.read() {
str.as_bytes().to_vec()
Expand All @@ -59,3 +68,96 @@ pub fn log_buffer() -> LockGuard<'static, LogBuffer> {
pub fn get_lb() -> &'static SpinLock<LogBuffer> {
&LB
}

#[cfg(test)]
mod tests {
use super::*;
use crate::types::LINE_BUFFER_SIZE;

#[test]
fn test_read_write_normal() {
let mut fs = FixedString::<LINE_BUFFER_SIZE>::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::<LINE_BUFFER_SIZE>::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::<LINE_BUFFER_SIZE>::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);
}
}
5 changes: 5 additions & 0 deletions kernel/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 45c52f8

Please sign in to comment.