1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10
11 // <span>
12
13 // constexpr iterator begin() const noexcept;
14 // constexpr const_iterator cbegin() const noexcept;
15
16 #include <span>
17 #include <cassert>
18 #include <string>
19
20 #include "test_macros.h"
21
22 template <class Span>
testConstexprSpan(Span s)23 constexpr bool testConstexprSpan(Span s)
24 {
25 bool ret = true;
26 typename Span::iterator b = s. begin();
27 typename Span::const_iterator cb = s.cbegin();
28
29 if (s.empty())
30 {
31 ret = ret && ( b == s.end());
32 ret = ret && (cb == s.cend());
33 }
34 else
35 {
36 ret = ret && ( *b == s[0]);
37 ret = ret && ( &*b == &s[0]);
38 ret = ret && ( *cb == s[0]);
39 ret = ret && (&*cb == &s[0]);
40 }
41 ret = ret && (b == cb);
42 return ret;
43 }
44
45
46 template <class Span>
testRuntimeSpan(Span s)47 void testRuntimeSpan(Span s)
48 {
49 typename Span::iterator b = s. begin();
50 typename Span::const_iterator cb = s.cbegin();
51
52 if (s.empty())
53 {
54 assert( b == s.end());
55 assert(cb == s.cend());
56 }
57 else
58 {
59 assert( *b == s[0]);
60 assert( &*b == &s[0]);
61 assert( *cb == s[0]);
62 assert(&*cb == &s[0]);
63 }
64 assert(b == cb);
65 }
66
67 struct A{};
operator ==(A,A)68 bool operator==(A, A) {return true;}
69
70 constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
71 int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
72
73
main()74 int main()
75 {
76 static_assert(testConstexprSpan(std::span<int>()), "");
77 static_assert(testConstexprSpan(std::span<long>()), "");
78 static_assert(testConstexprSpan(std::span<double>()), "");
79 static_assert(testConstexprSpan(std::span<A>()), "");
80 static_assert(testConstexprSpan(std::span<std::string>()), "");
81
82 static_assert(testConstexprSpan(std::span<int, 0>()), "");
83 static_assert(testConstexprSpan(std::span<long, 0>()), "");
84 static_assert(testConstexprSpan(std::span<double, 0>()), "");
85 static_assert(testConstexprSpan(std::span<A, 0>()), "");
86 static_assert(testConstexprSpan(std::span<std::string, 0>()), "");
87
88 static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "");
89 static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "");
90 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "");
91 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "");
92 static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)), "");
93
94
95 testRuntimeSpan(std::span<int> ());
96 testRuntimeSpan(std::span<long> ());
97 testRuntimeSpan(std::span<double> ());
98 testRuntimeSpan(std::span<A> ());
99 testRuntimeSpan(std::span<std::string>());
100
101 testRuntimeSpan(std::span<int, 0> ());
102 testRuntimeSpan(std::span<long, 0> ());
103 testRuntimeSpan(std::span<double, 0> ());
104 testRuntimeSpan(std::span<A, 0> ());
105 testRuntimeSpan(std::span<std::string, 0>());
106
107 testRuntimeSpan(std::span<int>(iArr2, 1));
108 testRuntimeSpan(std::span<int>(iArr2, 2));
109 testRuntimeSpan(std::span<int>(iArr2, 3));
110 testRuntimeSpan(std::span<int>(iArr2, 4));
111 testRuntimeSpan(std::span<int>(iArr2, 5));
112
113 std::string s;
114 testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0));
115 testRuntimeSpan(std::span<std::string>(&s, 1));
116 }
117