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