tests/test_string.c

changeset 1155
b77d56a27e9c
parent 1127
1fd31909a3f8
child 1162
e3bb67b72d33
--- a/tests/test_string.c	Sun Jan 26 11:59:16 2025 +0100
+++ b/tests/test_string.c	Sun Jan 26 12:24:49 2025 +0100
@@ -1064,28 +1064,28 @@
         CX_TEST_ASSERT(i64 == INT64_MIN);
 
         // group separators
-        CX_TEST_ASSERT(0 == cx_strtoi32(cx_str("  -123,456"), &i32, 10));
+        CX_TEST_ASSERT(0 == cx_strtoi32(cx_str("-123,456"), &i32, 10));
         CX_TEST_ASSERT(i32 == -123456);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("  -Xab,cd"), &i16, 16, "'"));
+        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-Xab,cd"), &i16, 16, "'"));
         CX_TEST_ASSERT(errno == EINVAL);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("  -X'ab'cd"), &i16, 16, "'"));
+        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-X'ab'cd"), &i16, 16, "'"));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str("  -X'67'89"), &i16, 16, "'"));
+        CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str("-X'67'89"), &i16, 16, "'"));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(i16 == -0x6789);
 
         // binary and (unusual notation of) signed binary
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" -1010 1011"), &i8, 2, " "));
+        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("-1010 1011"), &i8, 2, " "));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" 1010 1011"), &i8, 2, " "));
+        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("1010 1011"), &i8, 2, " "));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str(" -0101 0101"), &i8, 2, " "));
+        CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str("-0101 0101"), &i8, 2, " "));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(i8 == -0x55);
     }
@@ -1132,13 +1132,13 @@
         // --------------------------
 
         // group separators
-        CX_TEST_ASSERT(0 == cx_strtou32(cx_str("  123,456"), &u32, 10));
+        CX_TEST_ASSERT(0 == cx_strtou32(cx_str("123,456"), &u32, 10));
         CX_TEST_ASSERT(u32 == 123456);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str("  ab,cd"), &u16, 16, "'"));
+        CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str("ab,cd"), &u16, 16, "'"));
         CX_TEST_ASSERT(errno == EINVAL);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str("  ab'cd"), &u16, 16, "'"));
+        CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str("ab'cd"), &u16, 16, "'"));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(u16 == 0xabcd);
 
@@ -1147,7 +1147,7 @@
         CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " "));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str(" 1010 1011"), &u8, 2, " "));
+        CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str("1010 1011"), &u8, 2, " "));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(u8 == 0xAB);
     }
@@ -1226,6 +1226,34 @@
     }
 }
 
+CX_TEST(test_string_to_number_notrim) {
+    long long i;
+    unsigned long long u;
+    float f;
+    double d;
+    CX_TEST_DO {
+        CX_TEST_ASSERT(0 != cx_strtoll(cx_str("-42 "), &i, 10));
+        CX_TEST_ASSERT(0 != cx_strtoll(cx_str(" -42"), &i, 10));
+        CX_TEST_ASSERT(0 == cx_strtoll(cx_str("-42"), &i, 10));
+        CX_TEST_ASSERT(i == -42);
+
+        CX_TEST_ASSERT(0 != cx_strtoull(cx_str("42 "), &u, 10));
+        CX_TEST_ASSERT(0 != cx_strtoull(cx_str(" 42"), &u, 10));
+        CX_TEST_ASSERT(0 == cx_strtoull(cx_str("42"), &u, 10));
+        CX_TEST_ASSERT(u == 42);
+
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("13.37 "), &f));
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str(" 13.37"), &f));
+        CX_TEST_ASSERT(0 == cx_strtof(cx_str("13.37"), &f));
+        CX_TEST_ASSERT(0 == cx_vcmp_float(f, 13.37f));
+
+        CX_TEST_ASSERT(0 != cx_strtod(cx_str("13.37 "), &d));
+        CX_TEST_ASSERT(0 != cx_strtod(cx_str(" 13.37"), &d));
+        CX_TEST_ASSERT(0 == cx_strtod(cx_str("13.37"), &d));
+        CX_TEST_ASSERT(0 == cx_vcmp_double(d, 13.37));
+    }
+}
+
 CxTestSuite *cx_test_suite_string(void) {
     CxTestSuite *suite = cx_test_suite_new("string");
 
@@ -1268,6 +1296,7 @@
     cx_test_register(suite, test_string_to_unsigned_integer);
     cx_test_register(suite, test_string_to_float);
     cx_test_register(suite, test_string_to_double);
+    cx_test_register(suite, test_string_to_number_notrim);
 
     return suite;
 }

mercurial