| 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 | |
| 24 | void |
| 25 | test01() |
| 26 | { |
| 27 | std::ranges::single_view s{4}; |
| 28 | static_assert(std::same_as<std::ranges::range_value_t<decltype(s)>, int>); |
| 29 | static_assert(std::ranges::size(s) == 1); |
| 30 | |
| 31 | int count = 0; |
| 32 | for (auto i : s) |
| 33 | ++count; |
| 34 | VERIFY(count == 1); |
| 35 | VERIFY(*std::ranges::begin(s) == 4); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | test02() |
| 40 | { |
| 41 | std::ranges::single_view<long> s2; |
| 42 | static_assert(std::same_as<std::ranges::range_value_t<decltype(s2)>, long>); |
| 43 | static_assert(std::ranges::size(s2) == 1); |
| 44 | |
| 45 | int count = 0; |
| 46 | for (auto l : s2) |
| 47 | ++count; |
| 48 | VERIFY(count == 1); |
| 49 | VERIFY(*std::ranges::begin(s2) == 0L); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | test03() |
| 54 | { |
| 55 | auto s3 = std::ranges::views::single('a'); |
| 56 | static_assert(std::same_as<std::ranges::range_value_t<decltype(s3)>, char>); |
| 57 | static_assert(std::ranges::size(s3) == 1); |
| 58 | VERIFY(*std::ranges::begin(s3) == 'a'); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | test04() |
| 63 | { |
| 64 | // PR libstdc++/100475 |
| 65 | struct A { |
| 66 | A() = default; |
| 67 | A(int, int) { } |
| 68 | A(std::initializer_list<int>) = delete; |
| 69 | void operator&() const = delete; |
| 70 | }; |
| 71 | std::ranges::single_view<A> s(std::in_place, 0, 0); |
| 72 | s.data(); |
| 73 | std::as_const(s).data(); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | test05() |
| 78 | { |
| 79 | int i = 0; |
| 80 | static_assert(noexcept(std::ranges::single_view<int>())); |
| 81 | static_assert(noexcept(std::ranges::single_view<int>(i))); |
| 82 | static_assert(noexcept(std::ranges::single_view<int>(1))); |
| 83 | static_assert(noexcept(std::ranges::single_view<int>(std::in_place, 2))); |
| 84 | static_assert(noexcept(std::ranges::views::single(i))); |
| 85 | auto s = std::ranges::views::single(i); |
| 86 | static_assert(noexcept(s.begin())); |
| 87 | static_assert(noexcept(s.end())); |
| 88 | static_assert(noexcept(s.size())); |
| 89 | static_assert(noexcept(s.data())); |
| 90 | static_assert(noexcept(s.empty())); // view_interface::empty() |
| 91 | const auto cs = s; |
| 92 | static_assert(noexcept(cs.begin())); |
| 93 | static_assert(noexcept(cs.end())); |
| 94 | static_assert(noexcept(cs.size())); |
| 95 | static_assert(noexcept(cs.data())); |
| 96 | static_assert(noexcept(cs.empty())); // view_interface::empty() |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | test06() |
| 101 | { |
| 102 | // PR libstdc++/100475 comment #7 |
| 103 | struct S { |
| 104 | S() = default; |
| 105 | S(std::initializer_list<S>) = delete; |
| 106 | S(const S&) {} |
| 107 | }; |
| 108 | S obj; |
| 109 | auto x = std::views::single(obj); |
| 110 | auto y = std::views::single(std::move(obj)); |
| 111 | } |
| 112 | |
| 113 | template<auto single = std::views::single> |
| 114 | void |
| 115 | test07() |
| 116 | { |
| 117 | // Verify SFINAE behavior. |
| 118 | struct uncopyable { |
| 119 | uncopyable(); |
| 120 | uncopyable(const uncopyable&) = delete; |
| 121 | }; |
| 122 | static_assert(!requires { single(uncopyable{}); }); |
| 123 | } |
| 124 | |
| 125 | template<auto single = std::views::single> |
| 126 | void |
| 127 | test08() |
| 128 | { |
| 129 | struct move_only { |
| 130 | move_only() { } |
| 131 | move_only(move_only&&) { } |
| 132 | }; |
| 133 | #if __cpp_lib_ranges >= 202207L |
| 134 | // P2494R2 Relaxing range adaptors to allow for move only types |
| 135 | static_assert( requires { single(move_only{}); } ); |
| 136 | #else |
| 137 | static_assert( ! requires { single(move_only{}); } ); |
| 138 | #endif |
| 139 | } |
| 140 | |
| 141 | int main() |
| 142 | { |
| 143 | test01(); |
| 144 | test02(); |
| 145 | test03(); |
| 146 | test04(); |
| 147 | test05(); |
| 148 | test06(); |
| 149 | test07(); |
| 150 | test08(); |
| 151 | } |