Commit b8c843eb by Dino Radakovic Committed by Copybara-Service

`convert_test`: Extract loop over tested floats from helper function

This change is a step towards simplifying `TestWithMultipleFormatsHelper` to the point where we'll be
able to handle special cases (e.g. apple's handling of nan) by changing which inputs are fed into the helper, instead of skipping them within the helper and not testing them at all.

Extracting the loop also improves readability by reducing indentation.

PiperOrigin-RevId: 631038465
Change-Id: I8b2458539d9d276093d8e7b5f373efba6a33800c
parent a28ee5b5
...@@ -785,7 +785,7 @@ TEST_F(FormatConvertTest, Uint128) { ...@@ -785,7 +785,7 @@ TEST_F(FormatConvertTest, Uint128) {
} }
template <typename Floating> template <typename Floating>
void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) { void TestWithMultipleFormatsHelper(Floating tested_float) {
const NativePrintfTraits &native_traits = VerifyNativeImplementation(); const NativePrintfTraits &native_traits = VerifyNativeImplementation();
// Reserve the space to ensure we don't allocate memory in the output itself. // Reserve the space to ensure we don't allocate memory in the output itself.
std::string str_format_result; std::string str_format_result;
...@@ -816,17 +816,17 @@ void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) { ...@@ -816,17 +816,17 @@ void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) {
continue; continue;
} }
for (Floating d : floats) {
if (!native_traits.hex_float_prefers_denormal_repr && if (!native_traits.hex_float_prefers_denormal_repr &&
(f == 'a' || f == 'A') && std::fpclassify(d) == FP_SUBNORMAL) { (f == 'a' || f == 'A') &&
std::fpclassify(tested_float) == FP_SUBNORMAL) {
continue; continue;
} }
int i = -10; int i = -10;
FormatArgImpl args[2] = {FormatArgImpl(d), FormatArgImpl(i)}; FormatArgImpl args[2] = {FormatArgImpl(tested_float), FormatArgImpl(i)};
UntypedFormatSpecImpl format(fmt_str); UntypedFormatSpecImpl format(fmt_str);
string_printf_result.clear(); string_printf_result.clear();
StrAppend(&string_printf_result, fmt_str.c_str(), d, i); StrAppend(&string_printf_result, fmt_str.c_str(), tested_float, i);
str_format_result.clear(); str_format_result.clear();
{ {
...@@ -842,16 +842,16 @@ void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) { ...@@ -842,16 +842,16 @@ void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) {
continue; continue;
#elif defined(__APPLE__) #elif defined(__APPLE__)
// Apple formats NaN differently (+nan) vs. (nan) // Apple formats NaN differently (+nan) vs. (nan)
if (std::isnan(d)) continue; if (std::isnan(tested_float)) continue;
#endif #endif
if (string_printf_result != str_format_result) { if (string_printf_result != str_format_result) {
// We use ASSERT_EQ here because failures are usually correlated and a // We use ASSERT_EQ here because failures are usually correlated and a
// bug would print way too many failed expectations causing the test // bug would print way too many failed expectations causing the test
// to time out. // to time out.
ASSERT_EQ(string_printf_result, str_format_result) ASSERT_EQ(string_printf_result, str_format_result)
<< fmt_str << " " << StrPrint("%.18g", d) << " " << fmt_str << " " << StrPrint("%.18g", tested_float) << " "
<< StrPrint("%a", d) << " " << StrPrint("%.50f", d); << StrPrint("%a", tested_float) << " "
} << StrPrint("%.50f", tested_float);
} }
} }
} }
...@@ -905,7 +905,9 @@ TEST_F(FormatConvertTest, Float) { ...@@ -905,7 +905,9 @@ TEST_F(FormatConvertTest, Float) {
}); });
floats.erase(std::unique(floats.begin(), floats.end()), floats.end()); floats.erase(std::unique(floats.begin(), floats.end()), floats.end());
TestWithMultipleFormatsHelper(floats); for (float f : floats) {
TestWithMultipleFormatsHelper(f);
}
} }
TEST_F(FormatConvertTest, Double) { TEST_F(FormatConvertTest, Double) {
...@@ -956,7 +958,9 @@ TEST_F(FormatConvertTest, Double) { ...@@ -956,7 +958,9 @@ TEST_F(FormatConvertTest, Double) {
}); });
doubles.erase(std::unique(doubles.begin(), doubles.end()), doubles.end()); doubles.erase(std::unique(doubles.begin(), doubles.end()), doubles.end());
TestWithMultipleFormatsHelper(doubles); for (double d : doubles) {
TestWithMultipleFormatsHelper(d);
}
} }
TEST_F(FormatConvertTest, DoubleRound) { TEST_F(FormatConvertTest, DoubleRound) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment