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/>.
24 #include <dpkg/test.h>
25 #include <dpkg/varbuf.h>
32 test_varbuf_init(void)
37 test_pass(vb
.used
== 0);
38 test_pass(vb
.size
== 0);
39 test_pass(vb
.buf
== NULL
);
42 test_pass(vb
.used
== 0);
43 test_pass(vb
.size
== 0);
44 test_pass(vb
.buf
== NULL
);
48 test_varbuf_prealloc(void)
53 test_pass(vb
.used
== 0);
54 test_pass(vb
.size
>= 10);
55 test_pass(vb
.buf
!= NULL
);
58 test_pass(vb
.used
== 0);
59 test_pass(vb
.size
== 0);
60 test_pass(vb
.buf
== NULL
);
69 test_pass(vb
!= NULL
);
70 test_pass(vb
->used
== 0);
71 test_pass(vb
->size
== 0);
72 test_pass(vb
->buf
== NULL
);
76 test_pass(vb
!= NULL
);
77 test_pass(vb
->used
== 0);
78 test_pass(vb
->size
>= 10);
79 test_pass(vb
->buf
!= NULL
);
84 test_varbuf_grow(void)
94 /* Test that we grow when needed. */
95 varbuf_grow(&vb
, 100);
96 test_pass(vb
.used
== 0);
97 test_pass(vb
.size
>= 100);
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. */
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;
118 test_try(grow_jump
) {
119 varbuf_grow(&vb
, SIZE_MAX
- vb
.size
+ 2);
121 grow_overflow
= true;
123 test_pass(vb
.size
== old_size
&& grow_overflow
);
125 grow_overflow
= false;
127 test_try(grow_jump
) {
128 varbuf_grow(&vb
, (SIZE_MAX
- vb
.size
- 2) / 2);
130 grow_overflow
= true;
132 test_pass(vb
.size
== old_size
&& grow_overflow
);
138 test_varbuf_trunc(void)
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);
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");
190 test_varbuf_add_varbuf(void)
192 struct varbuf vb
, cb
;
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);
214 test_varbuf_add_buf(void)
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);
234 test_varbuf_add_str(void)
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");
256 test_varbuf_add_char(void)
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);
286 test_varbuf_dup_char(void)
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);
306 test_varbuf_map_char(void)
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);
323 test_varbuf_add_dir(void)
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/");
340 varbuf_add_dir(&vb
, "/foo/bar");
341 test_str(vb
.buf
, ==, "/foo/bar/");
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/");
356 test_varbuf_end_str(void)
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");
378 test_varbuf_str(void)
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");
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
);
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
);
474 test_varbuf_printf(void)
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");
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");
499 test_varbuf_reset(void)
503 varbuf_init(&vb
, 10);
505 varbuf_add_buf(&vb
, "1234567890", 10);
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);
520 test_varbuf_snapshot(void)
523 struct varbuf_state vbs
;
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
), ==, "");
565 test_varbuf_detach(void)
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
);
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
);
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
);
608 test_varbuf_prealloc();
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();
624 test_varbuf_printf();
626 test_varbuf_snapshot();
627 test_varbuf_detach();
629 /* TODO: Complete. */