--- a/tests/test_string.c Fri Dec 27 13:01:31 2024 +0100 +++ b/tests/test_string.c Sat Dec 28 15:06:15 2024 +0100 @@ -1050,13 +1050,54 @@ // TODO: roll out base 2 tests, but that needs C23 // do some special case tests + // -------------------------- // can fit only in unsigned long long errno = 0; CX_TEST_ASSERT(0 != cx_strtoll(cx_str("0x8df9CE03AbC90815"), &ll, 16)); CX_TEST_ASSERT(errno == ERANGE); - // TODO: implement more special cases + // edge case: only the sign bit is set + errno = 0; + CX_TEST_ASSERT(0 != cx_strtoi16(cx_str("0x8000"), &i16, 16)); + CX_TEST_ASSERT(errno == ERANGE); + errno = 0; + CX_TEST_ASSERT(0 == cx_strtoi16(cx_str("-0x8000"), &i16, 16)); + CX_TEST_ASSERT(errno == 0); + CX_TEST_ASSERT(i16 == INT16_MIN); + errno = 0; + CX_TEST_ASSERT(0 != cx_strtoi64(cx_str("X8000000000000000"), &i64, 16)); + CX_TEST_ASSERT(errno == ERANGE); + errno = 0; + CX_TEST_ASSERT(0 == cx_strtoi64(cx_str("-X8000000000000000"), &i64, 16)); + CX_TEST_ASSERT(errno == 0); + CX_TEST_ASSERT(i64 == INT64_MIN); + + // group separators + 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(errno == EINVAL); + errno = 0; + 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(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(errno == ERANGE); + errno = 0; + 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(errno == 0); + CX_TEST_ASSERT(i8 == -0x55); } } @@ -1098,8 +1139,27 @@ // TODO: roll out base 2 tests, but that needs C23 // do some special case tests + // -------------------------- - // TODO: implement tests + // group separators + 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(errno == EINVAL); + errno = 0; + CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str(" ab'cd"), &u16, 16, "'")); + CX_TEST_ASSERT(errno == 0); + CX_TEST_ASSERT(u16 == 0xabcd); + + // binary + errno = 0; + 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(errno == 0); + CX_TEST_ASSERT(u8 == 0xAB); } } @@ -1108,6 +1168,15 @@ CX_TEST_DO { CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f)); CX_TEST_ASSERT(11.3f == f); + + CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.67262192595e-27"), &f)); + CX_TEST_ASSERT(1.67262192595e-27f == f); + + CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, '.', ",")); + CX_TEST_ASSERT(138339.4f == f); + + CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, ',', ".")); + CX_TEST_ASSERT(138.3394f == f); } } @@ -1116,6 +1185,15 @@ CX_TEST_DO { CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d)); CX_TEST_ASSERT(11.3 == d); + + CX_TEST_ASSERT(0 == cx_strtod(cx_str("1.67262192595e-27"), &d)); + CX_TEST_ASSERT(1.67262192595e-27 == d); + + CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, '.', ",")); + CX_TEST_ASSERT(138339.4 == d); + + CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', ".")); + CX_TEST_ASSERT(138.3394 == d); } }