summaryrefslogtreecommitdiff
path: root/src/test/test-path-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-09-25 20:58:23 +0200
committerLennart Poettering <lennart@poettering.net>2013-09-25 21:04:35 +0200
commitfecffe5d0a1bd66d80e5a8728ff8a89673be0df7 (patch)
tree133a9f9b0dc791723a1e63fa6efb5fafa4a8ba60 /src/test/test-path-util.c
parentbc5fb0809e6f5784888f30057708548f14dcb459 (diff)
util: add macro for iterating through all prefixes of a path
Syntactic sugar in a macro PATH_FOREACH_PREFIX.
Diffstat (limited to 'src/test/test-path-util.c')
-rw-r--r--src/test/test-path-util.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index b0aeb11a63..e303e488e2 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -106,8 +106,35 @@ static void test_find_binary(void) {
assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
}
+static void test_prefixes(void) {
+ static const char* values[] = { "/a/b/c", "/a/b", "/a", "", NULL};
+ unsigned i = 0;
+ char s[PATH_MAX];
+
+ PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
+ log_error("---%s---", s);
+ assert_se(streq(s, values[i++]));
+ }
+
+ assert_se(values[i] == NULL);
+
+ i = 0;
+ PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
+ assert_se(streq(s, values[i++]));
+
+ assert_se(values[i] == NULL);
+
+ PATH_FOREACH_PREFIX(s, "////")
+ assert_se(streq(s, ""));
+
+ PATH_FOREACH_PREFIX(s, "")
+ assert_not_reached("wut?");
+
+}
+
int main(void) {
test_path();
test_find_binary();
+ test_prefixes();
return 0;
}