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<InputIterator InIter1, InputIterator InIter2, class OutIter,
12// Callable<auto, const InIter1::value_type&, const InIter2::value_type&> BinaryOp>
13// requires OutputIterator<OutIter, BinaryOp::result_type> && CopyConstructible<BinaryOp>
14// constexpr OutIter // constexpr after C++17
15// transform(InIter1 first1, InIter1 last1, InIter2 first2, OutIter result, BinaryOp binary_op);
16
17#include <algorithm>
18#include <functional>
19#include <cassert>
20
21#include "test_macros.h"
22#include "test_iterators.h"
23
24#if TEST_STD_VER > 17
25TEST_CONSTEXPR bool test_constexpr() {
26 const int ia[] = {1, 3, 6, 7};
27 const int ib[] = {2, 4, 7, 8};
28 int ic[] = {0, 0, 0, 0, 0}; // one bigger
29 const int expected[] = {3, 7, 13, 15};
30
31 auto it = std::transform(std::begin(ia), std::end(ia),
32 std::begin(ib), std::begin(ic), std::plus<int>());
33
34 return it == (std::begin(ic) + std::size(ia))
35 && *it == 0 // don't overwrite the last value in the output array
36 && std::equal(std::begin(expected), std::end(expected), std::begin(ic), it)
37 ;
38 }
39#endif
40
41
42template<class InIter1, class InIter2, class OutIter>
43void
44test()
45{
46 int ia[] = {0, 1, 2, 3, 4};
47 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
48 int ib[sa] = {1, 2, 3, 4, 5};
49 OutIter r = std::transform(InIter1(ib), InIter1(ib+sa), InIter2(ia),
50 OutIter(ib), std::minus<int>());
51 assert(base(r) == ib + sa);
52 assert(ib[0] == 1);
53 assert(ib[1] == 1);
54 assert(ib[2] == 1);
55 assert(ib[3] == 1);
56 assert(ib[4] == 1);
57}
58
59int main(int, char**)
60{
61 test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
62 test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
63 test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*>, forward_iterator<int*> >();
64 test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
65 test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
66 test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*>, int*>();
67
68 test<cpp17_input_iterator<const int*>, forward_iterator<const int*>, cpp17_output_iterator<int*> >();
69 test<cpp17_input_iterator<const int*>, forward_iterator<const int*>, cpp17_input_iterator<int*> >();
70 test<cpp17_input_iterator<const int*>, forward_iterator<const int*>, forward_iterator<int*> >();
71 test<cpp17_input_iterator<const int*>, forward_iterator<const int*>, bidirectional_iterator<int*> >();
72 test<cpp17_input_iterator<const int*>, forward_iterator<const int*>, random_access_iterator<int*> >();
73 test<cpp17_input_iterator<const int*>, forward_iterator<const int*>, int*>();
74
75 test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
76 test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
77 test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*>, forward_iterator<int*> >();
78 test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
79 test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*>, random_access_iterator<int*> >();
80 test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*>, int*>();
81
82 test<cpp17_input_iterator<const int*>, random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
83 test<cpp17_input_iterator<const int*>, random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
84 test<cpp17_input_iterator<const int*>, random_access_iterator<const int*>, forward_iterator<int*> >();
85 test<cpp17_input_iterator<const int*>, random_access_iterator<const int*>, bidirectional_iterator<int*> >();
86 test<cpp17_input_iterator<const int*>, random_access_iterator<const int*>, random_access_iterator<int*> >();
87 test<cpp17_input_iterator<const int*>, random_access_iterator<const int*>, int*>();
88
89 test<cpp17_input_iterator<const int*>, const int*, cpp17_output_iterator<int*> >();
90 test<cpp17_input_iterator<const int*>, const int*, cpp17_input_iterator<int*> >();
91 test<cpp17_input_iterator<const int*>, const int*, forward_iterator<int*> >();
92 test<cpp17_input_iterator<const int*>, const int*, bidirectional_iterator<int*> >();
93 test<cpp17_input_iterator<const int*>, const int*, random_access_iterator<int*> >();
94 test<cpp17_input_iterator<const int*>, const int*, int*>();
95
96 test<forward_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
97 test<forward_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
98 test<forward_iterator<const int*>, cpp17_input_iterator<const int*>, forward_iterator<int*> >();
99 test<forward_iterator<const int*>, cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
100 test<forward_iterator<const int*>, cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
101 test<forward_iterator<const int*>, cpp17_input_iterator<const int*>, int*>();
102
103 test<forward_iterator<const int*>, forward_iterator<const int*>, cpp17_output_iterator<int*> >();
104 test<forward_iterator<const int*>, forward_iterator<const int*>, cpp17_input_iterator<int*> >();
105 test<forward_iterator<const int*>, forward_iterator<const int*>, forward_iterator<int*> >();
106 test<forward_iterator<const int*>, forward_iterator<const int*>, bidirectional_iterator<int*> >();
107 test<forward_iterator<const int*>, forward_iterator<const int*>, random_access_iterator<int*> >();
108 test<forward_iterator<const int*>, forward_iterator<const int*>, int*>();
109
110 test<forward_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
111 test<forward_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
112 test<forward_iterator<const int*>, bidirectional_iterator<const int*>, forward_iterator<int*> >();
113 test<forward_iterator<const int*>, bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
114 test<forward_iterator<const int*>, bidirectional_iterator<const int*>, random_access_iterator<int*> >();
115 test<forward_iterator<const int*>, bidirectional_iterator<const int*>, int*>();
116
117 test<forward_iterator<const int*>, random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
118 test<forward_iterator<const int*>, random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
119 test<forward_iterator<const int*>, random_access_iterator<const int*>, forward_iterator<int*> >();
120 test<forward_iterator<const int*>, random_access_iterator<const int*>, bidirectional_iterator<int*> >();
121 test<forward_iterator<const int*>, random_access_iterator<const int*>, random_access_iterator<int*> >();
122 test<forward_iterator<const int*>, random_access_iterator<const int*>, int*>();
123
124 test<forward_iterator<const int*>, const int*, cpp17_output_iterator<int*> >();
125 test<forward_iterator<const int*>, const int*, cpp17_input_iterator<int*> >();
126 test<forward_iterator<const int*>, const int*, forward_iterator<int*> >();
127 test<forward_iterator<const int*>, const int*, bidirectional_iterator<int*> >();
128 test<forward_iterator<const int*>, const int*, random_access_iterator<int*> >();
129 test<forward_iterator<const int*>, const int*, int*>();
130
131 test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
132 test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
133 test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*>, forward_iterator<int*> >();
134 test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
135 test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
136 test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*>, int*>();
137
138 test<bidirectional_iterator<const int*>, forward_iterator<const int*>, cpp17_output_iterator<int*> >();
139 test<bidirectional_iterator<const int*>, forward_iterator<const int*>, cpp17_input_iterator<int*> >();
140 test<bidirectional_iterator<const int*>, forward_iterator<const int*>, forward_iterator<int*> >();
141 test<bidirectional_iterator<const int*>, forward_iterator<const int*>, bidirectional_iterator<int*> >();
142 test<bidirectional_iterator<const int*>, forward_iterator<const int*>, random_access_iterator<int*> >();
143 test<bidirectional_iterator<const int*>, forward_iterator<const int*>, int*>();
144
145 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
146 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
147 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*>, forward_iterator<int*> >();
148 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
149 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*>, random_access_iterator<int*> >();
150 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*>, int*>();
151
152 test<bidirectional_iterator<const int*>, random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
153 test<bidirectional_iterator<const int*>, random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
154 test<bidirectional_iterator<const int*>, random_access_iterator<const int*>, forward_iterator<int*> >();
155 test<bidirectional_iterator<const int*>, random_access_iterator<const int*>, bidirectional_iterator<int*> >();
156 test<bidirectional_iterator<const int*>, random_access_iterator<const int*>, random_access_iterator<int*> >();
157 test<bidirectional_iterator<const int*>, random_access_iterator<const int*>, int*>();
158
159 test<bidirectional_iterator<const int*>, const int*, cpp17_output_iterator<int*> >();
160 test<bidirectional_iterator<const int*>, const int*, cpp17_input_iterator<int*> >();
161 test<bidirectional_iterator<const int*>, const int*, forward_iterator<int*> >();
162 test<bidirectional_iterator<const int*>, const int*, bidirectional_iterator<int*> >();
163 test<bidirectional_iterator<const int*>, const int*, random_access_iterator<int*> >();
164 test<bidirectional_iterator<const int*>, const int*, int*>();
165
166 test<random_access_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
167 test<random_access_iterator<const int*>, cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
168 test<random_access_iterator<const int*>, cpp17_input_iterator<const int*>, forward_iterator<int*> >();
169 test<random_access_iterator<const int*>, cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
170 test<random_access_iterator<const int*>, cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
171 test<random_access_iterator<const int*>, cpp17_input_iterator<const int*>, int*>();
172
173 test<random_access_iterator<const int*>, forward_iterator<const int*>, cpp17_output_iterator<int*> >();
174 test<random_access_iterator<const int*>, forward_iterator<const int*>, cpp17_input_iterator<int*> >();
175 test<random_access_iterator<const int*>, forward_iterator<const int*>, forward_iterator<int*> >();
176 test<random_access_iterator<const int*>, forward_iterator<const int*>, bidirectional_iterator<int*> >();
177 test<random_access_iterator<const int*>, forward_iterator<const int*>, random_access_iterator<int*> >();
178 test<random_access_iterator<const int*>, forward_iterator<const int*>, int*>();
179
180 test<random_access_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
181 test<random_access_iterator<const int*>, bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
182 test<random_access_iterator<const int*>, bidirectional_iterator<const int*>, forward_iterator<int*> >();
183 test<random_access_iterator<const int*>, bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
184 test<random_access_iterator<const int*>, bidirectional_iterator<const int*>, random_access_iterator<int*> >();
185 test<random_access_iterator<const int*>, bidirectional_iterator<const int*>, int*>();
186
187 test<random_access_iterator<const int*>, random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
188 test<random_access_iterator<const int*>, random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
189 test<random_access_iterator<const int*>, random_access_iterator<const int*>, forward_iterator<int*> >();
190 test<random_access_iterator<const int*>, random_access_iterator<const int*>, bidirectional_iterator<int*> >();
191 test<random_access_iterator<const int*>, random_access_iterator<const int*>, random_access_iterator<int*> >();
192 test<random_access_iterator<const int*>, random_access_iterator<const int*>, int*>();
193
194 test<random_access_iterator<const int*>, const int*, cpp17_output_iterator<int*> >();
195 test<random_access_iterator<const int*>, const int*, cpp17_input_iterator<int*> >();
196 test<random_access_iterator<const int*>, const int*, forward_iterator<int*> >();
197 test<random_access_iterator<const int*>, const int*, bidirectional_iterator<int*> >();
198 test<random_access_iterator<const int*>, const int*, random_access_iterator<int*> >();
199 test<random_access_iterator<const int*>, const int*, int*>();
200
201 test<const int*, cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
202 test<const int*, cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
203 test<const int*, cpp17_input_iterator<const int*>, forward_iterator<int*> >();
204 test<const int*, cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
205 test<const int*, cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
206 test<const int*, cpp17_input_iterator<const int*>, int*>();
207
208 test<const int*, forward_iterator<const int*>, cpp17_output_iterator<int*> >();
209 test<const int*, forward_iterator<const int*>, cpp17_input_iterator<int*> >();
210 test<const int*, forward_iterator<const int*>, forward_iterator<int*> >();
211 test<const int*, forward_iterator<const int*>, bidirectional_iterator<int*> >();
212 test<const int*, forward_iterator<const int*>, random_access_iterator<int*> >();
213 test<const int*, forward_iterator<const int*>, int*>();
214
215 test<const int*, bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
216 test<const int*, bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
217 test<const int*, bidirectional_iterator<const int*>, forward_iterator<int*> >();
218 test<const int*, bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
219 test<const int*, bidirectional_iterator<const int*>, random_access_iterator<int*> >();
220 test<const int*, bidirectional_iterator<const int*>, int*>();
221
222 test<const int*, random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
223 test<const int*, random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
224 test<const int*, random_access_iterator<const int*>, forward_iterator<int*> >();
225 test<const int*, random_access_iterator<const int*>, bidirectional_iterator<int*> >();
226 test<const int*, random_access_iterator<const int*>, random_access_iterator<int*> >();
227 test<const int*, random_access_iterator<const int*>, int*>();
228
229 test<const int*, const int*, cpp17_output_iterator<int*> >();
230 test<const int*, const int*, cpp17_input_iterator<int*> >();
231 test<const int*, const int*, forward_iterator<int*> >();
232 test<const int*, const int*, bidirectional_iterator<int*> >();
233 test<const int*, const int*, random_access_iterator<int*> >();
234 test<const int*, const int*, int*>();
235
236#if TEST_STD_VER > 17
237 static_assert(test_constexpr());
238#endif
239
240 return 0;
241}
242

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/binary_transform.pass.cpp