From 43ccab85ff6a3a39e40c827d62791a01b28c45a9 Mon Sep 17 00:00:00 2001 From: Yi Kong Date: Fri, 5 Jan 2018 16:22:10 -0800 Subject: [PATCH] Fix complex number printing rules Prettifies the complex number debug printing and workaround Clang's implicit complex to real casting bug. Bug: 71638783 Test: mma Change-Id: I0482044ade136fa4cc44aaa0bb515061b40e1f76 --- tests/complex_test.cpp | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/tests/complex_test.cpp b/tests/complex_test.cpp index 85b20de40..2aaa19231 100644 --- a/tests/complex_test.cpp +++ b/tests/complex_test.cpp @@ -14,8 +14,6 @@ * limitations under the License. */ -#include - #if defined(__BIONIC_LP32_USE_LONG_DOUBLE) #define COMPLEX_TEST complex_h_force_long_double #else @@ -43,26 +41,27 @@ #include // For M_PI_2/M_PI_2l. -#if 0 -// Note that gtest doesn't support complex numbers, so the output from -// assertion failures is misleading/useless (at best you'll only see the real -// part). -// TODO: find out why gtest doesn't use these; until then they're only useful -// for manual printf^Woperator<< debugging. +// Prettify gtest Complex printing. #include -std::ostream& operator<<(std::ostream& os, const double _Complex c) { - os << "(" << creal(c) << "," << cimag(c) << "i)"; - return os; +namespace testing { +namespace internal { +inline void PrintTo(const double _Complex& c, std::ostream* os) { + *os << "(" << creal(c) << "," << cimag(c) << "i)"; } -std::ostream& operator<<(std::ostream& os, const float _Complex c) { - os << "(" << crealf(c) << "," << cimagf(c) << "i)"; - return os; +inline void PrintTo(const float _Complex& c, std::ostream* os) { + *os << "(" << crealf(c) << "," << cimagf(c) << "i)"; } -std::ostream& operator<<(std::ostream& os, const long double _Complex c) { - os << "(" << creall(c) << "," << cimagl(c) << "i)"; - return os; +inline void PrintTo(const long double _Complex& c, std::ostream* os) { + *os << "(" << creall(c) << "," << cimagl(c) << "i)"; } -#endif +} +} + +// Macro 'I' defined in complex.h conflicts with gtest.h. +#pragma push_macro("I") +#undef I +#include +#pragma pop_macro("I") TEST(COMPLEX_TEST, cabs) { ASSERT_EQ(0.0, cabs(0));