diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 10 | ||||
-rw-r--r-- | src/test/test-strv.c | 130 | ||||
-rw-r--r-- | src/test/test-util.c | 82 |
4 files changed, 174 insertions, 49 deletions
diff --git a/.gitignore b/.gitignore index 03ed5543b1..6c2944bfa1 100644 --- a/.gitignore +++ b/.gitignore @@ -112,6 +112,7 @@ /test-udev /test-unit-file /test-unit-name +/test-util /test-watchdog /timedatectl /udevadm diff --git a/Makefile.am b/Makefile.am index 88662c0144..93fd3773e6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1231,6 +1231,7 @@ noinst_tests += \ test-strv \ test-unit-name \ test-unit-file \ + test-util \ test-date \ test-sleep \ test-replace-var \ @@ -1314,6 +1315,15 @@ test_unit_file_CFLAGS = \ test_unit_file_LDADD = \ libsystemd-core.la +test_util_SOURCES = \ + src/test/test-util.c + +test_util_CFLAGS = \ + $(AM_CFLAGS) + +test_util_LDADD = \ + libsystemd-core.la + test_log_SOURCES = \ src/test/test-log.c diff --git a/src/test/test-strv.c b/src/test/test-strv.c index 7898ea7cf6..1df8157b59 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -4,6 +4,7 @@ This file is part of systemd. Copyright 2010 Lennart Poettering + 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 @@ -39,22 +40,35 @@ static void test_specifier_printf(void) { free(w); } -static void test_foreach_word_quoted(void) { - char *w, *state; - size_t l; - const char test[] = "test a b c 'd' e '' '' hhh '' ''"; - printf("<%s>\n", test); - FOREACH_WORD_QUOTED(w, l, test, state) { - char *t; - - assert_se(t = strndup(w, l)); - printf("<%s>\n", t); - free(t); - } +static void test_strv_find(void) { + const char * const input_table[] = { + "one", + "two", + "three", + NULL + }; + + assert(strv_find((char **)input_table, "three")); + assert(!strv_find((char **)input_table, "four")); +} + +static void test_strv_find_prefix(void) { + const char * const input_table[] = { + "one", + "two", + "three", + NULL + }; + + assert(strv_find_prefix((char **)input_table, "o")); + assert(strv_find_prefix((char **)input_table, "one")); + assert(strv_find_prefix((char **)input_table, "")); + assert(!strv_find_prefix((char **)input_table, "xxx")); + assert(!strv_find_prefix((char **)input_table, "onee")); } static void test_strv_join(void) { - char *r; + _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL; const char * const input_table_multiple[] = { "one", @@ -70,52 +84,70 @@ static void test_strv_join(void) { NULL }; - r = strv_join((char **)input_table_multiple, ", "); - assert_se(streq(r, "one, two, three")); - puts(r); - free(r); + p = strv_join((char **)input_table_multiple, ", "); + assert(streq(p, "one, two, three")); - r = strv_join((char **)input_table_multiple, ";"); - assert_se(streq(r, "one;two;three")); - puts(r); - free(r); + q = strv_join((char **)input_table_multiple, ";"); + assert(streq(q, "one;two;three")); r = strv_join((char **)input_table_multiple, NULL); - assert_se(streq(r, "one two three")); - puts(r); - free(r); - - r = strv_join((char **)input_table_one, ", "); - assert_se(streq(r, "one")); - puts(r); - free(r); - - r = strv_join((char **)input_table_none, ", "); - assert_se(streq(r, "")); - puts(r); - free(r); + assert(streq(r, "one two three")); + + s = strv_join((char **)input_table_one, ", "); + assert(streq(s, "one")); + + t = strv_join((char **)input_table_none, ", "); + assert(streq(t, "")); } -static void test_default_term_for_tty(void) { - printf("%s\n", default_term_for_tty("/dev/tty23")); - printf("%s\n", default_term_for_tty("/dev/ttyS23")); - printf("%s\n", default_term_for_tty("/dev/tty0")); - printf("%s\n", default_term_for_tty("/dev/pty0")); - printf("%s\n", default_term_for_tty("/dev/pts/0")); - printf("%s\n", default_term_for_tty("/dev/console")); - printf("%s\n", default_term_for_tty("tty23")); - printf("%s\n", default_term_for_tty("ttyS23")); - printf("%s\n", default_term_for_tty("tty0")); - printf("%s\n", default_term_for_tty("pty0")); - printf("%s\n", default_term_for_tty("pts/0")); - printf("%s\n", default_term_for_tty("console")); +static void test_strv_overlap(void) { + const char * const input_table[] = { + "one", + "two", + "three", + NULL + }; + const char * const input_table_overlap[] = { + "two", + NULL + }; + const char * const input_table_unique[] = { + "four", + "five", + "six", + NULL + }; + + assert(strv_overlap((char **)input_table, (char**)input_table_overlap)); + assert(!strv_overlap((char **)input_table, (char**)input_table_unique)); +} + +static void test_strv_sort(void) { + const char * const input_table[] = { + "durian", + "apple", + "citrus", + "CAPITAL LETTERS FIRST", + "banana", + NULL + }; + + strv_sort((char **)input_table); + + assert(streq(input_table[0], "CAPITAL LETTERS FIRST")); + assert(streq(input_table[1], "apple")); + assert(streq(input_table[2], "banana")); + assert(streq(input_table[3], "citrus")); + assert(streq(input_table[4], "durian")); } int main(int argc, char *argv[]) { - test_default_term_for_tty(); - test_foreach_word_quoted(); test_specifier_printf(); + test_strv_find(); + test_strv_find_prefix(); test_strv_join(); + test_strv_overlap(); + test_strv_sort(); return 0; } diff --git a/src/test/test-util.c b/src/test/test-util.c new file mode 100644 index 0000000000..ebbdcfc6de --- /dev/null +++ b/src/test/test-util.c @@ -0,0 +1,82 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + 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" + +static void test_streq_ptr(void) { + assert(streq_ptr(NULL, NULL)); + assert(!streq_ptr("abc", "cdef")); +} + +static void test_first_word(void) { + assert(first_word("Hello", "")); + assert(first_word("Hello", "Hello")); + assert(first_word("Hello world", "Hello")); + assert(first_word("Hello\tworld", "Hello")); + assert(first_word("Hello\nworld", "Hello")); + assert(first_word("Hello\rworld", "Hello")); + assert(first_word("Hello ", "Hello")); + + assert(!first_word("Hello", "Hellooo")); + assert(!first_word("Hello", "xxxxx")); + assert(!first_word("Hellooo", "Hello")); +} + +static void test_foreach_word_quoted(void) { + char *w, *state; + size_t l; + const char test[] = "test a b c 'd' e '' '' hhh '' ''"; + printf("<%s>\n", test); + FOREACH_WORD_QUOTED(w, l, test, state) { + char *t; + + assert_se(t = strndup(w, l)); + printf("<%s>\n", t); + free(t); + } +} + +static void test_default_term_for_tty(void) { + puts(default_term_for_tty("/dev/tty23")); + puts(default_term_for_tty("/dev/ttyS23")); + puts(default_term_for_tty("/dev/tty0")); + puts(default_term_for_tty("/dev/pty0")); + puts(default_term_for_tty("/dev/pts/0")); + puts(default_term_for_tty("/dev/console")); + puts(default_term_for_tty("tty23")); + puts(default_term_for_tty("ttyS23")); + puts(default_term_for_tty("tty0")); + puts(default_term_for_tty("pty0")); + puts(default_term_for_tty("pts/0")); + puts(default_term_for_tty("console")); +} + +int main(int argc, char *argv[]) { + test_streq_ptr(); + test_first_word(); + test_default_term_for_tty(); + test_foreach_word_quoted(); + + return 0; +} |