Contribute span implementation

This commit is contained in:
Glen Fernandes
2021-10-13 08:37:37 -04:00
parent e02c333706
commit fdf1ed78a7
10 changed files with 1739 additions and 0 deletions

View File

@ -65,6 +65,7 @@ criteria for inclusion is that the utility component be:
[include quick_exit.qbk]
[include ref.qbk]
[include scoped_enum.qbk]
[include span.qbk]
[include string_view.qbk]
[include swap.qbk]
[include typeinfo.qbk]

405
doc/span.qbk Normal file
View File

@ -0,0 +1,405 @@
[/
Copyright 2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
]
[section:span span]
[simplesect Authors]
* Glen Fernandes
[endsimplesect]
[section Overview]
This header <boost/core/span.hpp> provides class template `span`, which is a
view over a sequence of objects. It implements the C++20 standard library
`std::span` facility. This implementation supports C++11 and higher.
In addition to referencing the sequence of objects, the span knows the count of
objects. There are two kinds of spans:
* Dynamic size (`span<T>` or `span<T, dynamic_extent>`)
* Static size (`span<T, N>`)
Dynamic size spans have a count that can be a value known at run time. Static
size spans have a count that must be known at compile time.
[endsect]
[section Examples]
The following snippet shows a function to compute a SHA1 hash whose parameters
and return type use spans.
```
auto sha1(boost::span<const unsigned char> input,
boost::span<unsigned char, SHA_DIGEST_LENGTH> ouput)
{
SHA_CTX context;
SHA1_Init(&context);
SHA1_Update(&context, input.data(), input.size());
SHA1_Final(output.data(), &context);
return output;
}
```
[endsect]
[section Reference]
```
namespace boost {
constexpr std::size_t dynamic_extent = -1;
template<class T, std::size_t E = dynamic_extent>
class span {
public:
typedef T element_type;
typedef std::remove_cv_t<T> value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T* iterator;
typedef const T* const_iterator;
typedef std::reverse_iterator<T*> reverse_iterator;
typedef std::reverse_iterator<const T*> const_reverse_iterator;
static constexpr std::size_t extent = E;
constexpr span() noexcept;
explicit(E != dynamic_extent)
template<class I>
constexpr span(I* f, size_type c);
explicit(E != dynamic_extent)
template<class I, class L>
constexpr span(I* f, L* l);
template<std::size_t N>
constexpr span(type_identity_t<T> (&a)[N]);
template<class U, std::size_t N>
constexpr span(std::array<U, N>& a) noexcept;
template<class U, std::size_t N>
constexpr span(const std::array<U, N>& a) noexcept;
explicit(E != dynamic_extent)
template<class R>
constexpr span(R&& r);
explicit(E != dynamic_extent && N == dynamic_extent)
template<class U, std::size_t N>
constexpr span(const span<U, N>& s) noexcept;
template<std::size_t C>
constexpr span<T, C> first() const;
template<std::size_t C>
constexpr span<T, C> last() const;
template<std::size_t O, std::size_t C = dynamic_extent>
constexpr span<T, see below> subspan() const;
constexpr span<T, dynamic_extent> first(size_type c) const;
constexpr span<T, dynamic_extent> last(size_type c) const;
constexpr span<T, dynamic_extent> subspan(size_type o,
size_type c = dynamic_extent) const;
constexpr size_type size() const noexcept;
constexpr size_type size_bytes() const noexcept;
constexpr bool empty() const noexcept;
constexpr reference operator[](size_type i) const;
constexpr reference front() const;
constexpr reference back() const;
constexpr pointer data() const noexcept;
constexpr iterator begin() const noexcept;
constexpr iterator end() const noexcept;
constexpr reverse_iterator rbegin() const noexcept;
constexpr reverse_iterator rend() const noexcept;
constexpr const_iterator cbegin() const noexcept;
constexpr const_iterator cend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;
friend constexpr iterator begin(span s) noexcept {
return s.begin();
}
friend constexpr iterator end(span s) noexcept {
return s.end();
}
};
template<class I, class L>
span(I*, L) -> span<I>;
template<class T, std::size_t N>
span(T(&)[N]) -> span<T, N>;
template<class T, std::size_t N>
span(std::array<T, N>&) -> span<T, N>;
template<class T, std::size_t N>
span(const std::array<T, N>&) -> span<const T, N>;
template<class R>
span(R&&) -> span<std::remove_pointer_t<decltype(std::declval<R&>().data())> >;
template<class T, std::size_t E>
span<const std::byte, E == dynamic_extent ? dynamic_extent : sizeof(T) * E>
as_bytes(span<T, E> s) noexcept;
template<class T, std::size_t E>
span<std::byte, E == dynamic_extent ? dynamic_extent : sizeof(T) * E>
as_writable_bytes(span<T, E> s) noexcept;
} /* boost */
```
[section Constructors]
[variablelist
[[`constexpr span() noexcept;`]
[[variablelist
[[Constraints][`E == dynamic_extent || E == 0` is `true`.]]
[[Postconditions][`size() == 0 && data() == nullptr`.]]]]]
[[`explicit(E != dynamic_extent)
template<class I>
constexpr span(I* f, size_type c);`]
[[variablelist
[[Constraints]
[`is_convertible_v<I(*)[], T(*)[]>` is `true`.]]
[[Preconditions]
[[itemized_list
[`[f, f + c)` is a valid range.]
[If `E` is not equal to `dynamic_extent`, then `c` is equal to `E`.]]]]
[[Effects][Constructs a `span` with data `f` and size `c`.]]
[[Throws][Nothing.]]]]]
[[`explicit(E != dynamic_extent)
template<class I, class L>
constexpr span(I* f, L* l);`]
[[variablelist
[[Constraints]
[`is_convertible_v<I(*)[], T(*)[]>` is `true`.]]
[[Preconditions]
[[itemized_list
[If `E` is not equal to `dynamic_extent`, then `l - f` is equal to `E`.]
[`[f, l)` is a valid range.]]]]
[[Effects][Constructs a `span` with data `f` and size `l - f`.]]
[[Throws][Nothing.]]]]]
[[`template<std::size_t N>
constexpr span(type_identity_t<T> (&a)[N]);`]
[[variablelist
[[Constraints][`E == dynamic_extent || E == N` is `true`.]]
[[Effects][Constructs a `span` that is a view over the supplied array.]]
[[Postconditions][`size() == N && data() == &a[0]` is `true`.]]]]]
[[`template<class U, std::size_t N>
constexpr span(std::array<U, N>& a) noexcept;`]
[[variablelist
[[Constraints]
[[itemized_list
[`E == dynamic_extent || E == N` is `true`, and]
[`U(*)[]` is convertible to `T(*)[]`.]]]]
[[Effects][Constructs a `span` that is a view over the supplied array.]]
[[Postconditions][`size() == N && data() == a.data()` is `true`.]]]]]
[[`template<class U, std::size_t N>
constexpr span(const std::array<U, N>& a) noexcept;`]
[[variablelist
[[Constraints]
[[itemized_list
[`E == dynamic_extent || E == N` is `true`, and]
[`U(*)[]` is convertible to `T(*)[]`.]]]]
[[Effects][Constructs a `span` that is a view over the supplied array.]]
[[Postconditions][`size() == N && data() == a.data()` is `true`.]]]]]
[[`explicit(E != dynamic_extent)
template<class R>
constexpr span(R&& r);`]
[[variablelist
[[Constraints]
[[itemized_list
[`is_lvalue_reference_v<R> || is_const_v<T>` is `true`]
[`remove_cvref_t<R>` is not a specialization of `span`,]
[`remove_cvref_t<R>` is not a specialization of `array`,]
[`is_array_v<remove_cvref_t<R>>` is `false`,]
[`r.data()` is well-formed and
`is_convertible_v<remove_pointer_t<decltype(declval<R&>().data())>(*)[],
T(*)[]>` is `true`, and]
[`r.size()` is well-formed and
`is_convertible_v<decltype(declval<R&>().size()), size_t>` is `true`.]]]]
[[Effects][Constructs a `span` with data `r.data()` and size `r.size()`.]]
[[Throws][What and when r.data() and r.size() throw.]]]]]
[[`explicit(E != dynamic_extent && N == dynamic_extent)
template<class U, std::size_t N>
constexpr span(const span<U, N>& s) noexcept;`]
[[variablelist
[[Constraints]
[[itemized_list
[`E == dynamic_extent || N == dynamic_extent || E == N` is `true`, and]
[`is_convertible_v<U(*)[], T(*)[]>` is `true`.]]]]
[[Preconditions]
[If `E` is not equal to `dynamic_extent`, then `s.size()` is equal to `E`.]]
[[Effects]
[Constructs a `span` that is a view over the range
`[s.data(), s.data() + s.size())`.]]
[[Postconditions][`size() == s.size() && data() == s.data()`.]]]]]]
[endsect]
[section Subviews]
[variablelist
[[`template<std::size_t C> constexpr span<T, C> first() const;`]
[[variablelist
[[Mandates][`C <= E` is `true`.]]
[[Preconditions][`C <= size()` is `true`.]]
[[Effects]
[Equivalent to `return R{data(), C};` where `R` is the return type.]]]]]
[[`template<std::size_t C> constexpr span<T, C> last() const;`]
[[variablelist
[[Mandates][`C <= E` is `true`.]]
[[Preconditions][`C <= size()` is `true`.]]
[[Effects]
[Equivalent to `return R{data() + (size() - C), C};` where `R` is the return
type.]]]]]
[[`template<std::size_t O, std::size_t C = dynamic_extent>
constexpr span<T, see below> subspan() const;`]
[[variablelist
[[Mandates][`O <= E && (C == dynamic_extent || C <= E - O)` is `true`.]]
[[Preconditions]
[`O <= size() && (C == dynamic_extent || C <= size() - O)` is `true`.]]
[[Effects]
[Equivalent to
`return span<T, see below>(data() + O,
C != dynamic_extent ? C : size() - O);`.]]
[[Remarks]
[The second template argument of the returned span type is:
`C != dynamic_extent ? C : (E != dynamic_extent ? E - O :
dynamic_extent)`]]]]]
[[`constexpr span<T, dynamic_extent> first(size_type c) const;`]
[[variablelist
[[Preconditions][`c <= size()` is `true`.]]
[[Effects][Equivalent to: `return {data(), c};`]]]]]
[[`constexpr span<T, dynamic_extent> last(size_type c) const;`]
[[variablelist
[[Preconditions][`c <= size()` is `true`.]]
[[Effects][Equivalent to: `return {data() + (size() - c), c};`]]]]]
[[`constexpr span<T, dynamic_extent> subspan(size_type o,
size_type c = dynamic_extent) const;`]
[[variablelist
[[Preconditions]
[`o <= size() && (c == dynamic_extent || o + c <= size())` is `true`.]]
[[Effects]
[Equivalent to:
`return {data() + o, c == dynamic_extent ? size() - o : c};`]]]]]]
[endsect]
[section Observers]
[variablelist
[[`constexpr size_type size() const noexcept;`]
[[variablelist
[[Returns][The number of elements in the span.]]]]]
[[`constexpr size_type size_bytes() const noexcept;`]
[[variablelist
[[Effects][Equivalent to: `return size() * sizeof(T);`]]]]]
[[`constexpr bool empty() const noexcept;`]
[[variablelist
[[Effects][Equivalent to: `return size() == 0;`]]]]]]
[endsect]
[section Element access]
[variablelist
[[`constexpr reference operator[](size_type i) const;`]
[[variablelist
[[Preconditions][`i < size()` is `true`.]]
[[Effects][Equivalent to: `return *(data() + i);`]]]]]
[[`constexpr reference front() const;`]
[[variablelist
[[Preconditions][`empty()` is `false`.]]
[[Effects][Equivalent to: `return *data();`]]]]]
[[`constexpr reference back() const;`]
[[variablelist
[[Preconditions][`empty()` is `false`.]]
[[Effects][Equivalent to: `return *(data() + (size() - 1);`]]]]]
[[`constexpr pointer data() const noexcept;`]
[[variablelist
[[Returns][A pointer to the first element in the span.]]]]]]
[endsect]
[section Iterator support]
[variablelist
[[`constexpr iterator begin() const noexcept;`]
[[variablelist
[[Returns][A constant iterator referring to the first element in the span. If `empty()`,
then it returns the same value as `cend()`.]]]]]
[[`constexpr iterator end() const noexcept;`]
[[variablelist
[[Returns][A constant iterator which is the past-the-end value.]]]]]
[[`constexpr reverse_iterator rbegin() const noexcept;`]
[[variablelist
[[Effects][Equivalent to: `return reverse_iterator(end());`]]]]]
[[`constexpr reverse_iterator rend() const noexcept;`]
[[variablelist
[[Effects][Equivalent to: `return reverse_iterator(begin());`]]]]]
[[`constexpr const_iterator cbegin() const noexcept;`]
[[variablelist
[[Returns]
[A constant iterator referring to the first element in the span. If `empty()`,
then it returns the same value as `cend()`.]]]]]
[[`constexpr const_iterator cend() const noexcept;`]
[[variablelist
[[Returns][A constant iterator which is the past-the-end value.]]]]]
[[`constexpr const_reverse_iterator crbegin() const noexcept;`]
[[variablelist
[[Effects][Equivalent to: `return const_reverse_iterator(cend());`]]]]]
[[`constexpr const_reverse_iterator crend() const noexcept;`]
[[variablelist
[[Effects]
[Equivalent to: `return const_reverse_iterator(cbegin());`]]]]]]
[endsect]
[section Views of object representation]
[variablelist
[[`template<class T, std::size_t E>
span<const std::byte, E == dynamic_extent ? dynamic_extent : sizeof(T) * E>
as_bytes(span<T, E> s) noexcept;`]
[[variablelist
[[Effects]
[Equivalent to:
`return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};`.]]]]]
[[`template<class T, std::size_t E>
span<std::byte, E == dynamic_extent ? dynamic_extent : sizeof(T) * E>
as_writable_bytes(span<T, E> s) noexcept;`]
[[variablelist
[[Constraints][`is_const_v<T>` is `false`.]]
[[Effects]
[Equivalent to: `return R{reinterpret_cast<byte*>(s.data()), s.size_bytes()};`
where `R` is the return type.]]
[[Returns][`false`.]]]]]]
[endsect]
[endsect]
[endsect]

