1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // template<class T, output_iterator<const T&> O>
14 // constexpr O ranges::fill_n(O first, iter_difference_t<O> n, const T& value);
22 #include "almost_satisfies_types.h"
23 #include "test_iterators.h"
26 concept HasFillN
= requires(Iter iter
) { std::ranges::fill_n(iter
, int{}, int{}); };
30 static_assert(HasFillN
<int*>);
31 static_assert(!HasFillN
<WrongType
*>);
32 static_assert(!HasFillN
<OutputIteratorNotIndirectlyWritable
>);
33 static_assert(!HasFillN
<OutputIteratorNotInputOrOutputIterator
>);
35 template <class It
, class Sent
= It
>
36 constexpr void test_iterators() {
39 std::same_as
<It
> decltype(auto) ret
= std::ranges::fill_n(It(a
), 3, 1);
40 assert(std::all_of(a
, a
+ 3, [](int i
) { return i
== 1; }));
41 assert(base(ret
) == a
+ 3);
44 { // check that an empty range works
46 auto ret
= std::ranges::fill_n(It(a
.data()), 0, 1);
47 assert(base(ret
) == a
.data());
51 constexpr bool test() {
52 test_iterators
<cpp17_output_iterator
<int*>, sentinel_wrapper
<cpp17_output_iterator
<int*>>>();
53 test_iterators
<cpp20_output_iterator
<int*>, sentinel_wrapper
<cpp20_output_iterator
<int*>>>();
54 test_iterators
<forward_iterator
<int*>>();
55 test_iterators
<bidirectional_iterator
<int*>>();
56 test_iterators
<random_access_iterator
<int*>>();
57 test_iterators
<contiguous_iterator
<int*>>();
58 test_iterators
<int*>();
60 { // check that every element is copied once
63 constexpr S
& operator=(const S
&) {
71 std::ranges::fill_n(a
, 5, S
{});
72 assert(std::all_of(a
, a
+ 5, [](S
& s
) { return s
.copied
; }));
75 { // check that non-trivially copyable items are copied properly
76 std::array
<std::string
, 10> a
;
77 auto ret
= std::ranges::fill_n(a
.data(), 10, "long long string so no SSO");
78 assert(ret
== a
.data() + a
.size());
79 assert(std::all_of(a
.begin(), a
.end(), [](auto& s
) { return s
== "long long string so no SSO"; }));
85 int main(int, char**) {
87 static_assert(test());