Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eq and ne operators for compare utility. #863

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion include/etl/compare.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ SOFTWARE.
namespace etl
{
//***************************************************************************
/// Defines <=, >, >= in terms of <
/// Defines <=, >, >=, ==, != in terms of <
/// Default
//***************************************************************************
template <typename T, typename TLess = etl::less<T> >
Expand Down Expand Up @@ -73,6 +73,16 @@ namespace etl
{
return !lt(lhs, rhs);
}

static result_type eq(first_argument_type lhs, second_argument_type rhs)
{
return gte(lhs, rhs) && lte(lhs, rhs);
}

static result_type ne(first_argument_type lhs, second_argument_type rhs)
{
return !eq(lhs, rhs);
}
};
}

Expand Down
34 changes: 34 additions & 0 deletions test/test_compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ namespace
return (lhs.a + lhs.b) >= (rhs.a + rhs.b);
}

//***********************************
bool operator ==(const Test& lhs, const Test& rhs)
{
return (lhs.a + lhs.b) == (rhs.a + rhs.b);
}

//***********************************
bool operator !=(const Test& lhs, const Test& rhs)
{
return (lhs.a + lhs.b) != (rhs.a + rhs.b);
}

//***********************************
struct LessTest
{
Expand Down Expand Up @@ -126,5 +138,27 @@ namespace
CHECK_EQUAL(tb >= ta, (CompareTest::gte(tb, ta)));
CHECK_EQUAL(ta >= ta, (CompareTest::gte(ta, ta)));
}

//*************************************************************************
TEST(test_eq)
{
CHECK_EQUAL(a == b, (CompareInt::eq(a, b)));
CHECK_EQUAL(b == a, (CompareInt::eq(b, a)));
CHECK_EQUAL(a == a, (CompareInt::eq(a, a)));
CHECK_EQUAL(ta == tb, (CompareTest::eq(ta, tb)));
CHECK_EQUAL(tb == ta, (CompareTest::eq(tb, ta)));
CHECK_EQUAL(ta == ta, (CompareTest::eq(ta, ta)));
}

//*************************************************************************
TEST(test_ne)
{
CHECK_EQUAL(a != b, (CompareInt::ne(a, b)));
CHECK_EQUAL(b != a, (CompareInt::ne(b, a)));
CHECK_EQUAL(a != a, (CompareInt::ne(a, a)));
CHECK_EQUAL(ta != tb, (CompareTest::ne(ta, tb)));
CHECK_EQUAL(tb != ta, (CompareTest::ne(tb, ta)));
CHECK_EQUAL(ta != ta, (CompareTest::ne(ta, ta)));
}
};
}