]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-string-util: add a test for strjoin()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 20 Aug 2020 11:37:31 +0000 (13:37 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 20 Aug 2020 15:42:13 +0000 (17:42 +0200)
Strangely, we didn't have one so far. I mostly wanted to verify
that NULL can be used in any spot at behaves the same as "".

src/test/test-string-util.c

index 13936f6d25f23e2233e4a373c46fa6930567c5b1..1127d398a5f58ab55b3aa4caaef8e7d3438b9bee 100644 (file)
@@ -326,6 +326,38 @@ static void test_strjoina(void) {
         assert_se(streq(actual, "foo"));
 }
 
+static void test_strjoin(void) {
+        char *actual;
+
+        actual = strjoin("", "foo", "bar");
+        assert_se(streq(actual, "foobar"));
+        mfree(actual);
+
+        actual = strjoin("foo", "bar", "baz");
+        assert_se(streq(actual, "foobarbaz"));
+        mfree(actual);
+
+        actual = strjoin("foo", "", "bar", "baz");
+        assert_se(streq(actual, "foobarbaz"));
+        mfree(actual);
+
+        actual = strjoin("foo", NULL);
+        assert_se(streq(actual, "foo"));
+        mfree(actual);
+
+        actual = strjoin(NULL, NULL);
+        assert_se(streq(actual, ""));
+        mfree(actual);
+
+        actual = strjoin(NULL, "foo");
+        assert_se(streq(actual, ""));
+        mfree(actual);
+
+        actual = strjoin("foo", NULL, "bar");
+        assert_se(streq(actual, "foo"));
+        mfree(actual);
+}
+
 static void test_strcmp_ptr(void) {
         assert_se(strcmp_ptr(NULL, NULL) == 0);
         assert_se(strcmp_ptr("", NULL) > 0);
@@ -727,6 +759,7 @@ int main(int argc, char *argv[]) {
         test_ascii_strlower();
         test_strshorten();
         test_strjoina();
+        test_strjoin();
         test_strcmp_ptr();
         test_foreach_word();
         test_foreach_word_quoted();