From d30eee0cf8a0bd60a3f5de044e1bcf4144e2b04e Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 7 Sep 2024 15:43:47 +0100 Subject: [PATCH] Add tests for Allocator implementations --- tests/allocator/test_allocator.c | 37 ++++++++++++++++++++++++++++++++ tests/allocator/test_allocator.h | 17 +++++++++++++++ tests/wapptest.c | 4 +++- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/allocator/test_allocator.c create mode 100644 tests/allocator/test_allocator.h diff --git a/tests/allocator/test_allocator.c b/tests/allocator/test_allocator.c new file mode 100644 index 0000000..1f7f3ec --- /dev/null +++ b/tests/allocator/test_allocator.c @@ -0,0 +1,37 @@ +#include "test_allocator.h" +#include "libc/mem_libc.h" +#include "mem_allocator.h" +#include "mem_arena.h" +#include "tester.h" +#include +#include + +TestFuncResult test_libc_allocator(void) { + Allocator allocator = wapp_mem_libc_allocator(); + bool result = allocator.obj == NULL && allocator.alloc != NULL && + allocator.alloc_aligned != NULL && allocator.realloc != NULL && + allocator.realloc_aligned == NULL && allocator.free != NULL; + void *ptr = wapp_mem_allocator_alloc(&allocator, 20); + result = result && (ptr != NULL); + + if (ptr != NULL) { + wapp_mem_allocator_free(&allocator, &ptr); + result = result && (ptr == NULL); + } + + return wapp_tester_result(result); +} + +TestFuncResult test_arena_allocator(void) { + Allocator allocator = wapp_mem_arena_allocator_init(4096); + bool result = allocator.obj != NULL && allocator.alloc != NULL && + allocator.alloc_aligned != NULL && + allocator.realloc == NULL & allocator.realloc_aligned == NULL && + allocator.free == NULL; + void *ptr = wapp_mem_allocator_alloc(&allocator, 20); + result = result && (ptr != NULL); + + wapp_mem_arena_allocator_destroy(&allocator); + + return wapp_tester_result(result); +} diff --git a/tests/allocator/test_allocator.h b/tests/allocator/test_allocator.h new file mode 100644 index 0000000..6b3e20a --- /dev/null +++ b/tests/allocator/test_allocator.h @@ -0,0 +1,17 @@ +#ifndef TEST_ALLOCATOR_H +#define TEST_ALLOCATOR_H + +#include "tester.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +TestFuncResult test_libc_allocator(void); +TestFuncResult test_arena_allocator(void); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // !TEST_ALLOCATOR_H diff --git a/tests/wapptest.c b/tests/wapptest.c index 279e1b3..1427c84 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -1,3 +1,4 @@ +#include "test_allocator.h" #include "test_arena.h" #include "test_shell_commander.h" #include "tester.h" @@ -5,7 +6,8 @@ int main(void) { wapp_tester_run_tests( - test_arena_init, test_arena_init_succeeds_when_reserving_very_large_size, + test_libc_allocator, test_arena_allocator, test_arena_init, + test_arena_init_succeeds_when_reserving_very_large_size, test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_fails_when_over_capacity, test_arena_clear, test_arena_destroy, test_commander_cmd_success,