Skip to content

Commit

Permalink
Merge pull request #153 from Zildj1an/testutils
Browse files Browse the repository at this point in the history
utils: Add unit tests for util.rs
  • Loading branch information
joergroedel authored Nov 20, 2023
2 parents cf2693e + 14729bb commit 7aa8bd2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/utils/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,46 @@ pub fn zero_mem_region(start: VirtAddr, end: VirtAddr) {
// Zero region
unsafe { start.as_mut_ptr::<u8>().write_bytes(0, size) }
}

#[cfg(test)]
mod tests {

use crate::utils::util::*;

#[test]
fn test_mem_utils() {
// Align up
assert_eq!(align_up(7, 4), 8);
assert_eq!(align_up(15, 8), 16);
assert_eq!(align_up(10, 2), 10);
// Align down
assert_eq!(align_down(7, 4), 4);
assert_eq!(align_down(15, 8), 8);
assert_eq!(align_down(10, 2), 10);
// Page align up
assert_eq!(page_align_up(4096), 4096);
assert_eq!(page_align_up(4097), 8192);
assert_eq!(page_align_up(0), 0);
// Page offset
assert_eq!(page_offset(4096), 0);
assert_eq!(page_offset(4097), 1);
assert_eq!(page_offset(0), 0);
// Overlaps
assert!(overlap(1, 5, 3, 6));
assert!(overlap(0, 10, 5, 15));
assert!(!overlap(1, 5, 6, 8));
}

#[test]
fn test_zero_mem_region() {
let mut data: [u8; 10] = [1; 10];
let start = VirtAddr::new(&mut data[0] as *mut u8 as usize);
let end = start + 10;

zero_mem_region(start, end);

for byte in &data {
assert_eq!(*byte, 0);
}
}
}

0 comments on commit 7aa8bd2

Please sign in to comment.