summaryrefslogtreecommitdiff
path: root/tests/obe_arena_test.c
blob: 2ff97bf3548a8e501d24df91217f0f938cb275db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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;
}