| 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::size)); |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | constexpr int a[10] = { }; |
| 30 | static_assert( std::ranges::size(a) == 10 ); |
| 31 | static_assert( noexcept(std::ranges::size(a)) ); |
| 32 | |
| 33 | int a2[2]; |
| 34 | VERIFY( std::ranges::size(a2) == 2); |
| 35 | static_assert( noexcept(std::ranges::size(a2)) ); |
| 36 | |
| 37 | struct Incomplete; |
| 38 | using A = Incomplete[2]; // bounded array of incomplete type |
| 39 | extern A& f(); |
| 40 | static_assert( std::same_as<decltype(std::ranges::size(f())), std::size_t> ); |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | test02() |
| 45 | { |
| 46 | struct R |
| 47 | { |
| 48 | int size() { return 1; } |
| 49 | long size() const noexcept { return 2; } |
| 50 | }; |
| 51 | R r; |
| 52 | const R& c = r; |
| 53 | VERIFY( std::ranges::size(r) == 1 ); |
| 54 | static_assert( !noexcept(std::ranges::size(r)) ); |
| 55 | VERIFY( std::ranges::size(c) == 2L ); |
| 56 | static_assert( noexcept(std::ranges::size(c)) ); |
| 57 | |
| 58 | int a[3] = { }; |
| 59 | __gnu_test::test_sized_range<int, __gnu_test::input_iterator_wrapper> ri(a); |
| 60 | VERIFY( std::ranges::size(ri) == 3 ); |
| 61 | static_assert( noexcept(std::ranges::size(ri)) ); |
| 62 | } |
| 63 | |
| 64 | struct R3 |
| 65 | { |
| 66 | int* size() { return nullptr; } |
| 67 | friend int size(R3&) noexcept { return 1; } |
| 68 | friend long size(const R3&) { return 2L; } |
| 69 | friend unsigned int size(R3&&) { return 3U; } |
| 70 | friend unsigned long size(const R3&&) noexcept { return 4UL; } |
| 71 | }; |
| 72 | |
| 73 | void |
| 74 | test03() |
| 75 | { |
| 76 | R3 r; |
| 77 | const R3& c = r; |
| 78 | VERIFY( std::ranges::size(r) == 1 ); |
| 79 | static_assert( noexcept(std::ranges::size(r)) ); |
| 80 | // PR libstdc++/100824 |
| 81 | // ranges::size should treat the subexpression as an lvalue |
| 82 | VERIFY( std::ranges::size(std::move(r)) == 1 ); |
| 83 | static_assert( noexcept(std::ranges::size(std::move(r))) ); |
| 84 | VERIFY( std::ranges::size(c) == 2L ); |
| 85 | static_assert( !noexcept(std::ranges::size(c)) ); |
| 86 | VERIFY( std::ranges::size(std::move(c)) == 2L ); |
| 87 | static_assert( !noexcept(std::ranges::size(std::move(c))) ); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | test04() |
| 92 | { |
| 93 | int a[] = { 0, 1 }; |
| 94 | __gnu_test::test_range<int, __gnu_test::random_access_iterator_wrapper> r(a); |
| 95 | VERIFY( std::ranges::size(r) == unsigned(std::ranges::end(r) - std::ranges::begin(r)) ); |
| 96 | } |
| 97 | |
| 98 | struct R5 |
| 99 | { |
| 100 | int size() const noexcept { return 0; } |
| 101 | R5* begin() { return this; } |
| 102 | R5* end() { return this + 1; } |
| 103 | }; |
| 104 | |
| 105 | template<> |
| 106 | constexpr bool std::ranges::disable_sized_range<R5> = true; |
| 107 | |
| 108 | void |
| 109 | test05() |
| 110 | { |
| 111 | R5 r; |
| 112 | VERIFY( std::ranges::size(r) == 1 ); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | test06() |
| 117 | { |
| 118 | // PR libstdc++/100824 |
| 119 | // ranges::size should treat the subexpression as an lvalue |
| 120 | struct R { constexpr int size() & { return 42; } }; |
| 121 | static_assert( std::ranges::size(R{}) == 42 ); |
| 122 | } |
| 123 | |
| 124 | template<typename T> |
| 125 | concept has_size = requires (T& t) { std::ranges::size(t); }; |
| 126 | |
| 127 | // If T is an array of unknown bound, ranges::size(E) is ill-formed. |
| 128 | static_assert( ! has_size<int[]> ); |
| 129 | static_assert( ! has_size<int(&)[]> ); |
| 130 | static_assert( ! has_size<int[][2]> ); |
| 131 | struct Incomplete; |
| 132 | static_assert( ! has_size<Incomplete[]> ); |
| 133 | |
| 134 | int |
| 135 | main() |
| 136 | { |
| 137 | test01(); |
| 138 | test02(); |
| 139 | test03(); |
| 140 | test04(); |
| 141 | test05(); |
| 142 | test06(); |
| 143 | } |