[NFC][analyzer][docs] Crosslink MallocChecker's ownership attributes (#121939)
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.fill / ranges.fill.pass.cpp
blob5dc375e0e8dc0d9a911ae20f9da77013a32bc46f
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
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);
18 #include <algorithm>
19 #include <array>
20 #include <cassert>
21 #include <ranges>
22 #include <string>
24 #include "almost_satisfies_types.h"
25 #include "test_iterators.h"
27 template <class Iter, class Sent = sentinel_wrapper<Iter>>
28 concept HasFillIt = requires(Iter iter, Sent sent) { std::ranges::fill(iter, sent, int{}); };
30 static_assert(HasFillIt<int*>);
31 static_assert(!HasFillIt<OutputIteratorNotIndirectlyWritable>);
32 static_assert(!HasFillIt<OutputIteratorNotInputOrOutputIterator>);
33 static_assert(!HasFillIt<int*, SentinelForNotSemiregular>);
34 static_assert(!HasFillIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
36 template <class Range>
37 concept HasFillR = requires(Range range) { std::ranges::fill(range, int{}); };
39 static_assert(HasFillR<UncheckedRange<int*>>);
40 static_assert(!HasFillR<OutputRangeNotIndirectlyWritable>);
41 static_assert(!HasFillR<OutputRangeNotInputOrOutputIterator>);
42 static_assert(!HasFillR<OutputRangeNotSentinelSemiregular>);
43 static_assert(!HasFillR<OutputRangeNotSentinelEqualityComparableWith>);
45 template <class It, class Sent = It>
46 constexpr void test_iterators() {
47 { // simple test
49 int a[3];
50 std::same_as<It> auto ret = std::ranges::fill(It(a), Sent(It(a + 3)), 1);
51 assert(std::all_of(a, a + 3, [](int i) { return i == 1; }));
52 assert(base(ret) == a + 3);
55 int a[3];
56 auto range = std::ranges::subrange(It(a), Sent(It(a + 3)));
57 std::same_as<It> auto ret = std::ranges::fill(range, 1);
58 assert(std::all_of(a, a + 3, [](int i) { return i == 1; }));
59 assert(base(ret) == a + 3);
63 { // check that an empty range works
65 std::array<int, 0> a;
66 auto ret = std::ranges::fill(It(a.data()), Sent(It(a.data())), 1);
67 assert(base(ret) == a.data());
70 std::array<int, 0> a;
71 auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data())));
72 auto ret = std::ranges::fill(range, 1);
73 assert(base(ret) == a.data());
78 constexpr bool test() {
79 test_iterators<cpp17_output_iterator<int*>, sentinel_wrapper<cpp17_output_iterator<int*>>>();
80 test_iterators<cpp20_output_iterator<int*>, sentinel_wrapper<cpp20_output_iterator<int*>>>();
81 test_iterators<forward_iterator<int*>>();
82 test_iterators<bidirectional_iterator<int*>>();
83 test_iterators<random_access_iterator<int*>>();
84 test_iterators<contiguous_iterator<int*>>();
85 test_iterators<int*>();
87 { // check that every element is copied once
88 struct S {
89 bool copied = false;
90 constexpr S& operator=(const S&) {
91 copied = true;
92 return *this;
96 S a[5];
97 std::ranges::fill(a, a + 5, S {true});
98 assert(std::all_of(a, a + 5, [](S& s) { return s.copied; }));
101 S a[5];
102 std::ranges::fill(a, S {true});
103 assert(std::all_of(a, a + 5, [](S& s) { return s.copied; }));
107 { // check that std::ranges::dangling is returned
108 [[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =
109 std::ranges::fill(std::array<int, 10> {}, 1);
112 { // check that std::ranges::dangling isn't returned with a borrowing range
113 std::array<int, 10> a{};
114 [[maybe_unused]] std::same_as<std::array<int, 10>::iterator> decltype(auto) ret =
115 std::ranges::fill(std::views::all(a), 1);
116 assert(std::all_of(a.begin(), a.end(), [](int i) { return i == 1; }));
119 { // check that non-trivially copyable items are copied properly
121 std::array<std::string, 10> a;
122 auto ret = std::ranges::fill(a.begin(), a.end(), "long long string so no SSO");
123 assert(ret == a.end());
124 assert(std::all_of(a.begin(), a.end(), [](auto& s) { return s == "long long string so no SSO"; }));
127 std::array<std::string, 10> a;
128 auto ret = std::ranges::fill(a, "long long string so no SSO");
129 assert(ret == a.end());
130 assert(std::all_of(a.begin(), a.end(), [](auto& s) { return s == "long long string so no SSO"; }));
134 return true;
137 int main(int, char**) {
138 test();
139 static_assert(test());
141 return 0;