tests/test_string.c

changeset 1063
e453e717876e
parent 1061
c7d23892eab5
equal deleted inserted replaced
1062:8baed9b38bc6 1063:e453e717876e
28 28
29 #include "cx/test.h" 29 #include "cx/test.h"
30 #include "util_allocator.h" 30 #include "util_allocator.h"
31 31
32 #include "cx/string.h" 32 #include "cx/string.h"
33 #include "cx/compare.h"
33 34
34 #include <limits.h> 35 #include <limits.h>
35 #include <errno.h> 36 #include <errno.h>
36 37
37 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \ 38 #define ASSERT_ZERO_TERMINATED(str) CX_TEST_ASSERTM((str).ptr[(str).length] == '\0', \
1165 1166
1166 CX_TEST(test_string_to_float) { 1167 CX_TEST(test_string_to_float) {
1167 float f; 1168 float f;
1168 CX_TEST_DO { 1169 CX_TEST_DO {
1169 CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f)); 1170 CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f));
1170 CX_TEST_ASSERT(11.3f == f); 1171 CX_TEST_ASSERT(0 == cx_vcmp_float(11.3f, f));
1172
1173 CX_TEST_ASSERT(0 == cx_strtof(cx_str("-4.711e+1"), &f));
1174 CX_TEST_ASSERT(0 == cx_vcmp_float(-47.11f, f));
1171 1175
1172 CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.67262192595e-27"), &f)); 1176 CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.67262192595e-27"), &f));
1173 CX_TEST_ASSERT(1.67262192595e-27f == f); 1177 CX_TEST_ASSERT(0 == cx_vcmp_float(1.67262192595e-27f, f));
1174 1178
1175 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, '.', ",")); 1179 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, '.', ","));
1176 CX_TEST_ASSERT(138339.4f == f); 1180 CX_TEST_ASSERT(0 == cx_vcmp_float(138339.4f, f));
1177 1181
1178 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, ',', ".")); 1182 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, ',', "."));
1179 CX_TEST_ASSERT(138.3394f == f); 1183 CX_TEST_ASSERT(0 == cx_vcmp_float(138.3394f, f));
1184
1185 errno = 0;
1186 CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e"), &f));
1187 CX_TEST_ASSERT(errno == EINVAL);
1188 errno = 0;
1189 CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e+"), &f));
1190 CX_TEST_ASSERT(errno == EINVAL);
1191 errno = 0;
1192 CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e-"), &f));
1193 CX_TEST_ASSERT(errno == EINVAL);
1194 CX_TEST_ASSERT(0 == cx_strtof(cx_str("15e-0"), &f));
1195 CX_TEST_ASSERT(0 == cx_vcmp_float(15.f, f));
1196
1197 CX_TEST_ASSERT(0 == cx_strtof(cx_str("3e38"), &f));
1198 CX_TEST_ASSERT(0 == cx_vcmp_float(3e38f, f));
1199 errno = 0;
1200 CX_TEST_ASSERT(0 != cx_strtof(cx_str("3e39"), &f));
1201 CX_TEST_ASSERT(errno == ERANGE);
1202 CX_TEST_ASSERT(0 == cx_strtof(cx_str("-3e38"), &f));
1203 CX_TEST_ASSERT(0 == cx_vcmp_float(-3e38f, f));
1204 errno = 0;
1205 CX_TEST_ASSERT(0 != cx_strtof(cx_str("-3e39"), &f));
1206 CX_TEST_ASSERT(errno == ERANGE);
1207 CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.18e-38"), &f));
1208 CX_TEST_ASSERT(0 == cx_vcmp_float(1.18e-38f, f));
1209 errno = 0;
1210 CX_TEST_ASSERT(0 != cx_strtof(cx_str("1.17e-38"), &f));
1211 CX_TEST_ASSERT(errno == ERANGE);
1180 } 1212 }
1181 } 1213 }
1182 1214
1183 CX_TEST(test_string_to_double) { 1215 CX_TEST(test_string_to_double) {
1184 double d; 1216 double d;
1185 CX_TEST_DO { 1217 CX_TEST_DO {
1186 CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d)); 1218 CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d));
1187 CX_TEST_ASSERT(11.3 == d); 1219 CX_TEST_ASSERT(0 == cx_vcmp_double(11.3, d));
1220
1221 CX_TEST_ASSERT(0 == cx_strtod(cx_str("-13.37"), &d));
1222 CX_TEST_ASSERT(0 == cx_vcmp_double(-13.37, d));
1223
1224 CX_TEST_ASSERT(0 == cx_strtod(cx_str("-4.711e+1"), &d));
1225 CX_TEST_ASSERT(0 == cx_vcmp_double(-47.11, d));
1188 1226
1189 CX_TEST_ASSERT(0 == cx_strtod(cx_str("1.67262192595e-27"), &d)); 1227 CX_TEST_ASSERT(0 == cx_strtod(cx_str("1.67262192595e-27"), &d));
1190 CX_TEST_ASSERT(1.67262192595e-27 == d); 1228 CX_TEST_ASSERT(0 == cx_vcmp_double(1.67262192595e-27, d));
1191 1229
1192 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, '.', ",")); 1230 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, '.', ","));
1193 CX_TEST_ASSERT(138339.4 == d); 1231 CX_TEST_ASSERT(0 == cx_vcmp_double(138339.4, d));
1194 1232
1195 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', ".")); 1233 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', "."));
1196 CX_TEST_ASSERT(138.3394 == d); 1234 CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d));
1197 } 1235
1198 } 1236 // TODO: test and improve support for big numbers, precision, and out-of-range detection
1199
1200 CX_TEST(test_string_to_float_german) {
1201 float f;
1202 CX_TEST_DO {
1203 // TODO: implement
1204 (void)f;
1205 } 1237 }
1206 } 1238 }
1207 1239
1208 CxTestSuite *cx_test_suite_string(void) { 1240 CxTestSuite *cx_test_suite_string(void) {
1209 CxTestSuite *suite = cx_test_suite_new("string"); 1241 CxTestSuite *suite = cx_test_suite_new("string");
1246 1278
1247 cx_test_register(suite, test_string_to_signed_integer); 1279 cx_test_register(suite, test_string_to_signed_integer);
1248 cx_test_register(suite, test_string_to_unsigned_integer); 1280 cx_test_register(suite, test_string_to_unsigned_integer);
1249 cx_test_register(suite, test_string_to_float); 1281 cx_test_register(suite, test_string_to_float);
1250 cx_test_register(suite, test_string_to_double); 1282 cx_test_register(suite, test_string_to_double);
1251 cx_test_register(suite, test_string_to_float_german);
1252 1283
1253 return suite; 1284 return suite;
1254 } 1285 }

mercurial