]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/containers/views/span.iterators/rbegin.pass.cpp
Vendor import of libc++ trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / std / containers / views / span.iterators / rbegin.pass.cpp
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  rbegin() const noexcept;
14 // constexpr const_reverse_iterator crbegin() const noexcept;
15
16 #include <span>
17 #include <cassert>
18 #include <string>
19
20 #include "test_macros.h"
21
22 template <class Span>
23 constexpr bool testConstexprSpan(Span s)
24 {
25     bool ret = true;
26     typename Span::reverse_iterator b        = s. rbegin();
27     typename Span::const_reverse_iterator cb = s.crbegin();
28     if (s.empty())
29     {
30         ret = ret &&  ( b ==  s.rend());
31         ret = ret &&  (cb == s.crend());
32     }
33     else
34     {
35         const typename Span::index_type last = s.size() - 1;
36         ret = ret &&  (  *b ==  s[last]);
37         ret = ret &&  ( &*b == &s[last]);
38         ret = ret &&  ( *cb ==  s[last]);
39         ret = ret &&  (&*cb == &s[last]);
40     }
41     ret = ret &&  (b == cb);
42     return ret;
43 }
44
45
46 template <class Span>
47 void testRuntimeSpan(Span s)
48 {
49     typename Span::reverse_iterator b        = s. rbegin();
50     typename Span::const_reverse_iterator cb = s.crbegin();
51     if (s.empty())
52     {
53         assert( b ==  s.rend());
54         assert(cb == s.crend());
55     }
56     else
57     {
58         const typename Span::index_type last = s.size() - 1;
59         assert(  *b ==  s[last]);
60         assert( &*b == &s[last]);
61         assert( *cb ==  s[last]);
62         assert(&*cb == &s[last]);
63     }
64     assert(b == cb);
65 }
66
67
68 struct A{};
69 bool operator==(A, A) {return true;}
70
71 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
72           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
73
74
75 int main()
76 {
77     static_assert(testConstexprSpan(std::span<int>()),            "");
78     static_assert(testConstexprSpan(std::span<long>()),           "");
79     static_assert(testConstexprSpan(std::span<double>()),         "");
80     static_assert(testConstexprSpan(std::span<A>()),              "");
81     static_assert(testConstexprSpan(std::span<std::string>()),    "");
82
83     static_assert(testConstexprSpan(std::span<int, 0>()),         "");
84     static_assert(testConstexprSpan(std::span<long, 0>()),        "");
85     static_assert(testConstexprSpan(std::span<double, 0>()),      "");
86     static_assert(testConstexprSpan(std::span<A, 0>()),           "");
87     static_assert(testConstexprSpan(std::span<std::string, 0>()), "");
88
89     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)),    "");
90     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)),    "");
91     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)),    "");
92     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)),    "");
93     static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)),    "");
94
95
96     testRuntimeSpan(std::span<int>        ());
97     testRuntimeSpan(std::span<long>       ());
98     testRuntimeSpan(std::span<double>     ());
99     testRuntimeSpan(std::span<A>          ());
100     testRuntimeSpan(std::span<std::string>());
101
102     testRuntimeSpan(std::span<int, 0>        ());
103     testRuntimeSpan(std::span<long, 0>       ());
104     testRuntimeSpan(std::span<double, 0>     ());
105     testRuntimeSpan(std::span<A, 0>          ());
106     testRuntimeSpan(std::span<std::string, 0>());
107
108     testRuntimeSpan(std::span<int>(iArr2, 1));
109     testRuntimeSpan(std::span<int>(iArr2, 2));
110     testRuntimeSpan(std::span<int>(iArr2, 3));
111     testRuntimeSpan(std::span<int>(iArr2, 4));
112     testRuntimeSpan(std::span<int>(iArr2, 5));
113
114     std::string s;
115     testRuntimeSpan(std::span<std::string>(&s, static_cast<std::ptrdiff_t>(0)));
116     testRuntimeSpan(std::span<std::string>(&s, 1));
117 }