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