|
16 | 16 | // Extent == dynamic_extent || Extent == OtherExtent is true, and
|
17 | 17 | // OtherElementType(*)[] is convertible to ElementType(*)[].
|
18 | 18 |
|
19 |
| - |
20 | 19 | #include <span>
|
21 | 20 | #include <cassert>
|
22 | 21 | #include <string>
|
23 | 22 |
|
24 | 23 | #include "test_macros.h"
|
25 | 24 |
|
26 |
| -void checkCV() |
27 |
| -{ |
28 |
| - std::span< int> sp; |
29 |
| -// std::span<const int> csp; |
30 |
| - std::span< volatile int> vsp; |
31 |
| -// std::span<const volatile int> cvsp; |
| 25 | +template <class T, class From> |
| 26 | +TEST_CONSTEXPR_CXX20 void check() { |
| 27 | + // dynamic -> dynamic |
| 28 | + { |
| 29 | + { |
| 30 | + std::span<From> from; |
| 31 | + std::span<T> span{from}; |
| 32 | + ASSERT_NOEXCEPT(std::span<T>(from)); |
| 33 | + assert(span.data() == nullptr); |
| 34 | + assert(span.size() == 0); |
| 35 | + } |
| 36 | + { |
| 37 | + From array[3] = {}; |
| 38 | + std::span<From> from(array); |
| 39 | + std::span<T> span{from}; |
| 40 | + ASSERT_NOEXCEPT(std::span<T>(from)); |
| 41 | + assert(span.data() == array); |
| 42 | + assert(span.size() == 3); |
| 43 | + } |
| 44 | + } |
32 | 45 |
|
33 |
| - std::span< int, 0> sp0; |
34 |
| -// std::span<const int, 0> csp0; |
35 |
| - std::span< volatile int, 0> vsp0; |
36 |
| -// std::span<const volatile int, 0> cvsp0; |
| 46 | + // static -> static |
| 47 | + { |
| 48 | + { |
| 49 | + std::span<From, 0> from; |
| 50 | + std::span<T, 0> span{from}; |
| 51 | + ASSERT_NOEXCEPT(std::span<T, 0>(from)); |
| 52 | + assert(span.data() == nullptr); |
| 53 | + assert(span.size() == 0); |
| 54 | + } |
37 | 55 |
|
38 |
| -// dynamic -> dynamic |
39 | 56 | {
|
40 |
| - std::span<const int> s1{ sp}; // a span<const int> pointing at int. |
41 |
| - std::span< volatile int> s2{ sp}; // a span< volatile int> pointing at int. |
42 |
| - std::span<const volatile int> s3{ sp}; // a span<const volatile int> pointing at int. |
43 |
| - std::span<const volatile int> s4{ vsp}; // a span<const volatile int> pointing at volatile int. |
44 |
| - assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); |
| 57 | + From array[3] = {}; |
| 58 | + std::span<From, 3> from(array); |
| 59 | + std::span<T, 3> span{from}; |
| 60 | + ASSERT_NOEXCEPT(std::span<T, 3>(from)); |
| 61 | + assert(span.data() == array); |
| 62 | + assert(span.size() == 3); |
45 | 63 | }
|
| 64 | + } |
46 | 65 |
|
47 |
| -// static -> static |
| 66 | + // static -> dynamic |
| 67 | + { |
48 | 68 | {
|
49 |
| - std::span<const int, 0> s1{ sp0}; // a span<const int> pointing at int. |
50 |
| - std::span< volatile int, 0> s2{ sp0}; // a span< volatile int> pointing at int. |
51 |
| - std::span<const volatile int, 0> s3{ sp0}; // a span<const volatile int> pointing at int. |
52 |
| - std::span<const volatile int, 0> s4{ vsp0}; // a span<const volatile int> pointing at volatile int. |
53 |
| - assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); |
| 69 | + std::span<From, 0> from; |
| 70 | + std::span<T> span{from}; |
| 71 | + ASSERT_NOEXCEPT(std::span<T>(from)); |
| 72 | + assert(span.data() == nullptr); |
| 73 | + assert(span.size() == 0); |
54 | 74 | }
|
55 | 75 |
|
56 |
| -// static -> dynamic |
57 | 76 | {
|
58 |
| - std::span<const int> s1{ sp0}; // a span<const int> pointing at int. |
59 |
| - std::span< volatile int> s2{ sp0}; // a span< volatile int> pointing at int. |
60 |
| - std::span<const volatile int> s3{ sp0}; // a span<const volatile int> pointing at int. |
61 |
| - std::span<const volatile int> s4{ vsp0}; // a span<const volatile int> pointing at volatile int. |
62 |
| - assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); |
| 77 | + From array[3] = {}; |
| 78 | + std::span<From, 3> from(array); |
| 79 | + std::span<T> span{from}; |
| 80 | + ASSERT_NOEXCEPT(std::span<T>(from)); |
| 81 | + assert(span.data() == array); |
| 82 | + assert(span.size() == 3); |
63 | 83 | }
|
| 84 | + } |
64 | 85 |
|
65 |
| -// dynamic -> static (not allowed) |
| 86 | + // dynamic -> static (not allowed) |
66 | 87 | }
|
67 | 88 |
|
| 89 | +template <class T> |
| 90 | +TEST_CONSTEXPR_CXX20 void check_cvs() { |
| 91 | + check<T, T>(); |
68 | 92 |
|
69 |
| -template <typename T> |
70 |
| -constexpr bool testConstexprSpan() |
71 |
| -{ |
72 |
| - std::span<T> s0{}; |
73 |
| - std::span<T, 0> s1{}; |
74 |
| - std::span<T> s2(s1); // static -> dynamic |
75 |
| - ASSERT_NOEXCEPT(std::span<T> {s0}); |
76 |
| - ASSERT_NOEXCEPT(std::span<T, 0>{s1}); |
77 |
| - ASSERT_NOEXCEPT(std::span<T> {s1}); |
| 93 | + check<T const, T>(); |
| 94 | + check<T const, T const>(); |
78 | 95 |
|
79 |
| - return |
80 |
| - s1.data() == nullptr && s1.size() == 0 |
81 |
| - && s2.data() == nullptr && s2.size() == 0; |
82 |
| -} |
83 |
| - |
84 |
| - |
85 |
| -template <typename T> |
86 |
| -void testRuntimeSpan() |
87 |
| -{ |
88 |
| - std::span<T> s0{}; |
89 |
| - std::span<T, 0> s1{}; |
90 |
| - std::span<T> s2(s1); // static -> dynamic |
91 |
| - ASSERT_NOEXCEPT(std::span<T> {s0}); |
92 |
| - ASSERT_NOEXCEPT(std::span<T, 0>{s1}); |
93 |
| - ASSERT_NOEXCEPT(std::span<T> {s1}); |
| 96 | + check<T volatile, T>(); |
| 97 | + check<T volatile, T volatile>(); |
94 | 98 |
|
95 |
| - assert(s1.data() == nullptr && s1.size() == 0); |
96 |
| - assert(s2.data() == nullptr && s2.size() == 0); |
| 99 | + check<T const volatile, T>(); |
| 100 | + check<T const volatile, T const>(); |
| 101 | + check<T const volatile, T volatile>(); |
| 102 | + check<T const volatile, T const volatile>(); |
97 | 103 | }
|
98 | 104 |
|
| 105 | +struct A {}; |
99 | 106 |
|
100 |
| -struct A{}; |
101 |
| - |
102 |
| -int main(int, char**) |
103 |
| -{ |
104 |
| - static_assert(testConstexprSpan<int>(), ""); |
105 |
| - static_assert(testConstexprSpan<long>(), ""); |
106 |
| - static_assert(testConstexprSpan<double>(), ""); |
107 |
| - static_assert(testConstexprSpan<A>(), ""); |
108 |
| - |
109 |
| - testRuntimeSpan<int>(); |
110 |
| - testRuntimeSpan<long>(); |
111 |
| - testRuntimeSpan<double>(); |
112 |
| - testRuntimeSpan<std::string>(); |
113 |
| - testRuntimeSpan<A>(); |
| 107 | +TEST_CONSTEXPR_CXX20 bool test() { |
| 108 | + check_cvs<int>(); |
| 109 | + check_cvs<long>(); |
| 110 | + check_cvs<double>(); |
| 111 | + check_cvs<std::string>(); |
| 112 | + check_cvs<A>(); |
| 113 | + return true; |
| 114 | +} |
114 | 115 |
|
115 |
| - checkCV(); |
| 116 | +int main(int, char**) { |
| 117 | + static_assert(test()); |
| 118 | + test(); |
116 | 119 |
|
117 | 120 | return 0;
|
118 | 121 | }
|
0 commit comments