From c572df265d4fe084004b2f022e661cedb3b13d5a Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 25 Nov 2022 14:53:23 +0000 Subject: [PATCH] tint/utils: Add operator<<() support to vector Helpful for tests and the like. Change-Id: I07f8c59af6db4d6a5629dca2dc985398b75eccf9 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/111760 Reviewed-by: Antonio Maiorano Kokoro: Kokoro Commit-Queue: Ben Clayton --- src/tint/sem/binding_point.h | 9 +++++++ src/tint/sem/sampler_texture_pair.h | 10 ++++++++ src/tint/utils/string.h | 10 ++++++++ src/tint/utils/vector.h | 40 +++++++++++++++++++++++++++++ src/tint/utils/vector_test.cc | 29 +++++++++++++++------ 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/tint/sem/binding_point.h b/src/tint/sem/binding_point.h index b779b73512..78403abdc8 100644 --- a/src/tint/sem/binding_point.h +++ b/src/tint/sem/binding_point.h @@ -18,6 +18,7 @@ #include #include +#include #include "src/tint/reflection.h" #include "src/tint/utils/hash.h" @@ -47,6 +48,14 @@ struct BindingPoint { inline bool operator!=(const BindingPoint& rhs) const { return !(*this == rhs); } }; +/// Prints the BindingPoint @p bp to @p o +/// @param o the std::ostream to write to +/// @param bp the BindingPoint +/// @return the std::ostream so calls can be chained +inline std::ostream& operator<<(std::ostream& o, const BindingPoint& bp) { + return o << "[group: " << bp.group << ", binding: " << bp.binding << "]"; +} + } // namespace tint::sem namespace std { diff --git a/src/tint/sem/sampler_texture_pair.h b/src/tint/sem/sampler_texture_pair.h index 71f3e3cafa..b3cf4f2e07 100644 --- a/src/tint/sem/sampler_texture_pair.h +++ b/src/tint/sem/sampler_texture_pair.h @@ -17,6 +17,7 @@ #include #include +#include #include "src/tint/sem/binding_point.h" @@ -43,6 +44,15 @@ struct SamplerTexturePair { inline bool operator!=(const SamplerTexturePair& rhs) const { return !(*this == rhs); } }; +/// Prints the SamplerTexturePair @p stp to @p o +/// @param o the std::ostream to write to +/// @param stp the SamplerTexturePair +/// @return the std::ostream so calls can be chained +inline std::ostream& operator<<(std::ostream& o, const SamplerTexturePair& stp) { + return o << "[sampler: " << stp.sampler_binding_point + << ", texture: " << stp.sampler_binding_point << "]"; +} + } // namespace tint::sem namespace std { diff --git a/src/tint/utils/string.h b/src/tint/utils/string.h index dd37396d93..abfe51a4bb 100644 --- a/src/tint/utils/string.h +++ b/src/tint/utils/string.h @@ -17,6 +17,7 @@ #include #include +#include namespace tint::utils { @@ -44,6 +45,15 @@ std::string ToString(const T& value) { return s.str(); } +/// @param value the variant to be printed as a string +/// @returns value printed as a string via the std::ostream `<<` operator +template +std::string ToString(const std::variant& value) { + std::stringstream s; + s << std::visit([&](auto& v) { return ToString(v); }, value); + return s.str(); +} + /// @param str the input string /// @param prefix the prefix string /// @returns true iff @p str has the prefix @p prefix diff --git a/src/tint/utils/vector.h b/src/tint/utils/vector.h index 705817a8c4..ecee17053e 100644 --- a/src/tint/utils/vector.h +++ b/src/tint/utils/vector.h @@ -20,12 +20,14 @@ #include #include #include +#include #include #include #include "src/tint/castable.h" #include "src/tint/traits.h" #include "src/tint/utils/bitcast.h" +#include "src/tint/utils/string.h" namespace tint::utils { @@ -832,6 +834,44 @@ Vector ToVector(const std::vector& vector) { return out; } +/// Prints the vector @p vec to @p o +/// @param o the std::ostream to write to +/// @param vec the vector +/// @return the std::ostream so calls can be chained +template +inline std::ostream& operator<<(std::ostream& o, const utils::Vector& vec) { + o << "["; + bool first = true; + for (auto& el : vec) { + if (!first) { + o << ", "; + } + first = false; + o << ToString(el); + } + o << "]"; + return o; +} + +/// Prints the vector @p vec to @p o +/// @param o the std::ostream to write to +/// @param vec the vector reference +/// @return the std::ostream so calls can be chained +template +inline std::ostream& operator<<(std::ostream& o, const utils::VectorRef& vec) { + o << "["; + bool first = true; + for (auto& el : vec) { + if (!first) { + o << ", "; + } + first = false; + o << ToString(el); + } + o << "]"; + return o; +} + } // namespace tint::utils #endif // SRC_TINT_UTILS_VECTOR_H_ diff --git a/src/tint/utils/vector_test.cc b/src/tint/utils/vector_test.cc index a3cc1f78f6..9a9bb4b455 100644 --- a/src/tint/utils/vector_test.cc +++ b/src/tint/utils/vector_test.cc @@ -1795,6 +1795,21 @@ TEST(TintVectorTest, ConstBeginEnd_WithSpill) { EXPECT_EQ(vec.end(), &vec[0] + 3); } +TEST(TintVectorTest, Equality) { + EXPECT_EQ((Vector{1, 2}), (Vector{1, 2})); + EXPECT_EQ((Vector{1, 2}), (Vector{1, 2})); + EXPECT_NE((Vector{1, 2}), (Vector{1})); + EXPECT_NE((Vector{1}), (Vector{1, 2})); + EXPECT_NE((Vector{1, 2}), (Vector{2, 1})); + EXPECT_NE((Vector{2, 1}), (Vector{1, 2})); +} + +TEST(TintVectorTest, ostream) { + std::stringstream ss; + ss << Vector{1, 2, 3}; + EXPECT_EQ(ss.str(), "[1, 2, 3]"); +} + //////////////////////////////////////////////////////////////////////////////// // TintVectorRefTest //////////////////////////////////////////////////////////////////////////////// @@ -2060,15 +2075,13 @@ TEST(TintVectorRefTest, ConstBeginEnd) { EXPECT_EQ(vec_ref.end(), &vec[0] + 3); } -TEST(TintVectorTest, Equality) { - EXPECT_EQ((Vector{1, 2}), (Vector{1, 2})); - EXPECT_EQ((Vector{1, 2}), (Vector{1, 2})); - EXPECT_NE((Vector{1, 2}), (Vector{1})); - EXPECT_NE((Vector{1}), (Vector{1, 2})); - EXPECT_NE((Vector{1, 2}), (Vector{2, 1})); - EXPECT_NE((Vector{2, 1}), (Vector{1, 2})); +TEST(TintVectorRefTest, ostream) { + std::stringstream ss; + Vector vec{1, 2, 3}; + const VectorRef vec_ref(vec); + ss << vec_ref; + EXPECT_EQ(ss.str(), "[1, 2, 3]"); } - } // namespace } // namespace tint::utils