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