mirror of
https://github.com/boostorg/core.git
synced 2025-05-09 14:57:46 +00:00
Add asserts to span
This commit is contained in:
parent
7178a52909
commit
24a8174ef1
24
include/boost/core/detail/assert.hpp
Normal file
24
include/boost/core/detail/assert.hpp
Normal 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
|
@ -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 {
|
||||
|
@ -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 ;
|
||||
|
72
test/span_constexpr_test.cpp
Normal file
72
test/span_constexpr_test.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
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)
|
||||
*/
|
||||
#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
|
Loading…
x
Reference in New Issue
Block a user