1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Thomas H.P. Andersen
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 static ssize_t add_string(struct strbuf *sb, const char *s) {
30 return strbuf_add_string(sb, s, strlen(s));
33 static void test_strbuf(void) {
35 _cleanup_strv_free_ char **l;
36 ssize_t a, b, c, d, e, f, g;
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 */
48 /* check the content of the buffer directly */
49 l = strv_parse_nulstr(sb->buf, sb->len);
51 assert(streq(l[0], "")); /* root*/
52 assert(streq(l[1], "waldo"));
53 assert(streq(l[2], "foo"));
54 assert(streq(l[3], "bar"));
55 assert(streq(l[4], "waldorf"));
57 assert(sb->nodes_count == 5); /* root + 4 non-duplicates */
58 assert(sb->dedup_count == 3);
59 assert(sb->in_count == 7);
61 assert(sb->in_len == 29); /* length of all strings added */
62 assert(sb->dedup_len == 11); /* length of all strings duplicated */
63 assert(sb->len == 23); /* buffer length: in - dedup + \0 for each node */
65 /* check the returned offsets and the respective content in the buffer */
74 assert(streq(sb->buf + a, "waldo"));
75 assert(streq(sb->buf + b, "foo"));
76 assert(streq(sb->buf + c, "bar"));
77 assert(streq(sb->buf + d, "waldo"));
78 assert(streq(sb->buf + e, "aldo"));
79 assert(streq(sb->buf + f, "do"));
80 assert(streq(sb->buf + g, "waldorf"));
83 assert(sb->root == NULL);
88 int main(int argc, const char *argv[])