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 <kthrush@jlg.com>
This commit is contained in:
KurtisT 2023-02-09 12:13:14 -05:00 committed by GitHub
parent 8634480fa6
commit 36a9d70d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 0 deletions

View File

@ -345,6 +345,24 @@ namespace etl
return pbegin[i];
}
//*************************************************************************
/// Compare two spans for equality.
//*************************************************************************
template <typename Type2, size_t N2, etl::enable_if_t<etl::is_same<etl::remove_cv_t<Type2>, value_type>::value, bool> = true>
ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span<Type2, N2>& other) const ETL_NOEXCEPT
{
return ((pbegin == other.pbegin) && (Extent == other.size()));
}
//*************************************************************************
/// Compare two spans for non-equality.
//*************************************************************************
template <typename Type2, size_t N2, etl::enable_if_t<etl::is_same<etl::remove_cv_t<Type2>, value_type>::value, bool> = true>
ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span<Type2, N2>& 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 <typename Type2, etl::enable_if_t<etl::is_same<etl::remove_cv_t<Type2>, value_type>::value, bool> = true>
ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span<Type2>& other) const ETL_NOEXCEPT
{
return ((pbegin == other.pbegin) && (pend == other.pend));
}
//*************************************************************************
/// Compare two spans for non-equality.
//*************************************************************************
template <typename Type2, etl::enable_if_t<etl::is_same<etl::remove_cv_t<Type2>, value_type>::value, bool> = true>
ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span<Type2>& other) const ETL_NOEXCEPT
{
return !(*this == other);
}
//*************************************************************************
/// Obtains a span that is a view over the first COUNT elements of this span.
//*************************************************************************

View File

@ -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"
};
}

View File

@ -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"
};
}