add test for cx_strcat() with zero additional strings

Wed, 31 Dec 2025 12:33:16 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 31 Dec 2025 12:33:16 +0100
changeset 1685
0344372c7115
parent 1684
89a07d18f86c
child 1686
d08b63b8c93a

add test for cx_strcat() with zero additional strings

src/string.c file | annotate | diff | comparison | revisions
tests/test_string.c file | annotate | diff | comparison | revisions
--- a/src/string.c	Wed Dec 31 12:24:39 2025 +0100
+++ b/src/string.c	Wed Dec 31 12:33:16 2025 +0100
@@ -106,7 +106,11 @@
         ...
 ) {
     if (count == 0) {
-        return cx_strdup_a(alloc, str);
+        if (cxReallocate(alloc, &str.ptr, str.length + 1)) {
+            return CX_NULLSTR; // LCOV_EXCL_LINE
+        }
+        str.ptr[str.length] = '\0';
+        return str;
     }
     va_list ap;
     va_start(ap, count);
--- a/tests/test_string.c	Wed Dec 31 12:24:39 2025 +0100
+++ b/tests/test_string.c	Wed Dec 31 12:33:16 2025 +0100
@@ -595,6 +595,36 @@
     cx_testing_allocator_destroy(&talloc);
 }
 
+CX_TEST(test_strcat_zero) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+
+    CX_TEST_DO {
+        cxmutstr t1 = cx_strcat_a(alloc, CX_NULLSTR, 0);
+        CX_TEST_ASSERT(0 == cx_strcmp(t1, ""));
+        ASSERT_ZERO_TERMINATED(t1);
+        cx_strfree_a(alloc, &t1);
+
+        cxmutstr s2 = cx_strdup_a(alloc, "testing");
+        s2.length = 4;
+        cxmutstr t2 = cx_strcat_a(alloc, s2, 0);
+        CX_TEST_ASSERT(0 == cx_strcmp(t2, "test"));
+        ASSERT_ZERO_TERMINATED(t2);
+        cx_strfree_a(alloc, &t2);
+
+        cxmutstr s3 = cx_strdup_a(alloc, "fake");
+        s3.length = 0;
+        cxmutstr t3 = cx_strcat_a(alloc, s3, 0);
+        CX_TEST_ASSERT(0 == cx_strcmp(t3, ""));
+        ASSERT_ZERO_TERMINATED(t3);
+        cx_strfree_a(alloc, &t3);
+
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CX_TEST(test_strcat_more_than_eight) {
     cxstring s1 = cx_str("12");
     cxstring s2 = cx_str("34");
@@ -1684,6 +1714,7 @@
     cx_test_register(suite, test_strcmp);
     cx_test_register(suite, test_strcasecmp);
     cx_test_register(suite, test_strcat);
+    cx_test_register(suite, test_strcat_zero);
     cx_test_register(suite, test_strcat_more_than_eight);
     cx_test_register(suite, test_strsplit);
     cx_test_register(suite, test_strsplit_a);

mercurial