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