[wpiutil] Add std::span implementation

Imported from https://github.com/tcbrindle/span with ifdef's removed
(as we require C++17).
This commit is contained in:
Peter Johnson
2021-05-22 10:09:39 -07:00
parent 6d20b12043
commit bc15b953b4
6 changed files with 1218 additions and 0 deletions

View File

@@ -40,6 +40,8 @@ units wpimath/src/main/native/include/units/
Eigen wpimath/src/main/native/eigeninclude/
wpimath/src/main/native/include/unsupported/
StackWalker wpiutil/src/main/native/windows/StackWalker.*
TCB span wpiutil/src/main/native/include/wpi/span.h
wpiutil/src/test/native/cpp/span/
Team 254 Library wpilibj/src/main/java/edu/wpi/first/wpilibj/spline/SplineParameterizer.java
wpilibj/src/main/java/edu/wpi/first/wpilibj/trajectory/TrajectoryParameterizer.java
wpilibc/src/main/native/include/spline/SplineParameterizer.h
@@ -856,3 +858,30 @@ the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What the **** You Want
to Public License, Version 2, as published by the WTFPL Task Force.
See http://www.wtfpl.net/ for more details.
======================
Boost Software License
======================
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View File

@@ -58,6 +58,8 @@ generatedFileExclude {
src/main/native/include/wpi/iterator_range\.h$
src/main/native/include/wpi/raw_os_ostream\.h$
src/main/native/include/wpi/raw_ostream\.h$
src/main/native/include/wpi/span\.h$
src/test/native/cpp/span/
src/main/native/include/wpi/type_traits\.h$
src/main/native/cpp/json
src/main/native/include/wpi/json

View File

@@ -0,0 +1,397 @@
/*
This is an implementation of C++20's std::span
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4820.pdf
*/
// Copyright Tristan Brindle 2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file ../../LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
#ifndef WPIUTIL_WPI_SPAN_HPP_INCLUDED
#define WPIUTIL_WPI_SPAN_HPP_INCLUDED
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <type_traits>
namespace wpi {
inline constexpr std::size_t dynamic_extent = SIZE_MAX;
template <typename ElementType, std::size_t Extent = dynamic_extent>
class span;
namespace detail {
template <typename E, std::size_t S>
struct span_storage {
constexpr span_storage() noexcept = default;
constexpr span_storage(E* p_ptr, std::size_t /*unused*/) noexcept
: ptr(p_ptr)
{}
E* ptr = nullptr;
static constexpr std::size_t size = S;
};
template <typename E>
struct span_storage<E, dynamic_extent> {
constexpr span_storage() noexcept = default;
constexpr span_storage(E* p_ptr, std::size_t p_size) noexcept
: ptr(p_ptr), size(p_size)
{}
E* ptr = nullptr;
std::size_t size = 0;
};
template <typename T>
using uncvref_t =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
template <typename>
struct is_span : std::false_type {};
template <typename T, std::size_t S>
struct is_span<span<T, S>> : std::true_type {};
template <typename>
struct is_std_array : std::false_type {};
template <typename T, std::size_t N>
struct is_std_array<std::array<T, N>> : std::true_type {};
template <typename, typename = void>
struct has_size_and_data : std::false_type {};
template <typename T>
struct has_size_and_data<T, std::void_t<decltype(std::size(std::declval<T>())),
decltype(std::data(std::declval<T>()))>>
: std::true_type {};
template <typename C, typename U = uncvref_t<C>>
struct is_container {
static constexpr bool value =
!is_span<U>::value && !is_std_array<U>::value &&
!std::is_array<U>::value && has_size_and_data<C>::value;
};
template <typename T>
using remove_pointer_t = typename std::remove_pointer<T>::type;
template <typename, typename, typename = void>
struct is_container_element_type_compatible : std::false_type {};
template <typename T, typename E>
struct is_container_element_type_compatible<
T, E,
typename std::enable_if<
!std::is_same<typename std::remove_cv<decltype(
std::data(std::declval<T>()))>::type,
void>::value>::type>
: std::is_convertible<
remove_pointer_t<decltype(std::data(std::declval<T>()))> (*)[],
E (*)[]> {};
template <typename, typename = size_t>
struct is_complete : std::false_type {};
template <typename T>
struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
} // namespace detail
template <typename ElementType, std::size_t Extent>
class span {
static_assert(std::is_object<ElementType>::value,
"A span's ElementType must be an object type (not a "
"reference type or void)");
static_assert(detail::is_complete<ElementType>::value,
"A span's ElementType must be a complete type (not a forward "
"declaration)");
static_assert(!std::is_abstract<ElementType>::value,
"A span's ElementType cannot be an abstract class type");
using storage_type = detail::span_storage<ElementType, Extent>;
public:
// constants and types
using element_type = ElementType;
using value_type = typename std::remove_cv<ElementType>::type;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = element_type*;
using const_pointer = const element_type*;
using reference = element_type&;
using const_reference = const element_type&;
using iterator = pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
static constexpr size_type extent = Extent;
// [span.cons], span constructors, copy, assignment, and destructor
template <
std::size_t E = Extent,
typename std::enable_if<(E == dynamic_extent || E <= 0), int>::type = 0>
constexpr span() noexcept
{}
constexpr span(pointer ptr, size_type count)
: storage_(ptr, count)
{
assert(extent == dynamic_extent || count == extent);
}
constexpr span(pointer first_elem, pointer last_elem)
: storage_(first_elem, last_elem - first_elem)
{
assert(extent == dynamic_extent ||
last_elem - first_elem ==
static_cast<std::ptrdiff_t>(extent));
}
template <std::size_t N, std::size_t E = Extent,
typename std::enable_if<
(E == dynamic_extent || N == E) &&
detail::is_container_element_type_compatible<
element_type (&)[N], ElementType>::value,
int>::type = 0>
constexpr span(element_type (&arr)[N]) noexcept : storage_(arr, N)
{}
template <std::size_t N, std::size_t E = Extent,
typename std::enable_if<
(E == dynamic_extent || N == E) &&
detail::is_container_element_type_compatible<
std::array<value_type, N>&, ElementType>::value,
int>::type = 0>
constexpr span(std::array<value_type, N>& arr) noexcept
: storage_(arr.data(), N)
{}
template <std::size_t N, std::size_t E = Extent,
typename std::enable_if<
(E == dynamic_extent || N == E) &&
detail::is_container_element_type_compatible<
const std::array<value_type, N>&, ElementType>::value,
int>::type = 0>
constexpr span(const std::array<value_type, N>& arr) noexcept
: storage_(arr.data(), N)
{}
template <
typename Container, std::size_t E = Extent,
typename std::enable_if<
E == dynamic_extent && detail::is_container<Container>::value &&
detail::is_container_element_type_compatible<
Container&, ElementType>::value,
int>::type = 0>
constexpr span(Container& cont)
: storage_(std::data(cont), std::size(cont))
{}
template <
typename Container, std::size_t E = Extent,
typename std::enable_if<
E == dynamic_extent && detail::is_container<Container>::value &&
detail::is_container_element_type_compatible<
const Container&, ElementType>::value,
int>::type = 0>
constexpr span(const Container& cont)
: storage_(std::data(cont), std::size(cont))
{}
constexpr span(const span& other) noexcept = default;
template <typename OtherElementType, std::size_t OtherExtent,
typename std::enable_if<
(Extent == OtherExtent || Extent == dynamic_extent) &&
std::is_convertible<OtherElementType (*)[],
ElementType (*)[]>::value,
int>::type = 0>
constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
: storage_(other.data(), other.size())
{}
~span() noexcept = default;
constexpr span&
operator=(const span& other) noexcept = default;
// [span.sub], span subviews
template <std::size_t Count>
constexpr span<element_type, Count> first() const
{
assert(Count <= size());
return {data(), Count};
}
template <std::size_t Count>
constexpr span<element_type, Count> last() const
{
assert(Count <= size());
return {data() + (size() - Count), Count};
}
template <std::size_t Offset, std::size_t Count = dynamic_extent>
using subspan_return_t =
span<ElementType, Count != dynamic_extent
? Count
: (Extent != dynamic_extent ? Extent - Offset
: dynamic_extent)>;
template <std::size_t Offset, std::size_t Count = dynamic_extent>
constexpr subspan_return_t<Offset, Count> subspan() const
{
assert(Offset <= size() &&
(Count == dynamic_extent || Offset + Count <= size()));
return {data() + Offset,
Count != dynamic_extent ? Count : size() - Offset};
}
constexpr span<element_type, dynamic_extent>
first(size_type count) const
{
assert(count <= size());
return {data(), count};
}
constexpr span<element_type, dynamic_extent>
last(size_type count) const
{
assert(count <= size());
return {data() + (size() - count), count};
}
constexpr span<element_type, dynamic_extent>
subspan(size_type offset, size_type count = dynamic_extent) const
{
assert(offset <= size() &&
(count == dynamic_extent || offset + count <= size()));
return {data() + offset,
count == dynamic_extent ? size() - offset : count};
}
// [span.obs], span observers
constexpr size_type size() const noexcept { return storage_.size; }
constexpr size_type size_bytes() const noexcept
{
return size() * sizeof(element_type);
}
[[nodiscard]] constexpr bool empty() const noexcept
{
return size() == 0;
}
// [span.elem], span element access
constexpr reference operator[](size_type idx) const
{
assert(idx < size());
return *(data() + idx);
}
constexpr reference front() const
{
assert(!empty());
return *data();
}
constexpr reference back() const
{
assert(!empty());
return *(data() + (size() - 1));
}
constexpr pointer data() const noexcept { return storage_.ptr; }
// [span.iterators], span iterator support
constexpr iterator begin() const noexcept { return data(); }
constexpr iterator end() const noexcept { return data() + size(); }
constexpr reverse_iterator rbegin() const noexcept
{
return reverse_iterator(end());
}
constexpr reverse_iterator rend() const noexcept
{
return reverse_iterator(begin());
}
private:
storage_type storage_{};
};
/* Deduction Guides */
template <class T, size_t N>
span(T (&)[N])->span<T, N>;
template <class T, size_t N>
span(std::array<T, N>&)->span<T, N>;
template <class T, size_t N>
span(const std::array<T, N>&)->span<const T, N>;
template <class Container>
span(Container&)->span<typename Container::value_type>;
template <class Container>
span(const Container&)->span<const typename Container::value_type>;
template <typename ElementType, std::size_t Extent>
span<const std::byte, ((Extent == dynamic_extent) ? dynamic_extent
: sizeof(ElementType) * Extent)>
as_bytes(span<ElementType, Extent> s) noexcept
{
return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
}
template <
class ElementType, size_t Extent,
typename std::enable_if<!std::is_const<ElementType>::value, int>::type = 0>
span<std::byte, ((Extent == dynamic_extent) ? dynamic_extent
: sizeof(ElementType) * Extent)>
as_writable_bytes(span<ElementType, Extent> s) noexcept
{
return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
}
template <std::size_t N, typename E, std::size_t S>
constexpr auto get(span<E, S> s) -> decltype(s[N])
{
return s[N];
}
} // namespace wpi
namespace std {
template <typename ElementType, size_t Extent>
class tuple_size<wpi::span<ElementType, Extent>>
: public integral_constant<size_t, Extent> {};
template <typename ElementType>
class tuple_size<wpi::span<
ElementType, wpi::dynamic_extent>>; // not defined
template <size_t I, typename ElementType, size_t Extent>
class tuple_element<I, wpi::span<ElementType, Extent>> {
public:
static_assert(Extent != wpi::dynamic_extent &&
I < Extent,
"");
using type = ElementType;
};
} // end namespace std
#endif // WPIUTIL_WPI_SPAN_HPP_INCLUDED

View File

@@ -0,0 +1,98 @@
#include "wpi/span.h"
#include "gtest/gtest.h"
#include <vector>
using wpi::span;
namespace {
template <typename R1, typename R2>
constexpr bool equal(R1&& r1, R2&& r2)
{
auto first1 = std::begin(r1);
const auto last1 = std::end(r1);
auto first2 = std::begin(r2);
const auto last2 = std::end(r2);
while (first1 != last1 && first2 != last2) {
if (*first1 != *first2) {
return false;
}
++first1;
++first2;
}
return true;
}
constexpr bool test_raw_array()
{
int arr[] = {1, 2, 3};
auto s = span{arr};
static_assert(std::is_same_v<decltype(s), span<int, 3>>);
return equal(arr, s);
}
constexpr bool test_const_raw_array()
{
constexpr int arr[] = {1, 2, 3};
auto s = span{arr};
static_assert(std::is_same_v<decltype(s), span<const int, 3>>);
return equal(arr, s);
}
constexpr bool test_std_array()
{
auto arr = std::array<int, 3>{1, 2, 3};
auto s = span{arr};
static_assert(std::is_same_v<decltype(s), span<int, 3>>);
return equal(arr, s);
}
constexpr bool test_const_std_array()
{
const auto arr = std::array<int, 3>{1, 2, 3};
auto s = span{arr};
static_assert(std::is_same_v<decltype(s), span<const int, 3>>);
return equal(arr, s);
}
bool test_vec()
{
auto arr = std::vector{1, 2, 3};
auto s = span{arr};
static_assert(std::is_same_v<decltype(s), span<int>>);
return equal(arr, s);
}
bool test_const_vec()
{
const auto arr = std::vector{1, 2, 3};
auto s = span{arr};
static_assert(std::is_same_v<decltype(s), span<const int>>);
return equal(arr, s);
}
}
TEST(Deduction, FromRawArrays)
{
static_assert(test_raw_array());
static_assert(test_const_raw_array());
static_assert(test_std_array());
static_assert(test_const_std_array());
ASSERT_TRUE(test_std_array());
ASSERT_TRUE(test_const_std_array());
ASSERT_TRUE(test_vec());
ASSERT_TRUE(test_const_vec());
}

View File

@@ -0,0 +1,662 @@
#include "wpi/span.h"
#include <cassert>
#include <deque>
#include <initializer_list>
#include <vector>
#include "gtest/gtest.h"
using wpi::span;
struct base {};
struct derived : base {};
TEST(Span, DefaultConstruction)
{
static_assert(std::is_nothrow_default_constructible<span<int>>::value, "");
static_assert(std::is_nothrow_default_constructible<span<int, 0>>::value,
"");
static_assert(!std::is_default_constructible<span<int, 42>>::value, "");
//SECTION("dynamic size")
{
constexpr span<int> s{};
static_assert(s.size() == 0, "");
static_assert(s.data() == nullptr, "");
// This causes an ICE on MSVC
#ifndef _MSC_VER
static_assert(s.begin() == s.end(), "");
#else
ASSERT_EQ(s.begin(), s.end());
#endif
}
//SECTION("fixed size")
{
constexpr span<int, 0> s{};
static_assert(s.size() == 0, "");
static_assert(s.data() == nullptr, "");
#ifndef _MSC_VER
static_assert(s.begin() == s.end(), "");
#else
ASSERT_EQ(s.begin(), s.end());
#endif
}
}
TEST(Span, PointerLengthConstruction)
{
static_assert(std::is_constructible<span<int>, int*, int>::value, "");
static_assert(std::is_constructible<span<const int>, int*, int>::value, "");
static_assert(
std::is_constructible<span<const int>, const int*, int>::value, "");
static_assert(std::is_constructible<span<int, 42>, int*, int>::value, "");
static_assert(std::is_constructible<span<const int, 42>, int*, int>::value,
"");
static_assert(
std::is_constructible<span<const int, 42>, const int*, int>::value, "");
//SECTION("dynamic size")
{
int arr[] = {1, 2, 3};
span<int> s(arr, 3);
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
//SECTION("fixed size")
{
int arr[] = {1, 2, 3};
span<int, 3> s(arr, 3);
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
}
TEST(Span, PointerPointerConstruction)
{
static_assert(std::is_constructible<span<int>, int*, int*>::value, "");
static_assert(!std::is_constructible<span<int>, float*, float*>::value, "");
static_assert(std::is_constructible<span<int, 42>, int*, int*>::value, "");
static_assert(!std::is_constructible<span<int, 42>, float*, float*>::value,
"");
//SECTION("dynamic size")
{
int arr[] = {1, 2, 3};
span<int> s{arr, arr + 3};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
//SECTION("fixed size")
{
int arr[] = {1, 2, 3};
span<int, 3> s{arr, arr + 3};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
}
TEST(Span, CArrayConstruction)
{
using int_array_t = int[3];
using float_array_t = float[3];
static_assert(std::is_nothrow_constructible<span<int>, int_array_t&>::value,
"");
static_assert(!std::is_constructible<span<int>, int_array_t const&>::value,
"");
static_assert(!std::is_constructible<span<int>, float_array_t>::value, "");
static_assert(
std::is_nothrow_constructible<span<const int>, int_array_t&>::value,
"");
static_assert(std::is_nothrow_constructible<span<const int>,
int_array_t const&>::value,
"");
static_assert(!std::is_constructible<span<const int>, float_array_t>::value,
"");
static_assert(
std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value, "");
static_assert(
!std::is_constructible<span<int, 3>, int_array_t const&>::value, "");
static_assert(!std::is_constructible<span<int, 3>, float_array_t&>::value,
"");
static_assert(
std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value,
"");
static_assert(std::is_nothrow_constructible<span<const int, 3>,
int_array_t const&>::value,
"");
static_assert(
!std::is_constructible<span<const int, 3>, float_array_t>::value, "");
static_assert(!std::is_constructible<span<int, 42>, int_array_t&>::value,
"");
static_assert(
!std::is_constructible<span<int, 42>, int_array_t const&>::value, "");
static_assert(!std::is_constructible<span<int, 42>, float_array_t&>::value,
"");
static_assert(
!std::is_constructible<span<const int, 42>, int_array_t&>::value, "");
static_assert(
!std::is_constructible<span<const int, 42>, int_array_t const&>::value,
"");
static_assert(
!std::is_constructible<span<const int, 42>, float_array_t&>::value, "");
//SECTION("non-const, dynamic size")
{
int arr[] = {1, 2, 3};
span<int> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
//SECTION("const, dynamic size")
{
int arr[] = {1, 2, 3};
span<int const> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
//SECTION("non-const, static size")
{
int arr[] = {1, 2, 3};
span<int, 3> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
//SECTION("const, dynamic size")
{
int arr[] = {1, 2, 3};
span<int const, 3> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.begin(), std::begin(arr));
ASSERT_EQ(s.end(), std::end(arr));
}
}
TEST(Span, StdArrayConstruction)
{
using int_array_t = std::array<int, 3>;
using float_array_t = std::array<float, 3>;
using zero_array_t = std::array<int, 0>;
static_assert(std::is_nothrow_constructible<span<int>, int_array_t&>::value,
"");
static_assert(!std::is_constructible<span<int>, int_array_t const&>::value,
"");
static_assert(!std::is_constructible<span<int>, float_array_t>::value, "");
static_assert(
std::is_nothrow_constructible<span<const int>, int_array_t&>::value,
"");
static_assert(std::is_nothrow_constructible<span<const int>,
int_array_t const&>::value,
"");
static_assert(
!std::is_constructible<span<const int>, float_array_t const&>::value,
"");
static_assert(
std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value, "");
static_assert(
!std::is_constructible<span<int, 3>, int_array_t const&>::value, "");
static_assert(!std::is_constructible<span<int, 3>, float_array_t>::value,
"");
static_assert(
std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value,
"");
static_assert(std::is_nothrow_constructible<span<const int, 3>,
int_array_t const&>::value,
"");
static_assert(
!std::is_constructible<span<const int, 3>, float_array_t const&>::value,
"");
static_assert(!std::is_constructible<span<int, 42>, int_array_t&>::value,
"");
static_assert(
!std::is_constructible<span<int, 42>, int_array_t const&>::value, "");
static_assert(
!std::is_constructible<span<int, 42>, float_array_t const&>::value, "");
static_assert(
!std::is_constructible<span<const int, 42>, int_array_t&>::value, "");
static_assert(
!std::is_constructible<span<const int, 42>, int_array_t const&>::value,
"");
static_assert(
!std::is_constructible<span<const int, 42>, float_array_t&>::value, "");
static_assert(std::is_constructible<span<int>, zero_array_t&>::value, "");
static_assert(!std::is_constructible<span<int>, const zero_array_t&>::value,
"");
static_assert(std::is_constructible<span<const int>, zero_array_t&>::value,
"");
static_assert(
std::is_constructible<span<const int>, const zero_array_t&>::value, "");
static_assert(std::is_constructible<span<int, 0>, zero_array_t&>::value,
"");
static_assert(
!std::is_constructible<span<int, 0>, const zero_array_t&>::value, "");
static_assert(
std::is_constructible<span<const int, 0>, zero_array_t&>::value, "");
static_assert(
std::is_constructible<span<const int, 0>, const zero_array_t&>::value,
"");
//SECTION("non-const, dynamic size")
{
int_array_t arr = {1, 2, 3};
span<int> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
//SECTION("const, dynamic size")
{
int_array_t arr = {1, 2, 3};
span<int const> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
//SECTION("non-const, static size")
{
int_array_t arr = {1, 2, 3};
span<int, 3> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
//SECTION("const, dynamic size")
{
int_array_t arr = {1, 2, 3};
span<int const, 3> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
}
TEST(Span, ConstructionFromOtherContainers)
{
using vec_t = std::vector<int>;
using deque_t = std::deque<int>;
static_assert(std::is_constructible<span<int>, vec_t&>::value, "");
static_assert(!std::is_constructible<span<int>, const vec_t&>::value, "");
static_assert(!std::is_constructible<span<int>, const deque_t&>::value, "");
static_assert(std::is_constructible<span<const int>, vec_t&>::value, "");
static_assert(std::is_constructible<span<const int>, const vec_t&>::value,
"");
static_assert(
!std::is_constructible<span<const int>, const deque_t&>::value, "");
static_assert(!std::is_constructible<span<int, 3>, vec_t&>::value, "");
static_assert(!std::is_constructible<span<int, 3>, const vec_t&>::value,
"");
static_assert(!std::is_constructible<span<int, 3>, const deque_t&>::value,
"");
static_assert(!std::is_constructible<span<const int, 3>, vec_t&>::value, "");
static_assert(
!std::is_constructible<span<const int, 3>, const vec_t&>::value, "");
static_assert(
!std::is_constructible<span<const int, 3>, const deque_t&>::value, "");
// vector<bool> is not contiguous and cannot be converted to span<bool>
// Regression test for https://github.com/tcbrindle/span/issues/24
static_assert(
!std::is_constructible<span<bool>, std::vector<bool>&>::value, "");
static_assert(!std::is_constructible<span<const bool>,
const std::vector<bool>&>::value, "");
//SECTION("non-const, dynamic size")
{
vec_t arr = {1, 2, 3};
span<int> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
//SECTION("const, dynamic size")
{
vec_t arr = {1, 2, 3};
span<int const> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
//SECTION("non-const, static size")
{
std::array<int, 3> arr = {1, 2, 3};
span<int, 3> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
//SECTION("const, dynamic size")
{
std::array<int, 3> arr = {1, 2, 3};
span<int const, 3> s{arr};
ASSERT_EQ(s.size(), 3u);
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.begin(), arr.data());
ASSERT_EQ(s.end(), arr.data() + 3);
}
}
TEST(Span, ConstructionFromSpansOfDifferentSize)
{
using zero_span = span<int, 0>;
using zero_const_span = span<const int, 0>;
using big_span = span<int, 1000000>;
using big_const_span = span<const int, 1000000>;
using dynamic_span = span<int>;
using dynamic_const_span = span<const int>;
static_assert(std::is_trivially_copyable<zero_span>::value, "");
static_assert(std::is_trivially_move_constructible<zero_span>::value, "");
static_assert(!std::is_constructible<zero_span, zero_const_span>::value,
"");
static_assert(!std::is_constructible<zero_span, big_span>::value, "");
static_assert(!std::is_constructible<zero_span, big_const_span>::value, "");
static_assert(!std::is_constructible<zero_span, dynamic_span>::value, "");
static_assert(!std::is_constructible<zero_span, dynamic_const_span>::value,
"");
static_assert(
std::is_nothrow_constructible<zero_const_span, zero_span>::value, "");
static_assert(std::is_trivially_copyable<zero_const_span>::value, "");
static_assert(std::is_trivially_move_constructible<zero_const_span>::value,
"");
static_assert(!std::is_constructible<zero_const_span, big_span>::value, "");
static_assert(
!std::is_constructible<zero_const_span, big_const_span>::value, "");
static_assert(!std::is_constructible<zero_const_span, dynamic_span>::value,
"");
static_assert(
!std::is_constructible<zero_const_span, dynamic_const_span>::value, "");
static_assert(!std::is_constructible<big_span, zero_span>::value, "");
static_assert(!std::is_constructible<big_span, zero_const_span>::value, "");
static_assert(std::is_trivially_copyable<big_span>::value, "");
static_assert(std::is_trivially_move_constructible<big_span>::value, "");
static_assert(!std::is_constructible<big_span, big_const_span>::value, "");
static_assert(!std::is_constructible<big_span, dynamic_span>::value, "");
static_assert(!std::is_constructible<big_span, dynamic_const_span>::value,
"");
static_assert(!std::is_constructible<big_const_span, zero_span>::value, "");
static_assert(
!std::is_constructible<big_const_span, zero_const_span>::value, "");
static_assert(std::is_trivially_copyable<big_const_span>::value, "");
static_assert(std::is_trivially_move_constructible<big_const_span>::value,
"");
static_assert(
std::is_nothrow_constructible<big_const_span, big_span>::value, "");
static_assert(!std::is_constructible<big_const_span, dynamic_span>::value,
"");
static_assert(
!std::is_constructible<big_const_span, dynamic_const_span>::value, "");
static_assert(std::is_nothrow_constructible<dynamic_span, zero_span>::value,
"");
static_assert(!std::is_constructible<dynamic_span, zero_const_span>::value,
"");
static_assert(std::is_nothrow_constructible<dynamic_span, big_span>::value,
"");
static_assert(!std::is_constructible<dynamic_span, big_const_span>::value,
"");
static_assert(std::is_trivially_copyable<dynamic_span>::value, "");
static_assert(std::is_trivially_move_constructible<dynamic_span>::value,
"");
static_assert(
!std::is_constructible<dynamic_span, dynamic_const_span>::value, "");
static_assert(
std::is_nothrow_constructible<dynamic_const_span, zero_span>::value,
"");
static_assert(std::is_nothrow_constructible<dynamic_const_span,
zero_const_span>::value,
"");
static_assert(
std::is_nothrow_constructible<dynamic_const_span, big_span>::value, "");
static_assert(std::is_nothrow_constructible<dynamic_const_span,
big_const_span>::value,
"");
static_assert(
std::is_nothrow_constructible<dynamic_const_span, dynamic_span>::value,
"");
static_assert(std::is_trivially_copyable<dynamic_const_span>::value, "");
static_assert(
std::is_trivially_move_constructible<dynamic_const_span>::value, "");
constexpr zero_const_span s0{};
constexpr dynamic_const_span d{s0};
static_assert(d.size() == 0, "");
static_assert(d.data() == nullptr, "");
#ifndef _MSC_VER
static_assert(d.begin() == d.end(), "");
#else
ASSERT_EQ(d.begin(), d.end());
#endif
}
TEST(Span, MemberSubviewOperations)
{
//SECTION("first<N>")
{
int arr[] = {1, 2, 3, 4, 5};
span<int, 5> s{arr};
auto f = s.first<3>();
static_assert(std::is_same<decltype(f), span<int, 3>>::value, "");
ASSERT_EQ(f.size(), 3u);
ASSERT_EQ(f.data(), arr);
ASSERT_EQ(f.begin(), arr);
ASSERT_EQ(f.end(), arr + 3);
}
//SECTION("last<N>")
{
int arr[] = {1, 2, 3, 4, 5};
span<int, 5> s{arr};
auto l = s.last<3>();
static_assert(std::is_same<decltype(l), span<int, 3>>::value, "");
ASSERT_EQ(l.size(), 3u);
ASSERT_EQ(l.data(), arr + 2);
ASSERT_EQ(l.begin(), arr + 2);
ASSERT_EQ(l.end(), std::end(arr));
}
//SECTION("subspan<N>")
{
int arr[] = {1, 2, 3, 4, 5};
span<int, 5> s{arr};
auto ss = s.subspan<1, 2>();
static_assert(std::is_same<decltype(ss), span<int, 2>>::value, "");
ASSERT_EQ(ss.size(), 2u);
ASSERT_EQ(ss.data(), arr + 1);
ASSERT_EQ(ss.begin(), arr + 1);
ASSERT_EQ(ss.end(), arr + 1 + 2);
}
//SECTION("first(n)")
{
int arr[] = {1, 2, 3, 4, 5};
span<int, 5> s{arr};
auto f = s.first(3);
static_assert(std::is_same<decltype(f), span<int>>::value, "");
ASSERT_EQ(f.size(), 3u);
ASSERT_EQ(f.data(), arr);
ASSERT_EQ(f.begin(), arr);
ASSERT_EQ(f.end(), arr + 3);
}
//SECTION("last(n)")
{
int arr[] = {1, 2, 3, 4, 5};
span<int, 5> s{arr};
auto l = s.last(3);
static_assert(std::is_same<decltype(l), span<int>>::value, "");
ASSERT_EQ(l.size(), 3u);
ASSERT_EQ(l.data(), arr + 2);
ASSERT_EQ(l.begin(), arr + 2);
ASSERT_EQ(l.end(), std::end(arr));
}
//SECTION("subspan(n)")
{
int arr[] = {1, 2, 3, 4, 5};
span<int, 5> s{arr};
auto ss = s.subspan(1, 2);
static_assert(std::is_same<decltype(ss), span<int>>::value, "");
ASSERT_EQ(ss.size(), 2u);
ASSERT_EQ(ss.data(), arr + 1);
ASSERT_EQ(ss.begin(), arr + 1);
ASSERT_EQ(ss.end(), arr + 1 + 2);
}
// TODO: Test all the dynamic subspan possibilities
}
TEST(Span, Observers)
{
// We already use this everywhere, but whatever
constexpr span<int, 0> empty{};
static_assert(empty.size() == 0, "");
static_assert(empty.empty(), "");
constexpr int arr[] = {1, 2, 3};
static_assert(span<const int>{arr}.size() == 3, "");
static_assert(!span<const int>{arr}.empty(), "");
}
TEST(Span, ElementAccess)
{
constexpr int arr[] = {1, 2, 3};
span<const int> s{arr};
ASSERT_EQ(s[0], arr[0]);
ASSERT_EQ(s[1], arr[1]);
ASSERT_EQ(s[2], arr[2]);
}
TEST(Span, IteratorSupport)
{
{
std::vector<int> vec;
span<int> s{vec};
std::sort(s.begin(), s.end());
ASSERT_TRUE(std::is_sorted(vec.cbegin(), vec.cend()));
}
{
const std::vector<int> vec{1, 2, 3};
span<const int> s{vec};
ASSERT_TRUE(std::equal(s.rbegin(), s.rend(), vec.crbegin()));
}
}
TEST(Span, MakeSpan)
{
{
int arr[3] = {1, 2, 3};
auto s = wpi::span(arr);
static_assert(std::is_same<decltype(s), span<int, 3>>::value, "");
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.size(), 3u);
}
{
const int arr[3] = {1, 2, 3};
auto s = wpi::span(arr);
static_assert(std::is_same<decltype(s), span<const int, 3>>::value, "");
ASSERT_EQ(s.data(), arr);
ASSERT_EQ(s.size(), 3u);
}
{
std::array<int, 3> arr = {1, 2, 3};
auto s = wpi::span(arr);
static_assert(std::is_same<decltype(s), span<int, 3>>::value, "");
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.size(), arr.size());
}
{
const std::array<int, 3> arr = {1, 2, 3};
auto s = wpi::span(arr);
static_assert(std::is_same<decltype(s), span<const int, 3>>::value, "");
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.size(), 3u);
}
{
std::vector<int> arr = {1, 2, 3};
auto s = wpi::span(arr);
static_assert(std::is_same<decltype(s), span<int>>::value, "");
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.size(), arr.size());
}
{
const std::vector<int> arr = {1, 2, 3};
auto s = wpi::span(arr);
static_assert(std::is_same<decltype(s), span<const int>>::value, "");
ASSERT_EQ(s.data(), arr.data());
ASSERT_EQ(s.size(), arr.size());
}
}

View File

@@ -0,0 +1,30 @@
#include "wpi/span.h"
#include "gtest/gtest.h"
using static_span_t = wpi::span<int, 3>;
using dynamic_span_t = wpi::span<int>;
static_assert(std::tuple_size_v<static_span_t> == static_span_t::extent);
static_assert(!wpi::detail::is_complete<std::tuple_size<dynamic_span_t>>::value);
TEST(StructuredBindings, Test)
{
// C++, why you no let me do constexpr structured bindings?
int arr[] = {1, 2, 3};
auto& [a1, a2, a3] = arr;
auto&& [s1, s2, s3] = wpi::span(arr);
ASSERT_EQ(a1, s1);
ASSERT_EQ(a2, s2);
ASSERT_EQ(a3, s3);
a1 = 99;
ASSERT_EQ(s1, 99);
s2 = 100;
ASSERT_EQ(a2, 100);
}