SomeGameEngineV2/engine/tests/base/unit/mem_arena_unit.c

70 lines
1.4 KiB
C

#include "memory_arena.h"
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
/**
* Memory arena Test
*
* At this level, the function should behave similarly to the standard lib's
* malloc, calloc, realloc, and free
*/
static int setup_mem_arena(void** state)
{
(void)state;
// Test with 8 MB
mem_arena_init(8);
return 0;
}
static int teardown_mem_arena(void** state)
{
(void)state;
mem_arena_deinit();
return 0;
}
static void test_simple_malloc(void **state)
{
(void)state;
void* buf = mem_arena_malloc(16);
assert_non_null(buf);
assert_int_equal(mem_arena_get_allocated(), 16);
mem_arena_print();
mem_arena_free(buf);
assert_int_equal(mem_arena_get_allocated(), 0);
}
static void test_free_unallocated(void **state)
{
(void)state;
int val = 0;
mem_arena_free(&val);
}
static void test_free_null(void **state)
{
(void)state;
mem_arena_free(NULL);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_simple_malloc, setup_mem_arena, teardown_mem_arena),
cmocka_unit_test_setup_teardown(test_free_unallocated, setup_mem_arena, teardown_mem_arena),
cmocka_unit_test_setup_teardown(test_free_null, setup_mem_arena, teardown_mem_arena),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}