LCOV - code coverage report
Current view: top level - test - test-strbuf.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 49 49 100.0 %
Date: 2019-08-23 13:36:53 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 29 58 50.0 %

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

Generated by: LCOV version 1.14