From d3ead501deb220f998123611a5427e4d1d8fa7de Mon Sep 17 00:00:00 2001 From: David Neto Date: Tue, 1 Sep 2015 14:56:09 -0400 Subject: [PATCH] Don't overload operators in std namespace. Use a spvtest::WordVector proxy object to easily print std::vector and spv_binary_t values. --- test/ExtInstGLSLstd450.cpp | 3 +- test/UnitSPIRV.h | 63 ++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/test/ExtInstGLSLstd450.cpp b/test/ExtInstGLSLstd450.cpp index 9cdc22858..7ae39fab0 100644 --- a/test/ExtInstGLSLstd450.cpp +++ b/test/ExtInstGLSLstd450.cpp @@ -107,7 +107,8 @@ OpFunctionEnd EXPECT_NE(binary->code + binary->wordCount, std::search(binary->code, binary->code + binary->wordCount, expected_contains.begin(), expected_contains.end())) - << "Cannot find\n" << expected_contains << "in\n" << *binary; + << "Cannot find\n" << spvtest::WordVector(expected_contains).str() + << "in\n" << spvtest::WordVector(*binary).str(); // Check round trip gives the same text. spv_text output_text; diff --git a/test/UnitSPIRV.h b/test/UnitSPIRV.h index 96bfbf191..d8f22fc0b 100644 --- a/test/UnitSPIRV.h +++ b/test/UnitSPIRV.h @@ -63,33 +63,50 @@ static const union { uint32_t value; } o32_host_order = {{0, 1, 2, 3}}; -inline ::std::ostream& operator<<(::std::ostream& os, - const spv_binary_t& binary) { - for (size_t i = 0; i < binary.wordCount; ++i) { - os << "0x" << std::setw(8) << std::setfill('0') << std::hex - << binary.code[i] << " "; - if (i % 8 == 7) { - os << std::endl; + +// A namespace for utilities used in SPIR-V Tools unit tests. +// TODO(dneto): Move other type declarations into this namespace. +namespace spvtest { + +class WordVector; + +// Emits the given word vector to the given stream. +// This function can be used by the gtest value printer. +void PrintTo(const WordVector& words, ::std::ostream* os); + +// A proxy class to allow us to easily write out vectors of SPIR-V words. +class WordVector { + public: + explicit WordVector(const std::vector& value) : value_(value) {} + explicit WordVector(const spv_binary_t& binary) + : value_(binary.code, binary.code + binary.wordCount) {} + + // Returns the underlying vector. + const std::vector& value() const { return value_; } + + // Returns the string representation of this word vector. + std::string str() const { + std::ostringstream os; + PrintTo(*this, &os); + return os.str(); + } + + private: + const std::vector value_; +}; + +inline void PrintTo(const WordVector& words, ::std::ostream* os) { + size_t count = 0; + for (uint32_t value : words.value()) { + *os << "0x" << std::setw(8) << std::setfill('0') << std::hex << value << " "; + if (count++ % 8 == 7) { + *os << std::endl; } } - os << std::endl; - return os; + *os << std::endl; } -namespace std { -inline ::std::ostream& operator<<(::std::ostream& os, - const std::vector& value) { - size_t count = 0; - for (size_t i : value) { - os << "0x" << std::setw(8) << std::setfill('0') << std::hex << i << " "; - if (count++ % 8 == 7) { - os << std::endl; - } - } - os << std::endl; - return os; -} -} +} // namespace spvtest // A type for easily creating spv_text_t values, with an implicit conversion to // spv_text.