Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/access/cend.cc

raw
1 // Copyright (C) 2019-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++20 } }
19
20 #include <ranges>
21 #include <utility> // as_const
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 static_assert(__gnu_test::is_customization_point_object(std::ranges::cend));
26
27 using std::same_as;
28
29 void
30 test01()
31 {
32 int a[2] = {};
33
34 static_assert(same_as<decltype(std::ranges::cend(a)), const int*>);
35 static_assert(noexcept(std::ranges::cend(a)));
36 VERIFY( std::ranges::cend(a) == (a + 2) );
37 }
38
39 struct R
40 {
41 int a[4] = { 0, 1, 2, 3 };
42
43 const int* begin() const { return nullptr; }
44 friend const int* begin(const R&&) noexcept { return nullptr; }
45
46 // Should be ignored because it doesn't return a sentinel for int*
47 const long* end() const { return nullptr; }
48
49 friend int* end(R& r) { return r.a + 0; }
50 friend int* end(R&& r) { return r.a + 1; }
51 friend const int* end(const R& r) noexcept { return r.a + 2; }
52 friend const int* end(const R&& r) noexcept { return r.a + 3; }
53 };
54
55 struct RV // view on an R
56 {
57 R& r;
58
59 friend const int* begin(RV& rv) { return rv.r.begin(); }
60 friend int* end(RV& rv) { return end(rv.r); }
61 friend const int* begin(const RV& rv) noexcept { return rv.r.begin(); }
62 friend const int* end(const RV& rv) noexcept { return end(std::as_const(rv.r)); }
63 };
64
65 // Allow ranges::end to work with RV&&
66 template<> constexpr bool std::ranges::enable_borrowed_range<RV> = true;
67
68 void
69 test03()
70 {
71 R r;
72 const R& c = r;
73 VERIFY( std::ranges::cend(r) == std::ranges::end(c) );
74 VERIFY( std::ranges::cend(c) == std::ranges::end(c) );
75
76 RV v{r};
77 VERIFY( std::ranges::cend(std::move(v)) == std::ranges::end(c) );
78
79 const RV cv{r};
80 VERIFY( std::ranges::cend(std::move(cv)) == std::ranges::end(c) );
81 }
82
83 struct RR
84 {
85 short s = 0;
86 short l = 0;
87 int a[4] = { 0, 1, 2, 3 };
88
89 const void* begin() const; // return type not an iterator
90
91 friend int* end(RR&) { throw 1; }
92 short* end() noexcept { return &s; }
93
94 friend const short* begin(const RR&) noexcept;
95 const short* end() const { return &l; }
96
97 friend int* begin(RR&&) noexcept;
98 friend int* end(RR&& r) { return r.a + 1; }
99
100 friend const int* begin(const RR&&) noexcept;
101 friend const int* end(const RR&& r) noexcept { return r.a + 3; }
102 };
103
104 // N.B. this is a lie, begin/end on an RR rvalue will return a dangling pointer.
105 template<> constexpr bool std::ranges::enable_borrowed_range<RR> = true;
106
107 void
108 test04()
109 {
110 RR r;
111 const RR& c = r;
112 VERIFY( std::ranges::cend(r) == std::ranges::end(c) );
113 VERIFY( std::ranges::cend(c) == std::ranges::end(c) );
114
115 VERIFY( std::ranges::cend(std::move(r)) == std::ranges::end(c) );
116 VERIFY( std::ranges::cend(std::move(c)) == std::ranges::end(c) );
117 }
118
119 int
120 main()
121 {
122 test01();
123 test03();
124 test04();
125 }