Debian Codegraph @

lib/util/test_tokenize.c

raw
1 /// \file
2 /// \brief basic unit tester for tokenize.h
3
4 #ifdef NDEBUG
5 #error this is not intended to be compiled with assertions off
6 #endif
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <util/strview.h>
12 #include <util/tokenize.h>
13
14 /// basic lifecycle
15 static void test_basic(void) {
16 const char input[] = "hello world";
17 tok_t t = tok(input, " ");
18
19 assert(!tok_end(&t));
20 strview_t hello = tok_get(&t);
21 assert(strview_str_eq(hello, "hello"));
22 tok_next(&t);
23
24 assert(!tok_end(&t));
25 strview_t world = tok_get(&t);
26 assert(strview_str_eq(world, "world"));
27 tok_next(&t);
28
29 assert(tok_end(&t));
30 }
31
32 /// create, but do not use a tokenizer in case ASan can detect leaking memory
33 static void test_no_usage(void) {
34 const char input[] = "hello world";
35 tok_t t = tok(input, " ");
36
37 // squash compiler warnings for `t` being unused
38 (void)t;
39 }
40
41 /// an example with multiple tokens
42 static void test_multiple(void) {
43 const char input[] = "foo/bar/baz";
44 tok_t t = tok(input, "/");
45
46 assert(!tok_end(&t));
47 strview_t foo = tok_get(&t);
48 assert(strview_str_eq(foo, "foo"));
49 tok_next(&t);
50
51 assert(!tok_end(&t));
52 strview_t bar = tok_get(&t);
53 assert(strview_str_eq(bar, "bar"));
54 tok_next(&t);
55
56 assert(!tok_end(&t));
57 strview_t baz = tok_get(&t);
58 assert(strview_str_eq(baz, "baz"));
59 tok_next(&t);
60
61 assert(tok_end(&t));
62 }
63
64 /// input that starts with a separator
65 static void test_leading(void) {
66 const char input[] = "/foo/bar";
67 tok_t t = tok(input, "/");
68
69 assert(!tok_end(&t));
70 strview_t empty = tok_get(&t);
71 assert(strview_str_eq(empty, ""));
72 tok_next(&t);
73
74 assert(!tok_end(&t));
75 strview_t foo = tok_get(&t);
76 assert(strview_str_eq(foo, "foo"));
77 tok_next(&t);
78
79 assert(!tok_end(&t));
80 strview_t bar = tok_get(&t);
81 assert(strview_str_eq(bar, "bar"));
82 tok_next(&t);
83
84 assert(tok_end(&t));
85 }
86
87 /// input that ends with a separator
88 static void test_trailing(void) {
89 const char input[] = "foo/bar/";
90 tok_t t = tok(input, "/");
91
92 assert(!tok_end(&t));
93 strview_t foo = tok_get(&t);
94 assert(strview_str_eq(foo, "foo"));
95 tok_next(&t);
96
97 assert(!tok_end(&t));
98 strview_t bar = tok_get(&t);
99 assert(strview_str_eq(bar, "bar"));
100 tok_next(&t);
101
102 assert(!tok_end(&t));
103 strview_t empty = tok_get(&t);
104 assert(strview_str_eq(empty, ""));
105 tok_next(&t);
106
107 assert(tok_end(&t));
108 }
109
110 /// check multiple separators are coalesced into one
111 static void test_coalesce(void) {
112 const char input[] = "foo//bar";
113 tok_t t = tok(input, "/");
114
115 assert(!tok_end(&t));
116 strview_t foo = tok_get(&t);
117 assert(strview_str_eq(foo, "foo"));
118 tok_next(&t);
119
120 assert(!tok_end(&t));
121 strview_t bar = tok_get(&t);
122 assert(strview_str_eq(bar, "bar"));
123 tok_next(&t);
124
125 assert(tok_end(&t));
126 }
127
128 /// tokenizing the empty string should produce one token
129 static void test_empty(void) {
130 const char input[] = "";
131 tok_t t = tok(input, "/");
132
133 assert(!tok_end(&t));
134 strview_t empty = tok_get(&t);
135 assert(strview_str_eq(empty, ""));
136 tok_next(&t);
137
138 assert(tok_end(&t));
139 }
140
141 /// multiple different separators
142 static void test_multiple_separators(void) {
143 const char input[] = "foo/bar:baz:/:qux";
144 tok_t t = tok(input, "/:");
145
146 assert(!tok_end(&t));
147 strview_t foo = tok_get(&t);
148 assert(strview_str_eq(foo, "foo"));
149 tok_next(&t);
150
151 assert(!tok_end(&t));
152 strview_t bar = tok_get(&t);
153 assert(strview_str_eq(bar, "bar"));
154 tok_next(&t);
155
156 assert(!tok_end(&t));
157 strview_t baz = tok_get(&t);
158 assert(strview_str_eq(baz, "baz"));
159 tok_next(&t);
160
161 assert(!tok_end(&t));
162 strview_t qux = tok_get(&t);
163 assert(strview_str_eq(qux, "qux"));
164 tok_next(&t);
165
166 assert(tok_end(&t));
167 }
168
169 /// input that consists solely of separators
170 static void test_only_separators(void) {
171 const char input[] = "//:g/";
172 tok_t t = tok(input, "/:g");
173
174 assert(!tok_end(&t));
175 strview_t empty = tok_get(&t);
176 assert(strview_str_eq(empty, ""));
177 tok_next(&t);
178
179 assert(!tok_end(&t));
180 strview_t empty2 = tok_get(&t);
181 assert(strview_str_eq(empty2, ""));
182 tok_next(&t);
183
184 assert(tok_end(&t));
185 }
186
187 int main(void) {
188
189 #define RUN(t) \
190 do { \
191 printf("running test_%s... ", #t); \
192 fflush(stdout); \
193 test_##t(); \
194 printf("OK\n"); \
195 } while (0)
196
197 RUN(basic);
198 RUN(no_usage);
199 RUN(multiple);
200 RUN(leading);
201 RUN(trailing);
202 RUN(coalesce);
203 RUN(empty);
204 RUN(multiple_separators);
205 RUN(only_separators);
206
207 #undef RUN
208
209 return EXIT_SUCCESS;
210 }