Debian Codegraph @

lib/util/test_arena.c

raw
1 /// @file
2 /// @brief Basic unit tester for arena.c
3
4 #ifdef NDEBUG
5 #error this is not intended to be compiled with assertions off
6 #endif
7
8 #define NO_CONFIG // suppress include of config.h in arena.c
9
10 #include <assert.h>
11 #include <limits.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <util/arena.c>
17
18 /// trivial lifecycle of an arena
19 static void test_basic_lifecycle(void) {
20
21 // create a new arena
22 arena_t a = {0};
23
24 // reset the arena
25 gv_arena_reset(&a);
26 }
27
28 // a more realistic lifecycle usage of an arena
29 static void test_lifecycle(void) {
30
31 // some arbitrary sizes we will allocate
32 static const size_t allocations[] = {123, 1, 42, 2, 3,
33 4096, 1024, 1023, 20000};
34
35 // length of the above array
36 enum { allocations_len = sizeof(allocations) / sizeof(allocations[0]) };
37
38 // somewhere to store pointers to memory we allocate
39 unsigned char *p[allocations_len];
40
41 // create a new arena
42 arena_t a = {0};
43
44 for (size_t i = 0; i < allocations_len; ++i) {
45
46 // create the allocation
47 const size_t ALIGN = 8;
48 p[i] = gv_arena_alloc(&a, ALIGN, allocations[i]);
49 assert(p[i] != NULL);
50 assert(((uintptr_t)p[i] & (ALIGN - 1)) == 0);
51
52 // confirm we can write to it without faulting
53 for (size_t j = 0; j < allocations[i]; ++j) {
54 volatile unsigned char *q = (volatile unsigned char *)&p[i][j];
55 *q = (unsigned char)(j % UCHAR_MAX);
56 }
57
58 // confirm we can read back what we wrote
59 for (size_t j = 0; j < allocations[i]; ++j) {
60 volatile unsigned char *q = (volatile unsigned char *)&p[i][j];
61 assert(*q == (unsigned char)(j % UCHAR_MAX));
62 }
63 }
64
65 // free a few allocations out of order from that in which we allocated
66 gv_arena_free(&a, p[3], allocations[3]);
67 gv_arena_free(&a, p[6], allocations[6]);
68 gv_arena_free(&a, p[5], allocations[5]);
69
70 // free the rest of the allocations in one sweep
71 gv_arena_reset(&a);
72
73 // try the allocate-write-read test in reverse order
74 for (size_t i = allocations_len - 1;; --i) {
75
76 // create the allocation
77 const size_t ALIGN = 8;
78 p[i] = gv_arena_alloc(&a, ALIGN, allocations[i]);
79 assert(p[i] != NULL);
80 assert(((uintptr_t)p[i] & (ALIGN - 1)) == 0);
81
82 // confirm we can write to it without faulting
83 for (size_t j = 0; j < allocations[i]; ++j) {
84 volatile unsigned char *q = (volatile unsigned char *)&p[i][j];
85 *q = (unsigned char)(j % UCHAR_MAX);
86 }
87
88 // confirm we can read back what we wrote
89 for (size_t j = 0; j < allocations[i]; ++j) {
90 volatile unsigned char *q = (volatile unsigned char *)&p[i][j];
91 assert(*q == (unsigned char)(j % UCHAR_MAX));
92 }
93
94 if (i == 0) {
95 break;
96 }
97 }
98
99 // clean up
100 gv_arena_reset(&a);
101 }
102
103 /// basic test of our strdup analogue
104 static void test_strdup(void) {
105
106 // create a new arena
107 arena_t a = {0};
108
109 const char s[] = "hello world";
110
111 // ask arena to strdup this
112 char *const t = gv_arena_strdup(&a, s);
113 assert(t != NULL);
114
115 // we should have received back the duplicated content
116 assert(strcmp(s, t) == 0);
117
118 // now ask the arena to strdup something it produced itself
119 char *const u = gv_arena_strdup(&a, t);
120 assert(u != NULL);
121
122 // we should still have the correct original string content
123 assert(strcmp(s, u) == 0);
124
125 // clean up
126 gv_arena_reset(&a);
127 }
128
129 int main(void) {
130
131 #define RUN(t) \
132 do { \
133 printf("running test_%s... ", #t); \
134 fflush(stdout); \
135 test_##t(); \
136 printf("OK\n"); \
137 } while (0)
138
139 RUN(basic_lifecycle);
140 RUN(lifecycle);
141 RUN(strdup);
142
143 #undef RUN
144
145 return EXIT_SUCCESS;
146 }