diff --git a/tests/arena/test_arena.c b/tests/arena/test_arena.c new file mode 100644 index 0000000..ad2695c --- /dev/null +++ b/tests/arena/test_arena.c @@ -0,0 +1,59 @@ +#include "test_arena.h" +#include "aliases.h" +#include "mem_arena.h" +#include "mem_utils.h" +#include "tester.h" +#include +#include + +#define ARENA_CAPACITY 1024 + +internal Arena *arena = NULL; +internal u64 count = 20; +internal i32 *array = NULL; + +TestFuncResult test_arena_init(void) { + bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY, + WAPP_MEM_ALLOC_RESERVE, false); + + return TEST_RESULT(result); +} + +TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void) { + array = wapp_mem_arena_alloc(arena, count * sizeof(i32)); + bool result = array != NULL; + + for (u64 i = 0; i < count; ++i) { + array[i] = i * 10; + } + + return TEST_RESULT(result); +} + +TestFuncResult test_arena_alloc_fails_when_over_capacity(void) { + u8 *bytes = wapp_mem_arena_alloc(arena, ARENA_CAPACITY * 2); + bool result = bytes == NULL; + + return TEST_RESULT(result); +} + +TestFuncResult test_arena_clear(void) { + wapp_mem_arena_clear(arena); + bool result = true; + + for (u64 i = 0; i < count; ++i) { + if (array[i] != 0) { + result = false; + break; + } + } + + return TEST_RESULT(result); +} + +TestFuncResult test_arena_destroy(void) { + wapp_mem_arena_destroy(&arena); + bool result = arena == NULL; + + return TEST_RESULT(result); +} diff --git a/tests/arena/test_arena.h b/tests/arena/test_arena.h new file mode 100644 index 0000000..8e6be9f --- /dev/null +++ b/tests/arena/test_arena.h @@ -0,0 +1,20 @@ +#ifndef TEST_ARENA_H +#define TEST_ARENA_H + +#include "tester.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +TestFuncResult test_arena_init(void); +TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void); +TestFuncResult test_arena_alloc_fails_when_over_capacity(void); +TestFuncResult test_arena_clear(void); +TestFuncResult test_arena_destroy(void); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // !TEST_ARENA_H diff --git a/tests/wapptest.c b/tests/wapptest.c new file mode 100644 index 0000000..3b361e5 --- /dev/null +++ b/tests/wapptest.c @@ -0,0 +1,11 @@ +#include "test_arena.h" +#include "tester.h" +#include + +int main(void) { + run_tests(test_arena_init, test_arena_alloc_succeeds_when_within_capacity, + test_arena_alloc_fails_when_over_capacity, test_arena_clear, + test_arena_destroy); + + return EXIT_SUCCESS; +}