]> 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)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Thu, 16 Apr 2026 19:07:43 +0000 (21:07 +0200)
Unfortunately empty_to_null(t) where t is char[] fails. But it
works with &t[0].

src/test/test-string-util.c

index 4f12ec710c11e26d0a9070159d7d7ad6a40f8173..31b3d862f4fbbcb341009ef9073c56a3f90e60a0 100644 (file)
@@ -617,6 +617,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"));