From 519329e98467d0cdcc39720cef0f69c9936b6d55 Mon Sep 17 00:00:00 2001 From: Omniscient Date: Fri, 17 May 2024 13:50:33 +1000 Subject: pool tests and try get macro working --- tests/pool_test_runner.c | 12 ++++++++ tests/pool_tests.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/pool_test_runner.c create mode 100644 tests/pool_tests.c (limited to 'tests') diff --git a/tests/pool_test_runner.c b/tests/pool_test_runner.c new file mode 100644 index 0000000..a6eff69 --- /dev/null +++ b/tests/pool_test_runner.c @@ -0,0 +1,12 @@ +#include "unity.h" +#include "unity_fixture.h" + +TEST_GROUP_RUNNER(Pool) { + // TODO: test cases + RUN_TEST_CASE(Pool, Initialisation); + RUN_TEST_CASE(Pool, TypedPool); +} + +static void RunAllTests(void) { RUN_TEST_GROUP(Pool); } + +int main(int argc, const char* argv[]) { return UnityMain(argc, argv, RunAllTests); } diff --git a/tests/pool_tests.c b/tests/pool_tests.c new file mode 100644 index 0000000..df65773 --- /dev/null +++ b/tests/pool_tests.c @@ -0,0 +1,72 @@ +#include +#include +#include "defines.h" +#include "unity.h" +#include "unity_fixture.h" + +#include "mem.h" + +#define ARENA_SIZE (1024 * 1024) + +static arena a; + +TEST_GROUP(Pool); + +TEST_SETUP(Pool) { a = arena_create(malloc(ARENA_SIZE), ARENA_SIZE); } + +TEST_TEAR_DOWN(Pool) { arena_free_all(&a); } + +TEST(Pool, SanityCheckTest) { TEST_ASSERT_EQUAL(true, true); } + +TEST(Pool, Initialisation) { + // u32 pool + void_pool pool = void_pool_create(&a, 256, sizeof(u32)); + u32 x_handle; + u32* x_ptr = (u32*)void_pool_alloc(&pool, &x_handle); + // store something in it + *x_ptr = 1024; + u32* x = void_pool_get(&pool, x_handle); + + TEST_ASSERT_EQUAL_UINT32(1024, *x); + /* TEST_ASSERT_EQUAL_MEMORY(&expected, &actual, sizeof(vec3)); */ +} + +typedef struct foo { + u32 a; + f32 b; + char c; +} foo; + +CORE_DEFINE_HANDLE(bar); +typedef struct bar_handle { + u32 raw; +} bar_handle; +TYPED_POOL(foo, bar); + +TEST(Pool, TypedPool) { + printf("Typed pool test\n"); + // create pool + bar_pool pool = bar_pool_create(&a, 2, sizeof(foo)); + + bar_handle first_handle, second_handle, third_handle; + foo* first = bar_pool_alloc(&pool, &first_handle); + foo* second = bar_pool_alloc(&pool, &second_handle); + // Third one shouldnt work + foo* third = bar_pool_alloc(&pool, &third_handle); + TEST_ASSERT_NULL(third); + + first->a = 32; + first->b = 2.0; + first->c = 'X'; + + foo* my_foo = bar_pool_get(&pool, first_handle); + TEST_ASSERT_EQUAL_UINT32(32, my_foo->a); + TEST_ASSERT_EQUAL(2.0, my_foo->b); + TEST_ASSERT_EQUAL_CHAR('X', my_foo->c); + + bar_pool_dealloc(&pool, first_handle); + + // next alloc should succeed + third = bar_pool_alloc(&pool, &third_handle); + TEST_ASSERT_NOT_NULL(third); +} -- cgit v1.2.3-70-g09d2