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