chiark / gitweb /
tests: add test of strbuf
authorThomas Hindoe Paaboel Andersen <phomes@gmail.com>
Sat, 9 Feb 2013 00:39:44 +0000 (01:39 +0100)
committerThomas Hindoe Paaboel Andersen <phomes@gmail.com>
Sat, 9 Feb 2013 00:40:52 +0000 (01:40 +0100)
.gitignore
Makefile.am
src/test/test-strbuf.c [new file with mode: 0644]

index 6c2944bfa11ba2fa9b612ff27e9f09ff230f4e59..f000719daf395c4545d545480f1cdd97c4e279b5 100644 (file)
 /test-sched-prio
 /test-sleep
 /test-strip-tab-ansi
+/test-strbuf
 /test-strv
 /test-udev
 /test-unit-file
index 7885a79be8a4a009743e7174ed0f6c8f02b9758c..f912c5dd52f666a0ba61b2a0ce34b4a1e4e07e9b 100644 (file)
@@ -1002,6 +1002,7 @@ noinst_PROGRAMS += \
 noinst_tests += \
        test-job-type \
        test-env-replace \
+       test-strbuf \
        test-strv \
        test-unit-name \
        test-unit-file \
@@ -1161,6 +1162,12 @@ test_env_replace_SOURCES = \
 test_env_replace_LDADD = \
        libsystemd-shared.la
 
+test_strbuf_SOURCES = \
+       src/test/test-strbuf.c
+
+test_strbuf_LDADD = \
+       libsystemd-shared.la
+
 test_strv_SOURCES = \
        src/test/test-strv.c
 
diff --git a/src/test/test-strbuf.c b/src/test/test-strbuf.c
new file mode 100644 (file)
index 0000000..49a3d2b
--- /dev/null
@@ -0,0 +1,93 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Thomas H.P. Andersen
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "strbuf.h"
+#include "strv.h"
+#include "util.h"
+
+static ssize_t add_string(struct strbuf *sb, const char *s) {
+        return strbuf_add_string(sb, s, strlen(s));
+}
+
+static void test_strbuf(void) {
+        struct strbuf *sb;
+       _cleanup_strv_free_ char **l;
+        ssize_t a, b, c, d, e, f, g;
+
+        sb = strbuf_new();
+
+        a = add_string(sb, "waldo");
+        b = add_string(sb, "foo");
+        c = add_string(sb, "bar");
+        d = add_string(sb, "waldo");   /* duplicate */
+        e = add_string(sb, "aldo");    /* duplicate */
+        f = add_string(sb, "do");      /* duplicate */
+        g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */
+
+        /* check the content of the buffer directly */
+        l = strv_parse_nulstr(sb->buf, sb->len);
+
+        assert(streq(l[0], "")); /* root*/
+        assert(streq(l[1], "waldo"));
+        assert(streq(l[2], "foo"));
+        assert(streq(l[3], "bar"));
+        assert(streq(l[4], "waldorf"));
+
+        assert(sb->nodes_count == 5); /* root + 4 non-duplicates */
+        assert(sb->dedup_count == 3);
+        assert(sb->in_count == 7);
+
+        assert(sb->in_len == 29);    /* length of all strings added */
+        assert(sb->dedup_len == 11); /* length of all strings duplicated */
+        assert(sb->len == 23);       /* buffer length: in - dedup + \0 for each node */
+
+        /* check the returned offsets and the respective content in the buffer */
+        assert(a == 1);
+        assert(b == 7);
+        assert(c == 11);
+        assert(d == 1);
+        assert(e == 2);
+        assert(f == 4);
+        assert(g == 15);
+
+        assert(streq(sb->buf + a, "waldo"));
+        assert(streq(sb->buf + b, "foo"));
+        assert(streq(sb->buf + c, "bar"));
+        assert(streq(sb->buf + d, "waldo"));
+        assert(streq(sb->buf + e, "aldo"));
+        assert(streq(sb->buf + f, "do"));
+        assert(streq(sb->buf + g, "waldorf"));
+
+        strbuf_complete(sb);
+        assert(sb->root == NULL);
+
+        strbuf_cleanup(sb);
+}
+
+int main(int argc, const char *argv[])
+{
+        test_strbuf();
+
+        return 0;
+}