From: Zbigniew Jędrzejewski-Szmek Date: Mon, 13 Apr 2026 19:46:12 +0000 (+0200) Subject: test-string-util: test empty_to_null on a char array X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=067aa9b767954d134b6f69a5b97ebbd19bbb9697;p=thirdparty%2Fsystemd.git test-string-util: test empty_to_null on a char array Unfortunately empty_to_null(t) where t is char[] fails. But it works with &t[0]. --- diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index 4f12ec710c1..31b3d862f4f 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -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"));