extern crate alloc;
mod test_utils;
use alloc::vec::Vec;
use assert_matches::assert_matches;
use ax_memory_addr::PAGE_SIZE_4K as PAGE_SIZE;
use axaddrspace::PhysFrame;
use axin::axin;
use test_utils::{BASE_PADDR, MockHal, mock_hal_test, test_dealloc_count};
#[test]
#[axin(decorator(mock_hal_test), on_exit(test_dealloc_count(1)))]
fn test_alloc_dealloc_cycle() {
let frame = PhysFrame::<MockHal>::alloc()
.unwrap_or_else(|e| panic!("Failed to allocate frame: {:?}", e));
assert_eq!(frame.start_paddr().as_usize(), BASE_PADDR);
drop(frame);
}
#[test]
#[axin(decorator(mock_hal_test), on_exit(test_dealloc_count(1)))]
fn test_alloc_zero() {
let frame = PhysFrame::<MockHal>::alloc_zero()
.unwrap_or_else(|e| panic!("Failed to allocate zero frame: {:?}", e));
assert_eq!(frame.start_paddr().as_usize(), BASE_PADDR);
let ptr = frame.as_mut_ptr();
let page = unsafe { &*(ptr as *const [u8; PAGE_SIZE]) };
assert!(page.iter().all(|&x| x == 0));
drop(frame);
}
#[test]
#[axin(decorator(mock_hal_test), on_exit(test_dealloc_count(1)))]
fn test_fill_operation() {
let mut frame = PhysFrame::<MockHal>::alloc()
.unwrap_or_else(|e| panic!("Failed to allocate frame: {:?}", e));
assert_eq!(frame.start_paddr().as_usize(), BASE_PADDR);
frame.fill(0xAA);
let ptr = frame.as_mut_ptr();
let page = unsafe { &*(ptr as *const [u8; PAGE_SIZE]) };
assert!(page.iter().all(|&x| x == 0xAA));
drop(frame);
}
#[test]
#[axin(decorator(mock_hal_test), on_exit(test_dealloc_count(5)))]
fn test_fill_multiple_frames() {
const NUM_FRAMES: usize = 5;
let mut frames = Vec::new();
let mut patterns = Vec::new();
for i in 0..NUM_FRAMES {
let mut frame = PhysFrame::<MockHal>::alloc().unwrap();
let pattern = (0xA0 + i) as u8;
frame.fill(pattern);
frames.push(frame);
patterns.push(pattern);
}
for i in 0..NUM_FRAMES {
let actual_page = unsafe { &*(frames[i].as_mut_ptr() as *mut [u8; PAGE_SIZE]) };
let expected_page = &[patterns[i]; PAGE_SIZE];
assert_eq!(
actual_page, expected_page,
"Frame verification failed for frame index {i}: Expected pattern 0x{:02x}",
patterns[i]
);
}
drop(frames);
}
#[test]
#[should_panic(expected = "uninitialized PhysFrame")]
fn test_uninit_access() {
let frame = unsafe { PhysFrame::<MockHal>::uninit() };
frame.start_paddr(); }
#[test]
#[axin(decorator(mock_hal_test), on_exit(test_dealloc_count(0)))]
fn test_alloc_no_memory() {
MockHal::set_alloc_fail(true);
let result = PhysFrame::<MockHal>::alloc();
assert_matches!(result, Err(ax_errno::AxError::NoMemory));
MockHal::set_alloc_fail(false); }