From: Michael Tremer Date: Tue, 5 Oct 2021 21:03:42 +0000 (+0000) Subject: util: Add tests for pakfire_string_join X-Git-Tag: 0.9.28~889 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4596c4a00a4bb3511664f60d0381e9d954947c37;p=pakfire.git util: Add tests for pakfire_string_join Signed-off-by: Michael Tremer --- diff --git a/tests/libpakfire/util.c b/tests/libpakfire/util.c index 4c5b0524e..4f6b46a11 100644 --- a/tests/libpakfire/util.c +++ b/tests/libpakfire/util.c @@ -206,6 +206,77 @@ FAIL: return EXIT_FAILURE; } +static int test_string_join(const struct test* t) { + char* s = NULL; + + // Some test elements + char* elements1[] = { + "A", + "B", + "C", + NULL, + }; + + // Join with newline + s = pakfire_string_join(elements1, "\n"); + ASSERT_STRING_EQUALS(s, "A\nB\nC"); + + if (s) { + free(s); + s = NULL; + } + + // Join with empty delimiter + s = pakfire_string_join(elements1, ""); + ASSERT_STRING_EQUALS(s, "ABC"); + + if (s) { + free(s); + s = NULL; + } + + char* elements2[] = { + "", + "", + "", + NULL, + }; + + // Join list with empty elements + s = pakfire_string_join(elements2, "X"); + ASSERT_STRING_EQUALS(s, "XX"); + + if (s) { + free(s); + s = NULL; + } + + // Invalid inputs + s = pakfire_string_join(NULL, NULL); + ASSERT_ERRNO(!s, EINVAL); + + s = pakfire_string_join(elements1, NULL); + ASSERT_ERRNO(!s, EINVAL); + + s = pakfire_string_join(NULL, "\n"); + ASSERT_ERRNO(!s, EINVAL); + + char* elements3[] = { + NULL, + }; + + // Join empty elements + ASSERT_NULL(pakfire_string_join(elements3, "\n")); + + return EXIT_SUCCESS; + +FAIL: + if (s) + free(s); + + return EXIT_FAILURE; +} + int main(int argc, char** argv) { testsuite_add_test(test_basename); testsuite_add_test(test_dirname); @@ -215,6 +286,7 @@ int main(int argc, char** argv) { testsuite_add_test(test_string_partition); testsuite_add_test(test_string_replace); testsuite_add_test(test_string_split); + testsuite_add_test(test_string_join); return testsuite_run(); }