Don't overload operators in std namespace.

Use a spvtest::WordVector proxy object to easily print
std::vector<uint32_t> and spv_binary_t values.
This commit is contained in:
David Neto 2015-09-01 14:56:09 -04:00
parent 0a8f219d1e
commit d3ead501de
2 changed files with 42 additions and 24 deletions

View File

@ -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;

View File

@ -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<uint32_t>& value) : value_(value) {}
explicit WordVector(const spv_binary_t& binary)
: value_(binary.code, binary.code + binary.wordCount) {}
// Returns the underlying vector.
const std::vector<uint32_t>& 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<uint32_t> 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<uint32_t>& 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.