xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
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 // template<class Iter, IntegralLike Size, class T>
12 //   requires OutputIterator<Iter, const T&>
13 //   constexpr OutputIterator      // constexpr after C++17
14 //   fill_n(Iter first, Size n, const T& value);
15 
16 #include <algorithm>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 #include "user_defined_integral.hpp"
22 
23 #if TEST_STD_VER > 17
24 TEST_CONSTEXPR bool test_constexpr() {
25     const size_t N = 5;
26     int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N
27 
28     auto it = std::fill_n(std::begin(ib), N, 5);
29     return it == (std::begin(ib) + N)
30         && std::all_of(std::begin(ib), it, [](int a) {return a == 5; })
31         && *it == 0 // don't overwrite the last value in the output array
32         ;
33     }
34 #endif
35 
36 typedef UserDefinedIntegral<unsigned> UDI;
37 
38 template <class Iter>
39 void
40 test_char()
41 {
42     const unsigned n = 4;
43     char ca[n] = {0};
44     assert(std::fill_n(Iter(ca), UDI(n), char(1)) == std::next(Iter(ca), n));
45     assert(ca[0] == 1);
46     assert(ca[1] == 1);
47     assert(ca[2] == 1);
48     assert(ca[3] == 1);
49 }
50 
51 template <class Iter>
52 void
53 test_int()
54 {
55     const unsigned n = 4;
56     int ia[n] = {0};
57     assert(std::fill_n(Iter(ia), UDI(n), 1) == std::next(Iter(ia), n));
58     assert(ia[0] == 1);
59     assert(ia[1] == 1);
60     assert(ia[2] == 1);
61     assert(ia[3] == 1);
62 }
63 
64 void
65 test_int_array()
66 {
67     const unsigned n = 4;
68     int ia[n] = {0};
69     assert(std::fill_n(ia, UDI(n), static_cast<char>(1)) == std::next(ia, n));
70     assert(ia[0] == 1);
71     assert(ia[1] == 1);
72     assert(ia[2] == 1);
73     assert(ia[3] == 1);
74 }
75 
76 struct source {
77     source() : i(0) { }
78 
79     operator int() const { return i++; }
80     mutable int i;
81 };
82 
83 void
84 test_int_array_struct_source()
85 {
86     const unsigned n = 4;
87     int ia[n] = {0};
88     assert(std::fill_n(ia, UDI(n), source()) == std::next(ia, n));
89     assert(ia[0] == 0);
90     assert(ia[1] == 1);
91     assert(ia[2] == 2);
92     assert(ia[3] == 3);
93 }
94 
95 struct test1 {
96     test1() : c(0) { }
97     test1(char xc) : c(xc + 1) { }
98     char c;
99 };
100 
101 void
102 test_struct_array()
103 {
104     const unsigned n = 4;
105     test1 test1a[n] = {0};
106     assert(std::fill_n(test1a, UDI(n), static_cast<char>(10)) == std::next(test1a, n));
107     assert(test1a[0].c == 11);
108     assert(test1a[1].c == 11);
109     assert(test1a[2].c == 11);
110     assert(test1a[3].c == 11);
111 }
112 
113 class A
114 {
115     char a_;
116 public:
117     A() {}
118     explicit A(char a) : a_(a) {}
119     operator unsigned char() const {return 'b';}
120 
121     friend bool operator==(const A& x, const A& y)
122         {return x.a_ == y.a_;}
123 };
124 
125 void
126 test5()
127 {
128     A a[3];
129     assert(std::fill_n(&a[0], UDI(3), A('a')) == a+3);
130     assert(a[0] == A('a'));
131     assert(a[1] == A('a'));
132     assert(a[2] == A('a'));
133 }
134 
135 struct Storage
136 {
137   union
138   {
139     unsigned char a;
140     unsigned char b;
141   };
142 };
143 
144 void test6()
145 {
146   Storage foo[5];
147   std::fill_n(&foo[0], UDI(5), Storage());
148 }
149 
150 
151 int main(int, char**)
152 {
153     test_char<forward_iterator<char*> >();
154     test_char<bidirectional_iterator<char*> >();
155     test_char<random_access_iterator<char*> >();
156     test_char<char*>();
157 
158     test_int<forward_iterator<int*> >();
159     test_int<bidirectional_iterator<int*> >();
160     test_int<random_access_iterator<int*> >();
161     test_int<int*>();
162 
163     test_int_array();
164     test_int_array_struct_source();
165     test_struct_array();
166 
167     test5();
168     test6();
169 
170 #if TEST_STD_VER > 17
171     static_assert(test_constexpr());
172 #endif
173 
174   return 0;
175 }
176