| 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 front() const noexcept; |
| 13 | // Expects: empty() is false. |
| 14 | // Effects: Equivalent to: return *data(); |
| 15 | // |
| 16 | |
| 17 | #include <span> |
| 18 | #include <cassert> |
| 19 | #include <string> |
| 20 | |
| 21 | #include "test_macros.h" |
| 22 | |
| 23 | template <typename Span> |
| 24 | constexpr bool testConstexprSpan(Span sp) { |
| 25 | LIBCPP_ASSERT(noexcept(sp.front())); |
| 26 | return std::addressof(sp.front()) == sp.data(); |
| 27 | } |
| 28 | |
| 29 | template <typename Span> |
| 30 | void testRuntimeSpan(Span sp) { |
| 31 | LIBCPP_ASSERT(noexcept(sp.front())); |
| 32 | assert(std::addressof(sp.front()) == sp.data()); |
| 33 | } |
| 34 | |
| 35 | template <typename Span> |
| 36 | void testEmptySpan(Span sp) { |
| 37 | if (!sp.empty()) [[maybe_unused]] |
| 38 | auto res = sp.front(); |
| 39 | } |
| 40 | |
| 41 | struct A {}; |
| 42 | constexpr int iArr1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 43 | int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; |
| 44 | |
| 45 | int main(int, char**) { |
| 46 | static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "" ); |
| 47 | static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "" ); |
| 48 | static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "" ); |
| 49 | static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "" ); |
| 50 | |
| 51 | static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1)), "" ); |
| 52 | static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2)), "" ); |
| 53 | static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3)), "" ); |
| 54 | static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4)), "" ); |
| 55 | |
| 56 | testRuntimeSpan(std::span<int>(iArr2, 1)); |
| 57 | testRuntimeSpan(std::span<int>(iArr2, 2)); |
| 58 | testRuntimeSpan(std::span<int>(iArr2, 3)); |
| 59 | testRuntimeSpan(std::span<int>(iArr2, 4)); |
| 60 | |
| 61 | testRuntimeSpan(std::span<int, 1>(iArr2, 1)); |
| 62 | testRuntimeSpan(std::span<int, 2>(iArr2, 2)); |
| 63 | testRuntimeSpan(std::span<int, 3>(iArr2, 3)); |
| 64 | testRuntimeSpan(std::span<int, 4>(iArr2, 4)); |
| 65 | |
| 66 | std::string s; |
| 67 | testRuntimeSpan(std::span<std::string>(&s, 1)); |
| 68 | testRuntimeSpan(std::span<std::string, 1>(&s, 1)); |
| 69 | |
| 70 | std::span<int, 0> sp; |
| 71 | testEmptySpan(sp); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |