Workaround a bug in formatting long double in MinGW, take 2

This commit is contained in:
vitaut 2015-05-07 07:25:39 -07:00
parent 4888000f32
commit 3e379829a0

View File

@ -74,19 +74,18 @@ namespace {
// Format value using the standard library. // Format value using the standard library.
template <typename Char, typename T> template <typename Char, typename T>
std::basic_string<Char> std_format(const T &value) { void std_format(const T &value, std::basic_string<Char> &result) {
std::basic_ostringstream<Char> os; std::basic_ostringstream<Char> os;
os << value; os << value;
return os.str(); result = os.str();
} }
#ifdef __MINGW32__ #ifdef __MINGW32__
// Workaround a bug in formatting long double in MinGW. // Workaround a bug in formatting long double in MinGW.
template <typename Char> void std_format(long double value, std::string &result) {
std::basic_string<Char> std_format(long double value) {
char buffer[100]; char buffer[100];
sprintf_s(buffer, sizeof(buffer), "%Lg", value); snprintf(buffer, sizeof(buffer), "%Lg", value);
return buffer; result = buffer;
} }
#endif #endif
@ -96,7 +95,8 @@ template <typename Char, typename T>
::testing::AssertionResult check_write(const T &value, const char *type) { ::testing::AssertionResult check_write(const T &value, const char *type) {
std::basic_string<Char> actual = std::basic_string<Char> actual =
(fmt::BasicMemoryWriter<Char>() << value).str(); (fmt::BasicMemoryWriter<Char>() << value).str();
std::basic_string<Char> expected = std_format<Char>(value); std::basic_string<Char> expected;
std_format<Char>(value, expected);
if (expected == actual) if (expected == actual)
return ::testing::AssertionSuccess(); return ::testing::AssertionSuccess();
return ::testing::AssertionFailure() return ::testing::AssertionFailure()