]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/std/ranges/access/cbegin.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / access / cbegin.cc
CommitLineData
a945c346 1// Copyright (C) 2019-2024 Free Software Foundation, Inc.
6d0dff49
JW
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
d4ac20b0 18// { dg-do run { target c++20 } }
6d0dff49 19
328b52d6 20#include <ranges>
261d5a4a 21#include <utility> // as_const
6d0dff49 22#include <testsuite_hooks.h>
b9e35ee6
JW
23#include <testsuite_iterators.h>
24
25static_assert(__gnu_test::is_customization_point_object(std::ranges::cbegin));
26
6d0dff49
JW
27using std::same_as;
28
29void
30test01()
31{
32 int a[2] = {};
33
34 static_assert(same_as<decltype(std::ranges::cbegin(a)), const int*>);
35 static_assert(noexcept(std::ranges::cbegin(a)));
36 VERIFY( std::ranges::cbegin(a) == (a + 0) );
37
38 constexpr long b[2] = {};
39 static_assert( std::ranges::cbegin(b) == (b + 0) );
40}
41
42struct R
43{
44 int a[4] = { 0, 1, 2, 3 };
45
46 friend int* begin(R& r) { return r.a + 0; }
b5b2e387 47 friend int* begin(R&&); // this function is not defined
6d0dff49 48 friend const int* begin(const R& r) noexcept { return r.a + 2; }
b5b2e387 49 friend const int* begin(const R&&); // this function is not defined
f12e26f3
JW
50
51#if __cpp_lib_ranges_as_const
52 friend const int* end(const R&) noexcept; // C++23 requires this.
53#endif
6d0dff49
JW
54};
55
b5b2e387
JW
56struct RV // view on an R
57{
58 R& r;
59
60 friend int* begin(RV&); // this function is not defined
61 friend const int* begin(const RV& rv) noexcept { return begin(std::as_const(rv.r)); }
f12e26f3
JW
62
63#if __cpp_lib_ranges_as_const
64 friend const int* end(const RV&) noexcept; // C++23 requires this.
65#endif
b5b2e387
JW
66};
67
68// Allow ranges::begin to work with RV&&
15411a64 69template<> constexpr bool std::ranges::enable_borrowed_range<RV> = true;
b5b2e387 70
6d0dff49
JW
71void
72test03()
73{
74 R r;
75 const R& c = r;
76 VERIFY(std::ranges::cbegin(r) == std::ranges::begin(c));
6d0dff49 77 VERIFY(std::ranges::cbegin(c) == std::ranges::begin(c));
b5b2e387
JW
78
79 RV v{r};
80 VERIFY(std::ranges::cbegin(std::move(v)) == std::ranges::begin(c));
81 const RV cv{r};
82 VERIFY(std::ranges::cbegin(std::move(cv)) == std::ranges::begin(c));
6d0dff49
JW
83}
84
85struct RR
86{
87 short s = 0;
88 long l = 0;
89 int a[4] = { 0, 1, 2, 3 };
90
91 short* begin() noexcept { return &s; }
92 const long* begin() const { return &l; }
93
94 friend int* begin(RR& r) { return r.a + 0; }
95 friend int* begin(RR&& r) { return r.a + 1; }
96 friend const int* begin(const RR& r) { return r.a + 2; }
97 friend const int* begin(const RR&& r) noexcept { return r.a + 3; }
f12e26f3
JW
98
99#if __cpp_lib_ranges_as_const
100 short* end() noexcept { return &s + 1; } // C++23 requires this.
101 const long* end() const { return &l + 1; } // C++23 requires this.
102#endif
6d0dff49
JW
103};
104
b5b2e387 105// N.B. this is a lie, cbegin on an RR rvalue will return a dangling pointer.
15411a64 106template<> constexpr bool std::ranges::enable_borrowed_range<RR> = true;
b5b2e387 107
6d0dff49
JW
108void
109test04()
110{
111 RR r;
112 const RR& c = r;
113 VERIFY(std::ranges::cbegin(r) == std::ranges::begin(c));
b5b2e387 114 VERIFY(std::ranges::cbegin(std::move(r)) == std::ranges::begin(c));
6d0dff49 115 VERIFY(std::ranges::cbegin(c) == std::ranges::begin(c));
b5b2e387 116 VERIFY(std::ranges::cbegin(std::move(c)) == std::ranges::begin(c));
6d0dff49
JW
117}
118
119int
120main()
121{
122 test01();
123 test03();
124 test04();
125}