Add test for Arena's reallocation functionality
This commit is contained in:
parent
7fea236618
commit
cd38672581
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
||||
|
@ -5,14 +5,12 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user