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

View File

@ -8,11 +8,11 @@
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);
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);
@ -24,12 +24,12 @@ TestFuncResult test_libc_allocator(void) {
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);
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);

View File

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

View File

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