diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-10-29 19:53:43 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-10-29 19:53:43 +0100 |
commit | 250a918dc4c8a15d927deecc3b3f6a0604657ae4 (patch) | |
tree | 18faf995d700c26c67ad500b81e7cce007bc0b95 /src/test | |
parent | 7b1132f60d2a447c31556c23ea90ea31447ac557 (diff) |
strv: introduce new strv_from_stdarg_alloca() macro to generate a string array from stdarg function parameters
This allows us to turn lists of strings passed in easily into string
arrays without having to allocate memory.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-strv.c | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/src/test/test-strv.c b/src/test/test-strv.c index de5cef0b17..7002b8b1c0 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -283,31 +283,31 @@ static void test_strv_append(void) { } static void test_strv_foreach(void) { - _cleanup_strv_free_ char **a; - unsigned i = 0; - char **check; + _cleanup_strv_free_ char **a; + unsigned i = 0; + char **check; - a = strv_new("one", "two", "three", NULL); + a = strv_new("one", "two", "three", NULL); - assert_se(a); + assert_se(a); - STRV_FOREACH(check, a) { - assert_se(streq(*check, input_table_multiple[i++])); - } + STRV_FOREACH(check, a) { + assert_se(streq(*check, input_table_multiple[i++])); + } } static void test_strv_foreach_backwards(void) { - _cleanup_strv_free_ char **a; - unsigned i = 2; - char **check; + _cleanup_strv_free_ char **a; + unsigned i = 2; + char **check; - a = strv_new("one", "two", "three", NULL); + a = strv_new("one", "two", "three", NULL); - assert_se(a); + assert_se(a); - STRV_FOREACH_BACKWARDS(check, a) { - assert_se(streq(*check, input_table_multiple[i--])); - } + STRV_FOREACH_BACKWARDS(check, a) { + assert_se(streq(*check, input_table_multiple[i--])); + } } static void test_strv_foreach_pair(void) { @@ -324,6 +324,26 @@ static void test_strv_foreach_pair(void) { } } +static void test_strv_from_stdarg_alloca_one(const char **l, const char *first, ...) { + char **j; + unsigned i; + + j = strv_from_stdarg_alloca(first); + + for (i = 0;; i++) { + assert_se(streq_ptr(l[i], j[i])); + + if (!l[i]) + break; + } +} + +static void test_strv_from_stdarg_alloca(void) { + test_strv_from_stdarg_alloca_one((const char*[]) { "foo", "bar", NULL }, "foo", "bar", NULL); + test_strv_from_stdarg_alloca_one((const char*[]) { "foo", "bar", NULL }, "foo", "bar", NULL); + test_strv_from_stdarg_alloca_one((const char*[]) { "foo", NULL }, "foo", NULL); +} + int main(int argc, char *argv[]) { test_specifier_printf(); test_strv_foreach(); @@ -346,6 +366,7 @@ int main(int argc, char *argv[]) { test_strv_merge(); test_strv_merge_concat(); test_strv_append(); + test_strv_from_stdarg_alloca(); return 0; } |