]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-strbuf.c
calendarspec: rename free_chain() to chain_free()
[thirdparty/systemd.git] / src / test / test-strbuf.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
97b5f608
TA
2
3#include <stdlib.h>
4#include <string.h>
5
6#include "strbuf.h"
07630cea 7#include "string-util.h"
97b5f608
TA
8#include "strv.h"
9#include "util.h"
10
11static ssize_t add_string(struct strbuf *sb, const char *s) {
12 return strbuf_add_string(sb, s, strlen(s));
13}
14
15static void test_strbuf(void) {
f201daec 16 _cleanup_(strbuf_cleanupp) struct strbuf *sb;
c62c294f 17 _cleanup_strv_free_ char **l;
435ce146 18 ssize_t a, b, c, d, e, f, g, h;
97b5f608
TA
19
20 sb = strbuf_new();
21
22 a = add_string(sb, "waldo");
23 b = add_string(sb, "foo");
24 c = add_string(sb, "bar");
25 d = add_string(sb, "waldo"); /* duplicate */
26 e = add_string(sb, "aldo"); /* duplicate */
27 f = add_string(sb, "do"); /* duplicate */
28 g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */
435ce146 29 h = add_string(sb, "");
97b5f608
TA
30
31 /* check the content of the buffer directly */
32 l = strv_parse_nulstr(sb->buf, sb->len);
33
f7340ab2 34 assert_se(streq(l[0], "")); /* root */
bdf7026e
TA
35 assert_se(streq(l[1], "waldo"));
36 assert_se(streq(l[2], "foo"));
37 assert_se(streq(l[3], "bar"));
38 assert_se(streq(l[4], "waldorf"));
435ce146 39 assert_se(l[5] == NULL);
97b5f608 40
bdf7026e 41 assert_se(sb->nodes_count == 5); /* root + 4 non-duplicates */
435ce146
ZJS
42 assert_se(sb->dedup_count == 4);
43 assert_se(sb->in_count == 8);
97b5f608 44
bdf7026e
TA
45 assert_se(sb->in_len == 29); /* length of all strings added */
46 assert_se(sb->dedup_len == 11); /* length of all strings duplicated */
47 assert_se(sb->len == 23); /* buffer length: in - dedup + \0 for each node */
97b5f608
TA
48
49 /* check the returned offsets and the respective content in the buffer */
bdf7026e
TA
50 assert_se(a == 1);
51 assert_se(b == 7);
52 assert_se(c == 11);
53 assert_se(d == 1);
54 assert_se(e == 2);
55 assert_se(f == 4);
56 assert_se(g == 15);
435ce146 57 assert_se(h == 0);
bdf7026e
TA
58
59 assert_se(streq(sb->buf + a, "waldo"));
60 assert_se(streq(sb->buf + b, "foo"));
61 assert_se(streq(sb->buf + c, "bar"));
62 assert_se(streq(sb->buf + d, "waldo"));
63 assert_se(streq(sb->buf + e, "aldo"));
64 assert_se(streq(sb->buf + f, "do"));
65 assert_se(streq(sb->buf + g, "waldorf"));
435ce146 66 assert_se(streq(sb->buf + h, ""));
97b5f608
TA
67
68 strbuf_complete(sb);
bdf7026e 69 assert_se(sb->root == NULL);
97b5f608
TA
70}
71
f168c273 72int main(int argc, const char *argv[]) {
97b5f608
TA
73 test_strbuf();
74
75 return 0;
76}