Skip to content

Commit

Permalink
Implemented relational operators for Point. (#207)
Browse files Browse the repository at this point in the history
* Implemented relational operators for Point.

Used the Boost.Operators mechanism to implement it.  Also refactored
existing +,- operators to work in terms of that.

* Reimplemented less-operator for point

std::tie is not constepxr on Travis, so this has to be factored out for now.
  • Loading branch information
KazDragon authored Aug 26, 2019
1 parent 55459a0 commit fe7b0e3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
35 changes: 13 additions & 22 deletions include/terminalpp/point.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "terminalpp/core.hpp"
#include <boost/operators.hpp>
#include <iosfwd>

namespace terminalpp {
Expand All @@ -13,6 +14,10 @@ namespace terminalpp {
/// axis.
//* =========================================================================
struct point
: private boost::less_than_comparable<point,
boost::equality_comparable<point,
boost::addable<point,
boost::subtractable<point>>>>
{
//* =====================================================================
/// \brief Default Constructor
Expand Down Expand Up @@ -62,35 +67,21 @@ struct point
};

// ==========================================================================
// OPERATOR==(POINT,POINT)
// ==========================================================================
constexpr bool operator==(point const &lhs, point const &rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}

// ==========================================================================
// OPERATOR!=(POINT,POINT)
// OPERATOR<(POINT,POINT)
// ==========================================================================
constexpr bool operator!=(point const &lhs, point const &rhs)
constexpr bool operator<(point const &lhs, point const &rhs)
{
return !(lhs == rhs);
// Note: reimplemented due to std::tie not being constexpr everywhere.
return lhs.y < rhs.y
|| (lhs.y == rhs.y && lhs.x < rhs.x);
}

// ==========================================================================
// OPERATOR+(POINT,POINT)
// ==========================================================================
constexpr point operator+(point lhs, point const &rhs)
{
return lhs += rhs;
}

// ==========================================================================
// OPERATOR-(POINT,POINT)
// OPERATOR==(POINT,POINT)
// ==========================================================================
constexpr point operator-(point lhs, point const &rhs)
constexpr bool operator==(point const &lhs, point const &rhs)
{
return lhs -= rhs;
return lhs.x == rhs.x && lhs.y == rhs.y;
}

//* =====================================================================
Expand Down
49 changes: 49 additions & 0 deletions test/point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,52 @@ INSTANTIATE_TEST_CASE_P(
point_string{ {-4, 63}, "point(-4,63)" },
point_string{ {96583, 1231234}, "point(96583,1231234)" }
}));

using point_relops_data = std::tuple<
terminalpp::point, // lhs
terminalpp::point, // rhs
bool, // less
bool, // less-equal
bool, // equal
bool, // greater-equal
bool // greater
>;

class points_compare : public testing::TestWithParam<point_relops_data>
{
};

TEST_P(points_compare, according_to_relops)
{
auto const &param = GetParam();
auto const &lhs = std::get<0>(param);
auto const &rhs = std::get<1>(param);
auto const &less = std::get<2>(param);
auto const &less_equal = std::get<3>(param);
auto const &equal = std::get<4>(param);
auto const &greater_equal = std::get<5>(param);
auto const &greater = std::get<6>(param);

ASSERT_EQ(less, lhs < rhs);
ASSERT_EQ(less_equal, lhs <= rhs);
ASSERT_EQ(equal, lhs == rhs);
ASSERT_EQ(!equal, lhs != rhs);
ASSERT_EQ(greater_equal, lhs >= rhs);
ASSERT_EQ(greater, lhs > rhs);
}

INSTANTIATE_TEST_CASE_P(
using_relational_operators,
points_compare,
ValuesIn(std::vector<point_relops_data>{
point_relops_data{{0, 0}, {1, 1}, true, true, false, false, false },
point_relops_data{{1, 0}, {1, 1}, true, true, false, false, false },
point_relops_data{{2, 0}, {1, 1}, true, true, false, false, false },
point_relops_data{{0, 1}, {1, 1}, true, true, false, false, false },
point_relops_data{{1, 1}, {1, 1}, false, true, true, true, false },
point_relops_data{{2, 1}, {1, 1}, false, false, false, true, true },
point_relops_data{{0, 2}, {1, 1}, false, false, false, true, true },
point_relops_data{{1, 2}, {1, 1}, false, false, false, true, true },
point_relops_data{{2, 2}, {1, 1}, false, false, false, true, true }
}));

0 comments on commit fe7b0e3

Please sign in to comment.