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_add_varbuf(void)
160 struct varbuf vb
, cb
;
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);
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);
183 test_varbuf_add_buf(void)
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);
203 test_varbuf_add_char(void)
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);
233 test_varbuf_dup_char(void)
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);
253 test_varbuf_map_char(void)
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);
270 test_varbuf_add_dir(void)
274 varbuf_init(&vb
, 10);
276 varbuf_add_dir(&vb
, "");
278 test_str(vb
.buf
, ==, "/");
279 varbuf_add_dir(&vb
, "");
281 test_str(vb
.buf
, ==, "/");
282 varbuf_add_dir(&vb
, "aa");
284 test_str(vb
.buf
, ==, "/aa/");
285 varbuf_add_dir(&vb
, "");
287 test_str(vb
.buf
, ==, "/aa/");
291 varbuf_add_dir(&vb
, "/foo/bar");
293 test_str(vb
.buf
, ==, "/foo/bar/");
297 varbuf_add_dir(&vb
, "/foo/bar/");
299 test_str(vb
.buf
, ==, "/foo/bar/");
300 varbuf_add_dir(&vb
, "quux");
302 test_str(vb
.buf
, ==, "/foo/bar/quux/");
303 varbuf_add_dir(&vb
, "zoo");
305 test_str(vb
.buf
, ==, "/foo/bar/quux/zoo/");
311 test_varbuf_end_str(void)
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);
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");
334 test_varbuf_get_str(void)
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");
363 test_varbuf_printf(void)
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");
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");
388 test_varbuf_reset(void)
392 varbuf_init(&vb
, 10);
394 varbuf_add_buf(&vb
, "1234567890", 10);
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);
409 test_varbuf_snapshot(void)
412 struct varbuf_state vbs
;
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);
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
);
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);
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);
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
);
451 test_pass(vb
.used
== 10);
452 test_pass(varbuf_rollback_len(&vbs
) == 0);
453 test_str(varbuf_rollback_start(&vbs
), ==, "");
459 test_varbuf_detach(void)
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
);
483 test_varbuf_prealloc();
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();
497 test_varbuf_snapshot();
498 test_varbuf_detach();
500 /* TODO: Complete. */