Add tests

This commit is contained in:
Abdelrahman Said 2024-06-02 23:35:26 +01:00
parent 6ee3c762df
commit 0b63bc746d
3 changed files with 90 additions and 0 deletions

59
tests/arena/test_arena.c Normal file
View File

@ -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);
}

20
tests/arena/test_arena.h Normal file
View File

@ -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

11
tests/wapptest.c Normal file
View File

@ -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;
}