]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-string-util: test empty_to_null on a char array
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Mon, 13 Apr 2026 19:46:12 +0000 (21:46 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 22 May 2026 12:32:04 +0000 (13:32 +0100)
Unfortunately empty_to_null(t) where t is char[] fails. But it
works with &t[0].

(cherry picked from commit 067aa9b767954d134b6f69a5b97ebbd19bbb9697)

src/test/test-string-util.c

index 094983a1724b85805ccfbf47c88b301dd782f1f4..8490f34f696ebdc924d31c1509a700d42129c14d 100644 (file)
@@ -619,6 +619,28 @@ TEST(split_pair) {
         ASSERT_STREQ(b, "=");
 }
 
+TEST(empty_to_null) {
+        const char *s = "asdf", *n = NULL, *e = "";
+        char *t = (char*) "asdf";
+        const char p[] = "asdf";
+        char q[] = "asdf";
+
+        /* empty_to_null cannot be used with constant strings, e.g.
+         * empty_to_null("") fails with 'error: cast specifies array type'. */
+
+        ASSERT_NULL(empty_to_null(NULL));
+        ASSERT_NULL(empty_to_null(n));
+        ASSERT_NULL(empty_to_null(e));
+        ASSERT_STREQ(empty_to_null(s), "asdf");
+        ASSERT_NULL(empty_to_null(s + 4));
+        ASSERT_STREQ(empty_to_null(t), "asdf");
+        ASSERT_NULL(empty_to_null(t + 4));
+        ASSERT_STREQ(empty_to_null(&p[0]), "asdf");
+        ASSERT_NULL(empty_to_null(&p[0] + 4));
+        ASSERT_STREQ(empty_to_null(&q[0]), "asdf");
+        ASSERT_NULL(empty_to_null(&q[0] + 4));
+}
+
 TEST(first_word) {
         assert_se(first_word("Hello", ""));
         assert_se(first_word("Hello", "Hello"));