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