From 10bb8a05088f1d3bb24f7167f609b5f6fb0ba026 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 30 Oct 2024 22:58:03 +0100 Subject: bootstrap project Signed-off-by: Johnny Richard --- tests/Makefile.am | 12 +++++ tests/obe_arena_test.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/obe_array_test.c | 85 ++++++++++++++++++++++++++++++++++ 3 files changed, 218 insertions(+) create mode 100644 tests/Makefile.am create mode 100644 tests/obe_arena_test.c create mode 100644 tests/obe_array_test.c (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..23baae3 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,12 @@ +AM_CPPFLAGS = -I$(top_srcdir)/include + +TESTS = arena_test array_test +check_PROGRAMS = arena_test array_test + +arena_test_SOURCES = obe_arena_test.c $(top_srcdir)/src/arena.c +arena_test_CFLAGS = @CHECK_CFLAGS@ +arena_test_LDADD = @CHECK_LIBS@ + +array_test_SOURCES = obe_array_test.c $(top_srcdir)/src/arena.c $(top_srcdir)/src/array.c +array_test_CFLAGS = @CHECK_CFLAGS@ +array_test_LDADD = @CHECK_LIBS@ 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 + * + * 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 . + */ +#include +#include +#include + +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; +} diff --git a/tests/obe_array_test.c b/tests/obe_array_test.c new file mode 100644 index 0000000..5439eec --- /dev/null +++ b/tests/obe_array_test.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2025 Johnny Richard + * + * 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 . + */ +#include +#include +#include +#include + +static obe_arena_t arena = { 0 }; + +void +setup(void) +{ +} + +void +teardown(void) +{ + obe_arena_release(&arena); +} + +START_TEST(obe_array_append) +{ + int* arr = obe_array(&arena, int); + + obe_array_append(arr, 1); + obe_array_append(arr, 2); + obe_array_append(arr, 3); + obe_array_append(arr, 4); + obe_array_append(arr, 5); + + ck_assert_int_eq(obe_array_length(arr), 5); +} +END_TEST + +Suite* +array_suite(void) +{ + Suite* s; + TCase* tc_append; + + s = suite_create("Array"); + + tc_append = tcase_create("tc_append"); + + tcase_add_checked_fixture(tc_append, setup, teardown); + + tcase_add_test(tc_append, obe_array_append); + suite_add_tcase(s, tc_append); + + return s; +} + +int +main(void) +{ + int number_failed; + Suite* s; + SRunner* sr; + + s = array_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; +} -- cgit v1.2.3