diff --git a/test/Region.cpp b/test/Region.cpp index b3b9983..b5e9450 100644 --- a/test/Region.cpp +++ b/test/Region.cpp @@ -1,9 +1,8 @@ -#include "storm/Region.hpp" -#include "test/Test.hpp" +#include "RegionTest.hpp" TEST_CASE("SRgnCreate", "[region]") { SECTION("sets handle pointer to new region handle") { - HSRGN region; + HSRGN region = nullptr; SRgnCreate(®ion, 0); REQUIRE(region != nullptr); @@ -13,28 +12,19 @@ TEST_CASE("SRgnCreate", "[region]") { } TEST_CASE("SRgnCombineRectf", "[region]") { - SECTION("combines the region with a single given rect 1") { - HSRGN region; - SRgnCreate(®ion, 0); + RgnDataTest region; + SECTION("combines the region with a single given rect 1") { RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f }; SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR); RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f }; SRgnGetBoundingRectf(region, &boundingRect); - REQUIRE(boundingRect.left == 0.0f); - REQUIRE(boundingRect.bottom == 0.0f); - REQUIRE(boundingRect.right == 1.0f); - REQUIRE(boundingRect.top == 1.0f); - - SRgnDelete(region); + CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 1.0f, 1.0f })); } SECTION("combines the region with multiple given rects 1") { - HSRGN region; - SRgnCreate(®ion, 0); - RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f }; SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR); @@ -44,18 +34,10 @@ TEST_CASE("SRgnCombineRectf", "[region]") { RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f }; SRgnGetBoundingRectf(region, &boundingRect); - REQUIRE(boundingRect.left == 0.0f); - REQUIRE(boundingRect.bottom == 0.0f); - REQUIRE(boundingRect.right == 1.0f); - REQUIRE(boundingRect.top == 1.0f); - - SRgnDelete(region); + CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 1.0f, 1.0f })); } SECTION("combines the region with multiple given rects 2") { - HSRGN region; - SRgnCreate(®ion, 0); - RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f }; SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR); @@ -65,18 +47,10 @@ TEST_CASE("SRgnCombineRectf", "[region]") { RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f }; SRgnGetBoundingRectf(region, &boundingRect); - REQUIRE(boundingRect.left == 0.0f); - REQUIRE(boundingRect.bottom == 0.0f); - REQUIRE(boundingRect.right == 0.0f); - REQUIRE(boundingRect.top == 0.0f); - - SRgnDelete(region); + CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 0.0f, 0.0f })); } SECTION("combines the region with multiple given rects 3") { - HSRGN region; - SRgnCreate(®ion, 0); - RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f }; SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR); @@ -86,80 +60,52 @@ TEST_CASE("SRgnCombineRectf", "[region]") { RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f }; SRgnGetBoundingRectf(region, &boundingRect); - REQUIRE(boundingRect.left == 0.0f); - REQUIRE(boundingRect.bottom == 0.0f); - REQUIRE(boundingRect.right == 1.0f); - REQUIRE(boundingRect.top == 1.0f); - - SRgnDelete(region); + CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 1.0f, 1.0f })); } } TEST_CASE("SRgnGetRectsf", "[region]") { - SECTION("retrieves empty list if nothing put in") { - HSRGN rgn; - SRgnCreate(&rgn, 0); + RgnDataTest region; + SECTION("retrieves empty list if nothing put in") { uint32_t numrects = 1; RECTF buffer[1]; - SRgnGetRectsf(rgn, &numrects, buffer); + SRgnGetRectsf(region, &numrects, buffer); CHECK(numrects == 0); - - SRgnDelete(rgn); } SECTION("retrieves all rects that were put in") { - HSRGN rgn; - SRgnCreate(&rgn, 0); - RECTF rct1 = { 5, 5, 10, 10 }; RECTF rct2 = { 0, 0, 1, 1 }; - SRgnCombineRectf(rgn, &rct1, nullptr, SRGN_OR); - SRgnCombineRectf(rgn, &rct2, nullptr, SRGN_OR); + SRgnCombineRectf(region, &rct1, nullptr, SRGN_OR); + SRgnCombineRectf(region, &rct2, nullptr, SRGN_OR); uint32_t numrects = 0; RECTF buffer[2]; - SRgnGetRectsf(rgn, &numrects, nullptr); + SRgnGetRectsf(region, &numrects, nullptr); REQUIRE(numrects == 2); - SRgnGetRectsf(rgn, &numrects, buffer); + SRgnGetRectsf(region, &numrects, buffer); - CHECK(buffer[0].left == rct2.left); - CHECK(buffer[0].bottom == rct2.bottom); - CHECK(buffer[0].right == rct2.right); - CHECK(buffer[0].top == rct2.top); - - CHECK(buffer[1].left == rct1.left); - CHECK(buffer[1].bottom == rct1.bottom); - CHECK(buffer[1].right == rct1.right); - CHECK(buffer[1].top == rct1.top); - - SRgnDelete(rgn); + CHECK_THAT(buffer[0], MatchesRect(rct2)); + CHECK_THAT(buffer[1], MatchesRect(rct1)); } SECTION("automatically merges overlapping rects") { - HSRGN rgn; - SRgnCreate(&rgn, 0); - RECTF rct1 = { -10, -10, 10, 10 }; RECTF rct2 = { 0, 0, 1, 1 }; - SRgnCombineRectf(rgn, &rct1, nullptr, SRGN_OR); - SRgnCombineRectf(rgn, &rct2, nullptr, SRGN_OR); + SRgnCombineRectf(region, &rct1, nullptr, SRGN_OR); + SRgnCombineRectf(region, &rct2, nullptr, SRGN_OR); uint32_t numrects = 0; RECTF buffer[1]; - SRgnGetRectsf(rgn, &numrects, nullptr); + SRgnGetRectsf(region, &numrects, nullptr); REQUIRE(numrects == 1); - SRgnGetRectsf(rgn, &numrects, buffer); + SRgnGetRectsf(region, &numrects, buffer); - CHECK(buffer[0].left == -10); - CHECK(buffer[0].bottom == -10); - CHECK(buffer[0].right == 10); - CHECK(buffer[0].top == 10); - - SRgnDelete(rgn); + CHECK_THAT(buffer[0], MatchesRect(rct1)); } } diff --git a/test/RegionTest.hpp b/test/RegionTest.hpp new file mode 100644 index 0000000..b9c21e9 --- /dev/null +++ b/test/RegionTest.hpp @@ -0,0 +1,50 @@ +#include "storm/Region.hpp" +#include "test/Test.hpp" +#include + +// Fixture for repetitive handling of Rgn objects. +struct RgnDataTest { + HSRGN rgn = nullptr; + + RgnDataTest() { + SRgnCreate(&rgn, 0); + } + + ~RgnDataTest() { + SRgnDelete(rgn); + rgn = nullptr; + } + + HSRGN* operator &() { return &rgn; } + operator HSRGN() const { return rgn; } + HSRGN operator->() const { return rgn; } +}; + +// Helpers for comparing RECTF structs +std::ostream& operator <<(std::ostream& os, RECTF const& value) { + os << "{ " << value.left << ", " << value.bottom << ", " << value.right << ", " << value.top << " }"; + return os; +} + +template +class RECTMatcher : public Catch::MatcherBase { +private: + T cmp; + +public: + RECTMatcher(T arg) : cmp(arg) {} + + bool match(T const& in) const override { + return cmp.left == in.left && cmp.bottom == in.bottom && cmp.right == in.right && cmp.top == in.top; + } + + std::string describe() const override { + std::ostringstream ss; + ss << "equals " << cmp; + return ss.str(); + } +}; + +RECTMatcher MatchesRect(RECTF arg) { + return { arg }; +}