From 36a9d70d7cdabb71b84a4deee7c35bdc8b10a4a3 Mon Sep 17 00:00:00 2001 From: KurtisT Date: Thu, 9 Feb 2023 12:13:14 -0500 Subject: [PATCH] Feature/span equality operators (#670) * feat: implement equality operator * test: test equality operator * feat: implement not equal operator * test: test not equal operator --------- Co-authored-by: Kurtis Thrush --- include/etl/span.h | 36 +++++++++++++++++++++++++++++++ test/test_span_dynamic_extent.cpp | 28 ++++++++++++++++++++++++ test/test_span_fixed_extent.cpp | 28 ++++++++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/include/etl/span.h b/include/etl/span.h index 32f27106..bfd7a4f4 100644 --- a/include/etl/span.h +++ b/include/etl/span.h @@ -345,6 +345,24 @@ namespace etl return pbegin[i]; } + //************************************************************************* + /// Compare two spans for equality. + //************************************************************************* + template , value_type>::value, bool> = true> + ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span& other) const ETL_NOEXCEPT + { + return ((pbegin == other.pbegin) && (Extent == other.size())); + } + + //************************************************************************* + /// Compare two spans for non-equality. + //************************************************************************* + template , value_type>::value, bool> = true> + ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span& other) const ETL_NOEXCEPT + { + return !(*this == other); + } + //************************************************************************* /// Obtains a span that is a view over the first COUNT elements of this span. //************************************************************************* @@ -731,6 +749,24 @@ namespace etl return pbegin[i]; } + //************************************************************************* + /// Compare two spans for equality. + //************************************************************************* + template , value_type>::value, bool> = true> + ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span& other) const ETL_NOEXCEPT + { + return ((pbegin == other.pbegin) && (pend == other.pend)); + } + + //************************************************************************* + /// Compare two spans for non-equality. + //************************************************************************* + template , value_type>::value, bool> = true> + ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span& other) const ETL_NOEXCEPT + { + return !(*this == other); + } + //************************************************************************* /// Obtains a span that is a view over the first COUNT elements of this span. //************************************************************************* diff --git a/test/test_span_dynamic_extent.cpp b/test/test_span_dynamic_extent.cpp index 8223fbfd..944b991e 100644 --- a/test/test_span_dynamic_extent.cpp +++ b/test/test_span_dynamic_extent.cpp @@ -970,6 +970,34 @@ namespace } } + //************************************************************************* + TEST(test_operator_equality) + { + etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + View view1{data1}; + View view2{data1}; + View view3{data2}; + + CHECK_TRUE((view1 == view2)); + CHECK_FALSE((view1 == view3)); + } + + //************************************************************************* + TEST(test_operator_not_equal) + { + etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + View view1{data1}; + View view2{data1}; + View view3{data2}; + + CHECK_FALSE((view1 != view2)); + CHECK_TRUE((view1 != view3)); + } + #include "etl/private/diagnostic_pop.h" }; } diff --git a/test/test_span_fixed_extent.cpp b/test/test_span_fixed_extent.cpp index dba32320..82b3a51f 100644 --- a/test/test_span_fixed_extent.cpp +++ b/test/test_span_fixed_extent.cpp @@ -960,6 +960,34 @@ namespace } } + //************************************************************************* + TEST(test_operator_equality) + { + etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + View view1{data1}; + View view2{data1}; + View view3{data2}; + + CHECK_TRUE((view1 == view2)); + CHECK_FALSE((view1 == view3)); + } + + //************************************************************************* + TEST(test_operator_not_equal) + { + etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + View view1{data1}; + View view2{data1}; + View view3{data2}; + + CHECK_FALSE((view1 != view2)); + CHECK_TRUE((view1 != view3)); + } + #include "etl/private/diagnostic_pop.h" }; }