1062 CX_TEST_ASSERT(0 == cx_strtoi64(cx_str("-X8000000000000000"), &i64, 16)); |
1062 CX_TEST_ASSERT(0 == cx_strtoi64(cx_str("-X8000000000000000"), &i64, 16)); |
1063 CX_TEST_ASSERT(errno == 0); |
1063 CX_TEST_ASSERT(errno == 0); |
1064 CX_TEST_ASSERT(i64 == INT64_MIN); |
1064 CX_TEST_ASSERT(i64 == INT64_MIN); |
1065 |
1065 |
1066 // group separators |
1066 // group separators |
1067 CX_TEST_ASSERT(0 == cx_strtoi32(cx_str(" -123,456"), &i32, 10)); |
1067 CX_TEST_ASSERT(0 == cx_strtoi32(cx_str("-123,456"), &i32, 10)); |
1068 CX_TEST_ASSERT(i32 == -123456); |
1068 CX_TEST_ASSERT(i32 == -123456); |
1069 errno = 0; |
1069 errno = 0; |
1070 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str(" -Xab,cd"), &i16, 16, "'")); |
1070 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-Xab,cd"), &i16, 16, "'")); |
1071 CX_TEST_ASSERT(errno == EINVAL); |
1071 CX_TEST_ASSERT(errno == EINVAL); |
1072 errno = 0; |
1072 errno = 0; |
1073 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str(" -X'ab'cd"), &i16, 16, "'")); |
1073 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-X'ab'cd"), &i16, 16, "'")); |
1074 CX_TEST_ASSERT(errno == ERANGE); |
1074 CX_TEST_ASSERT(errno == ERANGE); |
1075 errno = 0; |
1075 errno = 0; |
1076 CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str(" -X'67'89"), &i16, 16, "'")); |
1076 CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str("-X'67'89"), &i16, 16, "'")); |
1077 CX_TEST_ASSERT(errno == 0); |
1077 CX_TEST_ASSERT(errno == 0); |
1078 CX_TEST_ASSERT(i16 == -0x6789); |
1078 CX_TEST_ASSERT(i16 == -0x6789); |
1079 |
1079 |
1080 // binary and (unusual notation of) signed binary |
1080 // binary and (unusual notation of) signed binary |
1081 errno = 0; |
1081 errno = 0; |
1082 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" -1010 1011"), &i8, 2, " ")); |
1082 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("-1010 1011"), &i8, 2, " ")); |
1083 CX_TEST_ASSERT(errno == ERANGE); |
1083 CX_TEST_ASSERT(errno == ERANGE); |
1084 errno = 0; |
1084 errno = 0; |
1085 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" 1010 1011"), &i8, 2, " ")); |
1085 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("1010 1011"), &i8, 2, " ")); |
1086 CX_TEST_ASSERT(errno == ERANGE); |
1086 CX_TEST_ASSERT(errno == ERANGE); |
1087 errno = 0; |
1087 errno = 0; |
1088 CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str(" -0101 0101"), &i8, 2, " ")); |
1088 CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str("-0101 0101"), &i8, 2, " ")); |
1089 CX_TEST_ASSERT(errno == 0); |
1089 CX_TEST_ASSERT(errno == 0); |
1090 CX_TEST_ASSERT(i8 == -0x55); |
1090 CX_TEST_ASSERT(i8 == -0x55); |
1091 } |
1091 } |
1092 } |
1092 } |
1093 |
1093 |
1130 |
1130 |
1131 // do some special case tests |
1131 // do some special case tests |
1132 // -------------------------- |
1132 // -------------------------- |
1133 |
1133 |
1134 // group separators |
1134 // group separators |
1135 CX_TEST_ASSERT(0 == cx_strtou32(cx_str(" 123,456"), &u32, 10)); |
1135 CX_TEST_ASSERT(0 == cx_strtou32(cx_str("123,456"), &u32, 10)); |
1136 CX_TEST_ASSERT(u32 == 123456); |
1136 CX_TEST_ASSERT(u32 == 123456); |
1137 errno = 0; |
1137 errno = 0; |
1138 CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str(" ab,cd"), &u16, 16, "'")); |
1138 CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str("ab,cd"), &u16, 16, "'")); |
1139 CX_TEST_ASSERT(errno == EINVAL); |
1139 CX_TEST_ASSERT(errno == EINVAL); |
1140 errno = 0; |
1140 errno = 0; |
1141 CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str(" ab'cd"), &u16, 16, "'")); |
1141 CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str("ab'cd"), &u16, 16, "'")); |
1142 CX_TEST_ASSERT(errno == 0); |
1142 CX_TEST_ASSERT(errno == 0); |
1143 CX_TEST_ASSERT(u16 == 0xabcd); |
1143 CX_TEST_ASSERT(u16 == 0xabcd); |
1144 |
1144 |
1145 // binary |
1145 // binary |
1146 errno = 0; |
1146 errno = 0; |
1147 CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " ")); |
1147 CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " ")); |
1148 CX_TEST_ASSERT(errno == ERANGE); |
1148 CX_TEST_ASSERT(errno == ERANGE); |
1149 errno = 0; |
1149 errno = 0; |
1150 CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str(" 1010 1011"), &u8, 2, " ")); |
1150 CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str("1010 1011"), &u8, 2, " ")); |
1151 CX_TEST_ASSERT(errno == 0); |
1151 CX_TEST_ASSERT(errno == 0); |
1152 CX_TEST_ASSERT(u8 == 0xAB); |
1152 CX_TEST_ASSERT(u8 == 0xAB); |
1153 } |
1153 } |
1154 } |
1154 } |
1155 |
1155 |
1221 |
1221 |
1222 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', ".")); |
1222 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', ".")); |
1223 CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d)); |
1223 CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d)); |
1224 |
1224 |
1225 // TODO: test and improve support for big numbers, precision, and out-of-range detection |
1225 // TODO: test and improve support for big numbers, precision, and out-of-range detection |
|
1226 } |
|
1227 } |
|
1228 |
|
1229 CX_TEST(test_string_to_number_notrim) { |
|
1230 long long i; |
|
1231 unsigned long long u; |
|
1232 float f; |
|
1233 double d; |
|
1234 CX_TEST_DO { |
|
1235 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("-42 "), &i, 10)); |
|
1236 CX_TEST_ASSERT(0 != cx_strtoll(cx_str(" -42"), &i, 10)); |
|
1237 CX_TEST_ASSERT(0 == cx_strtoll(cx_str("-42"), &i, 10)); |
|
1238 CX_TEST_ASSERT(i == -42); |
|
1239 |
|
1240 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("42 "), &u, 10)); |
|
1241 CX_TEST_ASSERT(0 != cx_strtoull(cx_str(" 42"), &u, 10)); |
|
1242 CX_TEST_ASSERT(0 == cx_strtoull(cx_str("42"), &u, 10)); |
|
1243 CX_TEST_ASSERT(u == 42); |
|
1244 |
|
1245 CX_TEST_ASSERT(0 != cx_strtof(cx_str("13.37 "), &f)); |
|
1246 CX_TEST_ASSERT(0 != cx_strtof(cx_str(" 13.37"), &f)); |
|
1247 CX_TEST_ASSERT(0 == cx_strtof(cx_str("13.37"), &f)); |
|
1248 CX_TEST_ASSERT(0 == cx_vcmp_float(f, 13.37f)); |
|
1249 |
|
1250 CX_TEST_ASSERT(0 != cx_strtod(cx_str("13.37 "), &d)); |
|
1251 CX_TEST_ASSERT(0 != cx_strtod(cx_str(" 13.37"), &d)); |
|
1252 CX_TEST_ASSERT(0 == cx_strtod(cx_str("13.37"), &d)); |
|
1253 CX_TEST_ASSERT(0 == cx_vcmp_double(d, 13.37)); |
1226 } |
1254 } |
1227 } |
1255 } |
1228 |
1256 |
1229 CxTestSuite *cx_test_suite_string(void) { |
1257 CxTestSuite *cx_test_suite_string(void) { |
1230 CxTestSuite *suite = cx_test_suite_new("string"); |
1258 CxTestSuite *suite = cx_test_suite_new("string"); |
1266 |
1294 |
1267 cx_test_register(suite, test_string_to_signed_integer); |
1295 cx_test_register(suite, test_string_to_signed_integer); |
1268 cx_test_register(suite, test_string_to_unsigned_integer); |
1296 cx_test_register(suite, test_string_to_unsigned_integer); |
1269 cx_test_register(suite, test_string_to_float); |
1297 cx_test_register(suite, test_string_to_float); |
1270 cx_test_register(suite, test_string_to_double); |
1298 cx_test_register(suite, test_string_to_double); |
|
1299 cx_test_register(suite, test_string_to_number_notrim); |
1271 |
1300 |
1272 return suite; |
1301 return suite; |
1273 } |
1302 } |