[systemd-commits] src/shared src/test

Lennart Poettering lennart at kemper.freedesktop.org
Tue Mar 18 16:55:10 PDT 2014


 src/shared/strv.h    |   12 ++++++++++++
 src/test/test-util.c |   20 ++++++++++++++++++++
 2 files changed, 32 insertions(+)

New commits:
commit c4a7b2c56e907e39fa9a6cdb21c1970464abd44e
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Mar 19 00:54:41 2014 +0100

    util: add new FOREACH_STRING() macro as syntactic sugar to iterate through a number of fixed strings

diff --git a/src/shared/strv.h b/src/shared/strv.h
index b356d03..e26ab82 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -119,3 +119,15 @@ void strv_print(char **l);
         })
 
 #define STR_IN_SET(x, ...) strv_contains(STRV_MAKE(__VA_ARGS__), x)
+
+#define FOREACH_STRING(x, ...)                               \
+        for (char **_l = ({                                  \
+                char **_ll = STRV_MAKE(__VA_ARGS__);         \
+                x = _ll ? _ll[0] : NULL;                     \
+                _ll;                                         \
+        });                                                  \
+        _l && *_l;                                           \
+        x = ({                                               \
+                _l ++;                                       \
+                _l[0];                                       \
+        }))
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 1d952eb..93929cd 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -656,6 +656,25 @@ static void test_log2i(void) {
         assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
 }
 
+static void test_foreach_string(void) {
+        const char * const t[] = {
+                "foo",
+                "bar",
+                "waldo",
+                NULL
+        };
+        const char *x;
+        unsigned i = 0;
+
+        FOREACH_STRING(x, "foo", "bar", "waldo")
+                assert_se(streq_ptr(t[i++], x));
+
+        assert_se(i == 3);
+
+        FOREACH_STRING(x, "zzz")
+                assert_se(streq(x, "zzz"));
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -699,6 +718,7 @@ int main(int argc, char *argv[]) {
         test_writing_tmpfile();
         test_hexdump();
         test_log2i();
+        test_foreach_string();
 
         return 0;
 }



More information about the systemd-commits mailing list