From cd38672581b20a00de1bbc668c27c6fd8742b095 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 14 Sep 2024 17:28:07 +0100 Subject: [PATCH] Add test for Arena's reallocation functionality --- tests/allocator/test_allocator.c | 2 +- tests/arena/test_arena.c | 46 ++++++++++++++++++++++++++++++++ tests/arena/test_arena.h | 2 ++ tests/wapptest.c | 14 +++++----- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/tests/allocator/test_allocator.c b/tests/allocator/test_allocator.c index 97852c9..8cb883c 100644 --- a/tests/allocator/test_allocator.c +++ b/tests/allocator/test_allocator.c @@ -26,7 +26,7 @@ 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.realloc != NULL & allocator.realloc_aligned != NULL && allocator.free == NULL; void *ptr = wapp_mem_allocator_alloc(&allocator, 20); result = result && (ptr != NULL); diff --git a/tests/arena/test_arena.c b/tests/arena/test_arena.c index 9f74ed9..5aaef41 100644 --- a/tests/arena/test_arena.c +++ b/tests/arena/test_arena.c @@ -47,6 +47,52 @@ TestFuncResult test_arena_alloc_fails_when_over_capacity(void) { return wapp_tester_result(result); } +TestFuncResult test_arena_realloc_bigger_size(void) { + u64 count = 10; + u64 new_count = 20; + i32 *bytes = wapp_mem_arena_alloc(arena, count * sizeof(i32)); + + for (u64 i = 0; i < count; ++i) { + bytes[i] = i; + } + + i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, count * sizeof(i32), new_count * sizeof(i32)); + if (!new_bytes) { + return wapp_tester_result(false); + } + + for (u64 i = 0; i < new_count; ++i) { + if (i < count && new_bytes[i] != bytes[i]) { + return wapp_tester_result(false); + } + } + + return wapp_tester_result(true); +} + +TestFuncResult test_arena_realloc_smaller_size(void) { + u64 count = 10; + u64 new_count = 5; + i32 *bytes = wapp_mem_arena_alloc(arena, count * sizeof(i32)); + + for (u64 i = 0; i < count; ++i) { + bytes[i] = i; + } + + i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, count * sizeof(i32), new_count * sizeof(i32)); + if (!new_bytes) { + return wapp_tester_result(false); + } + + for (u64 i = 0; i < new_count; ++i) { + if (i < new_count && new_bytes[i] != bytes[i]) { + return wapp_tester_result(false); + } + } + + return wapp_tester_result(true); +} + TestFuncResult test_arena_clear(void) { wapp_mem_arena_clear(arena); bool result = true; diff --git a/tests/arena/test_arena.h b/tests/arena/test_arena.h index 07b066f..4625b0f 100644 --- a/tests/arena/test_arena.h +++ b/tests/arena/test_arena.h @@ -11,6 +11,8 @@ TestFuncResult test_arena_init(void); TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void); TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void); TestFuncResult test_arena_alloc_fails_when_over_capacity(void); +TestFuncResult test_arena_realloc_bigger_size(void); +TestFuncResult test_arena_realloc_smaller_size(void); TestFuncResult test_arena_clear(void); TestFuncResult test_arena_destroy(void); diff --git a/tests/wapptest.c b/tests/wapptest.c index 1427c84..04e6fbe 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -5,14 +5,12 @@ #include int main(void) { - wapp_tester_run_tests( - 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, - test_commander_cmd_failure, test_commander_cmd_out_buf_success, - test_commander_cmd_out_buf_failure); + wapp_tester_run_tests(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_realloc_bigger_size, test_arena_realloc_smaller_size, test_arena_clear, + test_arena_destroy, test_commander_cmd_success, test_commander_cmd_failure, + test_commander_cmd_out_buf_success, test_commander_cmd_out_buf_failure); return EXIT_SUCCESS; }