Print small buffers on test expectation failure

This commit is contained in:
Austin Eng 2017-07-17 09:37:46 -04:00 committed by Austin Eng
parent 4234c39a09
commit 1b8c64d3e2
1 changed files with 27 additions and 1 deletions

View File

@ -378,9 +378,35 @@ namespace detail {
NXT_ASSERT(size == sizeof(T) * expected.size()); NXT_ASSERT(size == sizeof(T) * expected.size());
const T* actual = reinterpret_cast<const T*>(data); const T* actual = reinterpret_cast<const T*>(data);
testing::AssertionResult failure = testing::AssertionFailure();
for (size_t i = 0; i < expected.size(); ++i) { for (size_t i = 0; i < expected.size(); ++i) {
if (actual[i] != expected[i]) { if (actual[i] != expected[i]) {
return testing::AssertionFailure() << "Expected data[" << i << "] to be " << expected[i] << ", actual " << actual[i] << std::endl; testing::AssertionResult result = testing::AssertionFailure() << "Expected data[" << i << "] to be " << expected[i] << ", actual " << actual[i] << std::endl;
auto printBuffer = [&](const T* buffer) {
static constexpr unsigned int kBytes = sizeof(T);
for (size_t index = 0; index < expected.size(); ++index) {
auto byteView = reinterpret_cast<const uint8_t*>(buffer + index);
for (unsigned int b = 0; b < kBytes; ++b) {
char buf[4];
sprintf(buf, "%02X ", byteView[b]);
result << buf;
}
}
result << std::endl;
};
if (expected.size() <= 1024) {
result << "Expected:" << std::endl;
printBuffer(expected.data());
result << "Actual:" << std::endl;
printBuffer(actual);
}
return result;
} }
} }