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 reverse_iterator rbegin() const noexcept;
13// constexpr const_reverse_iterator crbegin() const noexcept;
14
15#include <span>
16#include <cassert>
17#include <string>
18
19#include "test_macros.h"
20
21template <class Span>
22constexpr bool testConstexprSpan(Span s) {
23 bool ret = true;
24 typename Span::reverse_iterator b = s.rbegin();
25 if (s.empty()) {
26 ret = ret && (b == s.rend());
27 } else {
28 const typename Span::size_type last = s.size() - 1;
29 ret = ret && (*b == s[last]);
30 ret = ret && (&*b == &s[last]);
31 }
32 return ret;
33}
34
35template <class Span>
36void testRuntimeSpan(Span s) {
37 typename Span::reverse_iterator b = s.rbegin();
38 if (s.empty()) {
39 assert(b == s.rend());
40 } else {
41 const typename Span::size_type last = s.size() - 1;
42 assert(*b == s[last]);
43 assert(&*b == &s[last]);
44 }
45}
46
47struct A {};
48bool operator==(A, A) { return true; }
49
50constexpr int iArr1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
51int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
52
53int main(int, char**) {
54 static_assert(testConstexprSpan(std::span<int>()), "");
55 static_assert(testConstexprSpan(std::span<long>()), "");
56 static_assert(testConstexprSpan(std::span<double>()), "");
57 static_assert(testConstexprSpan(std::span<A>()), "");
58 static_assert(testConstexprSpan(std::span<std::string>()), "");
59
60 static_assert(testConstexprSpan(std::span<int, 0>()), "");
61 static_assert(testConstexprSpan(std::span<long, 0>()), "");
62 static_assert(testConstexprSpan(std::span<double, 0>()), "");
63 static_assert(testConstexprSpan(std::span<A, 0>()), "");
64 static_assert(testConstexprSpan(std::span<std::string, 0>()), "");
65
66 static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "");
67 static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "");
68 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "");
69 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "");
70 static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)), "");
71
72 testRuntimeSpan(std::span<int>());
73 testRuntimeSpan(std::span<long>());
74 testRuntimeSpan(std::span<double>());
75 testRuntimeSpan(std::span<A>());
76 testRuntimeSpan(std::span<std::string>());
77
78 testRuntimeSpan(std::span<int, 0>());
79 testRuntimeSpan(std::span<long, 0>());
80 testRuntimeSpan(std::span<double, 0>());
81 testRuntimeSpan(std::span<A, 0>());
82 testRuntimeSpan(std::span<std::string, 0>());
83
84 testRuntimeSpan(std::span<int>(iArr2, 1));
85 testRuntimeSpan(std::span<int>(iArr2, 2));
86 testRuntimeSpan(std::span<int>(iArr2, 3));
87 testRuntimeSpan(std::span<int>(iArr2, 4));
88 testRuntimeSpan(std::span<int>(iArr2, 5));
89
90 std::string s;
91 testRuntimeSpan(std::span<std::string>(&s, static_cast<std::size_t>(0)));
92 testRuntimeSpan(std::span<std::string>(&s, 1));
93
94 return 0;
95}
96

source code of libcxx/test/std/containers/views/views.span/span.iterators/rbegin.pass.cpp