| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <algorithm> |
| 10 | |
| 11 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 12 | |
| 13 | // template<class T, output_iterator<const T&> O, sentinel_for<O> S> |
| 14 | // constexpr O ranges::fill(O first, S last, const T& value); |
| 15 | // template<class T, output_range<const T&> R> |
| 16 | // constexpr borrowed_iterator_t<R> ranges::fill(R&& r, const T& value); |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <array> |
| 20 | #include <cassert> |
| 21 | #include <ranges> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "sized_allocator.h" |
| 26 | #include "almost_satisfies_types.h" |
| 27 | #include "test_iterators.h" |
| 28 | #include "test_macros.h" |
| 29 | |
| 30 | template <class Iter, class Sent = sentinel_wrapper<Iter>> |
| 31 | concept HasFillIt = requires(Iter iter, Sent sent) { std::ranges::fill(iter, sent, int{}); }; |
| 32 | |
| 33 | static_assert(HasFillIt<int*>); |
| 34 | static_assert(!HasFillIt<OutputIteratorNotIndirectlyWritable>); |
| 35 | static_assert(!HasFillIt<OutputIteratorNotInputOrOutputIterator>); |
| 36 | static_assert(!HasFillIt<int*, SentinelForNotSemiregular>); |
| 37 | static_assert(!HasFillIt<int*, SentinelForNotWeaklyEqualityComparableWith>); |
| 38 | |
| 39 | template <class Range> |
| 40 | concept HasFillR = requires(Range range) { std::ranges::fill(range, int{}); }; |
| 41 | |
| 42 | static_assert(HasFillR<UncheckedRange<int*>>); |
| 43 | static_assert(!HasFillR<OutputRangeNotIndirectlyWritable>); |
| 44 | static_assert(!HasFillR<OutputRangeNotInputOrOutputIterator>); |
| 45 | static_assert(!HasFillR<OutputRangeNotSentinelSemiregular>); |
| 46 | static_assert(!HasFillR<OutputRangeNotSentinelEqualityComparableWith>); |
| 47 | |
| 48 | template <class It, class Sent = It> |
| 49 | constexpr void test_iterators() { |
| 50 | { // simple test |
| 51 | { |
| 52 | int a[3]; |
| 53 | std::same_as<It> auto ret = std::ranges::fill(It(a), Sent(It(a + 3)), 1); |
| 54 | assert(std::all_of(a, a + 3, [](int i) { return i == 1; })); |
| 55 | assert(base(ret) == a + 3); |
| 56 | } |
| 57 | { |
| 58 | int a[3]; |
| 59 | auto range = std::ranges::subrange(It(a), Sent(It(a + 3))); |
| 60 | std::same_as<It> auto ret = std::ranges::fill(range, 1); |
| 61 | assert(std::all_of(a, a + 3, [](int i) { return i == 1; })); |
| 62 | assert(base(ret) == a + 3); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | { // check that an empty range works |
| 67 | { |
| 68 | std::array<int, 0> a; |
| 69 | auto ret = std::ranges::fill(It(a.data()), Sent(It(a.data())), 1); |
| 70 | assert(base(ret) == a.data()); |
| 71 | } |
| 72 | { |
| 73 | std::array<int, 0> a; |
| 74 | auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); |
| 75 | auto ret = std::ranges::fill(range, 1); |
| 76 | assert(base(ret) == a.data()); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // The `ranges::{fill, fill_n}` algorithms require `vector<bool, Alloc>::iterator` to satisfy |
| 82 | // the `std::indirectly_writable` concept when used with `vector<bool, Alloc>`, which is only |
| 83 | // satisfied since C++23. |
| 84 | #if TEST_STD_VER >= 23 |
| 85 | constexpr bool test_vector_bool(std::size_t N) { |
| 86 | { // Test cases validating leading/trailing bits unfilled remain unchanged |
| 87 | { // Leading bits are not filled |
| 88 | std::vector<bool> in(N, false); |
| 89 | std::vector<bool> expected(N, true); |
| 90 | expected[0] = expected[1] = false; |
| 91 | std::ranges::fill(std::ranges::subrange(in.begin() + 2, in.end()), true); |
| 92 | assert(in == expected); |
| 93 | } |
| 94 | { // Trailing bits are not filled |
| 95 | std::vector<bool> in(N, false); |
| 96 | std::vector<bool> expected(N, true); |
| 97 | expected[N - 1] = expected[N - 2] = false; |
| 98 | std::ranges::fill(std::ranges::subrange(in.begin(), in.end() - 2), true); |
| 99 | assert(in == expected); |
| 100 | } |
| 101 | { // Leading and trailing bits are not filled |
| 102 | std::vector<bool> in(N, false); |
| 103 | std::vector<bool> expected(N, true); |
| 104 | expected[0] = expected[1] = expected[N - 1] = expected[N - 2] = false; |
| 105 | std::ranges::fill(std::ranges::subrange(in.begin() + 2, in.end() - 2), true); |
| 106 | assert(in == expected); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | { // Test cases with full or partial bytes filled |
| 111 | { // Full bytes filled |
| 112 | std::vector<bool> in(N, false); |
| 113 | std::vector<bool> expected(N, true); |
| 114 | std::ranges::fill(in, true); |
| 115 | assert(in == expected); |
| 116 | } |
| 117 | { // Partial bytes with offset filled |
| 118 | std::vector<bool> in(N, false); |
| 119 | std::vector<bool> expected(N, true); |
| 120 | std::ranges::fill(std::ranges::subrange(std::ranges::begin(in) + 4, std::ranges::end(in) - 4), true); |
| 121 | std::ranges::fill(std::ranges::subrange(std::ranges::begin(expected), std::ranges::begin(expected) + 4), false); |
| 122 | std::ranges::fill(std::ranges::subrange(std::ranges::end(expected) - 4, std::ranges::end(expected)), false); |
| 123 | assert(in == expected); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | constexpr bool test() { |
| 132 | test_iterators<cpp17_output_iterator<int*>, sentinel_wrapper<cpp17_output_iterator<int*>>>(); |
| 133 | test_iterators<cpp20_output_iterator<int*>, sentinel_wrapper<cpp20_output_iterator<int*>>>(); |
| 134 | test_iterators<forward_iterator<int*>>(); |
| 135 | test_iterators<bidirectional_iterator<int*>>(); |
| 136 | test_iterators<random_access_iterator<int*>>(); |
| 137 | test_iterators<contiguous_iterator<int*>>(); |
| 138 | test_iterators<int*>(); |
| 139 | |
| 140 | { // check that every element is copied once |
| 141 | struct S { |
| 142 | bool copied = false; |
| 143 | constexpr S& operator=(const S&) { |
| 144 | copied = true; |
| 145 | return *this; |
| 146 | } |
| 147 | }; |
| 148 | { |
| 149 | S a[5]; |
| 150 | std::ranges::fill(a, a + 5, S{true}); |
| 151 | assert(std::all_of(a, a + 5, [](S& s) { return s.copied; })); |
| 152 | } |
| 153 | { |
| 154 | S a[5]; |
| 155 | std::ranges::fill(a, S{true}); |
| 156 | assert(std::all_of(a, a + 5, [](S& s) { return s.copied; })); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | { // check that std::ranges::dangling is returned |
| 161 | [[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret = |
| 162 | std::ranges::fill(std::array<int, 10>{}, 1); |
| 163 | } |
| 164 | |
| 165 | { // check that std::ranges::dangling isn't returned with a borrowing range |
| 166 | std::array<int, 10> a{}; |
| 167 | [[maybe_unused]] std::same_as<std::array<int, 10>::iterator> decltype(auto) ret = |
| 168 | std::ranges::fill(std::views::all(a), 1); |
| 169 | assert(std::all_of(a.begin(), a.end(), [](int i) { return i == 1; })); |
| 170 | } |
| 171 | |
| 172 | { // check that non-trivially copyable items are copied properly |
| 173 | { |
| 174 | std::array<std::string, 10> a; |
| 175 | auto ret = std::ranges::fill(a.begin(), a.end(), "long long string so no SSO" ); |
| 176 | assert(ret == a.end()); |
| 177 | assert(std::all_of(a.begin(), a.end(), [](auto& s) { return s == "long long string so no SSO" ; })); |
| 178 | } |
| 179 | { |
| 180 | std::array<std::string, 10> a; |
| 181 | auto ret = std::ranges::fill(a, "long long string so no SSO" ); |
| 182 | assert(ret == a.end()); |
| 183 | assert(std::all_of(a.begin(), a.end(), [](auto& s) { return s == "long long string so no SSO" ; })); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #if TEST_STD_VER >= 23 |
| 188 | { // Test vector<bool>::iterator optimization |
| 189 | assert(test_vector_bool(8)); |
| 190 | assert(test_vector_bool(19)); |
| 191 | assert(test_vector_bool(32)); |
| 192 | assert(test_vector_bool(49)); |
| 193 | assert(test_vector_bool(64)); |
| 194 | assert(test_vector_bool(199)); |
| 195 | assert(test_vector_bool(256)); |
| 196 | |
| 197 | // Make sure std::ranges::fill behaves properly with std::vector<bool> iterators with custom |
| 198 | // size types. See https://github.com/llvm/llvm-project/pull/122410. |
| 199 | { |
| 200 | using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 201 | std::vector<bool, Alloc> in(100, false, Alloc(1)); |
| 202 | std::vector<bool, Alloc> expected(100, true, Alloc(1)); |
| 203 | std::ranges::fill(in, true); |
| 204 | assert(in == expected); |
| 205 | } |
| 206 | { |
| 207 | using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 208 | std::vector<bool, Alloc> in(200, false, Alloc(1)); |
| 209 | std::vector<bool, Alloc> expected(200, true, Alloc(1)); |
| 210 | std::ranges::fill(in, true); |
| 211 | assert(in == expected); |
| 212 | } |
| 213 | { |
| 214 | using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>; |
| 215 | std::vector<bool, Alloc> in(200, false, Alloc(1)); |
| 216 | std::vector<bool, Alloc> expected(200, true, Alloc(1)); |
| 217 | std::ranges::fill(in, true); |
| 218 | assert(in == expected); |
| 219 | } |
| 220 | { |
| 221 | using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>; |
| 222 | std::vector<bool, Alloc> in(200, false, Alloc(1)); |
| 223 | std::vector<bool, Alloc> expected(200, true, Alloc(1)); |
| 224 | std::ranges::fill(in, true); |
| 225 | assert(in == expected); |
| 226 | } |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | int main(int, char**) { |
| 234 | test(); |
| 235 | static_assert(test()); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |