LCOV - code coverage report
Current view: top level - test - test-strbuf.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 49 49 100.0 %
Date: 2019-08-22 15:41:25 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.14