etl::span checks (#1201)

* Implement checks

* handle cpp11 constexpr exceptions

* Cant use local variables

* Tests should be implemented

* Try to fix msvc

* Fix etl error text
This commit is contained in:
mike919192 2025-10-13 12:34:34 -04:00 committed by GitHub
parent 2c170118ff
commit 4147216231
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 217 additions and 26 deletions

View File

@ -159,7 +159,7 @@ namespace etl
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_DEBUG_NOT_USING_EXCEPTIONS)
{
// Throwing from c++11 constexpr requires special syntax
#if ETL_USING_CPP11 && !ETL_USING_CPP14 && ETL_USING_EXCEPTIONS && defined(ETL_CHECK_INDEX_OPERATOR)
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR
return i < SIZE ? _buffer[i] : throw(ETL_ERROR(array_out_of_range));
#else
ETL_ASSERT_CHECK_INDEX_OPERATOR(i < SIZE, ETL_ERROR(array_out_of_range));

View File

@ -375,15 +375,23 @@ namespace etl
//*************************************
#ifdef ETL_CHECK_INDEX_OPERATOR
#define ETL_CHECKING_INDEX_OPERATOR 1
#define ETL_NOT_CHECKING_INDEX_OPERATOR 0
#define ETL_ASSERT_CHECK_INDEX_OPERATOR(b, e) ETL_ASSERT(b,e)
#else
#define ETL_CHECKING_INDEX_OPERATOR 0
#define ETL_NOT_CHECKING_INDEX_OPERATOR 1
#define ETL_ASSERT_CHECK_INDEX_OPERATOR(b, e)
#endif
//*************************************
#ifdef ETL_CHECK_EXTRA
#define ETL_CHECKING_EXTRA 1
#define ETL_NOT_CHECKING_EXTRA 0
#define ETL_ASSERT_CHECK_EXTRA(b, e) ETL_ASSERT(b,e)
#else
#define ETL_CHECKING_EXTRA 0
#define ETL_NOT_CHECKING_EXTRA 1
#define ETL_ASSERT_CHECK_EXTRA(b, e)
#endif

View File

@ -102,6 +102,20 @@ namespace etl
}
};
//***************************************************************************
///\ingroup span
/// The out of range exceptions.
//***************************************************************************
class span_out_of_range : public span_exception
{
public:
span_out_of_range(string_type file_name_, numeric_type line_number_)
: span_exception(ETL_ERROR_TEXT("span:range", ETL_SPAN_FILE_ID"C"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Span - Fixed Extent
//***************************************************************************
@ -241,6 +255,8 @@ namespace etl
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR reference front() const ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(Extent > 0, "Span is empty");
return *pbegin;
}
@ -249,6 +265,8 @@ namespace etl
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR reference back() const ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(Extent > 0, "Span is empty");
return *((pbegin + Extent) - 1);
}
@ -386,7 +404,7 @@ namespace etl
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i)
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
ETL_ASSERT(i < size(), ETL_ERROR(span_out_of_range));
return pbegin[i];
}
@ -396,7 +414,7 @@ namespace etl
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
ETL_ASSERT(i < size(), ETL_ERROR(span_out_of_range));
return pbegin[i];
}
@ -406,7 +424,13 @@ namespace etl
//*************************************************************************
ETL_CONSTEXPR reference operator[](const size_t i) const
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR
return i < size() ? pbegin[i] : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(span_out_of_range));
return pbegin[i];
#endif
}
//*************************************************************************
@ -416,7 +440,7 @@ namespace etl
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> first() const ETL_NOEXCEPT
{
// If Extent is static, check that original span contains at least COUNT elements
ETL_STATIC_ASSERT((Extent != etl::dynamic_extent) ? COUNT <= Extent : true, "Original span does not contain COUNT elements");
ETL_STATIC_ASSERT(COUNT <= Extent, "Original span does not contain COUNT elements");
return etl::span<element_type, COUNT>(pbegin, pbegin + COUNT);
}
@ -424,9 +448,15 @@ namespace etl
//*************************************************************************
/// Obtains a span that is a view over the first count elements of this span.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> first(size_t count) const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> first(size_t count) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return count <= size() ? etl::span<element_type, etl::dynamic_extent>(pbegin, pbegin + count) : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(count <= size(), ETL_ERROR(span_out_of_range));
return etl::span<element_type, etl::dynamic_extent>(pbegin, pbegin + count);
#endif
}
//*************************************************************************
@ -436,7 +466,7 @@ namespace etl
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> last() const ETL_NOEXCEPT
{
// If Extent is static, check that original span contains at least COUNT elements
ETL_STATIC_ASSERT((Extent != etl::dynamic_extent) ? COUNT <= Extent : true, "Original span does not contain COUNT elements");
ETL_STATIC_ASSERT(COUNT <= Extent, "Original span does not contain COUNT elements");
return etl::span<element_type, COUNT>(pbegin + Extent - COUNT, (pbegin + Extent));
}
@ -444,9 +474,17 @@ namespace etl
//*************************************************************************
/// Obtains a span that is a view over the last count elements of this span.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> last(size_t count) const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> last(size_t count) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return count <= size() ?
etl::span<element_type, etl::dynamic_extent>((pbegin + Extent) - count, (pbegin + Extent)) :
throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(count <= size(), ETL_ERROR(span_out_of_range));
return etl::span<element_type, etl::dynamic_extent>((pbegin + Extent) - count, (pbegin + Extent));
#endif
}
#if ETL_USING_CPP11
@ -458,10 +496,10 @@ namespace etl
etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : Extent - OFFSET> subspan() const ETL_NOEXCEPT
{
// If Extent is static, check that OFFSET is within the original span
ETL_STATIC_ASSERT((Extent != etl::dynamic_extent) ? OFFSET <= Extent : true, "OFFSET is not within the original span");
ETL_STATIC_ASSERT(OFFSET <= Extent, "OFFSET is not within the original span");
// If count is also static, check that OFFSET + COUNT is within the original span
ETL_STATIC_ASSERT((Extent != etl::dynamic_extent) && (COUNT != etl::dynamic_extent) ? COUNT <= (Extent - OFFSET) : true, "OFFSET + COUNT is not within the original span");
ETL_STATIC_ASSERT((COUNT != etl::dynamic_extent) ? COUNT <= (Extent - OFFSET) : true, "OFFSET + COUNT is not within the original span");
return (COUNT == etl::dynamic_extent) ? etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : Extent - OFFSET>(pbegin + OFFSET, (pbegin + Extent))
: etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : Extent - OFFSET>(pbegin + OFFSET, pbegin + OFFSET + COUNT);
@ -474,10 +512,10 @@ namespace etl
etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : Extent - OFFSET> subspan() const
{
// If Extent is static, check that OFFSET is within the original span
ETL_STATIC_ASSERT((Extent != etl::dynamic_extent) ? OFFSET <= Extent : true, "OFFSET is not within the original span");
ETL_STATIC_ASSERT(OFFSET <= Extent, "OFFSET is not within the original span");
// If count is also static, check that OFFSET + COUNT is within the original span
ETL_STATIC_ASSERT((Extent != etl::dynamic_extent) && (COUNT != etl::dynamic_extent) ? COUNT <= (Extent - OFFSET) : true, "OFFSET + COUNT is not within the original span");
ETL_STATIC_ASSERT((COUNT != etl::dynamic_extent) ? COUNT <= (Extent - OFFSET) : true, "OFFSET + COUNT is not within the original span");
if (COUNT == etl::dynamic_extent)
{
@ -493,10 +531,20 @@ namespace etl
//*************************************************************************
/// Obtains a span that is a view from 'offset' over the next 'count' elements of this span.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> subspan(size_t offset, size_t count = etl::dynamic_extent) const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> subspan(size_t offset, size_t count = etl::dynamic_extent) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return (offset <= size()) && (count != etl::dynamic_extent ? count <= (size() - offset) : true) ?
((count == etl::dynamic_extent) ? etl::span<element_type, etl::dynamic_extent>(pbegin + offset, (pbegin + Extent))
: etl::span<element_type, etl::dynamic_extent>(pbegin + offset, pbegin + offset + count)) :
throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(offset <= size(), ETL_ERROR(span_out_of_range));
ETL_ASSERT_CHECK_EXTRA(count != etl::dynamic_extent ? count <= (size() - offset) : true, ETL_ERROR(span_out_of_range));
return (count == etl::dynamic_extent) ? etl::span<element_type, etl::dynamic_extent>(pbegin + offset, (pbegin + Extent))
: etl::span<element_type, etl::dynamic_extent>(pbegin + offset, pbegin + offset + count);
#endif
}
//*************************************************************************
@ -667,17 +715,29 @@ namespace etl
//*************************************************************************
/// Returns a reference to the first element.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR reference front() const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR reference front() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return size() > 0 ? *pbegin : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(size() > 0, ETL_ERROR(span_out_of_range));
return *pbegin;
#endif
}
//*************************************************************************
/// Returns a reference to the last element.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR reference back() const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR reference back() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return size() > 0 ? *(pend - 1) : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(size() > 0, ETL_ERROR(span_out_of_range));
return *(pend - 1);
#endif
}
//*************************************************************************
@ -815,7 +875,7 @@ namespace etl
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i)
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
ETL_ASSERT(i < size(), ETL_ERROR(span_out_of_range));
return pbegin[i];
}
@ -825,7 +885,7 @@ namespace etl
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const
{
ETL_ASSERT(i < size(), ETL_ERROR(array_out_of_range));
ETL_ASSERT(i < size(), ETL_ERROR(span_out_of_range));
return pbegin[i];
}
@ -835,41 +895,71 @@ namespace etl
//*************************************************************************
ETL_CONSTEXPR reference operator[](const size_t i) const
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR
return i < size() ? pbegin[i] : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(span_out_of_range));
return pbegin[i];
#endif
}
//*************************************************************************
/// Obtains a span that is a view over the first COUNT elements of this span.
//*************************************************************************
template <size_t COUNT>
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> first() const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> first() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return COUNT <= size() ? etl::span<element_type, COUNT>(pbegin, pbegin + COUNT) : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(COUNT <= size(), ETL_ERROR(span_out_of_range));
return etl::span<element_type, COUNT>(pbegin, pbegin + COUNT);
#endif
}
//*************************************************************************
/// Obtains a span that is a view over the first count elements of this span.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> first(size_t count) const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> first(size_t count) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return count <= size() ? etl::span<element_type, etl::dynamic_extent>(pbegin, pbegin + count) : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(count <= size(), ETL_ERROR(span_out_of_range));
return etl::span<element_type, etl::dynamic_extent>(pbegin, pbegin + count);
#endif
}
//*************************************************************************
/// Obtains a span that is a view over the last COUNT elements of this span.
//*************************************************************************
template <size_t COUNT>
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> last() const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> last() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return COUNT <= size() ? etl::span<element_type, COUNT>(pend - COUNT, pend) : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(COUNT <= size(), ETL_ERROR(span_out_of_range));
return etl::span<element_type, COUNT>(pend - COUNT, pend);
#endif
}
//*************************************************************************
/// Obtains a span that is a view over the last count elements of this span.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> last(size_t count) const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, etl::dynamic_extent> last(size_t count) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return count <= size() ? etl::span<element_type, etl::dynamic_extent>(pend - count, pend) : throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(count <= size(), ETL_ERROR(span_out_of_range));
return etl::span<element_type, etl::dynamic_extent>(pend - count, pend);
#endif
}
#if ETL_USING_CPP11
@ -878,10 +968,20 @@ namespace etl
//*************************************************************************
template <size_t OFFSET, size_t COUNT = etl::dynamic_extent>
ETL_NODISCARD ETL_CONSTEXPR
etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent> subspan() const ETL_NOEXCEPT
etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent> subspan() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return (OFFSET <= size()) && (COUNT != etl::dynamic_extent ? COUNT <= (size() - OFFSET) : true) ?
((COUNT == etl::dynamic_extent) ? etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent>(pbegin + OFFSET, pend)
: etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent>(pbegin + OFFSET, pbegin + OFFSET + COUNT)) :
throw(ETL_ERROR(span_out_of_range));
#else
ETL_ASSERT_CHECK_EXTRA(OFFSET <= size(), ETL_ERROR(span_out_of_range));
ETL_ASSERT_CHECK_EXTRA(COUNT != etl::dynamic_extent ? COUNT <= (size() - OFFSET) : true, ETL_ERROR(span_out_of_range));
return (COUNT == etl::dynamic_extent) ? etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent>(pbegin + OFFSET, pend)
: etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent>(pbegin + OFFSET, pbegin + OFFSET + COUNT);
#endif
}
#else
//*************************************************************************
@ -890,6 +990,9 @@ namespace etl
template <size_t OFFSET, size_t COUNT>
etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent> subspan() const
{
ETL_ASSERT_CHECK_EXTRA(OFFSET <= size(), ETL_ERROR(span_out_of_range));
ETL_ASSERT_CHECK_EXTRA(COUNT != etl::dynamic_extent ? COUNT <= (size() - OFFSET) : true, ETL_ERROR(span_out_of_range));
if (COUNT == etl::dynamic_extent)
{
return etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : etl::dynamic_extent>(pbegin + OFFSET, pend);
@ -904,8 +1007,11 @@ namespace etl
//*************************************************************************
/// Obtains a span that is a view from 'offset' over the next 'count' elements of this span.
//*************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 etl::span<element_type, etl::dynamic_extent> subspan(size_t offset, size_t count = etl::dynamic_extent) const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR14 etl::span<element_type, etl::dynamic_extent> subspan(size_t offset, size_t count = etl::dynamic_extent) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_EXTRA)
{
ETL_ASSERT_CHECK_EXTRA(offset <= size(), ETL_ERROR(span_out_of_range));
ETL_ASSERT_CHECK_EXTRA(count != etl::dynamic_extent ? count <= (size() - offset) : true, ETL_ERROR(span_out_of_range));
return (count == etl::dynamic_extent) ? etl::span<element_type, etl::dynamic_extent>(pbegin + offset, pend)
: etl::span<element_type, etl::dynamic_extent>(pbegin + offset, pbegin + offset + count);
}

View File

@ -438,6 +438,14 @@ namespace
CHECK_EQUAL(etldata.back(), view.back());
CHECK_EQUAL(etldata.back(), cview.back());
View empty_view;
CHECK_THROW({ auto front = empty_view.front(); (void)front; }, etl::span_out_of_range);
CHECK_THROW({ auto back = empty_view.back(); (void)back; }, etl::span_out_of_range);
CView empty_cview;
CHECK_THROW({ auto front = empty_cview.front(); (void)front; }, etl::span_out_of_range);
CHECK_THROW({ auto back = empty_cview.back(); (void)back; }, etl::span_out_of_range);
}
//*************************************************************************
@ -462,8 +470,8 @@ namespace
CHECK_EQUAL(etldata.at(i), cview.at(i));
}
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::array_out_of_range);
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::array_out_of_range);
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::span_out_of_range);
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::span_out_of_range);
}
//*************************************************************************
@ -477,6 +485,9 @@ namespace
CHECK_EQUAL(etldata[i], view[i]);
CHECK_EQUAL(etldata[i], cview[i]);
}
CHECK_THROW({ int d = view[view.size()]; (void)d; }, etl::span_out_of_range);
CHECK_THROW({ int d = cview[cview.size()]; (void)d; }, etl::span_out_of_range);
}
//*************************************************************************
@ -559,6 +570,9 @@ namespace
CHECK(isEqual);
CHECK_EQUAL(first.size(), cresult.extent);
CHECK_EQUAL(first.size(), cresult.size());
CHECK_THROW({ auto result2 = view.first<9>(); (void)result2; }, etl::span_out_of_range);
CHECK_THROW({ auto cresult2 = cview.first<9>(); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -580,6 +594,9 @@ namespace
isEqual = std::equal(cresult.begin(), cresult.end(), first.begin());
CHECK(isEqual);
CHECK_EQUAL(first.size(), cresult.size());
CHECK_THROW({ auto result2 = view.first(9); (void)result2; }, etl::span_out_of_range);
CHECK_THROW({ auto cresult2 = cview.first(9); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -603,6 +620,9 @@ namespace
CHECK(isEqual);
CHECK_EQUAL(last.size(), cresult.extent);
CHECK_EQUAL(last.size(), cresult.size());
CHECK_THROW({ auto result2 = view.last<9>(); (void)result2; }, etl::span_out_of_range);
CHECK_THROW({ auto cresult2 = cview.last<9>(); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -624,6 +644,9 @@ namespace
isEqual = std::equal(cresult.begin(), cresult.end(), last.begin());
CHECK(isEqual);
CHECK_EQUAL(last.size(), cresult.size());
CHECK_THROW({ auto result2 = view.last(9); (void)result2; }, etl::span_out_of_range);
CHECK_THROW({ auto cresult2 = cview.last(9); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -685,6 +708,19 @@ namespace
CHECK_EQUAL(etl::dynamic_extent, cspan4.extent);
CHECK_EQUAL(sub2.size(), cspan4.size());
CHECK_THROW({ auto span5 = view.subspan<9>(); (void)span5; }, etl::span_out_of_range);
CHECK_THROW({ auto cspan5 = cview.subspan<9>(); (void)cspan5; }, etl::span_out_of_range);
#define SPAN6_EXPR { auto span6 = view.subspan<2, 7>(); (void)span6; }
CHECK_THROW(SPAN6_EXPR, etl::span_out_of_range);
#define CSPAN6_EXPR { auto cspan6 = cview.subspan<2, 7>(); (void)cspan6; }
CHECK_THROW(CSPAN6_EXPR, etl::span_out_of_range);
CHECK_THROW({ auto span7 = view.subspan(9); (void)span7; }, etl::span_out_of_range);
CHECK_THROW({ auto cspan7 = cview.subspan(9); (void)cspan7; }, etl::span_out_of_range);
CHECK_THROW({ auto span8 = view.subspan(2, 7); (void)span8; }, etl::span_out_of_range);
CHECK_THROW({ auto cspan8 = cview.subspan(2, 7); (void)cspan8; }, etl::span_out_of_range);
}
//*************************************************************************

View File

@ -427,6 +427,15 @@ namespace
CHECK_EQUAL(etldata.back(), view.back());
CHECK_EQUAL(etldata.back(), cview.back());
//these should trigger static asserts
// auto empty_view = view.subspan<0, 0>();
// CHECK_THROW({ auto front = empty_view.front(); (void)front; }, etl::span_out_of_range);
// CHECK_THROW({ auto back = empty_view.back(); (void)back; }, etl::span_out_of_range);
// auto empty_cview = cview.subspan<0, 0>();
// CHECK_THROW({ auto front = empty_cview.front(); (void)front; }, etl::span_out_of_range);
// CHECK_THROW({ auto back = empty_cview.back(); (void)back; }, etl::span_out_of_range);
}
//*************************************************************************
@ -451,8 +460,8 @@ namespace
CHECK_EQUAL(etldata.at(i), cview.at(i));
}
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::array_out_of_range);
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::array_out_of_range);
CHECK_THROW({ int d = view.at(view.size()); (void)d; }, etl::span_out_of_range);
CHECK_THROW({ int d = cview.at(cview.size()); (void)d; }, etl::span_out_of_range);
}
//*************************************************************************
@ -466,6 +475,9 @@ namespace
CHECK_EQUAL(etldata[i], view[i]);
CHECK_EQUAL(etldata[i], cview[i]);
}
CHECK_THROW({ int d = view[view.size()]; (void)d; }, etl::span_out_of_range);
CHECK_THROW({ int d = cview[cview.size()]; (void)d; }, etl::span_out_of_range);
}
//*************************************************************************
@ -548,6 +560,10 @@ namespace
CHECK(isEqual);
CHECK_EQUAL(first.size(), cresult.extent);
CHECK_EQUAL(first.size(), cresult.size());
//these should trigger static asserts
// CHECK_THROW({ auto result2 = view.first<11>(); (void)result2; }, etl::span_out_of_range);
// CHECK_THROW({ auto cresult2 = cview.first<11>(); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -569,6 +585,9 @@ namespace
isEqual = std::equal(cresult.begin(), cresult.end(), first.begin());
CHECK(isEqual);
CHECK_EQUAL(first.size(), cresult.size());
CHECK_THROW({ auto result2 = view.first(11); (void)result2; }, etl::span_out_of_range);
CHECK_THROW({ auto cresult2 = cview.first(11); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -592,6 +611,10 @@ namespace
CHECK(isEqual);
CHECK_EQUAL(last.size(), cresult.extent);
CHECK_EQUAL(last.size(), cresult.size());
//these should trigger static asserts
// CHECK_THROW({ auto result2 = view.last<11>(); (void)result2; }, etl::span_out_of_range);
// CHECK_THROW({ auto cresult2 = cview.last<11>(); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -613,6 +636,9 @@ namespace
isEqual = std::equal(cresult.begin(), cresult.end(), last.begin());
CHECK(isEqual);
CHECK_EQUAL(last.size(), cresult.size());
CHECK_THROW({ auto result2 = view.last(11); (void)result2; }, etl::span_out_of_range);
CHECK_THROW({ auto cresult2 = cview.last(11); (void)cresult2; }, etl::span_out_of_range);
}
//*************************************************************************
@ -667,6 +693,21 @@ namespace
isEqual = std::equal(sub2.begin(), sub2.end(), cspan4.begin());
CHECK(isEqual);
CHECK_EQUAL(etl::dynamic_extent, cspan4.extent);
//these should trigger static asserts
// CHECK_THROW({ auto span5 = view.subspan<11>(); (void)span5; }, etl::span_out_of_range);
// CHECK_THROW({ auto cspan5 = cview.subspan<11>(); (void)cspan5; }, etl::span_out_of_range);
// #define SPAN6_EXPR { auto span6 = view.subspan<2, 9>(); (void)span6; }
// CHECK_THROW(SPAN6_EXPR, etl::span_out_of_range);
// #define CSPAN6_EXPR { auto cspan6 = cview.subspan<2, 9>(); (void)cspan6; }
// CHECK_THROW(CSPAN6_EXPR, etl::span_out_of_range);
CHECK_THROW({ auto span7 = view.subspan(11); (void)span7; }, etl::span_out_of_range);
CHECK_THROW({ auto cspan7 = cview.subspan(11); (void)cspan7; }, etl::span_out_of_range);
CHECK_THROW({ auto span8 = view.subspan(2, 9); (void)span8; }, etl::span_out_of_range);
CHECK_THROW({ auto cspan8 = cview.subspan(2, 9); (void)cspan8; }, etl::span_out_of_range);
}
//*************************************************************************