tests/test_string.c

changeset 1304
57e062a4bb05
parent 1297
0811fb9a8dba
--- a/tests/test_string.c	Sat Apr 19 11:36:53 2025 +0200
+++ b/tests/test_string.c	Sat Apr 19 14:43:16 2025 +0200
@@ -95,6 +95,45 @@
     cx_strfree(&dup);
 }
 
+CX_TEST(test_strcpy) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    const CxAllocator *alloc = &talloc.base;
+    cxstring str = CX_STR("test string");
+    str.length = 8; // test with a non-zero-terminated source
+    cxmutstr dup;
+    CX_TEST_DO {
+        // copy into a smaller string
+        dup = cx_strdup_a(alloc, CX_STR("hello"));
+        CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
+        ASSERT_ZERO_TERMINATED(dup);
+        cx_strfree_a(alloc, &dup);
+
+        // copy into a larger string
+        dup = cx_strdup_a(alloc, CX_STR("hello, world!"));
+        CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
+        ASSERT_ZERO_TERMINATED(dup);
+        cx_strfree_a(alloc, &dup);
+
+        // copy into an equal-length string
+        dup = cx_strdup_a(alloc, CX_STR("testing!"));
+        CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
+        ASSERT_ZERO_TERMINATED(dup);
+        cx_strfree_a(alloc, &dup);
+
+        // copy into a NULL-string
+        dup.ptr = NULL;
+        CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
+        ASSERT_ZERO_TERMINATED(dup);
+        cx_strfree_a(alloc, &dup);
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CX_TEST(test_strlen) {
     cxstring s1 = CX_STR("1234");
     cxstring s2 = CX_STR(".:.:.");
@@ -1263,6 +1302,7 @@
     cx_test_register(suite, test_strfree);
     cx_test_register(suite, test_strdup);
     cx_test_register(suite, test_strdup_shortened);
+    cx_test_register(suite, test_strcpy);
     cx_test_register(suite, test_strlen);
     cx_test_register(suite, test_strsubs);
     cx_test_register(suite, test_strchr);

mercurial