This commit is contained in:
Abdelrahman Said 2024-09-14 14:49:18 +01:00
parent a0dfe8acd0
commit 16a1b8fa35
4 changed files with 40 additions and 52 deletions

View File

@ -19,19 +19,16 @@ internal inline void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/***************************************************************************/ // /***************************************************************************/ //
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, MemAllocFlags flags, bool zero_buffer) {
MemAllocFlags flags,
bool zero_buffer) {
Allocator allocator = {0}; Allocator allocator = {0};
bool initialised = wapp_mem_arena_init_custom( bool initialised = wapp_mem_arena_init_custom((Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
(Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
if (!initialised) { if (!initialised) {
return allocator; return allocator;
} }
allocator.alloc = mem_arena_alloc; allocator.alloc = mem_arena_alloc;
allocator.alloc_aligned = mem_arena_alloc_aligned; allocator.alloc_aligned = mem_arena_alloc_aligned;
allocator.realloc = mem_arena_realloc; allocator.realloc = mem_arena_realloc;
allocator.realloc_aligned = mem_arena_realloc_aligned; allocator.realloc_aligned = mem_arena_realloc_aligned;
return allocator; return allocator;
@ -57,8 +54,7 @@ internal inline void *mem_arena_alloc(u64 size, void *alloc_obj) {
return wapp_mem_arena_alloc(arena, size); return wapp_mem_arena_alloc(arena, size);
} }
internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj) {
void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj; Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_alloc_aligned(arena, size, alignment); return wapp_mem_arena_alloc_aligned(arena, size, alignment);
} }

View File

@ -8,11 +8,11 @@
TestFuncResult test_libc_allocator(void) { TestFuncResult test_libc_allocator(void) {
Allocator allocator = wapp_mem_libc_allocator(); Allocator allocator = wapp_mem_libc_allocator();
bool result = allocator.obj == NULL && allocator.alloc != NULL && bool result = allocator.obj == NULL && allocator.alloc != NULL &&
allocator.alloc_aligned != NULL && allocator.realloc != NULL && allocator.alloc_aligned != NULL && allocator.realloc != NULL &&
allocator.realloc_aligned == NULL && allocator.free != NULL; allocator.realloc_aligned == 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);
if (ptr != NULL) { if (ptr != NULL) {
wapp_mem_allocator_free(&allocator, &ptr); wapp_mem_allocator_free(&allocator, &ptr);
@ -24,12 +24,12 @@ TestFuncResult test_libc_allocator(void) {
TestFuncResult test_arena_allocator(void) { 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);
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);

View File

@ -9,8 +9,8 @@
#define ARENA_CAPACITY 1024 #define ARENA_CAPACITY 1024
internal Arena *arena = NULL; internal Arena *arena = NULL;
internal i32 count = 20; internal i32 count = 20;
internal i32 *array = NULL; internal i32 *array = NULL;
TestFuncResult test_arena_init(void) { TestFuncResult test_arena_init(void) {
bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY); bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY);
@ -20,8 +20,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) {
Arena *large_arena = NULL; Arena *large_arena = NULL;
u64 capacity = GB(512); u64 capacity = GB(512);
bool result = wapp_mem_arena_init(&large_arena, capacity); bool result = wapp_mem_arena_init(&large_arena, capacity);
if (result) { if (result) {
wapp_mem_arena_destroy(&large_arena); wapp_mem_arena_destroy(&large_arena);
} }
@ -30,7 +30,7 @@ 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) {
array = wapp_mem_arena_alloc(arena, count * sizeof(i32)); array = wapp_mem_arena_alloc(arena, count * sizeof(i32));
bool result = array != NULL; bool result = array != NULL;
for (i32 i = 0; i < count; ++i) { for (i32 i = 0; i < count; ++i) {
@ -41,7 +41,7 @@ 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) {
u8 *bytes = wapp_mem_arena_alloc(arena, ARENA_CAPACITY * 2); u8 *bytes = wapp_mem_arena_alloc(arena, ARENA_CAPACITY * 2);
bool result = bytes == NULL; bool result = bytes == NULL;
return wapp_tester_result(result); return wapp_tester_result(result);

View File

@ -7,49 +7,41 @@
#include <string.h> #include <string.h>
TestFuncResult test_commander_cmd_success(void) { TestFuncResult test_commander_cmd_success(void) {
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, "echo", "hello world");
"echo", "hello world"); bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wapp_tester_result(succeeded); return wapp_tester_result(succeeded);
} }
TestFuncResult test_commander_cmd_failure(void) { TestFuncResult test_commander_cmd_failure(void) {
CMDResult result = CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, "grep");
wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, "grep"); bool failed = result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
bool failed = result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wapp_tester_result(failed); return wapp_tester_result(failed);
} }
TestFuncResult test_commander_cmd_out_buf_success(void) { TestFuncResult test_commander_cmd_out_buf_success(void) {
char buf[64] = {0}; char buf[64] = {0};
char expected_output[64] = {0}; char expected_output[64] = {0};
const char *msg = "hello world"; const char *msg = "hello world";
sprintf(expected_output, "%s\n", msg); sprintf(expected_output, "%s\n", msg);
CMDResult result = CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg);
wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg); bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR &&
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS && strcmp(buf, expected_output) == 0;
result.error == SHELL_ERR_NO_ERROR &&
strcmp(buf, expected_output) == 0;
return wapp_tester_result(succeeded); return wapp_tester_result(succeeded);
} }
TestFuncResult test_commander_cmd_out_buf_failure(void) { TestFuncResult test_commander_cmd_out_buf_failure(void) {
char buf[4] = {0}; char buf[4] = {0};
const char *msg = "hello world"; const char *msg = "hello world";
CMDResult result = CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 4, "echo", msg);
wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 4, "echo", msg); bool failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && strcmp(buf, msg) != 0;
bool failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && strcmp(buf, msg) != 0;
return wapp_tester_result(failed); return wapp_tester_result(failed);
} }