Debian Codegraph @

test/unit/lib/test_registry.c

raw
1 #include <stdlib.h>
2
3 #include "../../../src/lib/registry.h"
4
5 #include "../../lib/runner.h"
6
7 TEST_MODULE(lib_registry);
8
9 struct test_item
10 {
11 size_t id;
12 int *ptr;
13 };
14
15 static void test_item_init(struct test_item *i)
16 {
17 munit_assert(i != NULL);
18
19 i->ptr = (int *)sqlite3_malloc(sizeof(*(i->ptr)));
20 *i->ptr = 123;
21 }
22
23 static void test_item_close(struct test_item *i)
24 {
25 munit_assert(i != NULL);
26 munit_assert(i->ptr != NULL);
27
28 sqlite3_free(i->ptr);
29 }
30
31 static const char *test_item_hash(struct test_item *i)
32 {
33 munit_assert(i != NULL);
34
35 return "x";
36 }
37
38 REGISTRY(test_registry, test_item);
39 REGISTRY_METHODS(test_registry, test_item);
40
41 static void *setup(const MunitParameter params[], void *user_data)
42 {
43 struct test_registry *registry;
44
45 (void)params;
46 (void)user_data;
47
48 registry = (struct test_registry *)munit_malloc(sizeof(*registry));
49
50 test_registry_init(registry);
51
52 return registry;
53 }
54
55 static void tear_down(void *data)
56 {
57 struct test_registry *registry = data;
58
59 test_registry_close(registry);
60 free(registry);
61 }
62
63 TEST_SUITE(add);
64 TEST_SETUP(add, setup);
65 TEST_TEAR_DOWN(add, tear_down);
66
67 static char *test_add_n[] = {"1", "2", "3", "5", "6",
68 "7", "8", "9", "10", NULL};
69
70 static MunitParameterEnum test_add_params[] = {
71 {"n", test_add_n},
72 {NULL, NULL},
73 };
74
75 /* Add N items. */
76 TEST_CASE(add, basic, test_add_params)
77 {
78 struct test_registry *registry = data;
79 int err;
80 struct test_item *item;
81 int n;
82 int i;
83
84 n = atoi(munit_parameters_get(params, "n"));
85 munit_assert_int(n, >, 0);
86
87 for (i = 0; i < n; i++) {
88 err = test_registry_add(registry, &item);
89 munit_assert_int(err, ==, 0);
90
91 munit_assert_ptr_not_equal(item, NULL);
92 munit_assert_ptr_not_equal(item->ptr, NULL);
93 munit_assert_int(123, ==, *item->ptr);
94 }
95
96 return MUNIT_OK;
97 }
98
99 /* Add three items, delete the second, and then add another one. The original ID
100 * of the deleted item gets reused. */
101 TEST_CASE(add, del_add, NULL)
102 {
103 struct test_registry *registry = data;
104 int err;
105 struct test_item *item1;
106 struct test_item *item2;
107 struct test_item *item3;
108 struct test_item *item4;
109 int item2_id;
110
111 (void)params;
112
113 err = test_registry_add(registry, &item1);
114 munit_assert_int(err, ==, 0);
115
116 err = test_registry_add(registry, &item2);
117 munit_assert_int(err, ==, 0);
118 item2_id = item2->id;
119
120 err = test_registry_add(registry, &item3);
121 munit_assert_int(err, ==, 0);
122
123 err = test_registry_del(registry, item2);
124 munit_assert_int(err, ==, 0);
125
126 err = test_registry_add(registry, &item4);
127 munit_assert_int(err, ==, 0);
128
129 munit_assert_int(item4->id, ==, item2_id);
130
131 return MUNIT_OK;
132 }
133
134 /* Add N items and then delete them all. */
135 TEST_CASE(add, and_del, test_add_params)
136 {
137 struct test_registry *registry = data;
138 int err;
139 struct test_item **items;
140 int n;
141 int i;
142
143 n = atoi(munit_parameters_get(params, "n"));
144 munit_assert_int(n, >, 0);
145
146 items = munit_malloc(n * sizeof(*items));
147
148 for (i = 0; i < n; i++) {
149 err = test_registry_add(registry, &items[i]);
150 munit_assert_int(err, ==, 0);
151 }
152
153 for (i = 0; i < n; i++) {
154 err = test_registry_del(registry, items[i]);
155 munit_assert_int(err, ==, 0);
156 }
157
158 free(items);
159
160 return MUNIT_OK;
161 }
162
163 TEST_SUITE(get);
164 TEST_SETUP(get, setup);
165 TEST_TEAR_DOWN(get, tear_down);
166
167 /* Retrieve a previously added item. */
168 TEST_CASE(get, basic, NULL)
169 {
170 struct test_registry *registry = data;
171 int err;
172 struct test_item *item;
173
174 (void)params;
175
176 err = test_registry_add(registry, &item);
177 munit_assert_int(err, ==, 0);
178
179 munit_assert_ptr_equal(test_registry_get(registry, item->id), item);
180
181 return MUNIT_OK;
182 }
183
184 /* An item gets added and then deleted. Trying to fetch the item using its
185 * former ID results in a NULL pointer. */
186 TEST_CASE(get, deleted, NULL)
187 {
188 struct test_registry *registry = data;
189 int err;
190 struct test_item *item;
191 size_t id;
192
193 (void)params;
194
195 err = test_registry_add(registry, &item);
196 munit_assert_int(err, ==, 0);
197
198 id = item->id;
199
200 err = test_registry_del(registry, item);
201 munit_assert_int(err, ==, 0);
202
203 munit_assert_ptr_equal(test_registry_get(registry, id), NULL);
204
205 return MUNIT_OK;
206 }
207
208 /* Retrieve an item with an ID bigger than the current registry's length. */
209 TEST_CASE(get, out_of_bound, NULL)
210 {
211 struct test_registry *registry = data;
212 struct test_item *item = test_registry_get(registry, 123);
213
214 (void)params;
215
216 munit_assert_ptr_equal(item, NULL);
217
218 return MUNIT_OK;
219 }
220
221 TEST_SUITE(idx);
222 TEST_SETUP(idx, setup);
223 TEST_TEAR_DOWN(idx, tear_down);
224
225 /* Find the index of a matching item. */
226 TEST_CASE(idx, found, NULL)
227 {
228 struct test_registry *registry = data;
229 struct test_item *item;
230 size_t i;
231 int err;
232
233 (void)params;
234
235 err = test_registry_add(registry, &item);
236 munit_assert_int(err, ==, 0);
237
238 err = test_registry_idx(registry, "x", &i);
239 munit_assert_int(err, ==, 0);
240
241 munit_assert_int(i, ==, item->id);
242
243 return MUNIT_OK;
244 }
245
246 /* No matching item. */
247 TEST_CASE(idx, not_found, NULL)
248 {
249 struct test_registry *registry = data;
250 struct test_item *item1;
251 struct test_item *item2;
252 size_t i;
253 int err;
254
255 (void)params;
256
257 err = test_registry_add(registry, &item1);
258 munit_assert_int(err, ==, 0);
259
260 err = test_registry_add(registry, &item2);
261 munit_assert_int(err, ==, 0);
262
263 err = test_registry_del(registry, item1);
264 munit_assert_int(err, ==, 0);
265
266 err = test_registry_idx(registry, "y", &i);
267 munit_assert_int(err, ==, COWSQL_NOTFOUND);
268
269 return MUNIT_OK;
270 }
271
272 TEST_SUITE(del);
273 TEST_SETUP(del, setup);
274 TEST_TEAR_DOWN(del, tear_down);
275
276 /* Delete an item from the registry. */
277 TEST_CASE(del, basic, NULL)
278 {
279 struct test_registry *registry = data;
280 int err;
281 struct test_item *item;
282
283 (void)params;
284
285 err = test_registry_add(registry, &item);
286 munit_assert_int(err, ==, 0);
287
288 err = test_registry_del(registry, item);
289 munit_assert_int(err, ==, 0);
290
291 return MUNIT_OK;
292 }
293
294 /* Deleting an item twice results in an error. */
295 TEST_CASE(del, twice, NULL)
296 {
297 struct test_registry *registry = data;
298 int err;
299 struct test_item *item;
300 struct test_item item_clone;
301
302 (void)params;
303
304 err = test_registry_add(registry, &item);
305 munit_assert_int(err, ==, 0);
306 item_clone.id = item->id;
307
308 err = test_registry_del(registry, item);
309 munit_assert_int(err, ==, 0);
310
311 err = test_registry_del(registry, &item_clone);
312 munit_assert_int(err, ==, COWSQL_NOTFOUND);
313
314 return MUNIT_OK;
315 }
316
317 /* Deleting an item twice results in an error, also if the item being deleted
318 * again has an ID lower than the highest one. */
319 TEST_CASE(del, twice_middle, NULL)
320 {
321 struct test_registry *registry = data;
322 int err;
323 struct test_item *item1;
324 struct test_item *item2;
325 struct test_item item1_clone;
326
327 (void)params;
328
329 err = test_registry_add(registry, &item1);
330 munit_assert_int(err, ==, 0);
331 item1_clone.id = item1->id;
332
333 err = test_registry_add(registry, &item2);
334 munit_assert_int(err, ==, 0);
335
336 err = test_registry_del(registry, item1);
337 munit_assert_int(err, ==, 0);
338
339 err = test_registry_del(registry, &item1_clone);
340 munit_assert_int(err, ==, COWSQL_NOTFOUND);
341
342 return MUNIT_OK;
343 }
344
345 /* Deleting an item with an unknown ID results in an error. */
346 TEST_CASE(del, out_of_bounds, NULL)
347 {
348 struct test_registry *registry = data;
349 struct test_item item;
350 int err;
351
352 (void)params;
353
354 item.id = 123;
355
356 err = test_registry_del(registry, &item);
357
358 munit_assert_int(err, ==, COWSQL_NOTFOUND);
359
360 return MUNIT_OK;
361 }
362
363 /* Add several items and then delete them. */
364 TEST_CASE(del, many, NULL)
365 {
366 struct test_registry *registry = data;
367 int err;
368 struct test_item *item1;
369 struct test_item *item2;
370 struct test_item *item3;
371
372 (void)params;
373
374 err = test_registry_add(registry, &item1);
375 munit_assert_int(err, ==, 0);
376
377 munit_assert_int(item1->id, ==, 0);
378
379 err = test_registry_add(registry, &item2);
380 munit_assert_int(err, ==, 0);
381
382 munit_assert_int(item2->id, ==, 1);
383
384 err = test_registry_add(registry, &item3);
385 munit_assert_int(err, ==, 0);
386
387 munit_assert_int(item3->id, ==, 2);
388
389 munit_assert_int(3, ==, registry->len);
390 munit_assert_int(4, ==, registry->cap);
391
392 err = test_registry_del(registry, item3);
393 munit_assert_int(err, ==, 0);
394
395 munit_assert_int(2, ==, registry->len);
396 munit_assert_int(4, ==, registry->cap);
397
398 err = test_registry_del(registry, item2);
399 munit_assert_int(err, ==, 0);
400
401 munit_assert_int(1, ==, registry->len);
402 munit_assert_int(2, ==, registry->cap);
403
404 return MUNIT_OK;
405 }