| 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::rend)); |
| 25 | |
| 26 | struct R1 |
| 27 | { |
| 28 | int i = 0; |
| 29 | int j = 0; |
| 30 | |
| 31 | constexpr const int* rbegin() const { return &i; } |
| 32 | constexpr const int* rend() const { return &i + 1; } |
| 33 | friend constexpr const int* rbegin(const R1&&); // not defined |
| 34 | friend constexpr const int* rend(const R1&&); // not defined |
| 35 | }; |
| 36 | |
| 37 | // N.B. this is a lie, rend on an R1 rvalue will return a dangling pointer. |
| 38 | template<> constexpr bool std::ranges::enable_borrowed_range<R1> = true; |
| 39 | |
| 40 | void |
| 41 | test01() |
| 42 | { |
| 43 | constexpr R1 r; |
| 44 | |
| 45 | // decay-copy(t.rend()) if it is a valid expression |
| 46 | // and its type S models sentinel_for<decltype(ranges::rbegin(E))> |
| 47 | |
| 48 | static_assert( std::ranges::rend(r) == &r.i + 1 ); |
| 49 | static_assert( std::ranges::rend(std::move(r)) == &r.i + 1 ); |
| 50 | } |
| 51 | |
| 52 | struct R2 |
| 53 | { |
| 54 | int i = 0; |
| 55 | |
| 56 | int* rbegin() noexcept { return &i + 1; } |
| 57 | long* rend() noexcept { return nullptr; } // not a sentinel for rbegin() |
| 58 | |
| 59 | friend long* rbegin(R2&) noexcept { return nullptr; } |
| 60 | friend int* rend(R2& r) { return &r.i; } |
| 61 | }; |
| 62 | |
| 63 | void |
| 64 | test02() |
| 65 | { |
| 66 | R2 r; |
| 67 | |
| 68 | // Otherwise, decay-copy(rend(t)) if it is a valid expression |
| 69 | // and its type S models sentinel_for<decltype(ranges::rbegin(E))> |
| 70 | |
| 71 | auto i1 = std::ranges::rbegin(r); |
| 72 | auto i2 = rend(r); |
| 73 | static_assert( std::sentinel_for<decltype(i2), decltype(i1)> ); |
| 74 | VERIFY( std::ranges::rend(r) == &r.i ); |
| 75 | static_assert( !noexcept(std::ranges::rend(r)) ); |
| 76 | } |
| 77 | |
| 78 | struct R3 |
| 79 | { |
| 80 | int a[2] = { }; |
| 81 | long l[2] = { }; |
| 82 | |
| 83 | constexpr const int* begin() const { return a; } |
| 84 | constexpr const int* end() const { return a + 2; } |
| 85 | |
| 86 | friend constexpr const long* begin(const R3&& r) { return r.l; } |
| 87 | friend constexpr const long* end(const R3&& r) { return r.l + 2; } |
| 88 | }; |
| 89 | |
| 90 | // N.B. this is a lie, begin/end on an R3 rvalue will return a dangling pointer. |
| 91 | template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true; |
| 92 | |
| 93 | void |
| 94 | test03() |
| 95 | { |
| 96 | constexpr R3 r; |
| 97 | |
| 98 | // Otherwise, make_reverse_iterator(ranges::begin(t)) if both |
| 99 | // ranges::begin(t) and ranges::end(t) are valid expressions |
| 100 | // of the same type I which models bidirectional_iterator. |
| 101 | |
| 102 | static_assert( std::ranges::rend(r) |
| 103 | == std::make_reverse_iterator(std::ranges::begin(r)) ); |
| 104 | static_assert( std::ranges::rend(std::move(r)) |
| 105 | == std::make_reverse_iterator(std::ranges::begin(std::move(r))) ); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | test04() |
| 110 | { |
| 111 | struct R4 |
| 112 | : __gnu_test::test_range<int, __gnu_test::bidirectional_iterator_wrapper> |
| 113 | { |
| 114 | R4(int (&a)[2]) : test_range(a) { } |
| 115 | |
| 116 | using test_range::begin; |
| 117 | |
| 118 | // Replace test_range::end() to return same type as begin() |
| 119 | // so ranges::rend will wrap it in a reverse_iterator. |
| 120 | auto end() & |
| 121 | { |
| 122 | using __gnu_test::bidirectional_iterator_wrapper; |
| 123 | return bidirectional_iterator_wrapper<int>(bounds.last, &bounds); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | int a[2] = { }; |
| 128 | R4 r(a); |
| 129 | VERIFY( std::ranges::rend(r) == std::make_reverse_iterator(std::ranges::begin(r)) ); |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | main() |
| 134 | { |
| 135 | test01(); |
| 136 | test02(); |
| 137 | test03(); |
| 138 | test04(); |
| 139 | } |