chiark / gitweb /
test-strv.c: added STRV_FOREACH and STRV_FOREACH_BACKWARDS
[elogind.git] / src / test / test-strv.c
index 6513d2e07b01b5bd86164d04d076b8528b99f2a2..de5cef0b17ec3c3757e8e1329b1969b9fc04d8d7 100644 (file)
 #include "strv.h"
 
 static void test_specifier_printf(void) {
-        _cleanup_free_ char *w = NULL;
-        int r;
-
-        const Specifier table[] = {
+        static const Specifier table[] = {
                 { 'a', specifier_string, (char*) "AAAA" },
                 { 'b', specifier_string, (char*) "BBBB" },
                 { 'm', specifier_machine_id, NULL },
                 { 'B', specifier_boot_id, NULL },
                 { 'H', specifier_host_name, NULL },
                 { 'v', specifier_kernel_release, NULL },
-                { 0, NULL, NULL }
+                {}
         };
 
+        _cleanup_free_ char *w = NULL;
+        int r;
+
         r = specifier_printf("xxx a=%a b=%b yyy", table, NULL, &w);
         assert_se(r >= 0);
         assert_se(w);
@@ -282,6 +282,34 @@ static void test_strv_append(void) {
         assert_se(streq(c[0], "test3"));
 }
 
+static void test_strv_foreach(void) {
+       _cleanup_strv_free_ char **a;
+       unsigned i = 0;
+       char **check;
+
+       a = strv_new("one", "two", "three", NULL);
+
+       assert_se(a);
+
+       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;
+
+       a = strv_new("one", "two", "three", NULL);
+
+       assert_se(a);
+
+       STRV_FOREACH_BACKWARDS(check, a) {
+               assert_se(streq(*check, input_table_multiple[i--]));
+       }
+}
+
 static void test_strv_foreach_pair(void) {
         _cleanup_strv_free_ char **a = NULL;
         char **x, **y;
@@ -298,6 +326,8 @@ static void test_strv_foreach_pair(void) {
 
 int main(int argc, char *argv[]) {
         test_specifier_printf();
+        test_strv_foreach();
+        test_strv_foreach_backwards();
         test_strv_foreach_pair();
         test_strv_find();
         test_strv_find_prefix();