]> git.ipfire.org Git - pakfire.git/commitdiff
util: Add tests for pakfire_string_join
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 5 Oct 2021 21:03:42 +0000 (21:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 5 Oct 2021 21:03:42 +0000 (21:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/libpakfire/util.c

index 4c5b0524ece3eb582cef6d85d36556c0fc787938..4f6b46a111f6a945620db13ed29b868e4b298ced 100644 (file)
@@ -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();
 }