Add asserts to span

This commit is contained in:
Glen Fernandes
2025-01-16 19:12:31 -05:00
parent 7178a52909
commit 24a8174ef1
4 changed files with 109 additions and 7 deletions

View File

@@ -0,0 +1,24 @@
/*
Copyright 2025 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_CORE_DETAIL_ASSERT_HPP
#define BOOST_CORE_DETAIL_ASSERT_HPP
#include <cassert>
#if !defined(NDEBUG) && \
!defined(__clang__) && \
!defined(__INTEL_COMPILER) && \
defined(__GNUC__) && \
(__GNUC__ < 5)
#define BOOST_CORE_ASSERT(expr) \
((expr) ? void(0) : __assert_fail(#expr, __FILE__, __LINE__, 0))
#else
#define BOOST_CORE_ASSERT(expr) assert(expr)
#endif
#endif

View File

@@ -8,6 +8,7 @@ Distributed under the Boost Software License, Version 1.0.
#ifndef BOOST_CORE_SPAN_HPP
#define BOOST_CORE_SPAN_HPP
#include <boost/core/detail/assert.hpp>
#include <boost/core/data.hpp>
#include <array>
#include <iterator>
@@ -274,17 +275,21 @@ public:
}
constexpr span<T, dynamic_extent> first(size_type c) const {
return span<T, dynamic_extent>(s_.p, c);
return BOOST_CORE_ASSERT(c <= size()),
span<T, dynamic_extent>(s_.p, c);
}
constexpr span<T, dynamic_extent> last(size_type c) const {
return span<T, dynamic_extent>(s_.p + (s_.n - c), c);
return BOOST_CORE_ASSERT(c <= size()),
span<T, dynamic_extent>(s_.p + (s_.n - c), c);
}
constexpr span<T, dynamic_extent> subspan(size_type o,
size_type c = dynamic_extent) const {
return span<T, dynamic_extent>(s_.p + o,
c == dynamic_extent ? s_.n - o : c);
return BOOST_CORE_ASSERT(o <= size() &&
(c == dynamic_extent || c + o <= size())),
span<T, dynamic_extent>(s_.p + o,
c == dynamic_extent ? s_.n - o : c);
}
constexpr size_type size() const noexcept {
@@ -300,15 +305,15 @@ public:
}
constexpr reference operator[](size_type i) const {
return s_.p[i];
return BOOST_CORE_ASSERT(i < size()), s_.p[i];
}
constexpr reference front() const {
return *s_.p;
return BOOST_CORE_ASSERT(!empty()), *s_.p;
}
constexpr reference back() const {
return s_.p[s_.n - 1];
return BOOST_CORE_ASSERT(!empty()), s_.p[s_.n - 1];
}
constexpr pointer data() const noexcept {