Add test for Arena's reallocation functionality

This commit is contained in:
Abdelrahman Said 2024-09-14 17:28:07 +01:00
parent 7fea236618
commit cd38672581
4 changed files with 55 additions and 9 deletions

View File

@ -26,7 +26,7 @@ TestFuncResult test_arena_allocator(void) {
Allocator allocator = wapp_mem_arena_allocator_init(4096); Allocator allocator = wapp_mem_arena_allocator_init(4096);
bool result = allocator.obj != NULL && allocator.alloc != NULL && bool result = allocator.obj != NULL && allocator.alloc != NULL &&
allocator.alloc_aligned != NULL && allocator.alloc_aligned != NULL &&
allocator.realloc == NULL & allocator.realloc_aligned == NULL && allocator.realloc != NULL & allocator.realloc_aligned != NULL &&
allocator.free == NULL; allocator.free == NULL;
void *ptr = wapp_mem_allocator_alloc(&allocator, 20); void *ptr = wapp_mem_allocator_alloc(&allocator, 20);
result = result && (ptr != NULL); result = result && (ptr != NULL);

View File

@ -47,6 +47,52 @@ TestFuncResult test_arena_alloc_fails_when_over_capacity(void) {
return wapp_tester_result(result); 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) { TestFuncResult test_arena_clear(void) {
wapp_mem_arena_clear(arena); wapp_mem_arena_clear(arena);
bool result = true; bool result = true;

View File

@ -11,6 +11,8 @@ TestFuncResult test_arena_init(void);
TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(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_succeeds_when_within_capacity(void);
TestFuncResult test_arena_alloc_fails_when_over_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_clear(void);
TestFuncResult test_arena_destroy(void); TestFuncResult test_arena_destroy(void);

View File

@ -5,14 +5,12 @@
#include <stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
wapp_tester_run_tests( wapp_tester_run_tests(test_libc_allocator, test_arena_allocator, test_arena_init,
test_libc_allocator, test_arena_allocator, test_arena_init,
test_arena_init_succeeds_when_reserving_very_large_size, test_arena_init_succeeds_when_reserving_very_large_size,
test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_fails_when_over_capacity,
test_arena_alloc_fails_when_over_capacity, test_arena_clear, test_arena_realloc_bigger_size, test_arena_realloc_smaller_size, test_arena_clear,
test_arena_destroy, test_commander_cmd_success, test_arena_destroy, test_commander_cmd_success, test_commander_cmd_failure,
test_commander_cmd_failure, test_commander_cmd_out_buf_success, test_commander_cmd_out_buf_success, test_commander_cmd_out_buf_failure);
test_commander_cmd_out_buf_failure);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }