]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
97b5f608 TA |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Thomas H.P. Andersen | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | ||
24 | #include "strbuf.h" | |
07630cea | 25 | #include "string-util.h" |
97b5f608 TA |
26 | #include "strv.h" |
27 | #include "util.h" | |
28 | ||
29 | static ssize_t add_string(struct strbuf *sb, const char *s) { | |
30 | return strbuf_add_string(sb, s, strlen(s)); | |
31 | } | |
32 | ||
33 | static void test_strbuf(void) { | |
34 | struct strbuf *sb; | |
c62c294f | 35 | _cleanup_strv_free_ char **l; |
435ce146 | 36 | ssize_t a, b, c, d, e, f, g, h; |
97b5f608 TA |
37 | |
38 | sb = strbuf_new(); | |
39 | ||
40 | a = add_string(sb, "waldo"); | |
41 | b = add_string(sb, "foo"); | |
42 | c = add_string(sb, "bar"); | |
43 | d = add_string(sb, "waldo"); /* duplicate */ | |
44 | e = add_string(sb, "aldo"); /* duplicate */ | |
45 | f = add_string(sb, "do"); /* duplicate */ | |
46 | g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */ | |
435ce146 | 47 | h = add_string(sb, ""); |
97b5f608 TA |
48 | |
49 | /* check the content of the buffer directly */ | |
50 | l = strv_parse_nulstr(sb->buf, sb->len); | |
51 | ||
f7340ab2 | 52 | assert_se(streq(l[0], "")); /* root */ |
bdf7026e TA |
53 | assert_se(streq(l[1], "waldo")); |
54 | assert_se(streq(l[2], "foo")); | |
55 | assert_se(streq(l[3], "bar")); | |
56 | assert_se(streq(l[4], "waldorf")); | |
435ce146 | 57 | assert_se(l[5] == NULL); |
97b5f608 | 58 | |
bdf7026e | 59 | assert_se(sb->nodes_count == 5); /* root + 4 non-duplicates */ |
435ce146 ZJS |
60 | assert_se(sb->dedup_count == 4); |
61 | assert_se(sb->in_count == 8); | |
97b5f608 | 62 | |
bdf7026e TA |
63 | assert_se(sb->in_len == 29); /* length of all strings added */ |
64 | assert_se(sb->dedup_len == 11); /* length of all strings duplicated */ | |
65 | assert_se(sb->len == 23); /* buffer length: in - dedup + \0 for each node */ | |
97b5f608 TA |
66 | |
67 | /* check the returned offsets and the respective content in the buffer */ | |
bdf7026e TA |
68 | assert_se(a == 1); |
69 | assert_se(b == 7); | |
70 | assert_se(c == 11); | |
71 | assert_se(d == 1); | |
72 | assert_se(e == 2); | |
73 | assert_se(f == 4); | |
74 | assert_se(g == 15); | |
435ce146 | 75 | assert_se(h == 0); |
bdf7026e TA |
76 | |
77 | assert_se(streq(sb->buf + a, "waldo")); | |
78 | assert_se(streq(sb->buf + b, "foo")); | |
79 | assert_se(streq(sb->buf + c, "bar")); | |
80 | assert_se(streq(sb->buf + d, "waldo")); | |
81 | assert_se(streq(sb->buf + e, "aldo")); | |
82 | assert_se(streq(sb->buf + f, "do")); | |
83 | assert_se(streq(sb->buf + g, "waldorf")); | |
435ce146 | 84 | assert_se(streq(sb->buf + h, "")); |
97b5f608 TA |
85 | |
86 | strbuf_complete(sb); | |
bdf7026e | 87 | assert_se(sb->root == NULL); |
97b5f608 TA |
88 | |
89 | strbuf_cleanup(sb); | |
90 | } | |
91 | ||
f168c273 | 92 | int main(int argc, const char *argv[]) { |
97b5f608 TA |
93 | test_strbuf(); |
94 | ||
95 | return 0; | |
96 | } |