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