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
Signed-off-by: Vasant Karasulli <[email protected]>
  • Loading branch information
vsntk18 committed Feb 16, 2024
1 parent 1e06ea2 commit ecd3216
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 @@ -9,13 +9,16 @@ use core::fmt::Debug;

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 @@ -65,3 +68,92 @@ pub fn log_buffer() -> LockGuard<'static, LogBuffer> {
pub fn get_lb() -> &'static SpinLock<LogBuffer> {
unsafe { &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 @@ -46,4 +46,9 @@ 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;

0 comments on commit ecd3216

Please sign in to comment.