From aca7b1d05aedf92b3671d7469e5ffed11bb4d2a5 Mon Sep 17 00:00:00 2001 From: grigorev Date: Wed, 13 Mar 2024 17:25:45 +0300 Subject: [PATCH 1/4] Add eq and ne operators --- include/etl/compare.h | 12 +++++++++++- test/test_compare.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/include/etl/compare.h b/include/etl/compare.h index 479758d95..8b3bfd34e 100644 --- a/include/etl/compare.h +++ b/include/etl/compare.h @@ -44,7 +44,7 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// Defines <=, >, >= in terms of < + /// Defines <=, >, >=, ==, != in terms of < /// Default //*************************************************************************** template > @@ -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); + } }; } diff --git a/test/test_compare.cpp b/test/test_compare.cpp index a11d611ad..b929d3673 100644 --- a/test/test_compare.cpp +++ b/test/test_compare.cpp @@ -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 { @@ -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))); + } }; } From 320819275f57d07c85c0a53e2b74ea799ba95b83 Mon Sep 17 00:00:00 2001 From: grigorev Date: Fri, 15 Mar 2024 15:53:02 +0300 Subject: [PATCH 2/4] Add three-way comparison to compare utils. --- include/etl/compare.h | 24 +++++++++++++++++++++++- test/test_compare.cpp | 11 +++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/etl/compare.h b/include/etl/compare.h index 8b3bfd34e..5a28c83df 100644 --- a/include/etl/compare.h +++ b/include/etl/compare.h @@ -44,7 +44,7 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// Defines <=, >, >=, ==, != in terms of < + /// Defines <=, >, >=, ==, !=, <=> in terms of < /// Default //*************************************************************************** template > @@ -54,6 +54,13 @@ namespace etl typedef typename etl::parameter_type::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); @@ -83,6 +90,21 @@ namespace etl { 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; + } }; } diff --git a/test/test_compare.cpp b/test/test_compare.cpp index b929d3673..f75240f3a 100644 --- a/test/test_compare.cpp +++ b/test/test_compare.cpp @@ -160,5 +160,16 @@ namespace 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(a, b))); + CHECK_EQUAL(CompareInt::GREATER, (CompareInt::cmp(b, a))); + CHECK_EQUAL(CompareInt::EQUAL, (CompareInt::cmp(a, a))); + CHECK_EQUAL(CompareTest::LESS, (CompareTest::cmp(ta, tb))); + CHECK_EQUAL(CompareTest::GREATER, (CompareTest::cmp(tb, ta))); + CHECK_EQUAL(CompareTest::EQUAL, (CompareTest::cmp(ta, ta))); + } }; } From fc19b129bb1f1bb494e97e8926ff0402f815bbd8 Mon Sep 17 00:00:00 2001 From: grigorev Date: Fri, 15 Mar 2024 17:30:29 +0300 Subject: [PATCH 3/4] Three-way comparison test now using own test data. --- test/test_compare.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/test_compare.cpp b/test/test_compare.cpp index f75240f3a..838d48520 100644 --- a/test/test_compare.cpp +++ b/test/test_compare.cpp @@ -37,47 +37,47 @@ namespace typedef etl::compare CompareInt; - struct Test + struct TestStruct { int a; int b; }; - Test ta = { 1, 2 }; - Test tb = { 2, 3 }; + TestStruct ta = { 1, 2 }; + TestStruct tb = { 2, 3 }; //*********************************** - bool operator <(const Test& lhs, const Test& rhs) + bool operator <(const TestStruct& lhs, const TestStruct& rhs) { return (lhs.a + lhs.b) < (rhs.a + rhs.b); } //*********************************** - bool operator >(const Test& lhs, const Test& rhs) + bool operator >(const TestStruct& lhs, const TestStruct& rhs) { return (lhs.a + lhs.b) > (rhs.a + rhs.b); } //*********************************** - bool operator <=(const Test& lhs, const Test& rhs) + bool operator <=(const TestStruct& lhs, const TestStruct& rhs) { return (lhs.a + lhs.b) <= (rhs.a + rhs.b); } //*********************************** - bool operator >=(const Test& lhs, const Test& rhs) + bool operator >=(const TestStruct& lhs, const TestStruct& rhs) { return (lhs.a + lhs.b) >= (rhs.a + rhs.b); } //*********************************** - bool operator ==(const Test& lhs, const Test& rhs) + bool operator ==(const TestStruct& lhs, const TestStruct& rhs) { return (lhs.a + lhs.b) == (rhs.a + rhs.b); } //*********************************** - bool operator !=(const Test& lhs, const Test& rhs) + bool operator !=(const TestStruct& lhs, const TestStruct& rhs) { return (lhs.a + lhs.b) != (rhs.a + rhs.b); } @@ -85,13 +85,13 @@ namespace //*********************************** struct LessTest { - bool operator()(const Test& lhs, const Test& rhs) const + bool operator()(const TestStruct& lhs, const TestStruct& rhs) const { return (lhs.a + lhs.b) < (rhs.a + rhs.b); } }; - typedef etl::compare CompareTest; + typedef etl::compare CompareTest; SUITE(test_compare) { @@ -164,12 +164,12 @@ namespace //************************************************************************* TEST(test_cmp) { - CHECK_EQUAL(CompareInt::LESS, (CompareInt::cmp(a, b))); - CHECK_EQUAL(CompareInt::GREATER, (CompareInt::cmp(b, a))); - CHECK_EQUAL(CompareInt::EQUAL, (CompareInt::cmp(a, a))); - CHECK_EQUAL(CompareTest::LESS, (CompareTest::cmp(ta, tb))); - CHECK_EQUAL(CompareTest::GREATER, (CompareTest::cmp(tb, ta))); - CHECK_EQUAL(CompareTest::EQUAL, (CompareTest::cmp(ta, ta))); + 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(TestStruct(0, 1), TestStruct(2, 4)))); + CHECK_EQUAL(CompareTest::GREATER, (CompareTest::cmp(TestStruct(2, 4), TestStruct(0, 1)))); + CHECK_EQUAL(CompareTest::EQUAL, (CompareTest::cmp(TestStruct(2, 4), TestStruct(2, 4)))); } }; } From baf2eab6f46dfa875c6dc390d0503849324e8df0 Mon Sep 17 00:00:00 2001 From: grigorev Date: Fri, 15 Mar 2024 17:41:53 +0300 Subject: [PATCH 4/4] Rename test struct for three-way comparison test. --- test/test_compare.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/test_compare.cpp b/test/test_compare.cpp index 838d48520..5e666f574 100644 --- a/test/test_compare.cpp +++ b/test/test_compare.cpp @@ -37,47 +37,47 @@ namespace typedef etl::compare CompareInt; - struct TestStruct + struct Object { int a; int b; }; - TestStruct ta = { 1, 2 }; - TestStruct tb = { 2, 3 }; + Object ta = { 1, 2 }; + Object tb = { 2, 3 }; //*********************************** - bool operator <(const TestStruct& lhs, const TestStruct& rhs) + bool operator <(const Object& lhs, const Object& rhs) { return (lhs.a + lhs.b) < (rhs.a + rhs.b); } //*********************************** - bool operator >(const TestStruct& lhs, const TestStruct& rhs) + bool operator >(const Object& lhs, const Object& rhs) { return (lhs.a + lhs.b) > (rhs.a + rhs.b); } //*********************************** - bool operator <=(const TestStruct& lhs, const TestStruct& rhs) + bool operator <=(const Object& lhs, const Object& rhs) { return (lhs.a + lhs.b) <= (rhs.a + rhs.b); } //*********************************** - bool operator >=(const TestStruct& lhs, const TestStruct& rhs) + bool operator >=(const Object& lhs, const Object& rhs) { return (lhs.a + lhs.b) >= (rhs.a + rhs.b); } //*********************************** - bool operator ==(const TestStruct& lhs, const TestStruct& rhs) + bool operator ==(const Object& lhs, const Object& rhs) { return (lhs.a + lhs.b) == (rhs.a + rhs.b); } //*********************************** - bool operator !=(const TestStruct& lhs, const TestStruct& rhs) + bool operator !=(const Object& lhs, const Object& rhs) { return (lhs.a + lhs.b) != (rhs.a + rhs.b); } @@ -85,13 +85,13 @@ namespace //*********************************** struct LessTest { - bool operator()(const TestStruct& lhs, const TestStruct& rhs) const + bool operator()(const Object& lhs, const Object& rhs) const { return (lhs.a + lhs.b) < (rhs.a + rhs.b); } }; - typedef etl::compare CompareTest; + typedef etl::compare CompareTest; SUITE(test_compare) { @@ -167,9 +167,9 @@ namespace 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(TestStruct(0, 1), TestStruct(2, 4)))); - CHECK_EQUAL(CompareTest::GREATER, (CompareTest::cmp(TestStruct(2, 4), TestStruct(0, 1)))); - CHECK_EQUAL(CompareTest::EQUAL, (CompareTest::cmp(TestStruct(2, 4), TestStruct(2, 4)))); + 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)))); } }; }