1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
// -*- C++ -*-
//===------------------------------ span ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <span>
// template<size_t Offset, size_t Count = dynamic_extent>
// constexpr span<element_type, see below> subspan() const;
//
// constexpr span<element_type, dynamic_extent> subspan(
// size_type offset, size_type count = dynamic_extent) const;
//
// Requires: (0 <= Offset && Offset <= size())
// && (Count == dynamic_extent || Count >= 0 && Offset + Count <= size())
#include <span>
#include <cassert>
#include <algorithm>
#include <string>
#include "test_macros.h"
template <typename Span, size_t Offset, size_t Count>
constexpr bool testConstexprSpan(Span sp)
{
LIBCPP_ASSERT((noexcept(sp.template subspan<Offset, Count>())));
LIBCPP_ASSERT((noexcept(sp.subspan(Offset, Count))));
auto s1 = sp.template subspan<Offset, Count>();
auto s2 = sp.subspan(Offset, Count);
using S1 = decltype(s1);
using S2 = decltype(s2);
ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type);
ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type);
static_assert(S1::extent == Count);
static_assert(S2::extent == std::dynamic_extent, "");
return
s1.data() == s2.data()
&& s1.size() == s2.size()
&& std::equal(s1.begin(), s1.end(), sp.begin() + Offset);
}
template <typename Span, size_t Offset>
constexpr bool testConstexprSpan(Span sp)
{
LIBCPP_ASSERT((noexcept(sp.template subspan<Offset>())));
LIBCPP_ASSERT((noexcept(sp.subspan(Offset))));
auto s1 = sp.template subspan<Offset>();
auto s2 = sp.subspan(Offset);
using S1 = decltype(s1);
using S2 = decltype(s2);
ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type);
ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type);
static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Span::extent - Offset), "");
static_assert(S2::extent == std::dynamic_extent, "");
return
s1.data() == s2.data()
&& s1.size() == s2.size()
&& std::equal(s1.begin(), s1.end(), sp.begin() + Offset, sp.end());
}
template <typename Span, size_t Offset, size_t Count>
void testRuntimeSpan(Span sp)
{
LIBCPP_ASSERT((noexcept(sp.template subspan<Offset, Count>())));
LIBCPP_ASSERT((noexcept(sp.subspan(Offset, Count))));
auto s1 = sp.template subspan<Offset, Count>();
auto s2 = sp.subspan(Offset, Count);
using S1 = decltype(s1);
using S2 = decltype(s2);
ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type);
ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type);
static_assert(S1::extent == Count);
static_assert(S2::extent == std::dynamic_extent, "");
assert(s1.data() == s2.data());
assert(s1.size() == s2.size());
assert(std::equal(s1.begin(), s1.end(), sp.begin() + Offset));
}
template <typename Span, size_t Offset>
void testRuntimeSpan(Span sp)
{
LIBCPP_ASSERT((noexcept(sp.template subspan<Offset>())));
LIBCPP_ASSERT((noexcept(sp.subspan(Offset))));
auto s1 = sp.template subspan<Offset>();
auto s2 = sp.subspan(Offset);
using S1 = decltype(s1);
using S2 = decltype(s2);
ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type);
ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type);
static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Span::extent - Offset), "");
static_assert(S2::extent == std::dynamic_extent, "");
assert(s1.data() == s2.data());
assert(s1.size() == s2.size());
assert(std::equal(s1.begin(), s1.end(), sp.begin() + Offset, sp.end()));
}
constexpr int carr1[] = {1,2,3,4};
int arr1[] = {5,6,7};
int main(int, char**)
{
{
using Sp = std::span<const int>;
static_assert(testConstexprSpan<Sp, 0>(Sp{}), "");
static_assert(testConstexprSpan<Sp, 0, 4>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 3>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 2>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 1>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 0>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 1, 3>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 2, 2>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 3, 1>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 4, 0>(Sp{carr1}), "");
}
{
using Sp = std::span<const int, 4>;
static_assert(testConstexprSpan<Sp, 0, 4>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 3>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 2>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 1>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 0, 0>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 1, 3>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 2, 2>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 3, 1>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 4, 0>(Sp{carr1}), "");
}
{
using Sp = std::span<const int>;
static_assert(testConstexprSpan<Sp, 0>(Sp{}), "");
static_assert(testConstexprSpan<Sp, 0>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 1>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 2>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 3>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 4>(Sp{carr1}), "");
}
{
using Sp = std::span<const int, 4>;
static_assert(testConstexprSpan<Sp, 0>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 1>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 2>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 3>(Sp{carr1}), "");
static_assert(testConstexprSpan<Sp, 4>(Sp{carr1}), "");
}
{
using Sp = std::span<int>;
testRuntimeSpan<Sp, 0>(Sp{});
testRuntimeSpan<Sp, 0, 3>(Sp{arr1});
testRuntimeSpan<Sp, 0, 2>(Sp{arr1});
testRuntimeSpan<Sp, 0, 1>(Sp{arr1});
testRuntimeSpan<Sp, 0, 0>(Sp{arr1});
testRuntimeSpan<Sp, 1, 2>(Sp{arr1});
testRuntimeSpan<Sp, 2, 1>(Sp{arr1});
testRuntimeSpan<Sp, 3, 0>(Sp{arr1});
}
{
using Sp = std::span<int, 3>;
testRuntimeSpan<Sp, 0, 3>(Sp{arr1});
testRuntimeSpan<Sp, 0, 2>(Sp{arr1});
testRuntimeSpan<Sp, 0, 1>(Sp{arr1});
testRuntimeSpan<Sp, 0, 0>(Sp{arr1});
testRuntimeSpan<Sp, 1, 2>(Sp{arr1});
testRuntimeSpan<Sp, 2, 1>(Sp{arr1});
testRuntimeSpan<Sp, 3, 0>(Sp{arr1});
}
{
using Sp = std::span<int>;
testRuntimeSpan<Sp, 0>(Sp{});
testRuntimeSpan<Sp, 0>(Sp{arr1});
testRuntimeSpan<Sp, 1>(Sp{arr1});
testRuntimeSpan<Sp, 2>(Sp{arr1});
testRuntimeSpan<Sp, 3>(Sp{arr1});
}
{
using Sp = std::span<int, 3>;
testRuntimeSpan<Sp, 0>(Sp{arr1});
testRuntimeSpan<Sp, 1>(Sp{arr1});
testRuntimeSpan<Sp, 2>(Sp{arr1});
testRuntimeSpan<Sp, 3>(Sp{arr1});
}
return 0;
}
|