xref: /aosp_15_r20/external/libcxx/test/std/containers/views/span.iterators/rend.pass.cpp (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10 
11 // <span>
12 
13 // constexpr       reverse_iterator  rend() const noexcept;
14 // constexpr const_reverse_iterator crend() const noexcept;
15 
16 #include <span>
17 #include <cassert>
18 #include <string>
19 
20 #include "test_macros.h"
21 
22 template <class Span>
testConstexprSpan(Span s)23 constexpr bool testConstexprSpan(Span s)
24 {
25     bool ret = true;
26     typename Span::reverse_iterator e        = s. rend();
27     typename Span::const_reverse_iterator ce = s.crend();
28     if (s.empty())
29     {
30         ret = ret &&  ( e ==  s.rbegin());
31         ret = ret &&  (ce == s.crbegin());
32     }
33     else
34     {
35         ret = ret &&  ( e !=  s.rbegin());
36         ret = ret &&  (ce != s.crbegin());
37     }
38 
39     ret = ret &&  (( e -  s.rbegin()) == s.size());
40     ret = ret &&  ((ce - s.crbegin()) == s.size());
41 
42     ret = ret &&  (e == ce);
43     return ret;
44 }
45 
46 template <class Span>
testRuntimeSpan(Span s)47 void testRuntimeSpan(Span s)
48 {
49     typename Span::reverse_iterator e        = s. rend();
50     typename Span::const_reverse_iterator ce = s.crend();
51     if (s.empty())
52     {
53         assert( e ==  s.rbegin());
54         assert(ce == s.crbegin());
55     }
56     else
57     {
58         assert( e !=  s.rbegin());
59         assert(ce != s.crbegin());
60     }
61 
62     assert(( e -  s.rbegin()) == s.size());
63     assert((ce - s.crbegin()) == s.size());
64 
65     assert(e == ce);
66 }
67 
68 
69 struct A{};
operator ==(A,A)70 bool operator==(A, A) {return true;}
71 
72 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
73           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
74 
75 
main()76 int main()
77 {
78     static_assert(testConstexprSpan(std::span<int>()),            "");
79     static_assert(testConstexprSpan(std::span<long>()),           "");
80     static_assert(testConstexprSpan(std::span<double>()),         "");
81     static_assert(testConstexprSpan(std::span<A>()),              "");
82     static_assert(testConstexprSpan(std::span<std::string>()),    "");
83 
84     static_assert(testConstexprSpan(std::span<int, 0>()),         "");
85     static_assert(testConstexprSpan(std::span<long, 0>()),        "");
86     static_assert(testConstexprSpan(std::span<double, 0>()),      "");
87     static_assert(testConstexprSpan(std::span<A, 0>()),           "");
88     static_assert(testConstexprSpan(std::span<std::string, 0>()), "");
89 
90     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)),    "");
91     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)),    "");
92     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)),    "");
93     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)),    "");
94     static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)),    "");
95 
96 
97     testRuntimeSpan(std::span<int>        ());
98     testRuntimeSpan(std::span<long>       ());
99     testRuntimeSpan(std::span<double>     ());
100     testRuntimeSpan(std::span<A>          ());
101     testRuntimeSpan(std::span<std::string>());
102 
103     testRuntimeSpan(std::span<int, 0>        ());
104     testRuntimeSpan(std::span<long, 0>       ());
105     testRuntimeSpan(std::span<double, 0>     ());
106     testRuntimeSpan(std::span<A, 0>          ());
107     testRuntimeSpan(std::span<std::string, 0>());
108 
109     testRuntimeSpan(std::span<int>(iArr2, 1));
110     testRuntimeSpan(std::span<int>(iArr2, 2));
111     testRuntimeSpan(std::span<int>(iArr2, 3));
112     testRuntimeSpan(std::span<int>(iArr2, 4));
113     testRuntimeSpan(std::span<int>(iArr2, 5));
114 
115     std::string s;
116     testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0));
117     testRuntimeSpan(std::span<std::string>(&s, 1));
118 }
119