Just to make sure strjoin works as intended, add some simple unit tests. Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- test/self/string.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/self/string.c b/test/self/string.c index f03a7410cd64..d33e2d7918ca 100644 --- a/test/self/string.c +++ b/test/self/string.c @@ -168,8 +168,36 @@ static void test_strverscmp(void) strverscmp_assert_one("", "", 0); } +static void __expect_streq(const char *func, int line, + char *is, const char *expect, bool free_is) +{ + total_tests++; + if (strcmp(is, expect)) { + failed_tests++; + printf("%s:%d: got %s, but %s expected\n", func, line, is, expect); + } + + if (free_is) + free(is); +} + +#define expect_dynstreq(args...) \ + __expect_streq(__func__, __LINE__, args, true) + +static void test_strjoin(void) +{ + char *strs[] = { "ayy", "bee", "cee" }; + + expect_dynstreq(strjoin("", strs, ARRAY_SIZE(strs)), "ayybeecee"); + expect_dynstreq(strjoin(" ", strs, ARRAY_SIZE(strs)), "ayy bee cee"); + expect_dynstreq(strjoin(", ", strs, ARRAY_SIZE(strs)), "ayy, bee, cee"); + expect_dynstreq(strjoin(" ", strs, 1), "ayy"); + expect_dynstreq(strjoin(" ", NULL, 0), ""); +} + static void test_string(void) { test_strverscmp(); + test_strjoin(); } bselftest(parser, test_string); -- 2.39.2