[test] Also print hex representation of floats/doubles

In the CHECK_FLOAT_EQ and CHECK_DOUBLE_EQ wrappers, do also print hex
representations on failure. Otherwise, single bit flips might not be
visible in the output, like here:
Check failed: DoubleWrapper(x) == y (-2e+66 vs. -2e+66).

R=titzer@chromium.org

Change-Id: I2521706aedc6ff81c0dbb25259230f8e29ce9a3e
Reviewed-on: https://chromium-review.googlesource.com/1219630
Reviewed-by: Ben Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55799}
This commit is contained in:
Clemens Hammacher 2018-09-11 15:44:30 +02:00 committed by Commit Bot
parent 9c67143f4a
commit ae9a577c47

View File

@ -345,29 +345,37 @@ template <typename type>
struct FloatCompareWrapper { struct FloatCompareWrapper {
type value; type value;
explicit FloatCompareWrapper(type x) : value(x) {} explicit FloatCompareWrapper(type x) : value(x) {}
bool operator==(type other) const { bool operator==(FloatCompareWrapper<type> other) const {
return std::isnan(value) return std::isnan(value)
? std::isnan(other) ? std::isnan(other.value)
: value == other && std::signbit(value) == std::signbit(other); : value == other.value &&
std::signbit(value) == std::signbit(other.value);
} }
}; };
template <typename type> template <typename type>
std::ostream& operator<<(std::ostream& out, FloatCompareWrapper<type> wrapper) { std::ostream& operator<<(std::ostream& out, FloatCompareWrapper<type> wrapper) {
return out << wrapper.value; uint8_t bytes[sizeof(type)];
memcpy(bytes, &wrapper.value, sizeof(type));
out << wrapper.value << " (0x";
const char* kHexDigits = "0123456789ABCDEF";
for (unsigned i = 0; i < sizeof(type); ++i) {
out << kHexDigits[bytes[i] >> 4] << kHexDigits[bytes[i] & 15];
}
return out << ")";
} }
#define CHECK_FLOAT_EQ(lhs, rhs) \ #define CHECK_FLOAT_EQ(lhs, rhs) \
do { \ do { \
using FloatWrapper = ::v8::internal::compiler::FloatCompareWrapper<float>; \ using FloatWrapper = ::v8::internal::compiler::FloatCompareWrapper<float>; \
CHECK_EQ(FloatWrapper(lhs), rhs); \ CHECK_EQ(FloatWrapper(lhs), FloatWrapper(rhs)); \
} while (false) } while (false)
#define CHECK_DOUBLE_EQ(lhs, rhs) \ #define CHECK_DOUBLE_EQ(lhs, rhs) \
do { \ do { \
using DoubleWrapper = \ using DoubleWrapper = \
::v8::internal::compiler::FloatCompareWrapper<double>; \ ::v8::internal::compiler::FloatCompareWrapper<double>; \
CHECK_EQ(DoubleWrapper(lhs), rhs); \ CHECK_EQ(DoubleWrapper(lhs), DoubleWrapper(rhs)); \
} while (false) } while (false)
} // namespace compiler } // namespace compiler