From 8dd0c4f27aae02dd60f029db4cf03f9902cba26f Mon Sep 17 00:00:00 2001 From: Linnnus Date: Tue, 8 Apr 2025 01:07:22 +0000 Subject: feat: Initial commit At this point we have some allocation routines but no work on the actual language has been done. --- src/unit/test_page_allocator.c | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/unit/test_page_allocator.c (limited to 'src/unit/test_page_allocator.c') diff --git a/src/unit/test_page_allocator.c b/src/unit/test_page_allocator.c new file mode 100644 index 0000000..ea90355 --- /dev/null +++ b/src/unit/test_page_allocator.c @@ -0,0 +1,59 @@ +#include "../core/allocator.h" +#include "../core/page_allocator.h" +#include "./allocator_utils.h" + +#include "greatest.h" +#include + +TEST larger_than_page_size(void) { + SandAllocator *a = sand_get_page_allocator(); + + long page_size = sysconf(_SC_PAGESIZE); + if (page_size < 0) { + FAILm("Failed to get page size!?"); + } + + size_t size = page_size + 128; + unsigned char *ptr = sand_allocate(a, size); + + for (size_t i = 0; i < size; ++i) { // This will crash under ASAN if invalid memory is returned. + ptr[i] = 0xFE; + } + + sand_deallocate(a, ptr, size); + PASS(); +} + +TEST five_page_allocation(void) { + SandAllocator *a = sand_get_page_allocator(); + + long page_size = sysconf(_SC_PAGESIZE); + if (page_size < 0) { + FAILm("Failed to get page size!?"); + } + + size_t size = 5 * page_size; + unsigned char *ptr = sand_allocate(a, size); + ASSERT_NEQm("Allocating 5 pqgs should not fail", ptr, NULL); + + for (size_t i = 0; i < size; ++i) { // This will crash under ASAN if invalid memory is returned. + ptr[i] = 0xFE; + } + + sand_deallocate(a, ptr, size); + PASS(); +} + +SUITE(page_allocator) { +#define RUN_WITH_PAGE_ALLOC(test) \ + do { \ + SandAllocator *a = sand_get_page_allocator(); \ + RUN_TEST1(test, a); \ + } while (0); + + RUN_ALLOCATOR_TESTS(RUN_WITH_PAGE_ALLOC); +#undef X + + RUN_TEST(larger_than_page_size); + RUN_TEST(five_page_allocation); +} -- cgit v1.2.3