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 Jun 6, 2024
1 parent 619e8ee commit 3d2c06d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
92 changes: 92 additions & 0 deletions kernel/src/log_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ 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;

use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;

#[cfg(not(test))]
const BUF_SIZE: usize = PAGE_SIZE / core::mem::size_of::<char>();

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -79,3 +82,92 @@ pub fn log_buffer() -> LockGuard<'static, LogBuffer> {
pub fn get_lb() -> *const SpinLock<LogBuffer> {
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::<LINE_BUFFER_SIZE>::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::<LINE_BUFFER_SIZE>::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::<LINE_BUFFER_SIZE>::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);
}
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 3d2c06d

Please sign in to comment.