]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
97b5f608 TA |
2 | |
3 | #include <stdlib.h> | |
97b5f608 | 4 | |
08af3cc5 | 5 | #include "nulstr-util.h" |
97b5f608 | 6 | #include "strbuf.h" |
07630cea | 7 | #include "string-util.h" |
97b5f608 | 8 | #include "strv.h" |
4f7452a8 | 9 | #include "tests.h" |
97b5f608 TA |
10 | |
11 | static ssize_t add_string(struct strbuf *sb, const char *s) { | |
12 | return strbuf_add_string(sb, s, strlen(s)); | |
13 | } | |
14 | ||
4f7452a8 | 15 | TEST(strbuf) { |
3d41b6b8 DDM |
16 | _cleanup_(strbuf_freep) struct strbuf *sb = NULL; |
17 | _cleanup_strv_free_ char **l = NULL; | |
435ce146 | 18 | ssize_t a, b, c, d, e, f, g, h; |
97b5f608 TA |
19 | |
20 | sb = strbuf_new(); | |
21 | ||
22 | a = add_string(sb, "waldo"); | |
23 | b = add_string(sb, "foo"); | |
24 | c = add_string(sb, "bar"); | |
25 | d = add_string(sb, "waldo"); /* duplicate */ | |
26 | e = add_string(sb, "aldo"); /* duplicate */ | |
27 | f = add_string(sb, "do"); /* duplicate */ | |
28 | g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */ | |
435ce146 | 29 | h = add_string(sb, ""); |
97b5f608 TA |
30 | |
31 | /* check the content of the buffer directly */ | |
32 | l = strv_parse_nulstr(sb->buf, sb->len); | |
fedd793c | 33 | assert_se(l); |
97b5f608 | 34 | |
c79e88b3 IK |
35 | ASSERT_STREQ(l[0], ""); /* root */ |
36 | ASSERT_STREQ(l[1], "waldo"); | |
37 | ASSERT_STREQ(l[2], "foo"); | |
38 | ASSERT_STREQ(l[3], "bar"); | |
39 | ASSERT_STREQ(l[4], "waldorf"); | |
5152b845 | 40 | ASSERT_NULL(l[5]); |
97b5f608 | 41 | |
bdf7026e | 42 | assert_se(sb->nodes_count == 5); /* root + 4 non-duplicates */ |
435ce146 ZJS |
43 | assert_se(sb->dedup_count == 4); |
44 | assert_se(sb->in_count == 8); | |
97b5f608 | 45 | |
bdf7026e TA |
46 | assert_se(sb->in_len == 29); /* length of all strings added */ |
47 | assert_se(sb->dedup_len == 11); /* length of all strings duplicated */ | |
48 | assert_se(sb->len == 23); /* buffer length: in - dedup + \0 for each node */ | |
97b5f608 TA |
49 | |
50 | /* check the returned offsets and the respective content in the buffer */ | |
bdf7026e TA |
51 | assert_se(a == 1); |
52 | assert_se(b == 7); | |
53 | assert_se(c == 11); | |
54 | assert_se(d == 1); | |
55 | assert_se(e == 2); | |
56 | assert_se(f == 4); | |
57 | assert_se(g == 15); | |
435ce146 | 58 | assert_se(h == 0); |
bdf7026e | 59 | |
c79e88b3 IK |
60 | ASSERT_STREQ(sb->buf + a, "waldo"); |
61 | ASSERT_STREQ(sb->buf + b, "foo"); | |
62 | ASSERT_STREQ(sb->buf + c, "bar"); | |
63 | ASSERT_STREQ(sb->buf + d, "waldo"); | |
64 | ASSERT_STREQ(sb->buf + e, "aldo"); | |
65 | ASSERT_STREQ(sb->buf + f, "do"); | |
66 | ASSERT_STREQ(sb->buf + g, "waldorf"); | |
67 | ASSERT_STREQ(sb->buf + h, ""); | |
97b5f608 TA |
68 | |
69 | strbuf_complete(sb); | |
5152b845 | 70 | ASSERT_NULL(sb->root); |
97b5f608 TA |
71 | } |
72 | ||
4f7452a8 | 73 | DEFINE_TEST_MAIN(LOG_INFO); |