summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOmniscient <omniscient.oce@gmail.com>2024-05-17 13:50:33 +1000
committerOmniscient <omniscient.oce@gmail.com>2024-05-17 13:50:33 +1000
commit519329e98467d0cdcc39720cef0f69c9936b6d55 (patch)
tree2837063ce51984dee00c6e2194e6f58ba189e8d8 /tests
parent9df999df385b74be5096218d206dd39988784237 (diff)
pool tests and try get macro working
Diffstat (limited to 'tests')
-rw-r--r--tests/pool_test_runner.c12
-rw-r--r--tests/pool_tests.c72
2 files changed, 84 insertions, 0 deletions
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 <stdbool.h>
+#include <stdlib.h>
+#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);
+}