2 //===------------------------------ span ---------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===---------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // constexpr reference operator[](size_type idx) const;
14 // constexpr reference operator()(size_type idx) const;
22 #include "test_macros.h"
25 template <typename Span
>
26 constexpr bool testConstexprSpan(Span sp
, size_t idx
)
28 LIBCPP_ASSERT(noexcept(sp
[idx
]));
30 typename
Span::reference r1
= sp
[idx
];
31 typename
Span::reference r2
= *(sp
.data() + idx
);
36 template <typename Span
>
37 void testRuntimeSpan(Span sp
, size_t idx
)
39 LIBCPP_ASSERT(noexcept(sp
[idx
]));
41 typename
Span::reference r1
= sp
[idx
];
42 typename
Span::reference r2
= *(sp
.data() + idx
);
47 constexpr int iArr1
[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
48 int iArr2
[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
52 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 1), 0), "");
54 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 2), 0), "");
55 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 2), 1), "");
57 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 3), 0), "");
58 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 3), 1), "");
59 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 3), 2), "");
61 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 4), 0), "");
62 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 4), 1), "");
63 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 4), 2), "");
64 static_assert(testConstexprSpan(std::span
<const int>(iArr1
, 4), 3), "");
67 static_assert(testConstexprSpan(std::span
<const int, 1>(iArr1
, 1), 0), "");
69 static_assert(testConstexprSpan(std::span
<const int, 2>(iArr1
, 2), 0), "");
70 static_assert(testConstexprSpan(std::span
<const int, 2>(iArr1
, 2), 1), "");
72 static_assert(testConstexprSpan(std::span
<const int, 3>(iArr1
, 3), 0), "");
73 static_assert(testConstexprSpan(std::span
<const int, 3>(iArr1
, 3), 1), "");
74 static_assert(testConstexprSpan(std::span
<const int, 3>(iArr1
, 3), 2), "");
76 static_assert(testConstexprSpan(std::span
<const int, 4>(iArr1
, 4), 0), "");
77 static_assert(testConstexprSpan(std::span
<const int, 4>(iArr1
, 4), 1), "");
78 static_assert(testConstexprSpan(std::span
<const int, 4>(iArr1
, 4), 2), "");
79 static_assert(testConstexprSpan(std::span
<const int, 4>(iArr1
, 4), 3), "");
82 testRuntimeSpan(std::span
<int>(iArr2
, 1), 0);
84 testRuntimeSpan(std::span
<int>(iArr2
, 2), 0);
85 testRuntimeSpan(std::span
<int>(iArr2
, 2), 1);
87 testRuntimeSpan(std::span
<int>(iArr2
, 3), 0);
88 testRuntimeSpan(std::span
<int>(iArr2
, 3), 1);
89 testRuntimeSpan(std::span
<int>(iArr2
, 3), 2);
91 testRuntimeSpan(std::span
<int>(iArr2
, 4), 0);
92 testRuntimeSpan(std::span
<int>(iArr2
, 4), 1);
93 testRuntimeSpan(std::span
<int>(iArr2
, 4), 2);
94 testRuntimeSpan(std::span
<int>(iArr2
, 4), 3);
97 testRuntimeSpan(std::span
<int, 1>(iArr2
, 1), 0);
99 testRuntimeSpan(std::span
<int, 2>(iArr2
, 2), 0);
100 testRuntimeSpan(std::span
<int, 2>(iArr2
, 2), 1);
102 testRuntimeSpan(std::span
<int, 3>(iArr2
, 3), 0);
103 testRuntimeSpan(std::span
<int, 3>(iArr2
, 3), 1);
104 testRuntimeSpan(std::span
<int, 3>(iArr2
, 3), 2);
106 testRuntimeSpan(std::span
<int, 4>(iArr2
, 4), 0);
107 testRuntimeSpan(std::span
<int, 4>(iArr2
, 4), 1);
108 testRuntimeSpan(std::span
<int, 4>(iArr2
, 4), 2);
109 testRuntimeSpan(std::span
<int, 4>(iArr2
, 4), 3);
112 testRuntimeSpan(std::span
<std::string
> (&s
, 1), 0);
113 testRuntimeSpan(std::span
<std::string
, 1>(&s
, 1), 0);