From 68a5b55af922137bca9d05a214b13aef418864a3 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Wed, 18 Sep 2024 23:10:19 -0600 Subject: [PATCH] polylib: make winding printable for debugging/testing --- include/common/polylib.hh | 19 +++++++++++++++++++ tests/test_light.cc | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/common/polylib.hh b/include/common/polylib.hh index ef210230..0616d44a 100644 --- a/include/common/polylib.hh +++ b/include/common/polylib.hh @@ -16,6 +16,8 @@ #include #include +#include + #include namespace polylib @@ -1278,8 +1280,25 @@ public: return result; } + + // gtest support + // also, makes printable via fmt since we include fmt/ostream.h + friend std::ostream& operator<<(std::ostream& os, const winding_base_t &winding) { + os << "{"; + for (size_t i = 0; i < winding.size(); ++i) { + os << "(" << winding[i] << ")"; + if ((i + 1) < winding.size()) + os << ", "; + } + os << "}"; + return os; + } }; +// fmt support +template +struct fmt::formatter> : fmt::ostream_formatter {}; + // the default amount of points to keep on stack constexpr size_t STACK_POINTS_ON_WINDING = MAX_POINTS_ON_WINDING / 4; diff --git a/tests/test_light.cc b/tests/test_light.cc index ed3995df..c26c0856 100644 --- a/tests/test_light.cc +++ b/tests/test_light.cc @@ -980,6 +980,16 @@ TEST(mathlib, clampTexcoord) EXPECT_EQ(127, clamp_texcoord(-129.0f, 128)); } +TEST(mathlib, windingFormat) +{ + const polylib::winding_t poly{{0, 0, 0}, {0, 64, 0}, {64, 64, 0}, {64, 0, 0}}; + + const char *exp = "{(0 0 0), (0 64 0), (64 64 0), (64 0 0)}"; + + EXPECT_EQ(exp, testing::PrintToString(poly)); + EXPECT_EQ(exp, fmt::format("{}", poly)); +} + TEST(Settings, delayDefault) { light_t light;