tests/test_string.c

changeset 1426
3a89b31f0724
parent 1412
a6f357c18ac6
--- a/tests/test_string.c	Wed Oct 15 22:45:21 2025 +0200
+++ b/tests/test_string.c	Thu Oct 16 19:57:47 2025 +0200
@@ -55,6 +55,27 @@
     }
 }
 
+CX_TEST(test_string_cast) {
+    char *c1 = (char*) "123";
+    const char *c2 = "abcde";
+    unsigned char *c3 = (unsigned char*) "4711";
+    unsigned const char *c4 = (unsigned const char*) "xyz0815";
+    cxstring s1 = cx_strcast(c1);
+    cxstring s2 = cx_strcast(c2);
+    cxstring s3 = cx_strcast(c3);
+    cxstring s4 = cx_strcast(c4);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(s1.length == 3);
+        CX_TEST_ASSERT(strncmp(s1.ptr, "123", 3) == 0);
+        CX_TEST_ASSERT(s2.length == 5);
+        CX_TEST_ASSERT(strncmp(s2.ptr, "abcde", 5) == 0);
+        CX_TEST_ASSERT(s3.length == 4);
+        CX_TEST_ASSERT(strncmp(s3.ptr, "4711", 4) == 0);
+        CX_TEST_ASSERT(s4.length == 7);
+        CX_TEST_ASSERT(strncmp(s4.ptr, "xyz0815", 7) == 0);
+    }
+}
+
 CX_TEST(test_strfree) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
@@ -1314,6 +1335,7 @@
     CxTestSuite *suite = cx_test_suite_new("string");
 
     cx_test_register(suite, test_string_construct);
+    cx_test_register(suite, test_string_cast);
     cx_test_register(suite, test_strfree);
     cx_test_register(suite, test_strdup);
     cx_test_register(suite, test_strdup_shortened);

mercurial