diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index 09d924f..e255ed6 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -69,3 +69,9 @@ std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point) { stream << "[x:" << point.x << ",y:" << point.y << "]"; return stream; } + +bool operator<(const SDL2pp::Point& a, const SDL2pp::Point& b) { + if (a.x == b.x) + return a.y < b.y; + return a.x < b.x; +} diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 1fd8eda..5564c53 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -458,4 +458,15 @@ public: //////////////////////////////////////////////////////////// std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point); +//////////////////////////////////////////////////////////// +/// \brief Less-than operator for SDL2pp::Point +/// +/// \param[in] a First comparison argument +/// \param[in] b Second comparison argument +/// +/// \returns true if a < b +/// +//////////////////////////////////////////////////////////// +bool operator<(const SDL2pp::Point& a, const SDL2pp::Point& b); + #endif diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index 9035753..815de3f 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -88,3 +88,15 @@ std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect) { stream << "[x:" << rect.x << ",y:" << rect.y << ",w:" << rect.w << ",h:" << rect.h << "]"; return stream; } + +bool operator<(const SDL2pp::Rect& a, const SDL2pp::Rect& b) { + if (a.x == b.x) { + if (a.y == b.y) { + if (a.w == b.w) + return a.h < b.h; + return a.w < b.w; + } + return a.y < b.y; + } + return a.x < b.x; +} diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index c5397c3..b1ef101 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -534,4 +534,15 @@ public: //////////////////////////////////////////////////////////// std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect); +//////////////////////////////////////////////////////////// +/// \brief Less-than operator for SDL2pp::Rect +/// +/// \param[in] a First comparison argument +/// \param[in] b Second comparison argument +/// +/// \returns true if a < b +/// +//////////////////////////////////////////////////////////// +bool operator<(const SDL2pp::Rect& a, const SDL2pp::Rect& b); + #endif diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index c2676ec..e11cc08 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -79,6 +79,18 @@ BEGIN_TEST() EXPECT_EQUAL(p /= Point(5, 3), Point(3, 3)); EXPECT_EQUAL(p *= Point(10, 20), Point(30, 60)); EXPECT_EQUAL(p %= Point(7, 11), Point(2, 5)); + + // Less-than + EXPECT_TRUE(Point(0, 0) < Point(1, 0)); + EXPECT_TRUE(Point(0, 1) < Point(1, 0)); + EXPECT_TRUE(Point(0, 1) < Point(1, 1)); + EXPECT_TRUE(Point(0, 0) < Point(0, 1)); + + EXPECT_TRUE(!(Point(1, 0) < Point(0, 0))); + EXPECT_TRUE(!(Point(1, 0) < Point(0, 1))); + EXPECT_TRUE(!(Point(1, 1) < Point(0, 1))); + EXPECT_TRUE(!(Point(0, 1) < Point(0, 0))); + EXPECT_TRUE(!(Point(1, 1) < Point(1, 1))); } { @@ -286,6 +298,39 @@ BEGIN_TEST() EXPECT_EQUAL(r, Rect(-9, -18, 3, 4)); } + { + // Less-than + EXPECT_TRUE(!(Rect(0, 0, 0, 0) < Rect(0, 0, 0, 0))); + EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 0, 0, 1)); + EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 0, 1, 0)); + EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 1, 0, 0)); + EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(1, 0, 0, 0)); + + EXPECT_TRUE(!(Rect(0, 0, 0, 1) < Rect(0, 0, 0, 0))); + EXPECT_TRUE(!(Rect(0, 0, 0, 1) < Rect(0, 0, 0, 1))); + EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(0, 0, 1, 0)); + EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(0, 1, 0, 0)); + EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(1, 0, 0, 0)); + + EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 0, 0))); + EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 0, 1))); + EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 1, 0))); + EXPECT_TRUE(Rect(0, 0, 1, 0) < Rect(0, 1, 0, 0)); + EXPECT_TRUE(Rect(0, 0, 1, 0) < Rect(1, 0, 0, 0)); + + EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 0, 0))); + EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 0, 1))); + EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 1, 0))); + EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 1, 0, 0))); + EXPECT_TRUE(Rect(0, 1, 0, 0) < Rect(1, 0, 0, 0)); + + EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 0, 0))); + EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 0, 1))); + EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 1, 0))); + EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 1, 0, 0))); + EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(1, 0, 0, 0))); + } + { // Construction from and comparison with SDL objects SDL_Rect sdlrect = { 1, 2, 3, 4 };