po: Update German man pages translation
[dpkg.git] / lib / dpkg / t / t-varbuf.c
blob1ab1cf2066572b07e1dbaa419ca848285e00273d
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_set(void)
160 struct varbuf vb, cb;
162 varbuf_init(&vb, 10);
163 varbuf_init(&cb, 10);
165 varbuf_set_buf(&vb, "1234567890", 5);
166 test_pass(vb.used == 5);
167 test_mem(vb.buf, ==, "12345", 5);
169 varbuf_set_buf(&vb, "abcd", 4);
170 test_pass(vb.used == 4);
171 test_mem(vb.buf, ==, "abcd", 4);
173 varbuf_set_varbuf(&cb, &vb);
174 test_pass(cb.used == 4);
175 test_mem(cb.buf, ==, "abcd", 4);
177 varbuf_set_str(&vb, "12345");
178 test_pass(vb.used == 5);
179 test_str(vb.buf, ==, "12345");
181 varbuf_set_strn(&vb, "1234567890", 8);
182 test_pass(vb.used == 8);
183 test_str(vb.buf, ==, "12345678");
185 varbuf_destroy(&cb);
186 varbuf_destroy(&vb);
189 static void
190 test_varbuf_add_varbuf(void)
192 struct varbuf vb, cb;
194 varbuf_init(&vb, 5);
195 varbuf_init(&cb, 0);
197 varbuf_set_str(&vb, "1234567890");
198 varbuf_add_varbuf(&cb, &vb);
199 test_pass(cb.used == 10);
200 test_pass(cb.size >= cb.used);
201 test_mem(cb.buf, ==, "1234567890", 10);
203 varbuf_set_str(&vb, "abcde");
204 varbuf_add_varbuf(&cb, &vb);
205 test_pass(cb.used == 15);
206 test_pass(cb.size >= cb.used);
207 test_mem(cb.buf, ==, "1234567890abcde", 15);
209 varbuf_destroy(&cb);
210 varbuf_destroy(&vb);
213 static void
214 test_varbuf_add_buf(void)
216 struct varbuf vb;
218 varbuf_init(&vb, 5);
220 varbuf_add_buf(&vb, "1234567890", 10);
221 test_pass(vb.used == 10);
222 test_pass(vb.size >= vb.used);
223 test_mem(vb.buf, ==, "1234567890", 10);
225 varbuf_add_buf(&vb, "abcde", 5);
226 test_pass(vb.used == 15);
227 test_pass(vb.size >= vb.used);
228 test_mem(vb.buf, ==, "1234567890abcde", 15);
230 varbuf_destroy(&vb);
233 static void
234 test_varbuf_add_str(void)
236 struct varbuf vb;
238 varbuf_init(&vb, 5);
240 varbuf_add_str(&vb, "1234567890");
241 test_str(vb.buf, ==, "1234567890");
243 varbuf_add_str(&vb, "abcd");
244 test_str(vb.buf, ==, "1234567890abcd");
246 varbuf_add_strn(&vb, "1234567890", 5);
247 test_str(vb.buf, ==, "1234567890abcd12345");
249 varbuf_add_strn(&vb, "abcd", 0);
250 test_str(vb.buf, ==, "1234567890abcd12345");
252 varbuf_destroy(&vb);
255 static void
256 test_varbuf_add_char(void)
258 struct varbuf vb;
260 varbuf_init(&vb, 1);
262 varbuf_add_char(&vb, 'a');
263 test_pass(vb.used == 1);
264 test_pass(vb.size >= vb.used);
265 test_pass(vb.buf[0] == 'a');
267 varbuf_add_char(&vb, 'b');
268 test_pass(vb.used == 2);
269 test_pass(vb.size >= vb.used);
270 test_mem(vb.buf, ==, "ab", 2);
272 varbuf_add_char(&vb, 'c');
273 test_pass(vb.used == 3);
274 test_pass(vb.size >= vb.used);
275 test_mem(vb.buf, ==, "abc", 3);
277 varbuf_add_char(&vb, 'd');
278 test_pass(vb.used == 4);
279 test_pass(vb.size >= vb.used);
280 test_mem(vb.buf, ==, "abcd", 4);
282 varbuf_destroy(&vb);
285 static void
286 test_varbuf_dup_char(void)
288 struct varbuf vb;
290 varbuf_init(&vb, 5);
292 varbuf_dup_char(&vb, 'z', 10);
293 test_pass(vb.used == 10);
294 test_pass(vb.size >= vb.used);
295 test_mem(vb.buf, ==, "zzzzzzzzzz", 10);
297 varbuf_dup_char(&vb, 'y', 5);
298 test_pass(vb.used == 15);
299 test_pass(vb.size >= vb.used);
300 test_mem(vb.buf, ==, "zzzzzzzzzzyyyyy", 15);
302 varbuf_destroy(&vb);
305 static void
306 test_varbuf_map_char(void)
308 struct varbuf vb;
310 varbuf_init(&vb, 5);
312 varbuf_add_buf(&vb, "1234a5678a9012a", 15);
314 varbuf_map_char(&vb, 'a', 'z');
315 test_pass(vb.used == 15);
316 test_pass(vb.size >= vb.used);
317 test_mem(vb.buf, ==, "1234z5678z9012z", 15);
319 varbuf_destroy(&vb);
322 static void
323 test_varbuf_add_dir(void)
325 struct varbuf vb;
327 varbuf_init(&vb, 10);
329 varbuf_add_dir(&vb, "");
330 test_str(vb.buf, ==, "/");
331 varbuf_add_dir(&vb, "");
332 test_str(vb.buf, ==, "/");
333 varbuf_add_dir(&vb, "aa");
334 test_str(vb.buf, ==, "/aa/");
335 varbuf_add_dir(&vb, "");
336 test_str(vb.buf, ==, "/aa/");
338 varbuf_reset(&vb);
340 varbuf_add_dir(&vb, "/foo/bar");
341 test_str(vb.buf, ==, "/foo/bar/");
343 varbuf_reset(&vb);
345 varbuf_add_dir(&vb, "/foo/bar/");
346 test_str(vb.buf, ==, "/foo/bar/");
347 varbuf_add_dir(&vb, "quux");
348 test_str(vb.buf, ==, "/foo/bar/quux/");
349 varbuf_add_dir(&vb, "zoo");
350 test_str(vb.buf, ==, "/foo/bar/quux/zoo/");
352 varbuf_destroy(&vb);
355 static void
356 test_varbuf_end_str(void)
358 struct varbuf vb;
360 varbuf_init(&vb, 10);
362 varbuf_add_buf(&vb, "1234567890X", 11);
363 test_pass(vb.used == 11);
364 test_pass(vb.size >= vb.used);
365 test_mem(vb.buf, ==, "1234567890X", 11);
367 varbuf_trunc(&vb, 10);
369 test_pass(vb.used == 10);
370 test_pass(vb.size >= vb.used + 1);
371 test_pass(vb.buf[10] == '\0');
372 test_str(vb.buf, ==, "1234567890");
374 varbuf_destroy(&vb);
377 static void
378 test_varbuf_str(void)
380 struct varbuf vb;
381 const char *str;
383 varbuf_init(&vb, 10);
385 varbuf_add_buf(&vb, "1234567890", 10);
386 str = varbuf_str(&vb);
387 test_pass(vb.buf == str);
388 test_pass(vb.used == 10);
389 test_pass(vb.buf[vb.used] == '\0');
390 test_pass(str[vb.used] == '\0');
391 test_str(vb.buf, ==, "1234567890");
392 test_str(str, ==, "1234567890");
394 varbuf_add_buf(&vb, "abcde", 5);
395 str = varbuf_str(&vb);
396 test_pass(vb.buf == str);
397 test_pass(vb.used == 15);
398 test_pass(vb.buf[vb.used] == '\0');
399 test_pass(str[vb.used] == '\0');
400 test_str(vb.buf, ==, "1234567890abcde");
401 test_str(str, ==, "1234567890abcde");
403 varbuf_destroy(&vb);
406 static void
407 test_varbuf_has(void)
409 struct varbuf vb = VARBUF_OBJECT;
410 struct varbuf vb_prefix = VARBUF_OBJECT;
411 struct varbuf vb_suffix = VARBUF_OBJECT;
413 test_pass(varbuf_has_prefix(&vb, &vb_prefix));
414 test_pass(varbuf_has_suffix(&vb, &vb_suffix));
416 varbuf_set_str(&vb_prefix, "prefix");
417 varbuf_set_str(&vb_suffix, "suffix");
419 test_fail(varbuf_has_prefix(&vb, &vb_prefix));
420 test_fail(varbuf_has_suffix(&vb, &vb_suffix));
422 varbuf_set_str(&vb, "prefix and some text");
423 test_pass(varbuf_has_prefix(&vb, &vb_prefix));
424 test_fail(varbuf_has_prefix(&vb, &vb_suffix));
425 test_fail(varbuf_has_suffix(&vb, &vb_prefix));
426 test_fail(varbuf_has_suffix(&vb, &vb_suffix));
428 varbuf_set_str(&vb, "some text with suffix");
429 test_fail(varbuf_has_prefix(&vb, &vb_prefix));
430 test_fail(varbuf_has_prefix(&vb, &vb_suffix));
431 test_fail(varbuf_has_suffix(&vb, &vb_prefix));
432 test_pass(varbuf_has_suffix(&vb, &vb_suffix));
434 varbuf_set_str(&vb, "prefix and some text with suffix");
435 test_pass(varbuf_has_prefix(&vb, &vb_prefix));
436 test_fail(varbuf_has_prefix(&vb, &vb_suffix));
437 test_fail(varbuf_has_suffix(&vb, &vb_prefix));
438 test_pass(varbuf_has_suffix(&vb, &vb_suffix));
440 varbuf_destroy(&vb_prefix);
441 varbuf_destroy(&vb_suffix);
442 varbuf_destroy(&vb);
445 static void
446 test_varbuf_trim(void)
448 struct varbuf vb = VARBUF_OBJECT;
449 struct varbuf vb_prefix = VARBUF_OBJECT;
450 struct varbuf vb_suffix = VARBUF_OBJECT;
452 varbuf_set_str(&vb_prefix, "prefix");
453 varbuf_set_str(&vb_suffix, "suffix");
455 varbuf_set_str(&vb, "some text");
456 varbuf_trim_varbuf_prefix(&vb, &vb_prefix);
457 varbuf_trim_char_prefix(&vb, 'a');
458 test_str(vb.buf, ==, "some text");
460 varbuf_set_str(&vb, "prefix and some text");
461 varbuf_trim_varbuf_prefix(&vb, &vb_prefix);
462 test_str(vb.buf, ==, " and some text");
464 varbuf_set_str(&vb, " and some text");
465 varbuf_trim_char_prefix(&vb, ' ');
466 test_str(vb.buf, ==, "and some text");
468 varbuf_destroy(&vb_prefix);
469 varbuf_destroy(&vb_suffix);
470 varbuf_destroy(&vb);
473 static void
474 test_varbuf_printf(void)
476 struct varbuf vb;
478 varbuf_init(&vb, 5);
480 /* Test normal format printing. */
481 varbuf_printf(&vb, "format %s number %d", "string", 10);
482 test_pass(vb.used == strlen("format string number 10"));
483 test_pass(vb.size >= vb.used);
484 test_str(vb.buf, ==, "format string number 10");
486 varbuf_reset(&vb);
488 /* Test concatenated format printing. */
489 varbuf_printf(&vb, "format %s number %d", "string", 10);
490 varbuf_printf(&vb, " extra %s", "string");
491 test_pass(vb.used == strlen("format string number 10 extra string"));
492 test_pass(vb.size >= vb.used);
493 test_str(vb.buf, ==, "format string number 10 extra string");
495 varbuf_destroy(&vb);
498 static void
499 test_varbuf_reset(void)
501 struct varbuf vb;
503 varbuf_init(&vb, 10);
505 varbuf_add_buf(&vb, "1234567890", 10);
507 varbuf_reset(&vb);
508 test_pass(vb.used == 0);
509 test_pass(vb.size >= vb.used);
511 varbuf_add_buf(&vb, "abcdefghijklmno", 15);
512 test_pass(vb.used == 15);
513 test_pass(vb.size >= vb.used);
514 test_mem(vb.buf, ==, "abcdefghijklmno", 15);
516 varbuf_destroy(&vb);
519 static void
520 test_varbuf_snapshot(void)
522 struct varbuf vb;
523 struct varbuf_state vbs;
525 varbuf_init(&vb, 0);
527 test_pass(vb.used == 0);
528 varbuf_snapshot(&vb, &vbs);
529 test_pass(vb.used == 0);
530 test_pass(vb.used == vbs.used);
531 test_pass(varbuf_rollback_len(&vbs) == 0);
532 test_str(varbuf_rollback_start(&vbs), ==, "");
534 varbuf_add_buf(&vb, "1234567890", 10);
535 test_pass(vb.used == 10);
536 test_pass(varbuf_rollback_len(&vbs) == 10);
537 test_str(varbuf_rollback_start(&vbs), ==, "1234567890");
538 varbuf_rollback(&vbs);
539 test_pass(vb.used == 0);
540 test_pass(varbuf_rollback_len(&vbs) == 0);
541 test_str(varbuf_rollback_start(&vbs), ==, "");
543 varbuf_add_buf(&vb, "1234567890", 10);
544 test_pass(vb.used == 10);
545 test_pass(varbuf_rollback_len(&vbs) == 10);
546 test_str(varbuf_rollback_start(&vbs), ==, "1234567890");
547 varbuf_snapshot(&vb, &vbs);
548 test_pass(vb.used == 10);
549 test_pass(varbuf_rollback_len(&vbs) == 0);
550 test_str(varbuf_rollback_start(&vbs), ==, "");
552 varbuf_add_buf(&vb, "1234567890", 10);
553 test_pass(vb.used == 20);
554 test_pass(varbuf_rollback_len(&vbs) == 10);
555 test_str(varbuf_rollback_start(&vbs), ==, "1234567890");
556 varbuf_rollback(&vbs);
557 test_pass(vb.used == 10);
558 test_pass(varbuf_rollback_len(&vbs) == 0);
559 test_str(varbuf_rollback_start(&vbs), ==, "");
561 varbuf_destroy(&vb);
564 static void
565 test_varbuf_detach(void)
567 struct varbuf vb;
568 char *str;
570 varbuf_init(&vb, 0);
571 test_pass(vb.used == 0);
572 test_pass(vb.size == 0);
573 test_pass(vb.buf == NULL);
574 str = varbuf_detach(&vb);
575 test_str(str, ==, "");
576 test_pass(vb.used == 0);
577 test_pass(vb.size == 0);
578 test_pass(vb.buf == NULL);
579 free(str);
581 varbuf_init(&vb, 0);
582 varbuf_add_buf(&vb, NULL, 0);
583 test_pass(vb.used == 0);
584 test_pass(vb.size == 0);
585 test_pass(vb.buf == NULL);
586 str = varbuf_detach(&vb);
587 test_str(str, ==, "");
588 test_pass(vb.used == 0);
589 test_pass(vb.size == 0);
590 test_pass(vb.buf == NULL);
591 free(str);
593 varbuf_init(&vb, 0);
594 varbuf_add_buf(&vb, "1234567890", 10);
595 str = varbuf_detach(&vb);
596 test_mem(str, ==, "1234567890", 10);
597 test_pass(vb.used == 0);
598 test_pass(vb.size == 0);
599 test_pass(vb.buf == NULL);
600 free(str);
603 TEST_ENTRY(test)
605 test_plan(205);
607 test_varbuf_init();
608 test_varbuf_prealloc();
609 test_varbuf_new();
610 test_varbuf_grow();
611 test_varbuf_trunc();
612 test_varbuf_set();
613 test_varbuf_add_varbuf();
614 test_varbuf_add_buf();
615 test_varbuf_add_str();
616 test_varbuf_add_char();
617 test_varbuf_dup_char();
618 test_varbuf_map_char();
619 test_varbuf_add_dir();
620 test_varbuf_end_str();
621 test_varbuf_str();
622 test_varbuf_has();
623 test_varbuf_trim();
624 test_varbuf_printf();
625 test_varbuf_reset();
626 test_varbuf_snapshot();
627 test_varbuf_detach();
629 /* TODO: Complete. */