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