chiark / gitweb /
pam: externally our booleans are ints, not unsigneds
[elogind.git] / src / test / test-strv.c
index 25bee22dfe9c4f91e0d0c92a766f585f9c84fc0e..f32d02ed8574cebd50a39882028d62e9b0872e36 100644 (file)
 #include "strv.h"
 
 static void test_specifier_printf(void) {
-        _cleanup_free_ char *w = NULL;
-
-        const Specifier table[] = {
+        static const Specifier table[] = {
                 { 'a', specifier_string, (char*) "AAAA" },
                 { 'b', specifier_string, (char*) "BBBB" },
-                { 0, NULL, NULL }
+                { 'm', specifier_machine_id, NULL },
+                { 'B', specifier_boot_id, NULL },
+                { 'H', specifier_host_name, NULL },
+                { 'v', specifier_kernel_release, NULL },
+                {}
         };
 
-        w = specifier_printf("xxx a=%a b=%b yyy", table, NULL);
-        puts(w);
+        _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);
+
+        puts(w);
         assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
+
+        free(w);
+        r = specifier_printf("machine=%m, boot=%B, host=%H, version=%v", table, NULL, &w);
+        assert_se(r >= 0);
+        assert_se(w);
+        puts(w);
 }
 
 static const char* const input_table_multiple[] = {
@@ -270,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;
@@ -284,8 +324,30 @@ static void test_strv_foreach_pair(void) {
         }
 }
 
+static void test_strv_from_stdarg_alloca_one(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(STRV_MAKE("foo", "bar"), "foo", "bar", NULL);
+        test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL);
+        test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL);
+}
+
 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();
@@ -304,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;
 }