skip to content
==> tests/arena.c <==
1/* The arena: alignment, zeroing, growth in place and by copy, big blocks, reset and free. */
2#include "mesh/check.h"
3#include "tui/arena.h"
4
5#include <stdint.h>
6#include <string.h>
7
8int main(void) {
9    tui_arena a = {0};
10    char *s = tui_arena_fmt(&a, "%s-%d", "frame", 42);
11    CHECK_S(s, "frame-42");
12    for (int i = 0; i < 1000; i++) {
13        unsigned char *p = tui_arena_alloc(&a, (size_t)i + 1);
14        CHECK(((uintptr_t)p & 15) == 0);
15        int zero = 1;
16        for (int j = 0; j <= i; j++) zero &= p[j] == 0;
17        CHECK(zero);
18        memset(p, 0xab, (size_t)i + 1);
19    }
20    int *v = tui_arena_alloc(&a, 4 * sizeof *v);
21    for (int i = 0; i < 4; i++) v[i] = i;
22    int *w = tui_arena_grow(&a, v, 4 * sizeof *v, 64 * sizeof *v);
23    CHECK(w == v); /* the last allocation grows in place */
24    CHECK_I(w[3], 3);
25    CHECK_I(w[63], 0);
26    (void)tui_arena_alloc(&a, 8);
27    int *x = tui_arena_grow(&a, w, 64 * sizeof *w, 128 * sizeof *w);
28    CHECK(x != w); /* not the last any more: copied */
29    CHECK_I(x[3], 3);
30    char *big = tui_arena_alloc(&a, 1 << 20);
31    CHECK(big[0] == 0 && big[(1 << 20) - 1] == 0);
32    CHECK_S(tui_arena_strndup(&a, "abcdef", 3), "abc");
33    tui_arena_reset(&a);
34    CHECK_S(tui_arena_strdup(&a, "again"), "again");
35    tui_arena_free(&a);
36    tui_arena_free(&a);
37    return mesh_check_done();
38}