summaryrefslogtreecommitdiff
path: root/tests/obe_arena_test.c
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2024-10-30 22:58:03 +0100
committerJohnny Richard <johnny@johnnyrichard.com>2025-12-14 09:53:52 +0100
commit10bb8a05088f1d3bb24f7167f609b5f6fb0ba026 (patch)
tree7a4b3f69a461301c45204ed856b61f92a7d42233 /tests/obe_arena_test.c
bootstrap projectHEADmaster
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'tests/obe_arena_test.c')
-rw-r--r--tests/obe_arena_test.c121
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/obe_arena_test.c b/tests/obe_arena_test.c
new file mode 100644
index 0000000..2ff97bf
--- /dev/null
+++ b/tests/obe_arena_test.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#include <check.h>
+#include <obe/arena.h>
+#include <stdlib.h>
+
+START_TEST(obe_arena_alloc_test)
+{
+ obe_arena_t arena = { 0 };
+ int* ptr = obe_arena_alloc(&arena, sizeof(int));
+ ck_assert_ptr_nonnull(ptr);
+
+ *ptr = 10;
+ ck_assert_int_eq(*ptr, 10);
+
+ obe_arena_free(&arena);
+}
+END_TEST
+
+START_TEST(obe_arena_realloc_test)
+{
+ obe_arena_t arena = { 0 };
+ int* ptr = obe_arena_alloc(&arena, sizeof(int));
+ ck_assert_ptr_nonnull(ptr);
+
+ *ptr = 42;
+
+ int* old_ptr = ptr;
+ int* new_ptr = obe_arena_realloc(&arena, ptr, sizeof(int), sizeof(int) * 2);
+
+ ck_assert_int_eq(new_ptr[0], 42);
+ ck_assert_ptr_ne(new_ptr, old_ptr);
+
+ obe_arena_free(&arena);
+}
+END_TEST
+
+START_TEST(obe_arena_new_region_test)
+{
+ obe_arena_t arena = { 0 };
+ void* ptr = obe_arena_alloc(&arena, OBE_ARENA_REGION_DEFAULT_CAPACITY);
+ ck_assert_ptr_nonnull(ptr);
+ ck_assert_ptr_nonnull(arena.begin);
+ ck_assert_ptr_eq(arena.begin, arena.end);
+ ck_assert_ptr_null(arena.end->next);
+
+ ptr = obe_arena_alloc(&arena, sizeof(char));
+ ck_assert_ptr_nonnull(ptr);
+ ck_assert_ptr_ne(arena.begin, arena.end);
+ ck_assert_ptr_eq(arena.end->data, ptr);
+ ck_assert_ptr_null(arena.end->next);
+
+ obe_arena_free(&arena);
+}
+END_TEST
+
+START_TEST(obe_arena_strdup_test)
+{
+ obe_arena_t arena = { 0 };
+ char* s = "long string";
+ char* dup = obe_arena_strdup(&arena, s);
+
+ ck_assert_str_eq(dup, s);
+
+ obe_arena_free(&arena);
+}
+END_TEST
+
+Suite*
+arena_suite(void)
+{
+ Suite* s;
+ TCase* alloc_case;
+
+ s = suite_create("Arena");
+
+ /* Allocation test case */
+ alloc_case = tcase_create("Allocation");
+
+ tcase_add_test(alloc_case, obe_arena_alloc_test);
+ tcase_add_test(alloc_case, obe_arena_realloc_test);
+ tcase_add_test(alloc_case, obe_arena_new_region_test);
+ tcase_add_test(alloc_case, obe_arena_strdup_test);
+ suite_add_tcase(s, alloc_case);
+
+ return s;
+}
+
+int
+main(void)
+{
+ int number_failed;
+ Suite* s;
+ SRunner* sr;
+
+ s = arena_suite();
+ sr = srunner_create(s);
+
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}