--- a/tests/test_string.c Sat Jan 25 15:22:01 2025 +0100 +++ b/tests/test_string.c Tue Jan 28 18:31:17 2025 +0100 @@ -985,8 +985,7 @@ test_strtoint_impl(LL, num, base, i8, INT8_MIN, INT8_MAX); \ test_strtoint_impl(LL, num, base, i16, INT16_MIN, INT16_MAX); \ test_strtoint_impl(LL, num, base, i32, INT32_MIN, INT32_MAX); \ - test_strtoint_impl(LL, num, base, i64, INT64_MIN, INT64_MAX); \ - test_strtoint_impl(LL, num, base, z, -SSIZE_MAX-1, SSIZE_MAX) + test_strtoint_impl(LL, num, base, i64, INT64_MIN, INT64_MAX); #define test_strtoint_rollout_signed(num, base) \ test_strtoint_rollout_signed_impl(num, base); \ @@ -1001,7 +1000,7 @@ test_strtoint_impl(ULL, num, base, u16, 0, UINT16_MAX); \ test_strtoint_impl(ULL, num, base, u32, 0, UINT32_MAX); \ test_strtoint_impl(ULL, num, base, u64, 0, UINT64_MAX); \ - test_strtoint_impl(ULL, num, base, uz, 0, SIZE_MAX) + test_strtoint_impl(ULL, num, base, z, 0, SIZE_MAX) CX_TEST(test_string_to_signed_integer) { short s; @@ -1012,7 +1011,6 @@ int16_t i16; int32_t i32; int64_t i64; - ssize_t z; CX_TEST_DO { // do some brute force tests with all ranges test_strtoint_rollout_signed(47, 10); @@ -1064,28 +1062,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); } @@ -1100,7 +1098,7 @@ uint16_t u16; uint32_t u32; uint64_t u64; - size_t uz; + size_t z; CX_TEST_DO { // do some brute force tests with all ranges test_strtoint_rollout(47, 10); @@ -1132,13 +1130,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 +1145,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 +1224,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 +1294,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; }