| 856 |
858 |
| 857 CX_TEST(test_strprefix) { |
859 CX_TEST(test_strprefix) { |
| 858 cxstring str = cx_str("test my prefix and my suffix"); |
860 cxstring str = cx_str("test my prefix and my suffix"); |
| 859 cxstring empty = cx_str(""); |
861 cxstring empty = cx_str(""); |
| 860 CX_TEST_DO { |
862 CX_TEST_DO { |
| 861 CX_TEST_ASSERT(!cx_strprefix(empty, cx_str("pref"))); |
863 CX_TEST_ASSERT(!cx_strprefix(empty, "pref")); |
| 862 CX_TEST_ASSERT(cx_strprefix(str, empty)); |
864 CX_TEST_ASSERT(cx_strprefix(str, "")); |
| 863 CX_TEST_ASSERT(cx_strprefix(empty, empty)); |
865 CX_TEST_ASSERT(cx_strprefix(empty, "")); |
| 864 CX_TEST_ASSERT(cx_strprefix(str, cx_str("test "))); |
866 CX_TEST_ASSERT(cx_strprefix(str, "test ")); |
| 865 CX_TEST_ASSERT(!cx_strprefix(str, cx_str("8-) fsck "))); |
867 CX_TEST_ASSERT(!cx_strprefix(str, "8-) fsck ")); |
| 866 } |
868 } |
| 867 } |
869 } |
| 868 |
870 |
| 869 CX_TEST(test_strsuffix) { |
871 CX_TEST(test_strsuffix) { |
| 870 cxstring str = cx_str("test my prefix and my suffix"); |
872 cxstring str = cx_str("test my prefix and my suffix"); |
| 871 cxstring empty = cx_str(""); |
873 cxstring empty = cx_str(""); |
| 872 CX_TEST_DO { |
874 CX_TEST_DO { |
| 873 CX_TEST_ASSERT(!cx_strsuffix(empty, cx_str("suf"))); |
875 CX_TEST_ASSERT(!cx_strsuffix(empty, "suf")); |
| 874 CX_TEST_ASSERT(cx_strsuffix(str, empty)); |
876 CX_TEST_ASSERT(cx_strsuffix(str, "")); |
| 875 CX_TEST_ASSERT(cx_strsuffix(empty, empty)); |
877 CX_TEST_ASSERT(cx_strsuffix(empty, "")); |
| 876 CX_TEST_ASSERT(cx_strsuffix(str, cx_str("fix"))); |
878 CX_TEST_ASSERT(cx_strsuffix(str, "fix")); |
| 877 CX_TEST_ASSERT(!cx_strsuffix(str, cx_str("fox"))); |
879 CX_TEST_ASSERT(!cx_strsuffix(str, "fox")); |
| 878 } |
880 } |
| 879 } |
881 } |
| 880 |
882 |
| 881 CX_TEST(test_strcaseprefix) { |
883 CX_TEST(test_strcaseprefix) { |
| 882 cxstring str = cx_str("test my prefix and my suffix"); |
884 cxstring str = cx_str("test my prefix and my suffix"); |
| 883 cxstring empty = cx_str(""); |
885 cxstring empty = cx_str(""); |
| 884 CX_TEST_DO { |
886 CX_TEST_DO { |
| 885 CX_TEST_ASSERT(!cx_strcaseprefix(empty, cx_str("pREf"))); |
887 CX_TEST_ASSERT(!cx_strcaseprefix(empty, "pREf")); |
| 886 CX_TEST_ASSERT(cx_strcaseprefix(str, empty)); |
888 CX_TEST_ASSERT(cx_strcaseprefix(str, "")); |
| 887 CX_TEST_ASSERT(cx_strcaseprefix(empty, empty)); |
889 CX_TEST_ASSERT(cx_strcaseprefix(empty, "")); |
| 888 CX_TEST_ASSERT(cx_strcaseprefix(str, cx_str("TEST "))); |
890 CX_TEST_ASSERT(cx_strcaseprefix(str, "TEST ")); |
| 889 CX_TEST_ASSERT(!cx_strcaseprefix(str, cx_str("8-) fsck "))); |
891 CX_TEST_ASSERT(!cx_strcaseprefix(str, "8-) fsck ")); |
| 890 } |
892 } |
| 891 } |
893 } |
| 892 |
894 |
| 893 CX_TEST(test_strcasesuffix) { |
895 CX_TEST(test_strcasesuffix) { |
| 894 cxstring str = cx_str("test my prefix and my suffix"); |
896 cxstring str = cx_str("test my prefix and my suffix"); |
| 895 cxstring empty = cx_str(""); |
897 cxstring empty = cx_str(""); |
| 896 CX_TEST_DO { |
898 CX_TEST_DO { |
| 897 CX_TEST_ASSERT(!cx_strcasesuffix(empty, cx_str("sUf"))); |
899 CX_TEST_ASSERT(!cx_strcasesuffix(empty, "sUf")); |
| 898 CX_TEST_ASSERT(cx_strcasesuffix(str, empty)); |
900 CX_TEST_ASSERT(cx_strcasesuffix(str, "")); |
| 899 CX_TEST_ASSERT(cx_strcasesuffix(empty, empty)); |
901 CX_TEST_ASSERT(cx_strcasesuffix(empty, "")); |
| 900 CX_TEST_ASSERT(cx_strcasesuffix(str, cx_str("FIX"))); |
902 CX_TEST_ASSERT(cx_strcasesuffix(str, "FIX")); |
| 901 CX_TEST_ASSERT(!cx_strcasesuffix(str, cx_str("fox"))); |
903 CX_TEST_ASSERT(!cx_strcasesuffix(str, "fox")); |
| 902 } |
904 } |
| 903 } |
905 } |
| 904 |
906 |
| 905 CX_TEST(test_strreplace) { |
907 CX_TEST(test_strreplace) { |
| 906 CxTestingAllocator talloc; |
908 CxTestingAllocator talloc; |
| 1095 bool ret; |
1097 bool ret; |
| 1096 cxstring tok; |
1098 cxstring tok; |
| 1097 |
1099 |
| 1098 ret = cx_strtok_next(&ctx, &tok); |
1100 ret = cx_strtok_next(&ctx, &tok); |
| 1099 CX_TEST_ASSERT(ret); |
1101 CX_TEST_ASSERT(ret); |
| 1100 CX_TEST_ASSERT(0 == cx_strcmp(tok, cx_str("some"))); |
1102 CX_TEST_ASSERT(0 == cx_strcmp(tok, "some")); |
| 1101 CX_TEST_ASSERT(ctx.pos == 0); |
1103 CX_TEST_ASSERT(ctx.pos == 0); |
| 1102 CX_TEST_ASSERT(ctx.next_pos == 7); |
1104 CX_TEST_ASSERT(ctx.next_pos == 7); |
| 1103 CX_TEST_ASSERT(ctx.delim_pos == 4); |
1105 CX_TEST_ASSERT(ctx.delim_pos == 4); |
| 1104 CX_TEST_ASSERT(ctx.found == 1); |
1106 CX_TEST_ASSERT(ctx.found == 1); |
| 1105 |
1107 |
| 1106 ret = cx_strtok_next(&ctx, &tok); |
1108 ret = cx_strtok_next(&ctx, &tok); |
| 1107 CX_TEST_ASSERT(ret); |
1109 CX_TEST_ASSERT(ret); |
| 1108 CX_TEST_ASSERT(0 == cx_strcmp(tok, cx_str("otherwise"))); |
1110 CX_TEST_ASSERT(0 == cx_strcmp(tok, "otherwise")); |
| 1109 CX_TEST_ASSERT(ctx.pos == 7); |
1111 CX_TEST_ASSERT(ctx.pos == 7); |
| 1110 CX_TEST_ASSERT(ctx.next_pos == 19); |
1112 CX_TEST_ASSERT(ctx.next_pos == 19); |
| 1111 CX_TEST_ASSERT(ctx.delim_pos == 16); |
1113 CX_TEST_ASSERT(ctx.delim_pos == 16); |
| 1112 CX_TEST_ASSERT(ctx.found == 2); |
1114 CX_TEST_ASSERT(ctx.found == 2); |
| 1113 |
1115 |
| 1114 ret = cx_strtok_next(&ctx, &tok); |
1116 ret = cx_strtok_next(&ctx, &tok); |
| 1115 CX_TEST_ASSERT(ret); |
1117 CX_TEST_ASSERT(ret); |
| 1116 CX_TEST_ASSERT(0 == cx_strcmp(tok, cx_str("separated"))); |
1118 CX_TEST_ASSERT(0 == cx_strcmp(tok, "separated")); |
| 1117 CX_TEST_ASSERT(ctx.pos == 19); |
1119 CX_TEST_ASSERT(ctx.pos == 19); |
| 1118 CX_TEST_ASSERT(ctx.next_pos == 31); |
1120 CX_TEST_ASSERT(ctx.next_pos == 31); |
| 1119 CX_TEST_ASSERT(ctx.delim_pos == 28); |
1121 CX_TEST_ASSERT(ctx.delim_pos == 28); |
| 1120 CX_TEST_ASSERT(ctx.found == 3); |
1122 CX_TEST_ASSERT(ctx.found == 3); |
| 1121 |
1123 |
| 1122 ret = cx_strtok_next(&ctx, &tok); |
1124 ret = cx_strtok_next(&ctx, &tok); |
| 1123 CX_TEST_ASSERT(ret); |
1125 CX_TEST_ASSERT(ret); |
| 1124 CX_TEST_ASSERT(0 == cx_strcmp(tok, cx_str("string"))); |
1126 CX_TEST_ASSERT(0 == cx_strcmp(tok, "string")); |
| 1125 CX_TEST_ASSERT(ctx.pos == 31); |
1127 CX_TEST_ASSERT(ctx.pos == 31); |
| 1126 CX_TEST_ASSERT(ctx.next_pos == 40); |
1128 CX_TEST_ASSERT(ctx.next_pos == 40); |
| 1127 CX_TEST_ASSERT(ctx.delim_pos == 37); |
1129 CX_TEST_ASSERT(ctx.delim_pos == 37); |
| 1128 CX_TEST_ASSERT(ctx.found == 4); |
1130 CX_TEST_ASSERT(ctx.found == 4); |
| 1129 |
1131 |
| 1130 ret = cx_strtok_next(&ctx, &tok); |
1132 ret = cx_strtok_next(&ctx, &tok); |
| 1131 CX_TEST_ASSERT(ret); |
1133 CX_TEST_ASSERT(ret); |
| 1132 CX_TEST_ASSERT(0 == cx_strcmp(tok, cx_str(""))); |
1134 CX_TEST_ASSERT(0 == cx_strcmp(tok, "")); |
| 1133 CX_TEST_ASSERT(ctx.pos == 40); |
1135 CX_TEST_ASSERT(ctx.pos == 40); |
| 1134 CX_TEST_ASSERT(ctx.next_pos == 40); |
1136 CX_TEST_ASSERT(ctx.next_pos == 40); |
| 1135 CX_TEST_ASSERT(ctx.delim_pos == 40); |
1137 CX_TEST_ASSERT(ctx.delim_pos == 40); |
| 1136 CX_TEST_ASSERT(ctx.found == 5); |
1138 CX_TEST_ASSERT(ctx.found == 5); |
| 1137 |
1139 |
| 1300 // do some special case tests |
1302 // do some special case tests |
| 1301 // -------------------------- |
1303 // -------------------------- |
| 1302 |
1304 |
| 1303 // can fit only in unsigned long long |
1305 // can fit only in unsigned long long |
| 1304 errno = 0; |
1306 errno = 0; |
| 1305 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("0x8df9CE03AbC90815"), &ll, 16)); |
1307 CX_TEST_ASSERT(0 != cx_strtoll("0x8df9CE03AbC90815", &ll, 16)); |
| 1306 CX_TEST_ASSERT(errno == ERANGE); |
1308 CX_TEST_ASSERT(errno == ERANGE); |
| 1307 |
1309 |
| 1308 // negative overflow (we only test for 64 bit long long) |
1310 // negative overflow (we only test for 64 bit long long) |
| 1309 #if LLONG_MAX == 9223372036854775807ll |
1311 #if LLONG_MAX == 9223372036854775807ll |
| 1310 errno = 0; |
1312 errno = 0; |
| 1311 CX_TEST_ASSERT(0 == cx_strtoll(cx_str("-9223372036854775808"), &ll, 10)); |
1313 CX_TEST_ASSERT(0 == cx_strtoll("-9223372036854775808", &ll, 10)); |
| 1312 CX_TEST_ASSERT(ll == LLONG_MIN); |
1314 CX_TEST_ASSERT(ll == LLONG_MIN); |
| 1313 CX_TEST_ASSERT(errno == 0); |
1315 CX_TEST_ASSERT(errno == 0); |
| 1314 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("-9223372036854775809"), &ll, 10)); |
1316 CX_TEST_ASSERT(0 != cx_strtoll("-9223372036854775809", &ll, 10)); |
| 1315 CX_TEST_ASSERT(errno == ERANGE); |
1317 CX_TEST_ASSERT(errno == ERANGE); |
| 1316 #endif |
1318 #endif |
| 1317 |
1319 |
| 1318 // edge case: empty and NULL string |
1320 // edge case: empty and NULL string |
| 1319 errno = 0; |
1321 errno = 0; |
| 1320 CX_TEST_ASSERT(0 != cx_strtoll(cx_str(""), &ll, 16)); |
1322 CX_TEST_ASSERT(0 != cx_strtoll("", &ll, 16)); |
| 1321 CX_TEST_ASSERT(errno == EINVAL); |
1323 CX_TEST_ASSERT(errno == EINVAL); |
| 1322 errno = 0; |
1324 errno = 0; |
| 1323 CX_TEST_ASSERT(0 != cx_strtoll(cx_str(NULL), &ll, 16)); |
1325 CX_TEST_ASSERT(0 != cx_strtoll(CX_NULLSTR, &ll, 16)); |
| 1324 CX_TEST_ASSERT(errno == EINVAL); |
1326 CX_TEST_ASSERT(errno == EINVAL); |
| 1325 |
1327 |
| 1326 // edge case: unsupported base |
1328 // edge case: unsupported base |
| 1327 errno = 0; |
1329 errno = 0; |
| 1328 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("7"), &ll, 12)); |
1330 CX_TEST_ASSERT(0 != cx_strtoll("7", &ll, 12)); |
| 1329 CX_TEST_ASSERT(errno == EINVAL); |
1331 CX_TEST_ASSERT(errno == EINVAL); |
| 1330 |
1332 |
| 1331 // edge case: incorrect sign characters |
1333 // edge case: incorrect sign characters |
| 1332 errno = 0; |
1334 errno = 0; |
| 1333 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("-"), &ll, 10)); |
1335 CX_TEST_ASSERT(0 != cx_strtoll("-", &ll, 10)); |
| 1334 CX_TEST_ASSERT(errno == EINVAL); |
1336 CX_TEST_ASSERT(errno == EINVAL); |
| 1335 errno = 0; |
1337 errno = 0; |
| 1336 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("+"), &ll, 10)); |
1338 CX_TEST_ASSERT(0 != cx_strtoll("+", &ll, 10)); |
| 1337 CX_TEST_ASSERT(errno == EINVAL); |
1339 CX_TEST_ASSERT(errno == EINVAL); |
| 1338 errno = 0; |
1340 errno = 0; |
| 1339 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("-+15"), &ll, 10)); |
1341 CX_TEST_ASSERT(0 != cx_strtoll("-+15", &ll, 10)); |
| 1340 CX_TEST_ASSERT(errno == EINVAL); |
1342 CX_TEST_ASSERT(errno == EINVAL); |
| 1341 errno = 0; |
1343 errno = 0; |
| 1342 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("--15"), &ll, 10)); |
1344 CX_TEST_ASSERT(0 != cx_strtoll("--15", &ll, 10)); |
| 1343 CX_TEST_ASSERT(errno == EINVAL); |
1345 CX_TEST_ASSERT(errno == EINVAL); |
| 1344 errno = 0; |
1346 errno = 0; |
| 1345 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("+-15"), &ll, 10)); |
1347 CX_TEST_ASSERT(0 != cx_strtoll("+-15", &ll, 10)); |
| 1346 CX_TEST_ASSERT(errno == EINVAL); |
1348 CX_TEST_ASSERT(errno == EINVAL); |
| 1347 errno = 0; |
1349 errno = 0; |
| 1348 CX_TEST_ASSERT(0 != cx_strtoll(cx_str("++15"), &ll, 10)); |
1350 CX_TEST_ASSERT(0 != cx_strtoll("++15", &ll, 10)); |
| 1349 CX_TEST_ASSERT(errno == EINVAL); |
1351 CX_TEST_ASSERT(errno == EINVAL); |
| 1350 |
1352 |
| 1351 // edge case: only the sign bit is set |
1353 // edge case: only the sign bit is set |
| 1352 errno = 0; |
1354 errno = 0; |
| 1353 CX_TEST_ASSERT(0 != cx_strtoi16(cx_str("0x8000"), &i16, 16)); |
1355 CX_TEST_ASSERT(0 != cx_strtoi16("0x8000", &i16, 16)); |
| 1354 CX_TEST_ASSERT(errno == ERANGE); |
1356 CX_TEST_ASSERT(errno == ERANGE); |
| 1355 errno = 0; |
1357 errno = 0; |
| 1356 CX_TEST_ASSERT(0 == cx_strtoi16(cx_str("-0x8000"), &i16, 16)); |
1358 CX_TEST_ASSERT(0 == cx_strtoi16("-0x8000", &i16, 16)); |
| 1357 CX_TEST_ASSERT(errno == 0); |
1359 CX_TEST_ASSERT(errno == 0); |
| 1358 CX_TEST_ASSERT(i16 == INT16_MIN); |
1360 CX_TEST_ASSERT(i16 == INT16_MIN); |
| 1359 errno = 0; |
1361 errno = 0; |
| 1360 CX_TEST_ASSERT(0 != cx_strtoi64(cx_str("X8000000000000000"), &i64, 16)); |
1362 CX_TEST_ASSERT(0 != cx_strtoi64("X8000000000000000", &i64, 16)); |
| 1361 CX_TEST_ASSERT(errno == ERANGE); |
1363 CX_TEST_ASSERT(errno == ERANGE); |
| 1362 errno = 0; |
1364 errno = 0; |
| 1363 CX_TEST_ASSERT(0 == cx_strtoi64(cx_str("-X8000000000000000"), &i64, 16)); |
1365 CX_TEST_ASSERT(0 == cx_strtoi64("-X8000000000000000", &i64, 16)); |
| 1364 CX_TEST_ASSERT(errno == 0); |
1366 CX_TEST_ASSERT(errno == 0); |
| 1365 CX_TEST_ASSERT(i64 == INT64_MIN); |
1367 CX_TEST_ASSERT(i64 == INT64_MIN); |
| 1366 |
1368 |
| 1367 // group separators |
1369 // group separators |
| 1368 CX_TEST_ASSERT(0 == cx_strtoi32(cx_str("-123,456"), &i32, 10)); |
1370 CX_TEST_ASSERT(0 == cx_strtoi32("-123,456", &i32, 10)); |
| 1369 CX_TEST_ASSERT(i32 == -123456); |
1371 CX_TEST_ASSERT(i32 == -123456); |
| 1370 errno = 0; |
1372 errno = 0; |
| 1371 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-Xab,cd"), &i16, 16, "'")); |
1373 CX_TEST_ASSERT(0 != cx_strtoi16_lc("-Xab,cd", &i16, 16, "'")); |
| 1372 CX_TEST_ASSERT(errno == EINVAL); |
1374 CX_TEST_ASSERT(errno == EINVAL); |
| 1373 errno = 0; |
1375 errno = 0; |
| 1374 CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-X'ab'cd"), &i16, 16, "'")); |
1376 CX_TEST_ASSERT(0 != cx_strtoi16_lc("-X'ab'cd", &i16, 16, "'")); |
| 1375 CX_TEST_ASSERT(errno == ERANGE); |
1377 CX_TEST_ASSERT(errno == ERANGE); |
| 1376 errno = 0; |
1378 errno = 0; |
| 1377 CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str("-X'67'89"), &i16, 16, "'")); |
1379 CX_TEST_ASSERT(0 == cx_strtoi16_lc("-X'67'89", &i16, 16, "'")); |
| 1378 CX_TEST_ASSERT(errno == 0); |
1380 CX_TEST_ASSERT(errno == 0); |
| 1379 CX_TEST_ASSERT(i16 == -0x6789); |
1381 CX_TEST_ASSERT(i16 == -0x6789); |
| 1380 |
1382 |
| 1381 // binary and (unusual notation of) signed binary |
1383 // binary and (unusual notation of) signed binary |
| 1382 errno = 0; |
1384 errno = 0; |
| 1383 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("-1010 1011"), &i8, 2, " ")); |
1385 CX_TEST_ASSERT(0 != cx_strtoi8_lc("-1010 1011", &i8, 2, " ")); |
| 1384 CX_TEST_ASSERT(errno == ERANGE); |
1386 CX_TEST_ASSERT(errno == ERANGE); |
| 1385 errno = 0; |
1387 errno = 0; |
| 1386 CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("1010 1011"), &i8, 2, " ")); |
1388 CX_TEST_ASSERT(0 != cx_strtoi8_lc("1010 1011", &i8, 2, " ")); |
| 1387 CX_TEST_ASSERT(errno == ERANGE); |
1389 CX_TEST_ASSERT(errno == ERANGE); |
| 1388 errno = 0; |
1390 errno = 0; |
| 1389 CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str("-0101 0101"), &i8, 2, " ")); |
1391 CX_TEST_ASSERT(0 == cx_strtoi8_lc("-0101 0101", &i8, 2, " ")); |
| 1390 CX_TEST_ASSERT(errno == 0); |
1392 CX_TEST_ASSERT(errno == 0); |
| 1391 CX_TEST_ASSERT(i8 == -0x55); |
1393 CX_TEST_ASSERT(i8 == -0x55); |
| 1392 } |
1394 } |
| 1393 } |
1395 } |
| 1394 |
1396 |
| 1441 |
1443 |
| 1442 // do some special case tests |
1444 // do some special case tests |
| 1443 // -------------------------- |
1445 // -------------------------- |
| 1444 |
1446 |
| 1445 // leading plus |
1447 // leading plus |
| 1446 CX_TEST_ASSERT(0 == cx_strtou32(cx_str("+5"), &u32, 10)); |
1448 CX_TEST_ASSERT(0 == cx_strtou32("+5", &u32, 10)); |
| 1447 CX_TEST_ASSERT(u32 == 5); |
1449 CX_TEST_ASSERT(u32 == 5); |
| 1448 |
1450 |
| 1449 // group separators |
1451 // group separators |
| 1450 CX_TEST_ASSERT(0 == cx_strtou32(cx_str("123,456"), &u32, 10)); |
1452 CX_TEST_ASSERT(0 == cx_strtou32("123,456", &u32, 10)); |
| 1451 CX_TEST_ASSERT(u32 == 123456); |
1453 CX_TEST_ASSERT(u32 == 123456); |
| 1452 errno = 0; |
1454 errno = 0; |
| 1453 CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str("ab,cd"), &u16, 16, "'")); |
1455 CX_TEST_ASSERT(0 != cx_strtou16_lc("ab,cd", &u16, 16, "'")); |
| 1454 CX_TEST_ASSERT(errno == EINVAL); |
1456 CX_TEST_ASSERT(errno == EINVAL); |
| 1455 errno = 0; |
1457 errno = 0; |
| 1456 CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str("ab'cd"), &u16, 16, "'")); |
1458 CX_TEST_ASSERT(0 == cx_strtou16_lc("ab'cd", &u16, 16, "'")); |
| 1457 CX_TEST_ASSERT(errno == 0); |
1459 CX_TEST_ASSERT(errno == 0); |
| 1458 CX_TEST_ASSERT(u16 == 0xabcd); |
1460 CX_TEST_ASSERT(u16 == 0xabcd); |
| 1459 |
1461 |
| 1460 // binary |
1462 // binary |
| 1461 errno = 0; |
1463 errno = 0; |
| 1462 CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " ")); |
1464 CX_TEST_ASSERT(0 != cx_strtou8_lc("1 1010 1011", &u8, 2, " ")); |
| 1463 CX_TEST_ASSERT(errno == ERANGE); |
1465 CX_TEST_ASSERT(errno == ERANGE); |
| 1464 errno = 0; |
1466 errno = 0; |
| 1465 CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str("1010 1011"), &u8, 2, " ")); |
1467 CX_TEST_ASSERT(0 == cx_strtou8_lc("1010 1011", &u8, 2, " ")); |
| 1466 CX_TEST_ASSERT(errno == 0); |
1468 CX_TEST_ASSERT(errno == 0); |
| 1467 CX_TEST_ASSERT(u8 == 0xAB); |
1469 CX_TEST_ASSERT(u8 == 0xAB); |
| 1468 |
1470 |
| 1469 // edge case: empty and NULL string |
1471 // edge case: empty and NULL string |
| 1470 errno = 0; |
1472 errno = 0; |
| 1471 CX_TEST_ASSERT(0 != cx_strtoull(cx_str(""), &ull, 16)); |
1473 CX_TEST_ASSERT(0 != cx_strtoull("", &ull, 16)); |
| 1472 CX_TEST_ASSERT(errno == EINVAL); |
1474 CX_TEST_ASSERT(errno == EINVAL); |
| 1473 errno = 0; |
1475 errno = 0; |
| 1474 CX_TEST_ASSERT(0 != cx_strtoull(cx_str(NULL), &ull, 16)); |
1476 CX_TEST_ASSERT(0 != cx_strtoull(CX_NULLSTR, &ull, 16)); |
| 1475 CX_TEST_ASSERT(errno == EINVAL); |
1477 CX_TEST_ASSERT(errno == EINVAL); |
| 1476 |
1478 |
| 1477 // edge case: unsupported base |
1479 // edge case: unsupported base |
| 1478 errno = 0; |
1480 errno = 0; |
| 1479 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("7"), &ull, 12)); |
1481 CX_TEST_ASSERT(0 != cx_strtoull("7", &ull, 12)); |
| 1480 CX_TEST_ASSERT(errno == EINVAL); |
1482 CX_TEST_ASSERT(errno == EINVAL); |
| 1481 |
1483 |
| 1482 // edge case: prefix only |
1484 // edge case: prefix only |
| 1483 errno = 0; |
1485 errno = 0; |
| 1484 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("0b"), &ull, 2)); |
1486 CX_TEST_ASSERT(0 != cx_strtoull("0b", &ull, 2)); |
| 1485 CX_TEST_ASSERT(errno == EINVAL); |
1487 CX_TEST_ASSERT(errno == EINVAL); |
| 1486 errno = 0; |
1488 errno = 0; |
| 1487 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("b"), &ull, 2)); |
1489 CX_TEST_ASSERT(0 != cx_strtoull("b", &ull, 2)); |
| 1488 CX_TEST_ASSERT(errno == EINVAL); |
1490 CX_TEST_ASSERT(errno == EINVAL); |
| 1489 errno = 0; |
1491 errno = 0; |
| 1490 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("0x"), &ull, 16)); |
1492 CX_TEST_ASSERT(0 != cx_strtoull("0x", &ull, 16)); |
| 1491 CX_TEST_ASSERT(errno == EINVAL); |
1493 CX_TEST_ASSERT(errno == EINVAL); |
| 1492 errno = 0; |
1494 errno = 0; |
| 1493 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("x"), &ull, 16)); |
1495 CX_TEST_ASSERT(0 != cx_strtoull("x", &ull, 16)); |
| 1494 CX_TEST_ASSERT(errno == EINVAL); |
1496 CX_TEST_ASSERT(errno == EINVAL); |
| 1495 errno = 0; |
1497 errno = 0; |
| 1496 CX_TEST_ASSERT(0 != cx_strtoull(cx_str("#"), &ull, 16)); |
1498 CX_TEST_ASSERT(0 != cx_strtoull("#", &ull, 16)); |
| 1497 CX_TEST_ASSERT(errno == EINVAL); |
1499 CX_TEST_ASSERT(errno == EINVAL); |
| 1498 } |
1500 } |
| 1499 } |
1501 } |
| 1500 |
1502 |
| 1501 CX_TEST(test_string_to_float) { |
1503 CX_TEST(test_string_to_float) { |
| 1502 float f; |
1504 float f; |
| 1503 CX_TEST_DO { |
1505 CX_TEST_DO { |
| 1504 CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f)); |
1506 CX_TEST_ASSERT(0 == cx_strtof("11.3", &f)); |
| 1505 CX_TEST_ASSERT(0 == cx_vcmp_float(11.3f, f)); |
1507 CX_TEST_ASSERT(0 == cx_vcmp_float(11.3f, f)); |
| 1506 |
1508 |
| 1507 CX_TEST_ASSERT(0 == cx_strtof(cx_str("-4.711e+1"), &f)); |
1509 CX_TEST_ASSERT(0 == cx_strtof("-4.711e+1", &f)); |
| 1508 CX_TEST_ASSERT(0 == cx_vcmp_float(-47.11f, f)); |
1510 CX_TEST_ASSERT(0 == cx_vcmp_float(-47.11f, f)); |
| 1509 |
1511 |
| 1510 CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.67262192595e-27"), &f)); |
1512 CX_TEST_ASSERT(0 == cx_strtof("1.67262192595e-27", &f)); |
| 1511 CX_TEST_ASSERT(0 == cx_vcmp_float(1.67262192595e-27f, f)); |
1513 CX_TEST_ASSERT(0 == cx_vcmp_float(1.67262192595e-27f, f)); |
| 1512 |
1514 |
| 1513 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, '.', ",")); |
1515 CX_TEST_ASSERT(0 == cx_strtof_lc("138,339.4", &f, '.', ",")); |
| 1514 CX_TEST_ASSERT(0 == cx_vcmp_float(138339.4f, f)); |
1516 CX_TEST_ASSERT(0 == cx_vcmp_float(138339.4f, f)); |
| 1515 |
1517 |
| 1516 CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, ',', ".")); |
1518 CX_TEST_ASSERT(0 == cx_strtof_lc("138,339.4", &f, ',', ".")); |
| 1517 CX_TEST_ASSERT(0 == cx_vcmp_float(138.3394f, f)); |
1519 CX_TEST_ASSERT(0 == cx_vcmp_float(138.3394f, f)); |
| 1518 |
1520 |
| 1519 errno = 0; |
1521 errno = 0; |
| 1520 CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e"), &f)); |
1522 CX_TEST_ASSERT(0 != cx_strtof("15e", &f)); |
| 1521 CX_TEST_ASSERT(errno == EINVAL); |
1523 CX_TEST_ASSERT(errno == EINVAL); |
| 1522 errno = 0; |
1524 errno = 0; |
| 1523 CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e+"), &f)); |
1525 CX_TEST_ASSERT(0 != cx_strtof("15e+", &f)); |
| 1524 CX_TEST_ASSERT(errno == EINVAL); |
1526 CX_TEST_ASSERT(errno == EINVAL); |
| 1525 errno = 0; |
1527 errno = 0; |
| 1526 CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e-"), &f)); |
1528 CX_TEST_ASSERT(0 != cx_strtof("15e-", &f)); |
| 1527 CX_TEST_ASSERT(errno == EINVAL); |
1529 CX_TEST_ASSERT(errno == EINVAL); |
| 1528 CX_TEST_ASSERT(0 == cx_strtof(cx_str("15e-0"), &f)); |
1530 CX_TEST_ASSERT(0 == cx_strtof("15e-0", &f)); |
| 1529 CX_TEST_ASSERT(0 == cx_vcmp_float(15.f, f)); |
1531 CX_TEST_ASSERT(0 == cx_vcmp_float(15.f, f)); |
| 1530 |
1532 |
| 1531 CX_TEST_ASSERT(0 == cx_strtof(cx_str("3e38"), &f)); |
1533 CX_TEST_ASSERT(0 == cx_strtof("3e38", &f)); |
| 1532 CX_TEST_ASSERT(0 == cx_vcmp_float(3e38f, f)); |
1534 CX_TEST_ASSERT(0 == cx_vcmp_float(3e38f, f)); |
| 1533 errno = 0; |
1535 errno = 0; |
| 1534 CX_TEST_ASSERT(0 != cx_strtof(cx_str("3e39"), &f)); |
1536 CX_TEST_ASSERT(0 != cx_strtof("3e39", &f)); |
| 1535 CX_TEST_ASSERT(errno == ERANGE); |
1537 CX_TEST_ASSERT(errno == ERANGE); |
| 1536 CX_TEST_ASSERT(0 == cx_strtof(cx_str("-3e38"), &f)); |
1538 CX_TEST_ASSERT(0 == cx_strtof("-3e38", &f)); |
| 1537 CX_TEST_ASSERT(0 == cx_vcmp_float(-3e38f, f)); |
1539 CX_TEST_ASSERT(0 == cx_vcmp_float(-3e38f, f)); |
| 1538 errno = 0; |
1540 errno = 0; |
| 1539 CX_TEST_ASSERT(0 != cx_strtof(cx_str("-3e39"), &f)); |
1541 CX_TEST_ASSERT(0 != cx_strtof("-3e39", &f)); |
| 1540 CX_TEST_ASSERT(errno == ERANGE); |
1542 CX_TEST_ASSERT(errno == ERANGE); |
| 1541 CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.18e-38"), &f)); |
1543 CX_TEST_ASSERT(0 == cx_strtof("1.18e-38", &f)); |
| 1542 CX_TEST_ASSERT(0 == cx_vcmp_float(1.18e-38f, f)); |
1544 CX_TEST_ASSERT(0 == cx_vcmp_float(1.18e-38f, f)); |
| 1543 errno = 0; |
1545 errno = 0; |
| 1544 CX_TEST_ASSERT(0 != cx_strtof(cx_str("1.17e-38"), &f)); |
1546 CX_TEST_ASSERT(0 != cx_strtof("1.17e-38", &f)); |
| 1545 CX_TEST_ASSERT(errno == ERANGE); |
1547 CX_TEST_ASSERT(errno == ERANGE); |
| 1546 } |
1548 } |
| 1547 } |
1549 } |
| 1548 |
1550 |
| 1549 CX_TEST(test_string_to_double) { |
1551 CX_TEST(test_string_to_double) { |
| 1550 double d; |
1552 double d; |
| 1551 CX_TEST_DO { |
1553 CX_TEST_DO { |
| 1552 CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d)); |
1554 CX_TEST_ASSERT(0 == cx_strtod("11.3", &d)); |
| 1553 CX_TEST_ASSERT(0 == cx_vcmp_double(11.3, d)); |
1555 CX_TEST_ASSERT(0 == cx_vcmp_double(11.3, d)); |
| 1554 |
1556 |
| 1555 CX_TEST_ASSERT(0 == cx_strtod(cx_str("13."), &d)); |
1557 CX_TEST_ASSERT(0 == cx_strtod("13.", &d)); |
| 1556 CX_TEST_ASSERT(0 == cx_vcmp_double(13.0, d)); |
1558 CX_TEST_ASSERT(0 == cx_vcmp_double(13.0, d)); |
| 1557 |
1559 |
| 1558 CX_TEST_ASSERT(0 == cx_strtod(cx_str("47"), &d)); |
1560 CX_TEST_ASSERT(0 == cx_strtod("47", &d)); |
| 1559 CX_TEST_ASSERT(0 == cx_vcmp_double(47.0, d)); |
1561 CX_TEST_ASSERT(0 == cx_vcmp_double(47.0, d)); |
| 1560 |
1562 |
| 1561 CX_TEST_ASSERT(0 == cx_strtod(cx_str("-13.37"), &d)); |
1563 CX_TEST_ASSERT(0 == cx_strtod("-13.37", &d)); |
| 1562 CX_TEST_ASSERT(0 == cx_vcmp_double(-13.37, d)); |
1564 CX_TEST_ASSERT(0 == cx_vcmp_double(-13.37, d)); |
| 1563 |
1565 |
| 1564 CX_TEST_ASSERT(0 == cx_strtod(cx_str("-4.711e+1"), &d)); |
1566 CX_TEST_ASSERT(0 == cx_strtod("-4.711e+1", &d)); |
| 1565 CX_TEST_ASSERT(0 == cx_vcmp_double(-47.11, d)); |
1567 CX_TEST_ASSERT(0 == cx_vcmp_double(-47.11, d)); |
| 1566 |
1568 |
| 1567 CX_TEST_ASSERT(0 == cx_strtod(cx_str("1.67262192595e-27"), &d)); |
1569 CX_TEST_ASSERT(0 == cx_strtod("1.67262192595e-27", &d)); |
| 1568 CX_TEST_ASSERT(0 == cx_vcmp_double(1.67262192595e-27, d)); |
1570 CX_TEST_ASSERT(0 == cx_vcmp_double(1.67262192595e-27, d)); |
| 1569 |
1571 |
| 1570 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, '.', ",")); |
1572 CX_TEST_ASSERT(0 == cx_strtod_lc("138,339.4", &d, '.', ",")); |
| 1571 CX_TEST_ASSERT(0 == cx_vcmp_double(138339.4, d)); |
1573 CX_TEST_ASSERT(0 == cx_vcmp_double(138339.4, d)); |
| 1572 |
1574 |
| 1573 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', ".")); |
1575 CX_TEST_ASSERT(0 == cx_strtod_lc("138,339.4", &d, ',', ".")); |
| 1574 CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d)); |
1576 CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d)); |
| 1575 |
1577 |
| 1576 CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("13.37e04.7"), &d, ',', ".")); |
1578 CX_TEST_ASSERT(0 == cx_strtod_lc("13.37e04.7", &d, ',', ".")); |
| 1577 CX_TEST_ASSERT(0 == cx_vcmp_double(1337e47, d)); |
1579 CX_TEST_ASSERT(0 == cx_vcmp_double(1337e47, d)); |
| 1578 |
1580 |
| 1579 d = 47.11; |
1581 d = 47.11; |
| 1580 errno = 0; |
1582 errno = 0; |
| 1581 CX_TEST_ASSERT(0 != cx_strtod(cx_str(""), &d)); |
1583 CX_TEST_ASSERT(0 != cx_strtod("", &d)); |
| 1582 CX_TEST_ASSERT(errno == EINVAL); |
1584 CX_TEST_ASSERT(errno == EINVAL); |
| 1583 CX_TEST_ASSERT(d == 47.11); |
1585 CX_TEST_ASSERT(d == 47.11); |
| 1584 errno = 0; |
1586 errno = 0; |
| 1585 CX_TEST_ASSERT(0 != cx_strtod(cx_str(NULL), &d)); |
1587 CX_TEST_ASSERT(0 != cx_strtod(CX_NULLSTR, &d)); |
| 1586 CX_TEST_ASSERT(errno == EINVAL); |
1588 CX_TEST_ASSERT(errno == EINVAL); |
| 1587 CX_TEST_ASSERT(d == 47.11); |
1589 CX_TEST_ASSERT(d == 47.11); |
| 1588 errno = 0; |
1590 errno = 0; |
| 1589 CX_TEST_ASSERT(0 != cx_strtod(cx_str("+"), &d)); |
1591 CX_TEST_ASSERT(0 != cx_strtod("+", &d)); |
| 1590 CX_TEST_ASSERT(errno == EINVAL); |
1592 CX_TEST_ASSERT(errno == EINVAL); |
| 1591 CX_TEST_ASSERT(d == 47.11); |
1593 CX_TEST_ASSERT(d == 47.11); |
| 1592 errno = 0; |
1594 errno = 0; |
| 1593 CX_TEST_ASSERT(0 != cx_strtod(cx_str("-"), &d)); |
1595 CX_TEST_ASSERT(0 != cx_strtod("-", &d)); |
| 1594 CX_TEST_ASSERT(errno == EINVAL); |
1596 CX_TEST_ASSERT(errno == EINVAL); |
| 1595 CX_TEST_ASSERT(d == 47.11); |
1597 CX_TEST_ASSERT(d == 47.11); |
| 1596 errno = 0; |
1598 errno = 0; |
| 1597 CX_TEST_ASSERT(0 != cx_strtod(cx_str("+-5"), &d)); |
1599 CX_TEST_ASSERT(0 != cx_strtod("+-5", &d)); |
| 1598 CX_TEST_ASSERT(errno == EINVAL); |
1600 CX_TEST_ASSERT(errno == EINVAL); |
| 1599 CX_TEST_ASSERT(d == 47.11); |
1601 CX_TEST_ASSERT(d == 47.11); |
| 1600 errno = 0; |
1602 errno = 0; |
| 1601 CX_TEST_ASSERT(0 != cx_strtod(cx_str("-+5"), &d)); |
1603 CX_TEST_ASSERT(0 != cx_strtod("-+5", &d)); |
| 1602 CX_TEST_ASSERT(errno == EINVAL); |
1604 CX_TEST_ASSERT(errno == EINVAL); |
| 1603 CX_TEST_ASSERT(d == 47.11); |
1605 CX_TEST_ASSERT(d == 47.11); |
| 1604 errno = 0; |
1606 errno = 0; |
| 1605 CX_TEST_ASSERT(0 != cx_strtod(cx_str("++5"), &d)); |
1607 CX_TEST_ASSERT(0 != cx_strtod("++5", &d)); |
| 1606 CX_TEST_ASSERT(errno == EINVAL); |
1608 CX_TEST_ASSERT(errno == EINVAL); |
| 1607 CX_TEST_ASSERT(d == 47.11); |
1609 CX_TEST_ASSERT(d == 47.11); |
| 1608 errno = 0; |
1610 errno = 0; |
| 1609 CX_TEST_ASSERT(0 != cx_strtod(cx_str("--5"), &d)); |
1611 CX_TEST_ASSERT(0 != cx_strtod("--5", &d)); |
| 1610 CX_TEST_ASSERT(errno == EINVAL); |
1612 CX_TEST_ASSERT(errno == EINVAL); |
| 1611 CX_TEST_ASSERT(d == 47.11); |
1613 CX_TEST_ASSERT(d == 47.11); |
| 1612 |
1614 |
| 1613 errno = 0; |
1615 errno = 0; |
| 1614 CX_TEST_ASSERT(0 != cx_strtod_lc(cx_str("."), &d, '.', "'")); |
1616 CX_TEST_ASSERT(0 != cx_strtod_lc(".", &d, '.', "'")); |
| 1615 CX_TEST_ASSERT(errno == EINVAL); |
1617 CX_TEST_ASSERT(errno == EINVAL); |
| 1616 CX_TEST_ASSERT(d == 47.11); |
1618 CX_TEST_ASSERT(d == 47.11); |
| 1617 |
1619 |
| 1618 errno = 0; |
1620 errno = 0; |
| 1619 CX_TEST_ASSERT(0 != cx_strtod(cx_str("19e0x5"), &d)); |
1621 CX_TEST_ASSERT(0 != cx_strtod("19e0x5", &d)); |
| 1620 CX_TEST_ASSERT(errno == EINVAL); |
1622 CX_TEST_ASSERT(errno == EINVAL); |
| 1621 CX_TEST_ASSERT(d == 47.11); |
1623 CX_TEST_ASSERT(d == 47.11); |
| 1622 |
1624 |
| 1623 // TODO: test and improve support for big numbers, precision, and out-of-range detection |
1625 // TODO: test and improve support for big numbers, precision, and out-of-range detection |
| 1624 } |
1626 } |