From 0b63bc746dea6bd3825373e3fb3b1cb704c5dcdf Mon Sep 17 00:00:00 2001 From: Abdelrahman <said.abdelrahman89@gmail.com> Date: Sun, 2 Jun 2024 23:35:26 +0100 Subject: [PATCH] Add tests --- tests/arena/test_arena.c | 59 ++++++++++++++++++++++++++++++++++++++++ tests/arena/test_arena.h | 20 ++++++++++++++ tests/wapptest.c | 11 ++++++++ 3 files changed, 90 insertions(+) create mode 100644 tests/arena/test_arena.c create mode 100644 tests/arena/test_arena.h create mode 100644 tests/wapptest.c 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 <stdbool.h> +#include <stdlib.h> + +#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 <stdlib.h> + +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; +}