summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Dino Radakovic <dinor@google.com>2024-05-06 07:28:48 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2024-05-06 07:29:48 -0700
commitb8c843eb1054e9f77c62e0b2547c9a4fdbed3c77 (patch)
tree95147c5737cc2bdd4b60d9a55c25132a617ea27e
parenta28ee5b51c9ea41707d9a5d2d358ad77850264c4 (diff)
`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
-rw-r--r--absl/strings/internal/str_format/convert_test.cc32
1 files changed, 18 insertions, 14 deletions
diff --git a/absl/strings/internal/str_format/convert_test.cc b/absl/strings/internal/str_format/convert_test.cc
index 17c3607d..3a4c27d7 100644
--- a/absl/strings/internal/str_format/convert_test.cc
+++ b/absl/strings/internal/str_format/convert_test.cc
@@ -785,7 +785,7 @@ TEST_F(FormatConvertTest, Uint128) {
}
template <typename Floating>
-void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) {
+void TestWithMultipleFormatsHelper(Floating tested_float) {
const NativePrintfTraits &native_traits = VerifyNativeImplementation();
// Reserve the space to ensure we don't allocate memory in the output itself.
std::string str_format_result;
@@ -816,17 +816,17 @@ void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) {
continue;
}
- for (Floating d : floats) {
- if (!native_traits.hex_float_prefers_denormal_repr &&
- (f == 'a' || f == 'A') && std::fpclassify(d) == FP_SUBNORMAL) {
- continue;
- }
+ if (!native_traits.hex_float_prefers_denormal_repr &&
+ (f == 'a' || f == 'A') &&
+ std::fpclassify(tested_float) == FP_SUBNORMAL) {
+ continue;
+ }
int i = -10;
- FormatArgImpl args[2] = {FormatArgImpl(d), FormatArgImpl(i)};
+ FormatArgImpl args[2] = {FormatArgImpl(tested_float), FormatArgImpl(i)};
UntypedFormatSpecImpl format(fmt_str);
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();
{
@@ -842,17 +842,17 @@ void TestWithMultipleFormatsHelper(const std::vector<Floating> &floats) {
continue;
#elif defined(__APPLE__)
// Apple formats NaN differently (+nan) vs. (nan)
- if (std::isnan(d)) continue;
+ if (std::isnan(tested_float)) continue;
#endif
if (string_printf_result != str_format_result) {
// We use ASSERT_EQ here because failures are usually correlated and a
// bug would print way too many failed expectations causing the test
// to time out.
ASSERT_EQ(string_printf_result, str_format_result)
- << fmt_str << " " << StrPrint("%.18g", d) << " "
- << StrPrint("%a", d) << " " << StrPrint("%.50f", d);
+ << fmt_str << " " << StrPrint("%.18g", tested_float) << " "
+ << StrPrint("%a", tested_float) << " "
+ << StrPrint("%.50f", tested_float);
}
- }
}
}
}
@@ -905,7 +905,9 @@ TEST_F(FormatConvertTest, Float) {
});
floats.erase(std::unique(floats.begin(), floats.end()), floats.end());
- TestWithMultipleFormatsHelper(floats);
+ for (float f : floats) {
+ TestWithMultipleFormatsHelper(f);
+ }
}
TEST_F(FormatConvertTest, Double) {
@@ -956,7 +958,9 @@ TEST_F(FormatConvertTest, Double) {
});
doubles.erase(std::unique(doubles.begin(), doubles.end()), doubles.end());
- TestWithMultipleFormatsHelper(doubles);
+ for (double d : doubles) {
+ TestWithMultipleFormatsHelper(d);
+ }
}
TEST_F(FormatConvertTest, DoubleRound) {