tests/test_string.c

changeset 1668
3ffdfe1776b4
parent 1667
608cc0b25352
child 1671
cf19b7820ff0
--- a/tests/test_string.c	Thu Dec 25 11:10:13 2025 +0100
+++ b/tests/test_string.c	Thu Dec 25 11:39:26 2025 +0100
@@ -220,6 +220,31 @@
     }
 }
 
+CX_TEST(test_strat) {
+    cxstring str = cx_str("Hello, World!");
+
+    CX_TEST_DO {
+        // the entire string
+        for (size_t i = 0; i < str.length; i++) {
+            CX_TEST_ASSERT(cx_strat(str, i) == str.ptr[i]);
+        }
+        // the entire string backwards
+        for (off_t i = 1; i <= (off_t) str.length; i++) {
+            CX_TEST_ASSERT(cx_strat(str, -i) == str.ptr[str.length-i]);
+        }
+        // out of bounds (positive)
+        CX_TEST_ASSERT(cx_strat(str, 12) == '!');
+        CX_TEST_ASSERT(cx_strat(str, 13) == '\0');
+        CX_TEST_ASSERT(cx_strat(str, 14) == '\0');
+        CX_TEST_ASSERT(cx_strat(str, 651273) == '\0');
+        // out of bounds (negative)
+        CX_TEST_ASSERT(cx_strat(str, -13) == 'H');
+        CX_TEST_ASSERT(cx_strat(str, -14) == '\0');
+        CX_TEST_ASSERT(cx_strat(str, -15) == '\0');
+        CX_TEST_ASSERT(cx_strat(str, -651273) == '\0');
+    }
+}
+
 CX_TEST(test_strchr) {
     cxstring str = cx_str("I will find you - and I will kill you");
 
@@ -1511,6 +1536,7 @@
     cx_test_register(suite, test_strcpy);
     cx_test_register(suite, test_strlen);
     cx_test_register(suite, test_strsubs);
+    cx_test_register(suite, test_strat);
     cx_test_register(suite, test_strchr);
     cx_test_register(suite, test_strrchr);
     cx_test_register(suite, test_strstr);

mercurial