Add tests for Allocator implementations

This commit is contained in:
2024-09-07 15:43:47 +01:00
parent 59e56a75d3
commit d30eee0cf8
3 changed files with 57 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
#include "test_allocator.h"
#include "libc/mem_libc.h"
#include "mem_allocator.h"
#include "mem_arena.h"
#include "tester.h"
#include <stdbool.h>
#include <stdlib.h>
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);
if (ptr != NULL) {
wapp_mem_allocator_free(&allocator, &ptr);
result = result && (ptr == NULL);
}
return wapp_tester_result(result);
}
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);
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}