libdpkg: Add varbuf_add_varbuf() function
[dpkg.git] / lib / dpkg / t / t-varbuf.c
blobda9dd9a9f4f025fba646583d0eb15bb0a25c1e71
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * t-varbuf.c - test varbuf implementation
5 * Copyright © 2009-2011, 2013-2015 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <dpkg/test.h>
25 #include <dpkg/varbuf.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <stdlib.h>
31 static void
32 test_varbuf_init(void)
34 struct varbuf vb;
36 varbuf_init(&vb, 0);
37 test_pass(vb.used == 0);
38 test_pass(vb.size == 0);
39 test_pass(vb.buf == NULL);
41 varbuf_destroy(&vb);
42 test_pass(vb.used == 0);
43 test_pass(vb.size == 0);
44 test_pass(vb.buf == NULL);
47 static void
48 test_varbuf_prealloc(void)
50 struct varbuf vb;
52 varbuf_init(&vb, 10);
53 test_pass(vb.used == 0);
54 test_pass(vb.size >= 10);
55 test_pass(vb.buf != NULL);
57 varbuf_destroy(&vb);
58 test_pass(vb.used == 0);
59 test_pass(vb.size == 0);
60 test_pass(vb.buf == NULL);
63 static void
64 test_varbuf_new(void)
66 struct varbuf *vb;
68 vb = varbuf_new(0);
69 test_pass(vb != NULL);
70 test_pass(vb->used == 0);
71 test_pass(vb->size == 0);
72 test_pass(vb->buf == NULL);
73 varbuf_free(vb);
75 vb = varbuf_new(10);
76 test_pass(vb != NULL);
77 test_pass(vb->used == 0);
78 test_pass(vb->size >= 10);
79 test_pass(vb->buf != NULL);
80 varbuf_free(vb);
83 static void
84 test_varbuf_grow(void)
86 struct varbuf vb;
87 jmp_buf grow_jump;
88 size_t old_size;
89 bool grow_overflow;
90 int i;
92 varbuf_init(&vb, 10);
94 /* Test that we grow when needed. */
95 varbuf_grow(&vb, 100);
96 test_pass(vb.used == 0);
97 test_pass(vb.size >= 100);
99 old_size = vb.size;
101 /* Test that we are not leaking. */
102 for (i = 0; i < 10; i++) {
103 varbuf_grow(&vb, 100);
104 test_pass(vb.used == 0);
105 test_pass(vb.size >= 100);
106 test_pass(vb.size == old_size);
109 /* Test that we grow when needed, with used space. */
110 vb.used = 10;
111 varbuf_grow(&vb, 100);
112 test_pass(vb.used == 10);
113 test_pass(vb.size >= 110);
115 /* Test that we do not allow allocation overflows. */
116 grow_overflow = false;
117 old_size = vb.size;
118 test_try(grow_jump) {
119 varbuf_grow(&vb, SIZE_MAX - vb.size + 2);
120 } test_catch {
121 grow_overflow = true;
122 } test_finally;
123 test_pass(vb.size == old_size && grow_overflow);
125 grow_overflow = false;
126 old_size = vb.size;
127 test_try(grow_jump) {
128 varbuf_grow(&vb, (SIZE_MAX - vb.size - 2) / 2);
129 } test_catch {
130 grow_overflow = true;
131 } test_finally;
132 test_pass(vb.size == old_size && grow_overflow);
134 varbuf_destroy(&vb);
137 static void
138 test_varbuf_trunc(void)
140 struct varbuf vb;
142 varbuf_init(&vb, 50);
144 /* Test that we truncate (grow). */
145 varbuf_trunc(&vb, 20);
146 test_pass(vb.used == 20);
147 test_pass(vb.size >= 50);
149 /* Test that we truncate (shrink). */
150 varbuf_trunc(&vb, 10);
151 test_pass(vb.used == 10);
152 test_pass(vb.size >= 50);
154 varbuf_destroy(&vb);
157 static void
158 test_varbuf_add_varbuf(void)
160 struct varbuf vb, cb;
162 varbuf_init(&vb, 5);
163 varbuf_init(&cb, 0);
165 varbuf_add_str(&vb, "1234567890");
166 varbuf_add_varbuf(&cb, &vb);
167 test_pass(cb.used == 10);
168 test_pass(cb.size >= cb.used);
169 test_mem(cb.buf, ==, "1234567890", 10);
171 varbuf_reset(&vb);
172 varbuf_add_str(&vb, "abcde");
173 varbuf_add_varbuf(&cb, &vb);
174 test_pass(cb.used == 15);
175 test_pass(cb.size >= cb.used);
176 test_mem(cb.buf, ==, "1234567890abcde", 15);
178 varbuf_destroy(&cb);
179 varbuf_destroy(&vb);
182 static void
183 test_varbuf_add_buf(void)
185 struct varbuf vb;
187 varbuf_init(&vb, 5);
189 varbuf_add_buf(&vb, "1234567890", 10);
190 test_pass(vb.used == 10);
191 test_pass(vb.size >= vb.used);
192 test_mem(vb.buf, ==, "1234567890", 10);
194 varbuf_add_buf(&vb, "abcde", 5);
195 test_pass(vb.used == 15);
196 test_pass(vb.size >= vb.used);
197 test_mem(vb.buf, ==, "1234567890abcde", 15);
199 varbuf_destroy(&vb);
202 static void
203 test_varbuf_add_char(void)
205 struct varbuf vb;
207 varbuf_init(&vb, 1);
209 varbuf_add_char(&vb, 'a');
210 test_pass(vb.used == 1);
211 test_pass(vb.size >= vb.used);
212 test_pass(vb.buf[0] == 'a');
214 varbuf_add_char(&vb, 'b');
215 test_pass(vb.used == 2);
216 test_pass(vb.size >= vb.used);
217 test_mem(vb.buf, ==, "ab", 2);
219 varbuf_add_char(&vb, 'c');
220 test_pass(vb.used == 3);
221 test_pass(vb.size >= vb.used);
222 test_mem(vb.buf, ==, "abc", 3);
224 varbuf_add_char(&vb, 'd');
225 test_pass(vb.used == 4);
226 test_pass(vb.size >= vb.used);
227 test_mem(vb.buf, ==, "abcd", 4);
229 varbuf_destroy(&vb);
232 static void
233 test_varbuf_dup_char(void)
235 struct varbuf vb;
237 varbuf_init(&vb, 5);
239 varbuf_dup_char(&vb, 'z', 10);
240 test_pass(vb.used == 10);
241 test_pass(vb.size >= vb.used);
242 test_mem(vb.buf, ==, "zzzzzzzzzz", 10);
244 varbuf_dup_char(&vb, 'y', 5);
245 test_pass(vb.used == 15);
246 test_pass(vb.size >= vb.used);
247 test_mem(vb.buf, ==, "zzzzzzzzzzyyyyy", 15);
249 varbuf_destroy(&vb);
252 static void
253 test_varbuf_map_char(void)
255 struct varbuf vb;
257 varbuf_init(&vb, 5);
259 varbuf_add_buf(&vb, "1234a5678a9012a", 15);
261 varbuf_map_char(&vb, 'a', 'z');
262 test_pass(vb.used == 15);
263 test_pass(vb.size >= vb.used);
264 test_mem(vb.buf, ==, "1234z5678z9012z", 15);
266 varbuf_destroy(&vb);
269 static void
270 test_varbuf_add_dir(void)
272 struct varbuf vb;
274 varbuf_init(&vb, 10);
276 varbuf_add_dir(&vb, "");
277 varbuf_end_str(&vb);
278 test_str(vb.buf, ==, "/");
279 varbuf_add_dir(&vb, "");
280 varbuf_end_str(&vb);
281 test_str(vb.buf, ==, "/");
282 varbuf_add_dir(&vb, "aa");
283 varbuf_end_str(&vb);
284 test_str(vb.buf, ==, "/aa/");
285 varbuf_add_dir(&vb, "");
286 varbuf_end_str(&vb);
287 test_str(vb.buf, ==, "/aa/");
289 varbuf_reset(&vb);
291 varbuf_add_dir(&vb, "/foo/bar");
292 varbuf_end_str(&vb);
293 test_str(vb.buf, ==, "/foo/bar/");
295 varbuf_reset(&vb);
297 varbuf_add_dir(&vb, "/foo/bar/");
298 varbuf_end_str(&vb);
299 test_str(vb.buf, ==, "/foo/bar/");
300 varbuf_add_dir(&vb, "quux");
301 varbuf_end_str(&vb);
302 test_str(vb.buf, ==, "/foo/bar/quux/");
303 varbuf_add_dir(&vb, "zoo");
304 varbuf_end_str(&vb);
305 test_str(vb.buf, ==, "/foo/bar/quux/zoo/");
307 varbuf_destroy(&vb);
310 static void
311 test_varbuf_end_str(void)
313 struct varbuf vb;
315 varbuf_init(&vb, 10);
317 varbuf_add_buf(&vb, "1234567890X", 11);
318 test_pass(vb.used == 11);
319 test_pass(vb.size >= vb.used);
320 test_mem(vb.buf, ==, "1234567890X", 11);
322 varbuf_trunc(&vb, 10);
324 varbuf_end_str(&vb);
325 test_pass(vb.used == 10);
326 test_pass(vb.size >= vb.used + 1);
327 test_pass(vb.buf[10] == '\0');
328 test_str(vb.buf, ==, "1234567890");
330 varbuf_destroy(&vb);
333 static void
334 test_varbuf_get_str(void)
336 struct varbuf vb;
337 const char *str;
339 varbuf_init(&vb, 10);
341 varbuf_add_buf(&vb, "1234567890", 10);
342 str = varbuf_get_str(&vb);
343 test_pass(vb.buf == str);
344 test_pass(vb.used == 10);
345 test_pass(vb.buf[vb.used] == '\0');
346 test_pass(str[vb.used] == '\0');
347 test_str(vb.buf, ==, "1234567890");
348 test_str(str, ==, "1234567890");
350 varbuf_add_buf(&vb, "abcde", 5);
351 str = varbuf_get_str(&vb);
352 test_pass(vb.buf == str);
353 test_pass(vb.used == 15);
354 test_pass(vb.buf[vb.used] == '\0');
355 test_pass(str[vb.used] == '\0');
356 test_str(vb.buf, ==, "1234567890abcde");
357 test_str(str, ==, "1234567890abcde");
359 varbuf_destroy(&vb);
362 static void
363 test_varbuf_printf(void)
365 struct varbuf vb;
367 varbuf_init(&vb, 5);
369 /* Test normal format printing. */
370 varbuf_printf(&vb, "format %s number %d", "string", 10);
371 test_pass(vb.used == strlen("format string number 10"));
372 test_pass(vb.size >= vb.used);
373 test_str(vb.buf, ==, "format string number 10");
375 varbuf_reset(&vb);
377 /* Test concatenated format printing. */
378 varbuf_printf(&vb, "format %s number %d", "string", 10);
379 varbuf_printf(&vb, " extra %s", "string");
380 test_pass(vb.used == strlen("format string number 10 extra string"));
381 test_pass(vb.size >= vb.used);
382 test_str(vb.buf, ==, "format string number 10 extra string");
384 varbuf_destroy(&vb);
387 static void
388 test_varbuf_reset(void)
390 struct varbuf vb;
392 varbuf_init(&vb, 10);
394 varbuf_add_buf(&vb, "1234567890", 10);
396 varbuf_reset(&vb);
397 test_pass(vb.used == 0);
398 test_pass(vb.size >= vb.used);
400 varbuf_add_buf(&vb, "abcdefghijklmno", 15);
401 test_pass(vb.used == 15);
402 test_pass(vb.size >= vb.used);
403 test_mem(vb.buf, ==, "abcdefghijklmno", 15);
405 varbuf_destroy(&vb);
408 static void
409 test_varbuf_snapshot(void)
411 struct varbuf vb;
412 struct varbuf_state vbs;
414 varbuf_init(&vb, 0);
416 test_pass(vb.used == 0);
417 varbuf_snapshot(&vb, &vbs);
418 test_pass(vb.used == 0);
419 test_pass(vb.used == vbs.used);
420 test_pass(varbuf_rollback_len(&vbs) == 0);
421 test_str(varbuf_rollback_start(&vbs), ==, "");
423 varbuf_add_buf(&vb, "1234567890", 10);
424 varbuf_end_str(&vb);
425 test_pass(vb.used == 10);
426 test_pass(varbuf_rollback_len(&vbs) == 10);
427 test_str(varbuf_rollback_start(&vbs), ==, "1234567890");
428 varbuf_rollback(&vbs);
429 varbuf_end_str(&vb);
430 test_pass(vb.used == 0);
431 test_pass(varbuf_rollback_len(&vbs) == 0);
432 test_str(varbuf_rollback_start(&vbs), ==, "");
434 varbuf_add_buf(&vb, "1234567890", 10);
435 varbuf_end_str(&vb);
436 test_pass(vb.used == 10);
437 test_pass(varbuf_rollback_len(&vbs) == 10);
438 test_str(varbuf_rollback_start(&vbs), ==, "1234567890");
439 varbuf_snapshot(&vb, &vbs);
440 test_pass(vb.used == 10);
441 test_pass(varbuf_rollback_len(&vbs) == 0);
442 test_str(varbuf_rollback_start(&vbs), ==, "");
444 varbuf_add_buf(&vb, "1234567890", 10);
445 varbuf_end_str(&vb);
446 test_pass(vb.used == 20);
447 test_pass(varbuf_rollback_len(&vbs) == 10);
448 test_str(varbuf_rollback_start(&vbs), ==, "1234567890");
449 varbuf_rollback(&vbs);
450 varbuf_end_str(&vb);
451 test_pass(vb.used == 10);
452 test_pass(varbuf_rollback_len(&vbs) == 0);
453 test_str(varbuf_rollback_start(&vbs), ==, "");
455 varbuf_destroy(&vb);
458 static void
459 test_varbuf_detach(void)
461 struct varbuf vb;
462 char *str;
464 varbuf_init(&vb, 0);
466 varbuf_add_buf(&vb, "1234567890", 10);
468 str = varbuf_detach(&vb);
470 test_mem(str, ==, "1234567890", 10);
471 test_pass(vb.used == 0);
472 test_pass(vb.size == 0);
473 test_pass(vb.buf == NULL);
475 free(str);
478 TEST_ENTRY(test)
480 test_plan(158);
482 test_varbuf_init();
483 test_varbuf_prealloc();
484 test_varbuf_new();
485 test_varbuf_grow();
486 test_varbuf_trunc();
487 test_varbuf_add_varbuf();
488 test_varbuf_add_buf();
489 test_varbuf_add_char();
490 test_varbuf_dup_char();
491 test_varbuf_map_char();
492 test_varbuf_add_dir();
493 test_varbuf_end_str();
494 test_varbuf_get_str();
495 test_varbuf_printf();
496 test_varbuf_reset();
497 test_varbuf_snapshot();
498 test_varbuf_detach();
500 /* TODO: Complete. */