Skip to content

Commit

Permalink
Add asserts to span
Browse files Browse the repository at this point in the history
  • Loading branch information
glenfe committed Jan 17, 2025
1 parent 7178a52 commit 03adc3d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
26 changes: 26 additions & 0 deletions include/boost/core/detail/assert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2025 Glen Joseph Fernandes
([email protected])
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(__GNUC__) && __GNUC__ < 5 && \
!defined(__clang__) && \
!defined(__INTEL_COMPILER)
#if defined(NDEBUG)
#define BOOST_CORE_ASSERT(expr) void(0)
#else
#define BOOST_CORE_ASSERT(expr) \
((expr) ? void(0) : __assert_fail(#expr, __FILE__, __LINE__, 0))
#endif
#else
#define BOOST_CORE_ASSERT(expr) assert(expr)
#endif

#endif
19 changes: 12 additions & 7 deletions include/boost/core/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -274,17 +275,21 @@ class span {
}

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 {
Expand All @@ -300,15 +305,15 @@ class span {
}

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 {
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ run span_test.cpp ;
run span_types_test.cpp ;
run span_constructible_test.cpp ;
run span_deduction_guide_test.cpp ;
run span_constexpr_test.cpp ;
run as_bytes_test.cpp ;
run as_writable_bytes_test.cpp ;
compile span_boost_begin_test.cpp ;
Expand Down
72 changes: 72 additions & 0 deletions test/span_constexpr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2025 Glen Joseph Fernandes
([email protected])
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_CONSTEXPR)
#include <boost/core/span.hpp>
#include <boost/core/lightweight_test.hpp>

constexpr const int array[4]{ 5, 10, 15, 20 };

void test_first()
{
constexpr boost::span<const int> s =
boost::span<const int>(array, 4).first(2);
BOOST_TEST_EQ(s.data(), &array[0]);
BOOST_TEST_EQ(s.size(), 2);
}

void test_last()
{
constexpr boost::span<const int> s =
boost::span<const int>(array, 4).last(2);
BOOST_TEST_EQ(s.data(), &array[2]);
BOOST_TEST_EQ(s.size(), 2);
}

void test_subspan()
{
constexpr boost::span<const int> s =
boost::span<const int>(array, 4).subspan(1, 2);
BOOST_TEST_EQ(s.data(), &array[1]);
BOOST_TEST_EQ(s.size(), 2);
}

void test_index()
{
constexpr const int i = boost::span<const int>(array, 4)[1];
BOOST_TEST_EQ(i, 10);
}

void test_front()
{
constexpr const int i = boost::span<const int>(array, 4).front();
BOOST_TEST_EQ(i, 5);
}

void test_back()
{
constexpr const int i = boost::span<const int>(array, 4).back();
BOOST_TEST_EQ(i, 20);
}

int main()
{
test_first();
test_last();
test_subspan();
test_index();
test_front();
test_back();
return boost::report_errors();
}
#else
int main()
{
return 0;
}
#endif

0 comments on commit 03adc3d

Please sign in to comment.