]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-strbuf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "string-util.h"
10 static ssize_t
add_string(struct strbuf
*sb
, const char *s
) {
11 return strbuf_add_string(sb
, s
, strlen(s
));
14 static void test_strbuf(void) {
15 _cleanup_(strbuf_cleanupp
) struct strbuf
*sb
;
16 _cleanup_strv_free_
char **l
;
17 ssize_t a
, b
, c
, d
, e
, f
, g
, h
;
21 a
= add_string(sb
, "waldo");
22 b
= add_string(sb
, "foo");
23 c
= add_string(sb
, "bar");
24 d
= add_string(sb
, "waldo"); /* duplicate */
25 e
= add_string(sb
, "aldo"); /* duplicate */
26 f
= add_string(sb
, "do"); /* duplicate */
27 g
= add_string(sb
, "waldorf"); /* not a duplicate: matches from tail */
28 h
= add_string(sb
, "");
30 /* check the content of the buffer directly */
31 l
= strv_parse_nulstr(sb
->buf
, sb
->len
);
33 assert_se(streq(l
[0], "")); /* root */
34 assert_se(streq(l
[1], "waldo"));
35 assert_se(streq(l
[2], "foo"));
36 assert_se(streq(l
[3], "bar"));
37 assert_se(streq(l
[4], "waldorf"));
38 assert_se(l
[5] == NULL
);
40 assert_se(sb
->nodes_count
== 5); /* root + 4 non-duplicates */
41 assert_se(sb
->dedup_count
== 4);
42 assert_se(sb
->in_count
== 8);
44 assert_se(sb
->in_len
== 29); /* length of all strings added */
45 assert_se(sb
->dedup_len
== 11); /* length of all strings duplicated */
46 assert_se(sb
->len
== 23); /* buffer length: in - dedup + \0 for each node */
48 /* check the returned offsets and the respective content in the buffer */
58 assert_se(streq(sb
->buf
+ a
, "waldo"));
59 assert_se(streq(sb
->buf
+ b
, "foo"));
60 assert_se(streq(sb
->buf
+ c
, "bar"));
61 assert_se(streq(sb
->buf
+ d
, "waldo"));
62 assert_se(streq(sb
->buf
+ e
, "aldo"));
63 assert_se(streq(sb
->buf
+ f
, "do"));
64 assert_se(streq(sb
->buf
+ g
, "waldorf"));
65 assert_se(streq(sb
->buf
+ h
, ""));
68 assert_se(sb
->root
== NULL
);
71 int main(int argc
, const char *argv
[]) {