403
include/boost/core/span.hpp Normal file
View File

@ -0,0 +1,403 @@
/*
Copyright 2019 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_SPAN_HPP
#define BOOST_CORE_SPAN_HPP
#include <array>
#include <iterator>
#include <type_traits>
#include <cstddef>
namespace boost {
constexpr std::size_t dynamic_extent = -1;
template<class T, std::size_t E = dynamic_extent>
class span;
namespace detail {
template<class U, class T>
struct span_convertible {
static constexpr bool value = std::is_convertible<U(*)[], T(*)[]>::value;
};
template<std::size_t E, std::size_t N>
struct span_capacity {
static constexpr bool value = E == boost::dynamic_extent || E == N;
};
template<class T, std::size_t E, class U, std::size_t N>
struct span_compatible {
static constexpr bool value = span_capacity<E, N>::value &&
span_convertible<U, T>::value;
};
template<class T>
struct span_uncvref {
typedef typename std::remove_cv<typename
std::remove_reference<T>::type>::type type;
};
template<class>
struct span_is_span {
static constexpr bool value = false;
};
template<class T, std::size_t E>
struct span_is_span<boost::span<T, E> > {
static constexpr bool value = true;
};
template<class T>
struct span_is_array {
static constexpr bool value = false;
};
template<class T, std::size_t N>
struct span_is_array<std::array<T, N> > {
static constexpr bool value = true;
};
template<class, class = void>
struct span_data { };
template<class T>
struct span_data<T,
typename std::enable_if<std::is_pointer<decltype(std::declval<T
&>().data())>::value>::type> {
typedef typename std::remove_pointer<decltype(std::declval<T
&>().data())>::type type;
};
template<class, class, class = void>
struct span_has_data {
static constexpr bool value = false;
};
template<class R, class T>
struct span_has_data<R, T, typename std::enable_if<span_convertible<typename
span_data<R>::type, T>::value>::type> {
static constexpr bool value = true;
};
template<class, class = void>
struct span_has_size {
static constexpr bool value = false;
};
template<class R>
struct span_has_size<R, typename
std::enable_if<std::is_convertible<decltype(std::declval<R&>().size()),
std::size_t>::value>::type> {
static constexpr bool value = true;
};
template<class R, class T>
struct span_is_range {
static constexpr bool value = (std::is_const<T>::value ||
std::is_lvalue_reference<R>::value) &&
!span_is_span<typename span_uncvref<R>::type>::value &&
!span_is_array<typename span_uncvref<R>::type>::value &&
!std::is_array<typename span_uncvref<R>::type>::value &&
span_has_data<R, T>::value &&
span_has_size<R>::value;
};
template<std::size_t E, std::size_t N>
struct span_implicit {
static constexpr bool value = E == boost::dynamic_extent ||
N != boost::dynamic_extent;
};
template<class T, std::size_t E, class U, std::size_t N>
struct span_copyable {
static constexpr bool value = (N == boost::dynamic_extent ||
span_capacity<E, N>::value) && span_convertible<U, T>::value;
};
template<std::size_t E, std::size_t O>
struct span_sub {
static constexpr std::size_t value = E == boost::dynamic_extent ?
boost::dynamic_extent : E - O;
};
template<class T, std::size_t E>
struct span_store {
constexpr span_store(T* p_, std::size_t) noexcept
: p(p_) { }
static constexpr std::size_t n = E;
T* p;
};
template<class T>
struct span_store<T, boost::dynamic_extent> {
constexpr span_store(T* p_, std::size_t n_) noexcept
: p(p_)
, n(n_) { }
T* p;
std::size_t n;
};
template<class T, std::size_t E>
struct span_bytes {
static constexpr std::size_t value = E == boost::dynamic_extent ?
boost::dynamic_extent : sizeof(T) * E;
};
} /* detail */
template<class T, std::size_t E>
class span {
public:
typedef T element_type;
typedef typename std::remove_cv<T>::type value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T* iterator;
typedef const T* const_iterator;
typedef std::reverse_iterator<T*> reverse_iterator;
typedef std::reverse_iterator<const T*> const_reverse_iterator;
static constexpr std::size_t extent = E;
template<std::size_t N = E,
typename std::enable_if<N == dynamic_extent || N == 0, int>::type = 0>
constexpr span() noexcept
: s_(0, 0) { }
template<class I,
typename std::enable_if<E == dynamic_extent &&
detail::span_convertible<I, T>::value, int>::type = 0>
constexpr span(I* f, size_type c)
: s_(f, c) { }
template<class I,
typename std::enable_if<E != dynamic_extent &&
detail::span_convertible<I, T>::value, int>::type = 0>
explicit constexpr span(I* f, size_type c)
: s_(f, c) { }
template<class I, class L,
typename std::enable_if<E == dynamic_extent &&
detail::span_convertible<I, T>::value, int>::type = 0>
constexpr span(I* f, L* l)
: s_(f, l - f) { }
template<class I, class L,
typename std::enable_if<E != dynamic_extent &&
detail::span_convertible<I, T>::value, int>::type = 0>
explicit constexpr span(I* f, L* l)
: s_(f, l - f) { }
template<std::size_t N,
typename std::enable_if<detail::span_capacity<E, N>::value,
int>::type = 0>
constexpr span(typename std::enable_if<true, T>::type (&a)[N]) noexcept
: s_(a, N) { }
template<class U, std::size_t N,
typename std::enable_if<detail::span_compatible<T, E, U, N>::value,
int>::type = 0>
constexpr span(std::array<U, N>& a) noexcept
: s_(a.data(), N) { }
template<class U, std::size_t N,
typename std::enable_if<detail::span_compatible<T, E, const U,
N>::value, int>::type = 0>
constexpr span(const std::array<U, N>& a) noexcept
: s_(a.data(), N) { }
template<class R,
typename std::enable_if<E == dynamic_extent &&
detail::span_is_range<R, T>::value, int>::type = 0>
constexpr span(R&& r) noexcept(noexcept(r.data()) && noexcept(r.size()))
: s_(r.data(), r.size()) { }
template<class R,
typename std::enable_if<E != dynamic_extent &&
detail::span_is_range<R, T>::value, int>::type = 0>
explicit constexpr span(R&& r) noexcept(noexcept(r.data()) &&
noexcept(r.size()))
: s_(r.data(), r.size()) { }
template<class U, std::size_t N,
typename std::enable_if<detail::span_implicit<E, N>::value &&
detail::span_copyable<T, E, U, N>::value, int>::type = 0>
constexpr span(const span<U, N>& s) noexcept
: s_(s.data(), s.size()) { }
template<class U, std::size_t N,
typename std::enable_if<!detail::span_implicit<E, N>::value &&
detail::span_copyable<T, E, U, N>::value, int>::type = 0>
explicit constexpr span(const span<U, N>& s) noexcept
: s_(s.data(), s.size()) { }
template<std::size_t C>
constexpr span<T, C> first() const {
static_assert(C <= E, "Count <= Extent");
return span<T, C>(s_.p, C);
}
template<std::size_t C>
constexpr span<T, C> last() const {
static_assert(C <= E, "Count <= Extent");
return span<T, C>(s_.p + (s_.n - C), C);
}
template<std::size_t O, std::size_t C = dynamic_extent>
constexpr typename std::enable_if<C == dynamic_extent,
span<T, detail::span_sub<E, O>::value> >::type subspan() const {
static_assert(O <= E, "Offset <= Extent");
return span<T, detail::span_sub<E, O>::value>(s_.p + O, s_.n - O);
}
template<std::size_t O, std::size_t C = dynamic_extent>
constexpr typename std::enable_if<C != dynamic_extent,
span<T, C> >::type subspan() const {
static_assert(O <= E && C <= E - O,
"Offset <= Extent && Count <= Extent - Offset");
return span<T, C>(s_.p + O, C);
}
constexpr span<T, dynamic_extent> first(size_type c) const {
return 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);
}
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);
}
constexpr size_type size() const noexcept {
return s_.n;
}
constexpr size_type size_bytes() const noexcept {
return s_.n * sizeof(T);
}
constexpr bool empty() const noexcept {
return s_.n == 0;
}
constexpr reference operator[](size_type i) const {
return s_.p[i];
}
constexpr reference front() const {
return *s_.p;
}
constexpr reference back() const {
return s_.p[s_.n - 1];
}
constexpr pointer data() const noexcept {
return s_.p;
}
constexpr iterator begin() const noexcept {
return s_.p;
}
constexpr iterator end() const noexcept {
return s_.p + s_.n;
}
constexpr reverse_iterator rbegin() const noexcept {
return reverse_iterator(s_.p + s_.n);
}
constexpr reverse_iterator rend() const noexcept {
return reverse_iterator(s_.p);
}
constexpr const_iterator cbegin() const noexcept {
return s_.p;
}
constexpr const_iterator cend() const noexcept {
return s_.p + s_.n;
}
constexpr const_reverse_iterator crbegin() const noexcept {
return const_reverse_iterator(s_.p + s_.n);
}
constexpr const_reverse_iterator crend() const noexcept {
return const_reverse_iterator(s_.p);
}
friend constexpr iterator begin(span s) noexcept {
return s.begin();
}
friend constexpr iterator end(span s) noexcept {
return s.end();
}
private:
detail::span_store<T, E> s_;
};
template<class T, std::size_t E>
constexpr std::size_t span<T, E>::extent;
#ifdef __cpp_deduction_guides
template<class I, class L>
span(I*, L) -> span<I>;
template<class T, std::size_t N>
span(T(&)[N]) -> span<T, N>;
template<class T, std::size_t N>
span(std::array<T, N>&) -> span<T, N>;
template<class T, std::size_t N>
span(const std::array<T, N>&) -> span<const T, N>;
template<class R>
span(R&&) -> span<typename detail::span_data<R>::type>;
template<class T, std::size_t E>
span(span<T, E>) -> span<T, E>;
#endif
#ifdef __cpp_lib_byte
template<class T, std::size_t E>
inline span<const std::byte, detail::span_bytes<T, E>::value>
as_bytes(span<T, E> s) noexcept
{
return span<const std::byte, detail::span_bytes<T,
E>::value>(reinterpret_cast<const std::byte*>(s.data()),
s.size_bytes());
}
template<class T, std::size_t E>
inline typename std::enable_if<!std::is_const<T>::value,
span<std::byte, detail::span_bytes<T, E>::value> >::type
as_writable_bytes(span<T, E> s) noexcept
{
return span<std::byte, detail::span_bytes<T,
E>::value>(reinterpret_cast<std::byte*>(s.data()), s.size_bytes());
}
#endif
} /* boost */
#endif

