| 1 |
//===---------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===---------------------------------------------------------------------===// |
| 8 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 9 |
|
| 10 |
// <span> |
| 11 |
|
| 12 |
// template <class It, class End> |
| 13 |
// constexpr explicit(Extent != dynamic_extent) span(It first, End last); |
| 14 |
// Requires: [first, last) shall be a valid range. |
| 15 |
// If Extent is not equal to dynamic_extent, then last - first shall be equal to Extent. |
| 16 |
// Throws: When and what last - first throws. |
| 17 |
|
| 18 |
#include <array> |
| 19 |
#include <span> |
| 20 |
#include <cassert> |
| 21 |
#include <utility> |
| 22 |
|
| 23 |
#include "assert_macros.h" |
| 24 |
#include "test_iterators.h" |
| 25 |
#include "test_macros.h" |
| 26 |
|
| 27 |
template <class T, class Sentinel> |
| 28 |
constexpr bool test_ctor() { |
| 29 |
T val[2] = {}; |
| 30 |
auto s1 = std::span<T>(std::begin(val), Sentinel(std::end(val))); |
| 31 |
auto s2 = std::span<T, 2>(std::begin(val), Sentinel(std::end(val))); |
| 32 |
assert(s1.data() == std::data(val) && s1.size() == std::size(val)); |
| 33 |
assert(s2.data() == std::data(val) && s2.size() == std::size(val)); |
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
template <std::size_t Extent> |
| 38 |
constexpr void test_constructibility() { |
| 39 |
static_assert(std::is_constructible_v<std::span<int, Extent>, int*, int*>); |
| 40 |
static_assert(!std::is_constructible_v<std::span<int, Extent>, const int*, const int*>); |
| 41 |
static_assert(!std::is_constructible_v<std::span<int, Extent>, volatile int*, volatile int*>); |
| 42 |
static_assert(std::is_constructible_v<std::span<const int, Extent>, int*, int*>); |
| 43 |
static_assert(std::is_constructible_v<std::span<const int, Extent>, const int*, const int*>); |
| 44 |
static_assert(!std::is_constructible_v<std::span<const int, Extent>, volatile int*, volatile int*>); |
| 45 |
static_assert(std::is_constructible_v<std::span<volatile int, Extent>, int*, int*>); |
| 46 |
static_assert(!std::is_constructible_v<std::span<volatile int, Extent>, const int*, const int*>); |
| 47 |
static_assert(std::is_constructible_v<std::span<volatile int, Extent>, volatile int*, volatile int*>); |
| 48 |
static_assert(!std::is_constructible_v<std::span<int, Extent>, int*, float*>); // types wrong |
| 49 |
} |
| 50 |
|
| 51 |
constexpr bool test() { |
| 52 |
test_constructibility<std::dynamic_extent>(); |
| 53 |
test_constructibility<3>(); |
| 54 |
struct A {}; |
| 55 |
assert((test_ctor<int, int*>())); |
| 56 |
assert((test_ctor<int, sized_sentinel<int*>>())); |
| 57 |
assert((test_ctor<A, A*>())); |
| 58 |
assert((test_ctor<A, sized_sentinel<A*>>())); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 63 |
// A stripped down contiguous iterator that throws when using operator-. |
| 64 |
template <class It> |
| 65 |
class throw_operator_minus { |
| 66 |
It it_; |
| 67 |
|
| 68 |
public: |
| 69 |
typedef std::contiguous_iterator_tag iterator_category; |
| 70 |
typedef typename std::iterator_traits<It>::value_type value_type; |
| 71 |
typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 72 |
typedef It pointer; |
| 73 |
typedef typename std::iterator_traits<It>::reference reference; |
| 74 |
typedef std::remove_reference_t<reference> element_type; |
| 75 |
|
| 76 |
throw_operator_minus() : it_() {} |
| 77 |
explicit throw_operator_minus(It it) : it_(it) {} |
| 78 |
|
| 79 |
reference operator*() const { return *it_; } |
| 80 |
pointer operator->() const { return it_; } |
| 81 |
reference operator[](difference_type n) const { return it_[n]; } |
| 82 |
|
| 83 |
throw_operator_minus& operator++() { |
| 84 |
++it_; |
| 85 |
return *this; |
| 86 |
} |
| 87 |
throw_operator_minus& operator--() { |
| 88 |
--it_; |
| 89 |
return *this; |
| 90 |
} |
| 91 |
throw_operator_minus operator++(int) { return throw_operator_minus(it_++); } |
| 92 |
throw_operator_minus operator--(int) { return throw_operator_minus(it_--); } |
| 93 |
|
| 94 |
throw_operator_minus& operator+=(difference_type n) { |
| 95 |
it_ += n; |
| 96 |
return *this; |
| 97 |
} |
| 98 |
throw_operator_minus& operator-=(difference_type n) { |
| 99 |
it_ -= n; |
| 100 |
return *this; |
| 101 |
} |
| 102 |
friend throw_operator_minus operator+(throw_operator_minus x, difference_type n) { |
| 103 |
x += n; |
| 104 |
return x; |
| 105 |
} |
| 106 |
friend throw_operator_minus operator+(difference_type n, throw_operator_minus x) { |
| 107 |
x += n; |
| 108 |
return x; |
| 109 |
} |
| 110 |
friend throw_operator_minus operator-(throw_operator_minus x, difference_type n) { |
| 111 |
x -= n; |
| 112 |
return x; |
| 113 |
} |
| 114 |
friend difference_type operator-(throw_operator_minus, throw_operator_minus) { throw 42; }; |
| 115 |
|
| 116 |
friend bool operator==(const throw_operator_minus& x, const throw_operator_minus& y) { return x.it_ == y.it_; } |
| 117 |
friend bool operator<(const throw_operator_minus& x, const throw_operator_minus& y) { return x.it_ < y.it_; } |
| 118 |
friend bool operator>(const throw_operator_minus& x, const throw_operator_minus& y) { return x.it_ > y.it_; } |
| 119 |
friend bool operator<=(const throw_operator_minus& x, const throw_operator_minus& y) { return x.it_ <= y.it_; } |
| 120 |
friend bool operator>=(const throw_operator_minus& x, const throw_operator_minus& y) { return x.it_ >= y.it_; } |
| 121 |
}; |
| 122 |
|
| 123 |
template <class It> |
| 124 |
throw_operator_minus(It) -> throw_operator_minus<It>; |
| 125 |
|
| 126 |
void test_exceptions() { |
| 127 |
std::array a{42}; |
| 128 |
TEST_VALIDATE_EXCEPTION( |
| 129 |
int, |
| 130 |
[](int i) { assert(i == 42); }, |
| 131 |
(std::span<int>{throw_operator_minus{a.begin()}, throw_operator_minus{a.end()}})); |
| 132 |
TEST_VALIDATE_EXCEPTION( |
| 133 |
int, |
| 134 |
[](int i) { assert(i == 42); }, |
| 135 |
(std::span<int, 1>{throw_operator_minus{a.begin()}, throw_operator_minus{a.end()}})); |
| 136 |
} |
| 137 |
#endif // TEST_HAS_NO_EXCEPTIONS |
| 138 |
|
| 139 |
int main(int, char**) { |
| 140 |
test(); |
| 141 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 142 |
test_exceptions(); |
| 143 |
#endif |
| 144 |
static_assert(test()); |
| 145 |
|
| 146 |
return 0; |
| 147 |
} |