]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/strbuf: include empty strings in count
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 26 Mar 2018 08:32:42 +0000 (10:32 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 26 Mar 2018 13:28:03 +0000 (15:28 +0200)
Not that it matters much, but it seems cleaner to also count those
inputs, even if they do not consume extra storage space.

The test is extended to include an empty input and counts in the test are
adjusted to include it.

src/basic/strbuf.c
src/test/test-strbuf.c

index 5a416ce2866d22233116b3bf409ed131ce169a8f..8b8281bb3b31d55927ce2619256ef26952101359 100644 (file)
@@ -133,9 +133,12 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
                 return -EINVAL;
 
         /* search string; start from last character to find possibly matching tails */
-        if (len == 0)
-                return 0;
+
         str->in_count++;
+        if (len == 0) {
+                str->dedup_count++;
+                return 0;
+        }
         str->in_len += len;
 
         node = str->root;
index 891d7b1d42b53fd170c2c5530be879e48977f258..e7395ff96ce186245ffadb3f106c8417714ef7f2 100644 (file)
@@ -33,7 +33,7 @@ static ssize_t add_string(struct strbuf *sb, const char *s) {
 static void test_strbuf(void) {
         struct strbuf *sb;
         _cleanup_strv_free_ char **l;
-        ssize_t a, b, c, d, e, f, g;
+        ssize_t a, b, c, d, e, f, g, h;
 
         sb = strbuf_new();
 
@@ -44,6 +44,7 @@ static void test_strbuf(void) {
         e = add_string(sb, "aldo");    /* duplicate */
         f = add_string(sb, "do");      /* duplicate */
         g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */
+        h = add_string(sb, "");
 
         /* check the content of the buffer directly */
         l = strv_parse_nulstr(sb->buf, sb->len);
@@ -53,10 +54,11 @@ static void test_strbuf(void) {
         assert_se(streq(l[2], "foo"));
         assert_se(streq(l[3], "bar"));
         assert_se(streq(l[4], "waldorf"));
+        assert_se(l[5] == NULL);
 
         assert_se(sb->nodes_count == 5); /* root + 4 non-duplicates */
-        assert_se(sb->dedup_count == 3);
-        assert_se(sb->in_count == 7);
+        assert_se(sb->dedup_count == 4);
+        assert_se(sb->in_count == 8);
 
         assert_se(sb->in_len == 29);    /* length of all strings added */
         assert_se(sb->dedup_len == 11); /* length of all strings duplicated */
@@ -70,6 +72,7 @@ static void test_strbuf(void) {
         assert_se(e == 2);
         assert_se(f == 4);
         assert_se(g == 15);
+        assert_se(h == 0);
 
         assert_se(streq(sb->buf + a, "waldo"));
         assert_se(streq(sb->buf + b, "foo"));
@@ -78,6 +81,7 @@ static void test_strbuf(void) {
         assert_se(streq(sb->buf + e, "aldo"));
         assert_se(streq(sb->buf + f, "do"));
         assert_se(streq(sb->buf + g, "waldorf"));
+        assert_se(streq(sb->buf + h, ""));
 
         strbuf_complete(sb);
         assert_se(sb->root == NULL);