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
23template <typename Span>
24constexpr bool testConstexprSpan(Span sp, std::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
34template <typename Span>
35void testRuntimeSpan(Span sp, std::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
44constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
45 int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
46
47int main(int, char**)
48{
49 static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), 0), "");
50
51 static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 0), "");
52 static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 1), "");
53
54 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 0), "");
55 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 1), "");
56 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 2), "");
57
58 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 0), "");
59 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 1), "");
60 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 2), "");
61 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 3), "");
62
63
64 static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1), 0), "");
65
66 static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), 0), "");
67 static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), 1), "");
68
69 static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 0), "");
70 static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 1), "");
71 static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 2), "");
72
73 static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 0), "");
74 static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 1), "");
75 static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 2), "");
76 static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 3), "");
77
78
79 testRuntimeSpan(std::span<int>(iArr2, 1), 0);
80
81 testRuntimeSpan(std::span<int>(iArr2, 2), 0);
82 testRuntimeSpan(std::span<int>(iArr2, 2), 1);
83
84 testRuntimeSpan(std::span<int>(iArr2, 3), 0);
85 testRuntimeSpan(std::span<int>(iArr2, 3), 1);
86 testRuntimeSpan(std::span<int>(iArr2, 3), 2);
87
88 testRuntimeSpan(std::span<int>(iArr2, 4), 0);
89 testRuntimeSpan(std::span<int>(iArr2, 4), 1);
90 testRuntimeSpan(std::span<int>(iArr2, 4), 2);
91 testRuntimeSpan(std::span<int>(iArr2, 4), 3);
92
93
94 testRuntimeSpan(std::span<int, 1>(iArr2, 1), 0);
95
96 testRuntimeSpan(std::span<int, 2>(iArr2, 2), 0);
97 testRuntimeSpan(std::span<int, 2>(iArr2, 2), 1);
98
99 testRuntimeSpan(std::span<int, 3>(iArr2, 3), 0);
100 testRuntimeSpan(std::span<int, 3>(iArr2, 3), 1);
101 testRuntimeSpan(std::span<int, 3>(iArr2, 3), 2);
102
103 testRuntimeSpan(std::span<int, 4>(iArr2, 4), 0);
104 testRuntimeSpan(std::span<int, 4>(iArr2, 4), 1);
105 testRuntimeSpan(std::span<int, 4>(iArr2, 4), 2);
106 testRuntimeSpan(std::span<int, 4>(iArr2, 4), 3);
107
108 std::string s;
109 testRuntimeSpan(std::span<std::string> (&s, 1), 0);
110 testRuntimeSpan(std::span<std::string, 1>(&s, 1), 0);
111
112 return 0;
113}
114

source code of libcxx/test/std/containers/views/views.span/span.elem/op_idx.pass.cpp