Implement heap tests

This commit is contained in:
2025-12-20 16:32:33 +01:00
parent abdef70198
commit e22dc65588
6 changed files with 310 additions and 47 deletions

125
heap/src/tests.rs Normal file
View File

@@ -0,0 +1,125 @@
use super::*;
use rand::{self, random_range};
extern crate std;
static HEAP_SIZE: usize = 1024;
#[test]
fn test_heap_allocation() {
let heap_vector = Box::new([0u8; HEAP_SIZE]);
let mut heap = Heap::empty();
heap.init(
&heap_vector[0] as *const u8 as usize,
&heap_vector[HEAP_SIZE - 1] as *const u8 as usize,
);
let root_header = heap.start_address;
let malloc_size = random_range(0..(HEAP_SIZE - HEAP_HEADER_SIZE));
let malloc = heap.malloc(malloc_size).unwrap();
let malloc_header = Heap::get_header_ref_from_data_pointer(malloc);
assert_eq!(root_header, malloc_header);
unsafe {
let actual_alloc_size = (*malloc_header).size;
let actual_raw_size = actual_alloc_size + HEAP_HEADER_SIZE;
// Verify sizing
assert!(actual_alloc_size >= malloc_size);
assert_eq!(actual_alloc_size % MIN_BLOCK_SIZE, 0);
// Verify section is occupied
assert!((*malloc_header).free == false);
// Verify next header has been created
let next = (*malloc_header).next.unwrap();
assert_eq!(malloc_header.byte_add(actual_raw_size), next);
assert!((*next).free);
assert_eq!((*malloc_header).next.unwrap(), next);
assert_eq!((*next).before.unwrap(), malloc_header);
assert_eq!((*next).size, HEAP_SIZE - actual_raw_size - HEAP_HEADER_SIZE)
}
}
#[test]
fn test_full_heap() {
let heap_vector = Box::new([0u8; HEAP_SIZE]);
let mut heap = Heap::empty();
heap.init(
&heap_vector[0] as *const u8 as usize,
&heap_vector[HEAP_SIZE - 1] as *const u8 as usize,
);
let malloc_size = HEAP_SIZE - HEAP_HEADER_SIZE;
let malloc = heap.malloc(malloc_size).unwrap();
let malloc_header = Heap::get_header_ref_from_data_pointer(malloc);
unsafe {
assert_eq!((*malloc_header).free, false);
assert!((*malloc_header).next.is_none());
}
let malloc2 = heap.malloc(MIN_BLOCK_SIZE);
assert!(malloc2.is_err());
}
#[test]
fn test_freeing_root() {
let heap_vector = Box::new([0u8; HEAP_SIZE]);
let mut heap = Heap::empty();
heap.init(
&heap_vector[0] as *const u8 as usize,
&heap_vector[HEAP_SIZE - 1] as *const u8 as usize,
);
let root_header = heap.start_address;
let root_header_start_size = unsafe { (*root_header).size };
let malloc_size = random_range(0..((HEAP_SIZE - HEAP_HEADER_SIZE) / 2));
let malloc = heap.malloc(malloc_size).unwrap();
let malloc_header = Heap::get_header_ref_from_data_pointer(malloc);
unsafe {
assert_eq!((*malloc_header).free, false);
assert!((*malloc_header).size >= malloc_size);
assert!((*root_header).next.is_some());
assert!(heap.free(malloc).is_ok());
assert_eq!((*root_header).size, root_header_start_size);
assert!((*root_header).next.is_none());
}
fn test_merging_free_sections() {
let heap_vector = Box::new([0u8; HEAP_SIZE]);
let mut heap = Heap::empty();
heap.init(
&heap_vector[0] as *const u8 as usize,
&heap_vector[HEAP_SIZE - 1] as *const u8 as usize,
);
let root_header = heap.start_address;
let root_header_start_size = unsafe { (*root_header).size };
let malloc1 = heap.malloc(MIN_BLOCK_SIZE).unwrap();
let malloc_header_before = unsafe { *Heap::get_header_ref_from_data_pointer(malloc1) };
let malloc2 = heap.malloc(MIN_BLOCK_SIZE).unwrap();
let malloc3 = heap.malloc(MIN_BLOCK_SIZE).unwrap();
unsafe {
assert!(heap.free(malloc1).is_ok());
let malloc_header_free = *Heap::get_header_ref_from_data_pointer(malloc1);
assert_ne!(malloc_header_before.free, malloc_header_free.free);
assert_eq!(malloc_header_before.size, malloc_header_free.size);
assert!(heap.free(malloc2).is_ok());
let malloc_header_merge = *Heap::get_header_ref_from_data_pointer(malloc1);
assert!(malloc_header_merge.free);
assert_eq!(
malloc_header_merge.size,
malloc_header_free.size + MIN_BLOCK_SIZE + HEAP_HEADER_SIZE
);
}
}
}