View File

@ -8,6 +8,7 @@
import modules ;
import testing ;
import ../../config/checks/config : requires ;
# quick test (for CI)
run quick.cpp ;
@ -269,5 +270,12 @@ run sv_lt_test.cpp ;
run sv_stream_insert_test.cpp ;
run sv_conversion_test.cpp ;
run span_test.cpp : : : [ requires cxx11_constexpr cxx11_decltype ] ;
run span_types_test.cpp : : : [ requires cxx11_constexpr cxx11_decltype ] ;
run span_constructible_test.cpp : : : [ requires cxx11_constexpr cxx11_decltype ] ;
run span_deduction_guide_test.cpp : : : [ requires cpp_deduction_guides ] ;
run as_bytes_test.cpp : : : [ requires cpp_lib_byte ] ;
run as_writable_bytes_test.cpp : : : [ requires cpp_lib_byte ] ;
use-project /boost/core/swap : ./swap ;
build-project ./swap ;

34
test/as_bytes_test.cpp Normal file
View File

@ -0,0 +1,34 @@
/*
Copyright 2019 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/core/span.hpp>
#include <boost/core/lightweight_test.hpp>
void test_dynamic()
{
int a[4];
boost::span<const std::byte> s =
boost::as_bytes(boost::span<int>(&a[0], 4));
BOOST_TEST_EQ(s.data(), reinterpret_cast<const std::byte*>(&a[0]));
BOOST_TEST_EQ(s.size(), sizeof(int) * 4);
}
void test_static()
{
int a[4];
boost::span<const std::byte, sizeof(int) * 4> s =
boost::as_bytes(boost::span<int, 4>(&a[0], 4));
BOOST_TEST_EQ(s.data(), reinterpret_cast<const std::byte*>(&a[0]));
BOOST_TEST_EQ(s.size(), sizeof(int) * 4);
}
int main()
{
test_dynamic();
test_static();
return boost::report_errors();
}

View File

@ -0,0 +1,34 @@
/*
Copyright 2019 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/core/span.hpp>
#include <boost/core/lightweight_test.hpp>
void test_dynamic()
{
int a[4];
boost::span<std::byte> s =
boost::as_writable_bytes(boost::span<int>(&a[0], 4));
BOOST_TEST_EQ(s.data(), reinterpret_cast<std::byte*>(&a[0]));
BOOST_TEST_EQ(s.size(), sizeof(int) * 4);
}
void test_static()
{
int a[4];
boost::span<std::byte, sizeof(int) * 4> s =
boost::as_writable_bytes(boost::span<int, 4>(&a[0], 4));
BOOST_TEST_EQ(s.data(), reinterpret_cast<std::byte*>(&a[0]));
BOOST_TEST_EQ(s.size(), sizeof(int) * 4);
}
int main()
{
test_dynamic();
test_static();
return boost::report_errors();
}

View File

@ -0,0 +1,192 @@
/*
Copyright 2019 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/core/span.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct range {
T* data() {
return 0;
}
const T* data() const {
return 0;
}
std::size_t size() const {
return 0;
}
};
struct base { };
struct derived
: base { };
void test_default()
{
BOOST_TEST_TRAIT_TRUE((std::is_default_constructible<
boost::span<int> >));
BOOST_TEST_TRAIT_TRUE((std::is_default_constructible<
boost::span<int, 0> >));
BOOST_TEST_TRAIT_FALSE((std::is_default_constructible<
boost::span<int, 2> >));
}
void test_data_size()
{
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
int*, std::size_t>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
int*, std::size_t>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
int, std::size_t>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
const int*, std::size_t>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
derived*, std::size_t>));
}
void test_first_last()
{
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
int*, int*>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
int*, const int*>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
int*, int*>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
int, int*>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
const int*, int*>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
derived*, derived*>));
}
void test_array()
{
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
int(&)[4]>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int, 4>,
int(&)[4]>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 2>,
int(&)[4]>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
int(&)[2]>));
}
void test_std_array()
{
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
std::array<int, 4>&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
std::array<int, 4>&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int, 4>,
std::array<int, 4>&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int,
4>, std::array<int, 4>&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
std::array<const int, 4>&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
std::array<derived, 4>&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 2>,
std::array<int, 4>&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
std::array<const int, 4>&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base, 4>,
std::array<derived, 4>&>));
}
void test_const_std_array()
{
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
const std::array<int, 4> >));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int,
4>, const std::array<int, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
const std::array<int, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<const
base>, const std::array<derived, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<const int,
2>, const std::array<int, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
const std::array<int, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<const
base, 4>, const std::array<derived, 4> >));
}
void test_range()
{
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
range<int>&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
range<int>&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
range<int> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
int*>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
range<int> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
const range<int>&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
range<derived>&>));
}
void test_span()
{
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
boost::span<int> >));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
boost::span<int, 4> >));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int, 4>,
boost::span<int> >));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int,
4>, boost::span<int, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
boost::span<const int> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
boost::span<derived> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 2>,
boost::span<int, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
boost::span<const int, 4> >));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base, 4>,
boost::span<derived, 4> >));
}
void test_copy()
{
BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible<
boost::span<int> >));
BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible<
boost::span<int, 4> >));
}
void test_assign()
{
BOOST_TEST_TRAIT_TRUE((std::is_copy_assignable<
boost::span<int> >));
BOOST_TEST_TRAIT_TRUE((std::is_copy_assignable<
boost::span<int, 4> >));
}
int main()
{
test_default();
test_data_size();
test_first_last();
test_array();
test_std_array();
test_const_std_array();
test_range();
test_span();
test_copy();
test_assign();
return boost::report_errors();
}

View File

@ -0,0 +1,109 @@
/*
Copyright 2019 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/core/span.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
class range {
public:
T* data() {
return &v_[0];
}
std::size_t size() const {
return 4;
}
private:
T v_[4];
};
void test_data_size()
{
int a[4];
boost::span s(&a[0], 4);
BOOST_TEST_EQ(s.extent, boost::dynamic_extent);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_first_last()
{
int a[4];
boost::span s(&a[0], &a[4]);
BOOST_TEST_EQ(s.extent, boost::dynamic_extent);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_array()
{
int a[4];
boost::span s(a);
BOOST_TEST_EQ(s.extent, 4);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_std_array()
{
std::array<int, 4> a;
boost::span s(a);
BOOST_TEST_EQ(s.extent, 4);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_const_std_array()
{
const std::array<int, 4> a = std::array<int, 4>();
boost::span s(a);
BOOST_TEST_EQ(s.extent, 4);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_range()
{
range<int> c;
boost::span s(c);
BOOST_TEST_EQ(s.extent, boost::dynamic_extent);
BOOST_TEST_EQ(s.data(), c.data());
BOOST_TEST_EQ(s.size(), c.size());
}
void test_span_dynamic()
{
int a[4];
boost::span s(boost::span<int>(&a[0], 4));
BOOST_TEST_EQ(s.extent, boost::dynamic_extent);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_span_static()
{
int a[4];
boost::span s(boost::span<int, 4>(&a[0], 4));
BOOST_TEST_EQ(s.extent, 4);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
int main()
{
test_data_size();
test_first_last();
test_array();
test_std_array();
test_const_std_array();
test_range();
test_span_dynamic();
test_span_static();
return boost::report_errors();
}

425
test/span_test.cpp Normal file
View File

@ -0,0 +1,425 @@
/*
Copyright 2019 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/core/span.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
class range {
public:
T* data() {
return &v_[0];
}
std::size_t size() const {
return 4;
}
private:
T v_[4];
};
void test_extent()
{
BOOST_TEST_EQ(boost::span<int>::extent,
boost::dynamic_extent);
BOOST_TEST_EQ((boost::span<int, 2>::extent), 2);
}
void test_default_construct_dynamic()
{
boost::span<int> s;
BOOST_TEST_EQ(s.data(), static_cast<int*>(0));
BOOST_TEST_EQ(s.size(), 0);
}
void test_default_construct_static()
{
boost::span<int, 0> s;
BOOST_TEST_EQ(s.data(), static_cast<int*>(0));
BOOST_TEST_EQ(s.size(), 0);
}
void test_construct_data_size()
{
int a[4];
boost::span<int> s(&a[0], 4);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_construct_first_last()
{
int a[4];
boost::span<int> s(&a[0], &a[4]);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_construct_array_dynamic()
{
int a[4];
boost::span<int> s(a);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_construct_array_static()
{
int a[4];
boost::span<int, 4> s(a);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_construct_std_array_dynamic()
{
std::array<int, 4> a;
boost::span<int> s(a);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_construct_std_array_static()
{
std::array<int, 4> a;
boost::span<int, 4> s(a);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_construct_const_std_array_dynamic()
{
const std::array<int, 4> a = std::array<int, 4>();
boost::span<const int> s(a);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_construct_const_std_array_static()
{
const std::array<int, 4> a = std::array<int, 4>();
boost::span<const int, 4> s(a);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_construct_range()
{
range<int> c;
boost::span<int> s(c);
BOOST_TEST_EQ(s.data(), c.data());
BOOST_TEST_EQ(s.size(), c.size());
}
void test_construct_span_dynamic()
{
int a[4];
boost::span<const int> s(boost::span<int>(&a[0], 4));
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_construct_span_dynamic_static()
{
int a[4];
boost::span<int> s(boost::span<int, 4>(&a[0], 4));
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_construct_span_static()
{
int a[4];
boost::span<const int, 4> s(boost::span<int, 4>(&a[0], 4));
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_construct_span_static_dynamic()
{
int a[4];
boost::span<int, 4> s(boost::span<int>(&a[0], 4));
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_copy_dynamic()
{
int a[4];
boost::span<int> s1(&a[0], 4);
boost::span<int> s2(s1);
BOOST_TEST_EQ(s2.data(), &a[0]);
BOOST_TEST_EQ(s2.size(), 4);
}
void test_copy_static()
{
int a[4];
boost::span<int, 4> s1(&a[0], 4);
boost::span<int, 4> s2(s1);
BOOST_TEST_EQ(s2.data(), &a[0]);
BOOST_TEST_EQ(s2.size(), 4);
}
void test_assign_dynamic()
{
boost::span<int> s;
int a[4];
s = boost::span<int>(&a[0], 4);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_assign_static()
{
int a1[4];
boost::span<int, 4> s(&a1[0], 4);
int a2[4];
s = boost::span<int, 4>(&a2[0], 4);
BOOST_TEST_EQ(s.data(), &a2[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_first()
{
int a[4];
boost::span<int, 2> s = boost::span<int>(&a[0],
4).first<2>();
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 2);
}
void test_last()
{
int a[4];
boost::span<int, 2> s = boost::span<int>(&a[0], 4).last<2>();
BOOST_TEST_EQ(s.data(), &a[2]);
BOOST_TEST_EQ(s.size(), 2);
}
void test_subspan_dynamic()
{
int a[4];
boost::span<int> s = boost::span<int>(&a[0], 4).subspan<2>();
BOOST_TEST_EQ(s.data(), &a[2]);
BOOST_TEST_EQ(s.size(), 2);
}
void test_subspan_static()
{
int a[4];
boost::span<int, 2> s = boost::span<int, 4>(&a[0],
4).subspan<2>();
BOOST_TEST_EQ(s.data(), &a[2]);
BOOST_TEST_EQ(s.size(), 2);
}
void test_subspan()
{
int a[4];
boost::span<int, 1> s = boost::span<int>(&a[0],
4).subspan<2, 1>();
BOOST_TEST_EQ(s.data(), &a[2]);
BOOST_TEST_EQ(s.size(), 1);
}
void test_first_size()
{
int a[4];
boost::span<int> s = boost::span<int>(&a[0], 4).first(2);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 2);
}
void test_last_size()
{
int a[4];
boost::span<int> s = boost::span<int>(&a[0], 4).last(2);
BOOST_TEST_EQ(s.data(), &a[2]);
BOOST_TEST_EQ(s.size(), 2);
}
void test_subspan_range()
{
int a[4];
boost::span<int> s = boost::span<int>(&a[0], 4).subspan(2);
BOOST_TEST_EQ(s.data(), &a[2]);
BOOST_TEST_EQ(s.size(), 2);
}
void test_subspan_range_count()
{
int a[4];
boost::span<int> s = boost::span<int>(&a[0],
4).subspan(2, 1);
BOOST_TEST_EQ(s.data(), &a[2]);
BOOST_TEST_EQ(s.size(), 1);
}
void test_size()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).size()), 4);
}
void test_size_bytes()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).size_bytes()),
4 * sizeof(int));
}
void test_empty_dynamic()
{
int a[4];
BOOST_TEST(boost::span<int>().empty());
BOOST_TEST_NOT((boost::span<int>(&a[0], 4).empty()));
}
void test_empty_static()
{
int a[4];
BOOST_TEST((boost::span<int, 0>().empty()));
BOOST_TEST_NOT((boost::span<int, 4>(&a[0], 4).empty()));
}
void test_index()
{
int a[4] = { 1, 2, 3, 4 };
BOOST_TEST_EQ((boost::span<int>(&a[0], 4)[2]), 3);
}
void test_front()
{
int a[4] = { 1, 2, 3, 4 };
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).front()), 1);
}
void test_back()
{
int a[4] = { 1, 2, 3, 4 };
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).back()), 4);
}
void test_data()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).data()), &a[0]);
}
void test_begin()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).begin()), &a[0]);
}
void test_end()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).end()), &a[4]);
}
void test_rbegin()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).rbegin().base()), &a[4]);
}
void test_rend()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).rend().base()), &a[0]);
}
void test_cbegin()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).cbegin()), &a[0]);
}
void test_cend()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).cend()), &a[4]);
}
void test_crbegin()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).crbegin().base()), &a[4]);
}
void test_crend()
{
int a[4];
BOOST_TEST_EQ((boost::span<int>(&a[0], 4).crend().base()), &a[0]);
}
void test_begin_span()
{
int a[4];
BOOST_TEST_EQ((begin(boost::span<int>(&a[0], 4))), &a[0]);
}
void test_end_span()
{
int a[4];
BOOST_TEST_EQ((end(boost::span<int>(&a[0], 4))), &a[4]);
}
int main()
{
test_extent();
test_default_construct_dynamic();
test_default_construct_static();
test_construct_data_size();
test_construct_first_last();
test_construct_array_dynamic();
test_construct_array_static();
test_construct_std_array_dynamic();
test_construct_std_array_static();
test_construct_const_std_array_dynamic();
test_construct_const_std_array_static();
test_construct_range();
test_construct_span_dynamic();
test_construct_span_dynamic_static();
test_construct_span_static();
test_construct_span_static_dynamic();
test_copy_dynamic();
test_copy_static();
test_assign_dynamic();
test_assign_static();
test_first();
test_last();
test_subspan_dynamic();
test_subspan_static();
test_subspan();
test_first_size();
test_last_size();
test_subspan_range();
test_subspan_range_count();
test_size();
test_size_bytes();
test_empty_dynamic();
test_empty_static();
test_index();
test_front();
test_back();
test_data();
test_begin();
test_end();
test_rbegin();
test_rend();
test_cbegin();
test_cend();
test_crbegin();
test_crend();
test_begin_span();
test_end_span();
return boost::report_errors();
}

128
test/span_types_test.cpp Normal file
View File

@ -0,0 +1,128 @@
/*
Copyright 2019 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/core/span.hpp>
#include <boost/core/lightweight_test_trait.hpp>
void test_element_type()
{
BOOST_TEST_TRAIT_SAME(int,
boost::span<int>::element_type);
BOOST_TEST_TRAIT_SAME(char,
boost::span<char>::element_type);
}
void test_value_type()
{
BOOST_TEST_TRAIT_SAME(char,
boost::span<char>::value_type);
BOOST_TEST_TRAIT_SAME(int,
boost::span<int>::value_type);
BOOST_TEST_TRAIT_SAME(int,
boost::span<const int>::value_type);
BOOST_TEST_TRAIT_SAME(int,
boost::span<volatile int>::value_type);
BOOST_TEST_TRAIT_SAME(int,
boost::span<const volatile int>::value_type);
}
void test_size_type()
{
BOOST_TEST_TRAIT_SAME(std::size_t,
boost::span<char>::size_type);
BOOST_TEST_TRAIT_SAME(std::size_t,
boost::span<int>::size_type);
}
void test_difference_type()
{
BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
boost::span<char>::difference_type);
BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
boost::span<int>::difference_type);
}
void test_pointer()
{
BOOST_TEST_TRAIT_SAME(char*,
boost::span<char>::pointer);
BOOST_TEST_TRAIT_SAME(int*,
boost::span<int>::pointer);
}
void test_const_pointer()
{
BOOST_TEST_TRAIT_SAME(const char*,
boost::span<char>::const_pointer);
BOOST_TEST_TRAIT_SAME(const int*,
boost::span<int>::const_pointer);
}
void test_reference()
{
BOOST_TEST_TRAIT_SAME(char&,
boost::span<char>::reference);
BOOST_TEST_TRAIT_SAME(int&,
boost::span<int>::reference);
}
void test_const_reference()
{
BOOST_TEST_TRAIT_SAME(const char&,
boost::span<char>::const_reference);
BOOST_TEST_TRAIT_SAME(const int&,
boost::span<int>::const_reference);
}
void test_iterator()
{
BOOST_TEST_TRAIT_SAME(char*,
boost::span<char>::iterator);
BOOST_TEST_TRAIT_SAME(int*,
boost::span<int>::iterator);
}
void test_const_iterator()
{
BOOST_TEST_TRAIT_SAME(const char*,
boost::span<char>::const_iterator);
BOOST_TEST_TRAIT_SAME(const int*,
boost::span<int>::const_iterator);
}
void test_reverse_iterator()
{
BOOST_TEST_TRAIT_SAME(std::reverse_iterator<char*>,
boost::span<char>::reverse_iterator);
BOOST_TEST_TRAIT_SAME(std::reverse_iterator<int*>,
boost::span<int>::reverse_iterator);
}
void test_const_reverse_iterator()
{
BOOST_TEST_TRAIT_SAME(std::reverse_iterator<const char*>,
boost::span<char>::const_reverse_iterator);
BOOST_TEST_TRAIT_SAME(std::reverse_iterator<const int*>,
boost::span<int>::const_reverse_iterator);
}
int main()
{
test_element_type();
test_value_type();
test_size_type();
test_difference_type();
test_pointer();
test_const_pointer();
test_reference();
test_const_reference();
test_iterator();
test_const_iterator();
test_reverse_iterator();
test_const_reverse_iterator();
return boost::report_errors();
}