From df75f7f4e83d666ac5fa1b84066959635844922f Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 26 Jun 2019 16:59:56 +0200 Subject: [PATCH] Add an integration test for heap allocation --- tests/heap_allocation.rs | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/heap_allocation.rs diff --git a/tests/heap_allocation.rs b/tests/heap_allocation.rs new file mode 100644 index 00000000..a4548d9e --- /dev/null +++ b/tests/heap_allocation.rs @@ -0,0 +1,63 @@ +#![no_std] +#![no_main] +#![feature(custom_test_frameworks)] +#![test_runner(blog_os::test_runner)] +#![reexport_test_harness_main = "test_main"] + +extern crate alloc; + +use alloc::{boxed::Box, vec::Vec}; +use blog_os::{serial_print, serial_println}; +use bootloader::{entry_point, BootInfo}; +use core::panic::PanicInfo; + +entry_point!(main); + +fn main(boot_info: &'static BootInfo) -> ! { + use blog_os::allocator; + use blog_os::memory::{self, BootInfoFrameAllocator}; + + blog_os::init(); + let mut mapper = unsafe { memory::init(boot_info.physical_memory_offset) }; + let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) }; + + allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed"); + + test_main(); + loop {} +} + +#[test_case] +fn simple_allocation() { + serial_print!("simple_allocation... "); + let heap_value = Box::new(41); + assert_eq!(*heap_value, 41); + serial_println!("[ok]"); +} + +#[test_case] +fn large_vec() { + serial_print!("large_vec... "); + let n = 1000; + let mut vec = Vec::new(); + for i in 0..n { + vec.push(i); + } + assert_eq!(vec.iter().sum::(), (n - 1) * n / 2); + serial_println!("[ok]"); +} + +#[test_case] +fn many_boxes() { + serial_print!("many_boxes... "); + for i in 0..10_000 { + let x = Box::new(i); + assert_eq!(*x, i); + } + serial_println!("[ok]"); +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + blog_os::test_panic_handler(info) +}