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 // constexpr reference operator[](size_type idx) const;
13 //
14 
15 
16 #include <span>
17 #include <cassert>
18 #include <string>
19 
20 #include "test_macros.h"
21 
22 
23 template <typename Span>
24 constexpr bool testConstexprSpan(Span sp, size_t idx)
25 {
26     LIBCPP_ASSERT(noexcept(sp[idx]));
27 
28     typename Span::reference r1 = sp[idx];
29     typename Span::reference r2 = *(sp.data() + idx);
30     return r1 == r2;
31 }
32 
33 
34 template <typename Span>
35 void testRuntimeSpan(Span sp, size_t idx)
36 {
37     LIBCPP_ASSERT(noexcept(sp[idx]));
38 
39     typename Span::reference r1 = sp[idx];
40     typename Span::reference r2 = *(sp.data() + idx);
41     assert(r1 == r2);
42 }
43 
44 struct A{};
45 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
46           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
47 
48 int main(int, char**)
49 {
50     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), 0), "");
51 
52     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 0), "");
53     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 1), "");
54 
55     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 0), "");
56     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 1), "");
57     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 2), "");
58 
59     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 0), "");
60     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 1), "");
61     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 2), "");
62     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 3), "");
63 
64 
65     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1), 0), "");
66 
67     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), 0), "");
68     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), 1), "");
69 
70     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 0), "");
71     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 1), "");
72     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 2), "");
73 
74     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 0), "");
75     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 1), "");
76     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 2), "");
77     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 3), "");
78 
79 
80     testRuntimeSpan(std::span<int>(iArr2, 1), 0);
81 
82     testRuntimeSpan(std::span<int>(iArr2, 2), 0);
83     testRuntimeSpan(std::span<int>(iArr2, 2), 1);
84 
85     testRuntimeSpan(std::span<int>(iArr2, 3), 0);
86     testRuntimeSpan(std::span<int>(iArr2, 3), 1);
87     testRuntimeSpan(std::span<int>(iArr2, 3), 2);
88 
89     testRuntimeSpan(std::span<int>(iArr2, 4), 0);
90     testRuntimeSpan(std::span<int>(iArr2, 4), 1);
91     testRuntimeSpan(std::span<int>(iArr2, 4), 2);
92     testRuntimeSpan(std::span<int>(iArr2, 4), 3);
93 
94 
95     testRuntimeSpan(std::span<int, 1>(iArr2, 1), 0);
96 
97     testRuntimeSpan(std::span<int, 2>(iArr2, 2), 0);
98     testRuntimeSpan(std::span<int, 2>(iArr2, 2), 1);
99 
100     testRuntimeSpan(std::span<int, 3>(iArr2, 3), 0);
101     testRuntimeSpan(std::span<int, 3>(iArr2, 3), 1);
102     testRuntimeSpan(std::span<int, 3>(iArr2, 3), 2);
103 
104     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 0);
105     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 1);
106     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 2);
107     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 3);
108 
109     std::string s;
110     testRuntimeSpan(std::span<std::string>   (&s, 1), 0);
111     testRuntimeSpan(std::span<std::string, 1>(&s, 1), 0);
112 
113   return 0;
114 }
115