chiark / gitweb /
tests: add tests of strxcpyx
authorThomas Hindoe Paaboel Andersen <phomes@gmail.com>
Sun, 10 Feb 2013 20:56:03 +0000 (21:56 +0100)
committerThomas Hindoe Paaboel Andersen <phomes@gmail.com>
Sun, 10 Feb 2013 22:06:16 +0000 (23:06 +0100)
also fix a bad indentation in test-strbug.c

.gitignore
Makefile.am
src/test/test-strbuf.c
src/test/test-strxcpyx.c [new file with mode: 0644]

index f000719daf395c4545d545480f1cdd97c4e279b5..350aaf7a1ff24e7c84fc68bf86480621236ac5e4 100644 (file)
 /test-strip-tab-ansi
 /test-strbuf
 /test-strv
 /test-strip-tab-ansi
 /test-strbuf
 /test-strv
+/test-strxcpyx
 /test-udev
 /test-unit-file
 /test-unit-name
 /test-udev
 /test-unit-file
 /test-unit-name
index 2b7d78ccf5c0f502687fbcbf95ed1c1bc379579b..8a7c2b67aaf5d9677b4f6f7b7a55d8e1f0ea25da 100644 (file)
@@ -1007,6 +1007,7 @@ noinst_tests += \
        test-env-replace \
        test-strbuf \
        test-strv \
        test-env-replace \
        test-strbuf \
        test-strv \
+       test-strxcpyx \
        test-unit-name \
        test-unit-file \
        test-util \
        test-unit-name \
        test-unit-file \
        test-util \
@@ -1178,6 +1179,12 @@ test_strv_LDADD = \
        libsystemd-shared.la \
        libsystemd-id128-internal.la
 
        libsystemd-shared.la \
        libsystemd-id128-internal.la
 
+test_strxcpyx_SOURCES = \
+       src/test/test-strxcpyx.c
+
+test_strxcpyx_LDADD = \
+       libsystemd-shared.la
+
 test_install_SOURCES = \
        src/test/test-install.c
 
 test_install_SOURCES = \
        src/test/test-install.c
 
index 49a3d2bb7953b0fafea8170358a62c193d42b127..e9b6c033fdd1421d5524d063f6cb43ac4a90cf30 100644 (file)
@@ -32,7 +32,7 @@ static ssize_t add_string(struct strbuf *sb, const char *s) {
 
 static void test_strbuf(void) {
         struct strbuf *sb;
 
 static void test_strbuf(void) {
         struct strbuf *sb;
-       _cleanup_strv_free_ char **l;
+        _cleanup_strv_free_ char **l;
         ssize_t a, b, c, d, e, f, g;
 
         sb = strbuf_new();
         ssize_t a, b, c, d, e, f, g;
 
         sb = strbuf_new();
diff --git a/src/test/test-strxcpyx.c b/src/test/test-strxcpyx.c
new file mode 100644 (file)
index 0000000..9b4dae8
--- /dev/null
@@ -0,0 +1,101 @@
+/*-*- 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 <string.h>
+
+#include "util.h"
+#include "strv.h"
+#include "strxcpyx.h"
+
+static void test_strpcpy(void) {
+        char target[25];
+        char *s = target;
+        size_t space_left;
+
+        space_left = sizeof(target);
+        space_left = strpcpy(&s, space_left, "12345");
+        space_left = strpcpy(&s, space_left, "hey hey hey");
+        space_left = strpcpy(&s, space_left, "waldo");
+        space_left = strpcpy(&s, space_left, "ba");
+        space_left = strpcpy(&s, space_left, "r");
+        space_left = strpcpy(&s, space_left, "foo");
+
+        assert(streq(target, "12345hey hey heywaldobar"));
+        assert(space_left == 0);
+}
+
+static void test_strpcpyf(void) {
+        char target[25];
+        char *s = target;
+        size_t space_left;
+
+        space_left = sizeof(target);
+        space_left = strpcpyf(&s, space_left, "space left: %ld. ", space_left);
+        space_left = strpcpyf(&s, space_left, "foo%s", "bar");
+
+        assert(streq(target, "space left: 25. foobar"));
+        assert(space_left == 3);
+}
+
+static void test_strpcpyl(void) {
+        char target[25];
+        char *s = target;
+        size_t space_left;
+
+        space_left = sizeof(target);
+        space_left = strpcpyl(&s, space_left, "waldo", " test", " waldo. ", NULL);
+        space_left = strpcpyl(&s, space_left, "Banana", NULL);
+
+        assert(streq(target, "waldo test waldo. Banana"));
+        assert(space_left == 1);
+}
+
+static void test_strscpy(void) {
+        char target[25];
+        size_t space_left;
+
+        space_left = sizeof(target);
+        space_left = strscpy(target, space_left, "12345");
+
+        assert(streq(target, "12345"));
+        assert(space_left == 20);
+}
+
+static void test_strscpyl(void) {
+        char target[25];
+        size_t space_left;
+
+        space_left = sizeof(target);
+        space_left = strscpyl(target, space_left, "12345", "waldo", "waldo", NULL);
+
+        assert(streq(target, "12345waldowaldo"));
+        assert(space_left == 10);
+}
+
+int main(int argc, char *argv[]) {
+        test_strpcpy();
+        test_strpcpyf();
+        test_strpcpyl();
+        test_strscpy();
+        test_strscpyl();
+
+        return 0;
+}