Refactor parse_number_f64 related tests

This commit is contained in:
Nigel Tao committed 2020-07-11 13:16:00 +10:00
1 parent c5c9885db4
commit 37bc8cdaa5
1 file changed
+31 -38
+31 -38
View File
@@ -360,31 +360,33 @@ test_wuffs_strconv_hpd_rounded_integer() {
have, test_cases[tc].want); have, test_cases[tc].want);
} }
} }
return NULL;
}
const char* //
test_wuffs_strconv_parse_number_f64_options() {
CHECK_FOCUS(__func__);
// Test WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES. // Test WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES.
{ {
int o; int o;
for (o = 0; o < 2; o++) { for (o = 0; o < 2; o++) {
wuffs_base__private_implementation__high_prec_dec hpd;
const char* str = "001.25"; const char* str = "001.25";
wuffs_base__status status = wuffs_base__result_f64 r = wuffs_base__parse_number_f64(
wuffs_base__private_implementation__high_prec_dec__parse( wuffs_base__make_slice_u8((void*)str, strlen(str)),
&hpd, wuffs_base__make_slice_u8((void*)str, strlen(str)), (o ? WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES
(o ? WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES : WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
: WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
if (o == 0) { if (o == 0) {
if (status.repr != wuffs_base__error__bad_argument) { if (r.status.repr != wuffs_base__error__bad_argument) {
RETURN_FAIL( RETURN_FAIL(
"ALLOW_MULTIPLE_LEADING_ZEROES off: have \"%s\", want \"%s\"", "ALLOW_MULTIPLE_LEADING_ZEROES off: have \"%s\", want \"%s\"",
status.repr, wuffs_base__error__bad_argument); r.status.repr, wuffs_base__error__bad_argument);
} }
continue; continue;
} }
CHECK_STATUS("ALLOW_MULTIPLE_LEADING_ZEROES on", status); CHECK_STATUS("ALLOW_MULTIPLE_LEADING_ZEROES on", r.status);
wuffs_base__result_f64 r =
wuffs_base__private_implementation__high_prec_dec__to_f64(&hpd, 0);
uint64_t have = uint64_t have =
wuffs_base__ieee_754_bit_representation__from_f64(r.value); wuffs_base__ieee_754_bit_representation__from_f64(r.value);
uint64_t want = 0x3FF4000000000000; uint64_t want = 0x3FF4000000000000;
@@ -400,25 +402,21 @@ test_wuffs_strconv_hpd_rounded_integer() {
{ {
int o; int o;
for (o = 0; o < 2; o++) { for (o = 0; o < 2; o++) {
wuffs_base__private_implementation__high_prec_dec hpd;
const char* str = "_1.2__5"; const char* str = "_1.2__5";
wuffs_base__status status = wuffs_base__result_f64 r = wuffs_base__parse_number_f64(
wuffs_base__private_implementation__high_prec_dec__parse( wuffs_base__make_slice_u8((void*)str, strlen(str)),
&hpd, wuffs_base__make_slice_u8((void*)str, strlen(str)), (o ? WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES
(o ? WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES : WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
: WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
if (o == 0) { if (o == 0) {
if (status.repr != wuffs_base__error__bad_argument) { if (r.status.repr != wuffs_base__error__bad_argument) {
RETURN_FAIL("ALLOW_UNDERSCORES off: have \"%s\", want \"%s\"", RETURN_FAIL("ALLOW_UNDERSCORES off: have \"%s\", want \"%s\"",
status.repr, wuffs_base__error__bad_argument); r.status.repr, wuffs_base__error__bad_argument);
} }
continue; continue;
} }
CHECK_STATUS("ALLOW_UNDERSCORES on", status); CHECK_STATUS("ALLOW_UNDERSCORES on", r.status);
wuffs_base__result_f64 r =
wuffs_base__private_implementation__high_prec_dec__to_f64(&hpd, 0);
uint64_t have = uint64_t have =
wuffs_base__ieee_754_bit_representation__from_f64(r.value); wuffs_base__ieee_754_bit_representation__from_f64(r.value);
uint64_t want = 0x3FF4000000000000; uint64_t want = 0x3FF4000000000000;
@@ -434,26 +432,22 @@ test_wuffs_strconv_hpd_rounded_integer() {
{ {
int o; int o;
for (o = 0; o < 2; o++) { for (o = 0; o < 2; o++) {
wuffs_base__private_implementation__high_prec_dec hpd;
const char* str = "1,75"; const char* str = "1,75";
wuffs_base__status status = wuffs_base__result_f64 r = wuffs_base__parse_number_f64(
wuffs_base__private_implementation__high_prec_dec__parse( wuffs_base__make_slice_u8((void*)str, strlen(str)),
&hpd, wuffs_base__make_slice_u8((void*)str, strlen(str)), (o ? WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA
(o ? WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA : WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
: WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
if (o == 0) { if (o == 0) {
if (status.repr != wuffs_base__error__bad_argument) { if (r.status.repr != wuffs_base__error__bad_argument) {
RETURN_FAIL( RETURN_FAIL(
"DECIMAL_SEPARATOR_IS_A_COMMA off: have \"%s\", want \"%s\"", "DECIMAL_SEPARATOR_IS_A_COMMA off: have \"%s\", want \"%s\"",
status.repr, wuffs_base__error__bad_argument); r.status.repr, wuffs_base__error__bad_argument);
} }
continue; continue;
} }
CHECK_STATUS("DECIMAL_SEPARATOR_IS_A_COMMA on", status); CHECK_STATUS("DECIMAL_SEPARATOR_IS_A_COMMA on", r.status);
wuffs_base__result_f64 r =
wuffs_base__private_implementation__high_prec_dec__to_f64(&hpd, 0);
uint64_t have = uint64_t have =
wuffs_base__ieee_754_bit_representation__from_f64(r.value); wuffs_base__ieee_754_bit_representation__from_f64(r.value);
uint64_t want = 0x3FFC000000000000; uint64_t want = 0x3FFC000000000000;
@@ -465,9 +459,7 @@ test_wuffs_strconv_hpd_rounded_integer() {
} }
} }
// Test WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN, based on calling // Test WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN.
// wuffs_base__parse_number_f64. It does not call
// wuffs_base__private_implementation__high_prec_dec__parse directly.
{ {
int o; int o;
for (o = 0; o < 4; o++) { for (o = 0; o < 4; o++) {
@@ -709,7 +701,7 @@ test_wuffs_strconv_base_64() {
// ---------------- // ----------------
const char* // const char* //
test_wuffs_strconv_parse_number_f64() { test_wuffs_strconv_parse_number_f64_regular() {
CHECK_FOCUS(__func__); CHECK_FOCUS(__func__);
const uint64_t fail = 0xDEADBEEF; const uint64_t fail = 0xDEADBEEF;
@@ -3647,7 +3639,8 @@ proc g_tests[] = {
test_wuffs_strconv_base_64, test_wuffs_strconv_base_64,
test_wuffs_strconv_hpd_rounded_integer, test_wuffs_strconv_hpd_rounded_integer,
test_wuffs_strconv_hpd_shift, test_wuffs_strconv_hpd_shift,
test_wuffs_strconv_parse_number_f64, test_wuffs_strconv_parse_number_f64_options,
test_wuffs_strconv_parse_number_f64_regular,
test_wuffs_strconv_parse_number_i64, test_wuffs_strconv_parse_number_i64,
test_wuffs_strconv_parse_number_u64, test_wuffs_strconv_parse_number_u64,
test_wuffs_strconv_render_number_f64, test_wuffs_strconv_render_number_f64,