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