| 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 <testsuite_hooks.h> |
| 22 |
#include <testsuite_iterators.h> |
| 23 |
|
| 24 |
static_assert(__gnu_test::is_customization_point_object(std::ranges::crend)); |
| 25 |
|
| 26 |
struct R1 |
| 27 |
{ |
| 28 |
int i = 0; |
| 29 |
int j = 0; |
| 30 |
|
| 31 |
#if __cpp_lib_ranges_as_const |
| 32 |
const int *begin() const; |
| 33 |
const int *end() const; |
| 34 |
#endif |
| 35 |
|
| 36 |
constexpr const int* rbegin() const { return &i; } |
| 37 |
constexpr const int* rend() const { return &i + 1; } |
| 38 |
friend constexpr const int* rbegin(const R1&& r) { return &r.j; } |
| 39 |
friend constexpr const int* rend(const R1&& r) { return &r.j + 1; } |
| 40 |
}; |
| 41 |
|
| 42 |
// N.B. this is a lie, rend on an R1 rvalue will return a dangling pointer. |
| 43 |
template<> constexpr bool std::ranges::enable_borrowed_range<R1> = true; |
| 44 |
|
| 45 |
void |
| 46 |
test01() |
| 47 |
{ |
| 48 |
R1 r; |
| 49 |
const R1& c = r; |
| 50 |
VERIFY( std::ranges::crend(r) == std::ranges::rend(c) ); |
| 51 |
VERIFY( std::ranges::crend(c) == std::ranges::rend(c) ); |
| 52 |
VERIFY( std::ranges::crend(std::move(r)) == std::ranges::rend(c) ); |
| 53 |
VERIFY( std::ranges::crend(std::move(c)) == std::ranges::rend(c) ); |
| 54 |
} |
| 55 |
|
| 56 |
struct R2 |
| 57 |
{ |
| 58 |
int a[2] = { }; |
| 59 |
long l[2] = { }; |
| 60 |
|
| 61 |
const int* begin() const { return a; } |
| 62 |
const int* end() const { return a + 2; } |
| 63 |
|
| 64 |
friend const long* begin(const R2&& r) { return r.l; } |
| 65 |
friend const long* end(const R2&& r) { return r.l + 2; } |
| 66 |
}; |
| 67 |
|
| 68 |
// N.B. this is a lie, rend on an R2 rvalue will return a dangling pointer. |
| 69 |
template<> constexpr bool std::ranges::enable_borrowed_range<R2> = true; |
| 70 |
|
| 71 |
void |
| 72 |
test02() |
| 73 |
{ |
| 74 |
R2 r; |
| 75 |
const R2& c = r; |
| 76 |
VERIFY( std::ranges::crend(r) == std::ranges::rend(c) ); |
| 77 |
VERIFY( std::ranges::crend(c) == std::ranges::rend(c) ); |
| 78 |
VERIFY( std::ranges::crend(std::move(r)) == std::ranges::rend(std::move(c)) ); |
| 79 |
VERIFY( std::ranges::crend(std::move(c)) == std::ranges::rend(std::move(c)) ); |
| 80 |
} |
| 81 |
|
| 82 |
struct R3 |
| 83 |
{ |
| 84 |
int i = 0; |
| 85 |
|
| 86 |
#if __cpp_lib_ranges_as_const |
| 87 |
const int *begin() const; |
| 88 |
const int *end() const; |
| 89 |
#endif |
| 90 |
|
| 91 |
const int* rbegin() const noexcept { return &i + 1; } |
| 92 |
const long* rend() const noexcept { return nullptr; } // not a sentinel for rbegin() |
| 93 |
|
| 94 |
friend const long* rbegin(const R3&) noexcept { return nullptr; } |
| 95 |
friend const int* rend(const R3& r) { return &r.i; } |
| 96 |
}; |
| 97 |
|
| 98 |
struct R4 |
| 99 |
{ |
| 100 |
int i = 0; |
| 101 |
|
| 102 |
#if __cpp_lib_ranges_as_const |
| 103 |
// These members mean that range<R4> and range<const R4> are satisfied. |
| 104 |
const short* begin() const { return 0; } |
| 105 |
const short* end() const { return 0; } |
| 106 |
#endif |
| 107 |
|
| 108 |
const int* rbegin() const noexcept { return &i + 1; } |
| 109 |
const long* rend() const noexcept { return nullptr; } // not a sentinel for rbegin() |
| 110 |
|
| 111 |
friend const long* rbegin(const R4&) noexcept { return nullptr; } |
| 112 |
friend const int* rend(const R4& r) { return &r.i; } |
| 113 |
}; |
| 114 |
|
| 115 |
void |
| 116 |
test03() |
| 117 |
{ |
| 118 |
R3 r; |
| 119 |
const R3& c = r; |
| 120 |
VERIFY( std::ranges::crend(r) == std::ranges::rend(c) ); |
| 121 |
static_assert( !noexcept(std::ranges::crend(r)) ); |
| 122 |
VERIFY( std::ranges::crend(c) == std::ranges::rend(c) ); |
| 123 |
static_assert( !noexcept(std::ranges::crend(c)) ); |
| 124 |
|
| 125 |
R4 r4; |
| 126 |
const R4& c4 = r4; |
| 127 |
auto b = std::ranges::rbegin(r4); |
| 128 |
auto s0 = std::ranges::rend(r4); |
| 129 |
static_assert( std::ranges::__access::__adl_rend<R4&> ); |
| 130 |
auto s = std::ranges::crend(r4); |
| 131 |
auto s2 = std::ranges::rend(c4); |
| 132 |
// VERIFY( std::ranges::crend(r4) == std::ranges::rend(c4) ); |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
test04() |
| 137 |
{ |
| 138 |
int a[2] = { }; |
| 139 |
const auto& c = a; |
| 140 |
VERIFY( std::ranges::crend(a) == std::ranges::rend(c) ); |
| 141 |
VERIFY( std::ranges::crend(c) == std::ranges::rend(c) ); |
| 142 |
} |
| 143 |
|
| 144 |
int |
| 145 |
main() |
| 146 |
{ |
| 147 |
test01(); |
| 148 |
test02(); |
| 149 |
test03(); |
| 150 |
test04(); |
| 151 |
} |