mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-29 13:58:44 +08:00
feature/add utils compare equality operator (#865)
* Add eq and ne operators * Add three-way comparison to compare utils. * Three-way comparison test now using own test data. * Rename test struct for three-way comparison test. * Quick fix for pre c++20 aggregate initialization. --------- Co-authored-by: grigorev <grigorev@protei.ru>
This commit is contained in:
parent
a98d387a11
commit
d40c5be796
@ -44,7 +44,7 @@ SOFTWARE.
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Defines <=, >, >= in terms of <
|
||||
/// Defines <=, >, >=, ==, !=, <=> in terms of <
|
||||
/// Default
|
||||
//***************************************************************************
|
||||
template <typename T, typename TLess = etl::less<T> >
|
||||
@ -54,6 +54,13 @@ namespace etl
|
||||
typedef typename etl::parameter_type<T>::type second_argument_type;
|
||||
typedef bool result_type;
|
||||
|
||||
enum cmp_result
|
||||
{
|
||||
LESS = -1,
|
||||
EQUAL = 0,
|
||||
GREATER = 1
|
||||
};
|
||||
|
||||
static result_type lt(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return TLess()(lhs, rhs);
|
||||
@ -73,6 +80,31 @@ 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);
|
||||
}
|
||||
|
||||
static cmp_result cmp(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
if (lt(lhs, rhs))
|
||||
{
|
||||
return LESS;
|
||||
}
|
||||
|
||||
if (gt(lhs, rhs))
|
||||
{
|
||||
return GREATER;
|
||||
}
|
||||
|
||||
return EQUAL;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -37,49 +37,61 @@ namespace
|
||||
|
||||
typedef etl::compare<int> CompareInt;
|
||||
|
||||
struct Test
|
||||
struct Object
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
Test ta = { 1, 2 };
|
||||
Test tb = { 2, 3 };
|
||||
Object ta = { 1, 2 };
|
||||
Object tb = { 2, 3 };
|
||||
|
||||
//***********************************
|
||||
bool operator <(const Test& lhs, const Test& rhs)
|
||||
bool operator <(const Object& lhs, const Object& rhs)
|
||||
{
|
||||
return (lhs.a + lhs.b) < (rhs.a + rhs.b);
|
||||
}
|
||||
|
||||
//***********************************
|
||||
bool operator >(const Test& lhs, const Test& rhs)
|
||||
bool operator >(const Object& lhs, const Object& rhs)
|
||||
{
|
||||
return (lhs.a + lhs.b) > (rhs.a + rhs.b);
|
||||
}
|
||||
|
||||
//***********************************
|
||||
bool operator <=(const Test& lhs, const Test& rhs)
|
||||
bool operator <=(const Object& lhs, const Object& rhs)
|
||||
{
|
||||
return (lhs.a + lhs.b) <= (rhs.a + rhs.b);
|
||||
}
|
||||
|
||||
//***********************************
|
||||
bool operator >=(const Test& lhs, const Test& rhs)
|
||||
bool operator >=(const Object& lhs, const Object& rhs)
|
||||
{
|
||||
return (lhs.a + lhs.b) >= (rhs.a + rhs.b);
|
||||
}
|
||||
|
||||
//***********************************
|
||||
bool operator ==(const Object& lhs, const Object& rhs)
|
||||
{
|
||||
return (lhs.a + lhs.b) == (rhs.a + rhs.b);
|
||||
}
|
||||
|
||||
//***********************************
|
||||
bool operator !=(const Object& lhs, const Object& rhs)
|
||||
{
|
||||
return (lhs.a + lhs.b) != (rhs.a + rhs.b);
|
||||
}
|
||||
|
||||
//***********************************
|
||||
struct LessTest
|
||||
{
|
||||
bool operator()(const Test& lhs, const Test& rhs) const
|
||||
bool operator()(const Object& lhs, const Object& rhs) const
|
||||
{
|
||||
return (lhs.a + lhs.b) < (rhs.a + rhs.b);
|
||||
}
|
||||
};
|
||||
|
||||
typedef etl::compare<Test, LessTest> CompareTest;
|
||||
typedef etl::compare<Object, LessTest> CompareTest;
|
||||
|
||||
SUITE(test_compare)
|
||||
{
|
||||
@ -126,5 +138,38 @@ 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)));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_cmp)
|
||||
{
|
||||
CHECK_EQUAL(CompareInt::LESS, (CompareInt::cmp(2, 4)));
|
||||
CHECK_EQUAL(CompareInt::GREATER, (CompareInt::cmp(4, 2)));
|
||||
CHECK_EQUAL(CompareInt::EQUAL, (CompareInt::cmp(0, 0)));
|
||||
CHECK_EQUAL(CompareTest::LESS, (CompareTest::cmp(Object{0, 1}, Object{2, 4})));
|
||||
CHECK_EQUAL(CompareTest::GREATER, (CompareTest::cmp(Object{2, 4}, Object{0, 1})));
|
||||
CHECK_EQUAL(CompareTest::EQUAL, (CompareTest::cmp(Object{2, 4}, Object{2, 4})));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user