983 test_strtoint_impl(LL, num, base, l, LONG_MIN, LONG_MAX); \ |
983 test_strtoint_impl(LL, num, base, l, LONG_MIN, LONG_MAX); \ |
984 test_strtoint_impl(LL, num, base, ll, LLONG_MIN, LLONG_MAX); \ |
984 test_strtoint_impl(LL, num, base, ll, LLONG_MIN, LLONG_MAX); \ |
985 test_strtoint_impl(LL, num, base, i8, INT8_MIN, INT8_MAX); \ |
985 test_strtoint_impl(LL, num, base, i8, INT8_MIN, INT8_MAX); \ |
986 test_strtoint_impl(LL, num, base, i16, INT16_MIN, INT16_MAX); \ |
986 test_strtoint_impl(LL, num, base, i16, INT16_MIN, INT16_MAX); \ |
987 test_strtoint_impl(LL, num, base, i32, INT32_MIN, INT32_MAX); \ |
987 test_strtoint_impl(LL, num, base, i32, INT32_MIN, INT32_MAX); \ |
988 test_strtoint_impl(LL, num, base, i64, INT64_MIN, INT64_MAX); \ |
988 test_strtoint_impl(LL, num, base, i64, INT64_MIN, INT64_MAX); |
989 test_strtoint_impl(LL, num, base, z, -SSIZE_MAX-1, SSIZE_MAX) |
|
990 |
989 |
991 #define test_strtoint_rollout_signed(num, base) \ |
990 #define test_strtoint_rollout_signed(num, base) \ |
992 test_strtoint_rollout_signed_impl(num, base); \ |
991 test_strtoint_rollout_signed_impl(num, base); \ |
993 test_strtoint_rollout_signed_impl(-num, base) |
992 test_strtoint_rollout_signed_impl(-num, base) |
994 |
993 |
999 test_strtoint_impl(ULL, num, base, ull, 0, ULLONG_MAX); \ |
998 test_strtoint_impl(ULL, num, base, ull, 0, ULLONG_MAX); \ |
1000 test_strtoint_impl(ULL, num, base, u8, 0, UINT8_MAX); \ |
999 test_strtoint_impl(ULL, num, base, u8, 0, UINT8_MAX); \ |
1001 test_strtoint_impl(ULL, num, base, u16, 0, UINT16_MAX); \ |
1000 test_strtoint_impl(ULL, num, base, u16, 0, UINT16_MAX); \ |
1002 test_strtoint_impl(ULL, num, base, u32, 0, UINT32_MAX); \ |
1001 test_strtoint_impl(ULL, num, base, u32, 0, UINT32_MAX); \ |
1003 test_strtoint_impl(ULL, num, base, u64, 0, UINT64_MAX); \ |
1002 test_strtoint_impl(ULL, num, base, u64, 0, UINT64_MAX); \ |
1004 test_strtoint_impl(ULL, num, base, uz, 0, SIZE_MAX) |
1003 test_strtoint_impl(ULL, num, base, z, 0, SIZE_MAX) |
1005 |
1004 |
1006 CX_TEST(test_string_to_signed_integer) { |
1005 CX_TEST(test_string_to_signed_integer) { |
1007 short s; |
1006 short s; |
1008 int i; |
1007 int i; |
1009 long l; |
1008 long l; |
1010 long long ll; |
1009 long long ll; |
1011 int8_t i8; |
1010 int8_t i8; |
1012 int16_t i16; |
1011 int16_t i16; |
1013 int32_t i32; |
1012 int32_t i32; |
1014 int64_t i64; |
1013 int64_t i64; |
1015 ssize_t z; |
|
1016 CX_TEST_DO { |
1014 CX_TEST_DO { |
1017 // do some brute force tests with all ranges |
1015 // do some brute force tests with all ranges |
1018 test_strtoint_rollout_signed(47, 10); |
1016 test_strtoint_rollout_signed(47, 10); |
1019 test_strtoint_rollout_signed(210, 10); |
1017 test_strtoint_rollout_signed(210, 10); |
1020 test_strtoint_rollout_signed(5678, 10); |
1018 test_strtoint_rollout_signed(5678, 10); |
1062 CX_TEST_ASSERT(0 == cx_strtoi64(cx_str("-X8000000000000000"), &i64, 16)); |
1060 CX_TEST_ASSERT(0 == cx_strtoi64(cx_str("-X8000000000000000"), &i64, 16)); |
1063 CX_TEST_ASSERT(errno == 0); |
1061 CX_TEST_ASSERT(errno == 0); |
1064 CX_TEST_ASSERT(i64 == INT64_MIN); |
1062 CX_TEST_ASSERT(i64 == INT64_MIN); |
1065 |
1063 |
1066 // group separators |
1064 // group separators |
1067 CX_TEST_ASSERT(0 == cx_strtoi32(cx_str(" -123,456"), &i32, 10)); |
1065 CX_TEST_ASSERT(0 == cx_strtoi32(cx_str("-123,456"), &i32, 10)); |
1068 CX_TEST_ASSERT(i32 == -123456); |
1066 CX_TEST_ASSERT(i32 == -123456); |
1069 errno = 0; |
1067 errno = 0; |
1070 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str(" -Xab,cd"), &i16, 16, "'")); |
1068 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-Xab,cd"), &i16, 16, "'")); |
1071 CX_TEST_ASSERT(errno == EINVAL); |
1069 CX_TEST_ASSERT(errno == EINVAL); |
1072 errno = 0; |
1070 errno = 0; |
1073 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str(" -X'ab'cd"), &i16, 16, "'")); |
1071 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-X'ab'cd"), &i16, 16, "'")); |
1074 CX_TEST_ASSERT(errno == ERANGE); |
1072 CX_TEST_ASSERT(errno == ERANGE); |
1075 errno = 0; |
1073 errno = 0; |
1076 CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str(" -X'67'89"), &i16, 16, "'")); |
1074 CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str("-X'67'89"), &i16, 16, "'")); |
1077 CX_TEST_ASSERT(errno == 0); |
1075 CX_TEST_ASSERT(errno == 0); |
1078 CX_TEST_ASSERT(i16 == -0x6789); |
1076 CX_TEST_ASSERT(i16 == -0x6789); |
1079 |
1077 |
1080 // binary and (unusual notation of) signed binary |
1078 // binary and (unusual notation of) signed binary |
1081 errno = 0; |
1079 errno = 0; |
1082 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" -1010 1011"), &i8, 2, " ")); |
1080 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("-1010 1011"), &i8, 2, " ")); |
1083 CX_TEST_ASSERT(errno == ERANGE); |
1081 CX_TEST_ASSERT(errno == ERANGE); |
1084 errno = 0; |
1082 errno = 0; |
1085 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" 1010 1011"), &i8, 2, " ")); |
1083 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("1010 1011"), &i8, 2, " ")); |
1086 CX_TEST_ASSERT(errno == ERANGE); |
1084 CX_TEST_ASSERT(errno == ERANGE); |
1087 errno = 0; |
1085 errno = 0; |
1088 CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str(" -0101 0101"), &i8, 2, " ")); |
1086 CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str("-0101 0101"), &i8, 2, " ")); |
1089 CX_TEST_ASSERT(errno == 0); |
1087 CX_TEST_ASSERT(errno == 0); |
1090 CX_TEST_ASSERT(i8 == -0x55); |
1088 CX_TEST_ASSERT(i8 == -0x55); |
1091 } |
1089 } |
1092 } |
1090 } |
1093 |
1091 |
1130 |
1128 |
1131 // do some special case tests |
1129 // do some special case tests |
1132 // -------------------------- |
1130 // -------------------------- |
1133 |
1131 |
1134 // group separators |
1132 // group separators |
1135 CX_TEST_ASSERT(0 == cx_strtou32(cx_str(" 123,456"), &u32, 10)); |
1133 CX_TEST_ASSERT(0 == cx_strtou32(cx_str("123,456"), &u32, 10)); |
1136 CX_TEST_ASSERT(u32 == 123456); |
1134 CX_TEST_ASSERT(u32 == 123456); |
1137 errno = 0; |
1135 errno = 0; |
1138 CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str(" ab,cd"), &u16, 16, "'")); |
1136 CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str("ab,cd"), &u16, 16, "'")); |
1139 CX_TEST_ASSERT(errno == EINVAL); |
1137 CX_TEST_ASSERT(errno == EINVAL); |
1140 errno = 0; |
1138 errno = 0; |
1141 CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str(" ab'cd"), &u16, 16, "'")); |
1139 CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str("ab'cd"), &u16, 16, "'")); |
1142 CX_TEST_ASSERT(errno == 0); |
1140 CX_TEST_ASSERT(errno == 0); |
1143 CX_TEST_ASSERT(u16 == 0xabcd); |
1141 CX_TEST_ASSERT(u16 == 0xabcd); |
1144 |
1142 |
1145 // binary |
1143 // binary |
1146 errno = 0; |
1144 errno = 0; |
1147 CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " ")); |
1145 CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " ")); |
1148 CX_TEST_ASSERT(errno == ERANGE); |
1146 CX_TEST_ASSERT(errno == ERANGE); |
1149 errno = 0; |
1147 errno = 0; |
1150 CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str(" 1010 1011"), &u8, 2, " ")); |
1148 CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str("1010 1011"), &u8, 2, " ")); |
1151 CX_TEST_ASSERT(errno == 0); |
1149 CX_TEST_ASSERT(errno == 0); |
1152 CX_TEST_ASSERT(u8 == 0xAB); |
1150 CX_TEST_ASSERT(u8 == 0xAB); |
1153 } |
1151 } |
1154 } |
1152 } |
1155 |
1153 |
1221 |
1219 |
1222 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', ".")); |
1220 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', ".")); |
1223 CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d)); |
1221 CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d)); |
1224 |
1222 |
1225 // TODO: test and improve support for big numbers, precision, and out-of-range detection |
1223 // TODO: test and improve support for big numbers, precision, and out-of-range detection |
|
1224 } |
|
1225 } |
|
1226 |
|
1227 CX_TEST(test_string_to_number_notrim) { |
|
1228 long long i; |
|
1229 unsigned long long u; |
|
1230 float f; |
|
1231 double d; |
|
1232 CX_TEST_DO { |
|
1233 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("-42 "), &i, 10)); |
|
1234 CX_TEST_ASSERT(0 != cx_strtoll(cx_str(" -42"), &i, 10)); |
|
1235 CX_TEST_ASSERT(0 == cx_strtoll(cx_str("-42"), &i, 10)); |
|
1236 CX_TEST_ASSERT(i == -42); |
|
1237 |
|
1238 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("42 "), &u, 10)); |
|
1239 CX_TEST_ASSERT(0 != cx_strtoull(cx_str(" 42"), &u, 10)); |
|
1240 CX_TEST_ASSERT(0 == cx_strtoull(cx_str("42"), &u, 10)); |
|
1241 CX_TEST_ASSERT(u == 42); |
|
1242 |
|
1243 CX_TEST_ASSERT(0 != cx_strtof(cx_str("13.37 "), &f)); |
|
1244 CX_TEST_ASSERT(0 != cx_strtof(cx_str(" 13.37"), &f)); |
|
1245 CX_TEST_ASSERT(0 == cx_strtof(cx_str("13.37"), &f)); |
|
1246 CX_TEST_ASSERT(0 == cx_vcmp_float(f, 13.37f)); |
|
1247 |
|
1248 CX_TEST_ASSERT(0 != cx_strtod(cx_str("13.37 "), &d)); |
|
1249 CX_TEST_ASSERT(0 != cx_strtod(cx_str(" 13.37"), &d)); |
|
1250 CX_TEST_ASSERT(0 == cx_strtod(cx_str("13.37"), &d)); |
|
1251 CX_TEST_ASSERT(0 == cx_vcmp_double(d, 13.37)); |
1226 } |
1252 } |
1227 } |
1253 } |
1228 |
1254 |
1229 CxTestSuite *cx_test_suite_string(void) { |
1255 CxTestSuite *cx_test_suite_string(void) { |
1230 CxTestSuite *suite = cx_test_suite_new("string"); |
1256 CxTestSuite *suite = cx_test_suite_new("string"); |
1266 |
1292 |
1267 cx_test_register(suite, test_string_to_signed_integer); |
1293 cx_test_register(suite, test_string_to_signed_integer); |
1268 cx_test_register(suite, test_string_to_unsigned_integer); |
1294 cx_test_register(suite, test_string_to_unsigned_integer); |
1269 cx_test_register(suite, test_string_to_float); |
1295 cx_test_register(suite, test_string_to_float); |
1270 cx_test_register(suite, test_string_to_double); |
1296 cx_test_register(suite, test_string_to_double); |
|
1297 cx_test_register(suite, test_string_to_number_notrim); |
1271 |
1298 |
1272 return suite; |
1299 return suite; |
1273 } |
1300 } |