mirror of
https://github.com/thunderbrewhq/squall.git
synced 2026-02-04 16:59:08 +00:00
chore(region): simplify region tests with fixture and matcher
This commit is contained in:
parent
e1587a932c
commit
c5ee72aabb
2 changed files with 72 additions and 76 deletions
|
|
@ -1,9 +1,8 @@
|
||||||
#include "storm/Region.hpp"
|
#include "RegionTest.hpp"
|
||||||
#include "test/Test.hpp"
|
|
||||||
|
|
||||||
TEST_CASE("SRgnCreate", "[region]") {
|
TEST_CASE("SRgnCreate", "[region]") {
|
||||||
SECTION("sets handle pointer to new region handle") {
|
SECTION("sets handle pointer to new region handle") {
|
||||||
HSRGN region;
|
HSRGN region = nullptr;
|
||||||
SRgnCreate(®ion, 0);
|
SRgnCreate(®ion, 0);
|
||||||
|
|
||||||
REQUIRE(region != nullptr);
|
REQUIRE(region != nullptr);
|
||||||
|
|
@ -13,28 +12,19 @@ TEST_CASE("SRgnCreate", "[region]") {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("SRgnCombineRectf", "[region]") {
|
TEST_CASE("SRgnCombineRectf", "[region]") {
|
||||||
SECTION("combines the region with a single given rect 1") {
|
RgnDataTest region;
|
||||||
HSRGN region;
|
|
||||||
SRgnCreate(®ion, 0);
|
|
||||||
|
|
||||||
|
SECTION("combines the region with a single given rect 1") {
|
||||||
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||||
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
||||||
|
|
||||||
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||||
SRgnGetBoundingRectf(region, &boundingRect);
|
SRgnGetBoundingRectf(region, &boundingRect);
|
||||||
|
|
||||||
REQUIRE(boundingRect.left == 0.0f);
|
CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 1.0f, 1.0f }));
|
||||||
REQUIRE(boundingRect.bottom == 0.0f);
|
|
||||||
REQUIRE(boundingRect.right == 1.0f);
|
|
||||||
REQUIRE(boundingRect.top == 1.0f);
|
|
||||||
|
|
||||||
SRgnDelete(region);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("combines the region with multiple given rects 1") {
|
SECTION("combines the region with multiple given rects 1") {
|
||||||
HSRGN region;
|
|
||||||
SRgnCreate(®ion, 0);
|
|
||||||
|
|
||||||
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||||
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
||||||
|
|
||||||
|
|
@ -44,18 +34,10 @@ TEST_CASE("SRgnCombineRectf", "[region]") {
|
||||||
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||||
SRgnGetBoundingRectf(region, &boundingRect);
|
SRgnGetBoundingRectf(region, &boundingRect);
|
||||||
|
|
||||||
REQUIRE(boundingRect.left == 0.0f);
|
CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 1.0f, 1.0f }));
|
||||||
REQUIRE(boundingRect.bottom == 0.0f);
|
|
||||||
REQUIRE(boundingRect.right == 1.0f);
|
|
||||||
REQUIRE(boundingRect.top == 1.0f);
|
|
||||||
|
|
||||||
SRgnDelete(region);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("combines the region with multiple given rects 2") {
|
SECTION("combines the region with multiple given rects 2") {
|
||||||
HSRGN region;
|
|
||||||
SRgnCreate(®ion, 0);
|
|
||||||
|
|
||||||
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||||
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
||||||
|
|
||||||
|
|
@ -65,18 +47,10 @@ TEST_CASE("SRgnCombineRectf", "[region]") {
|
||||||
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||||
SRgnGetBoundingRectf(region, &boundingRect);
|
SRgnGetBoundingRectf(region, &boundingRect);
|
||||||
|
|
||||||
REQUIRE(boundingRect.left == 0.0f);
|
CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 0.0f, 0.0f }));
|
||||||
REQUIRE(boundingRect.bottom == 0.0f);
|
|
||||||
REQUIRE(boundingRect.right == 0.0f);
|
|
||||||
REQUIRE(boundingRect.top == 0.0f);
|
|
||||||
|
|
||||||
SRgnDelete(region);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("combines the region with multiple given rects 3") {
|
SECTION("combines the region with multiple given rects 3") {
|
||||||
HSRGN region;
|
|
||||||
SRgnCreate(®ion, 0);
|
|
||||||
|
|
||||||
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
RECTF baseRect = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||||
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &baseRect, nullptr, SRGN_OR);
|
||||||
|
|
||||||
|
|
@ -86,80 +60,52 @@ TEST_CASE("SRgnCombineRectf", "[region]") {
|
||||||
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
RECTF boundingRect = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||||
SRgnGetBoundingRectf(region, &boundingRect);
|
SRgnGetBoundingRectf(region, &boundingRect);
|
||||||
|
|
||||||
REQUIRE(boundingRect.left == 0.0f);
|
CHECK_THAT(boundingRect, MatchesRect({ 0.0f, 0.0f, 1.0f, 1.0f }));
|
||||||
REQUIRE(boundingRect.bottom == 0.0f);
|
|
||||||
REQUIRE(boundingRect.right == 1.0f);
|
|
||||||
REQUIRE(boundingRect.top == 1.0f);
|
|
||||||
|
|
||||||
SRgnDelete(region);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("SRgnGetRectsf", "[region]") {
|
TEST_CASE("SRgnGetRectsf", "[region]") {
|
||||||
SECTION("retrieves empty list if nothing put in") {
|
RgnDataTest region;
|
||||||
HSRGN rgn;
|
|
||||||
SRgnCreate(&rgn, 0);
|
|
||||||
|
|
||||||
|
SECTION("retrieves empty list if nothing put in") {
|
||||||
uint32_t numrects = 1;
|
uint32_t numrects = 1;
|
||||||
RECTF buffer[1];
|
RECTF buffer[1];
|
||||||
|
|
||||||
SRgnGetRectsf(rgn, &numrects, buffer);
|
SRgnGetRectsf(region, &numrects, buffer);
|
||||||
|
|
||||||
CHECK(numrects == 0);
|
CHECK(numrects == 0);
|
||||||
|
|
||||||
SRgnDelete(rgn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("retrieves all rects that were put in") {
|
SECTION("retrieves all rects that were put in") {
|
||||||
HSRGN rgn;
|
|
||||||
SRgnCreate(&rgn, 0);
|
|
||||||
|
|
||||||
RECTF rct1 = { 5, 5, 10, 10 };
|
RECTF rct1 = { 5, 5, 10, 10 };
|
||||||
RECTF rct2 = { 0, 0, 1, 1 };
|
RECTF rct2 = { 0, 0, 1, 1 };
|
||||||
SRgnCombineRectf(rgn, &rct1, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &rct1, nullptr, SRGN_OR);
|
||||||
SRgnCombineRectf(rgn, &rct2, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &rct2, nullptr, SRGN_OR);
|
||||||
|
|
||||||
uint32_t numrects = 0;
|
uint32_t numrects = 0;
|
||||||
RECTF buffer[2];
|
RECTF buffer[2];
|
||||||
SRgnGetRectsf(rgn, &numrects, nullptr);
|
SRgnGetRectsf(region, &numrects, nullptr);
|
||||||
|
|
||||||
REQUIRE(numrects == 2);
|
REQUIRE(numrects == 2);
|
||||||
SRgnGetRectsf(rgn, &numrects, buffer);
|
SRgnGetRectsf(region, &numrects, buffer);
|
||||||
|
|
||||||
CHECK(buffer[0].left == rct2.left);
|
CHECK_THAT(buffer[0], MatchesRect(rct2));
|
||||||
CHECK(buffer[0].bottom == rct2.bottom);
|
CHECK_THAT(buffer[1], MatchesRect(rct1));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("automatically merges overlapping rects") {
|
SECTION("automatically merges overlapping rects") {
|
||||||
HSRGN rgn;
|
|
||||||
SRgnCreate(&rgn, 0);
|
|
||||||
|
|
||||||
RECTF rct1 = { -10, -10, 10, 10 };
|
RECTF rct1 = { -10, -10, 10, 10 };
|
||||||
RECTF rct2 = { 0, 0, 1, 1 };
|
RECTF rct2 = { 0, 0, 1, 1 };
|
||||||
SRgnCombineRectf(rgn, &rct1, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &rct1, nullptr, SRGN_OR);
|
||||||
SRgnCombineRectf(rgn, &rct2, nullptr, SRGN_OR);
|
SRgnCombineRectf(region, &rct2, nullptr, SRGN_OR);
|
||||||
|
|
||||||
uint32_t numrects = 0;
|
uint32_t numrects = 0;
|
||||||
RECTF buffer[1];
|
RECTF buffer[1];
|
||||||
SRgnGetRectsf(rgn, &numrects, nullptr);
|
SRgnGetRectsf(region, &numrects, nullptr);
|
||||||
|
|
||||||
REQUIRE(numrects == 1);
|
REQUIRE(numrects == 1);
|
||||||
SRgnGetRectsf(rgn, &numrects, buffer);
|
SRgnGetRectsf(region, &numrects, buffer);
|
||||||
|
|
||||||
CHECK(buffer[0].left == -10);
|
CHECK_THAT(buffer[0], MatchesRect(rct1));
|
||||||
CHECK(buffer[0].bottom == -10);
|
|
||||||
CHECK(buffer[0].right == 10);
|
|
||||||
CHECK(buffer[0].top == 10);
|
|
||||||
|
|
||||||
SRgnDelete(rgn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
50
test/RegionTest.hpp
Normal file
50
test/RegionTest.hpp
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
#include "storm/Region.hpp"
|
||||||
|
#include "test/Test.hpp"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// 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 T>
|
||||||
|
class RECTMatcher : public Catch::MatcherBase<T> {
|
||||||
|
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<RECTF> MatchesRect(RECTF arg) {
|
||||||
|
return { arg };
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue