1 /*=============================================================================
2 Copyright (c) 2005-2007 Dan Marsden
3 Copyright (c) 2005-2007 Joel de Guzman
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8
9 #include <boost/phoenix/core.hpp>
10 #include <boost/phoenix/stl/algorithm/transformation.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12
13 #include <functional>
14 #include <list>
15
16 namespace
17 {
18 struct even
19 {
operator ()__anon2a5be80c0111::even20 bool operator()(const int i) const
21 {
22 return i % 2 == 0;
23 }
24 };
25
26 struct mod_2_comparison
27 {
operator ()__anon2a5be80c0111::mod_2_comparison28 bool operator()(
29 const int lhs,
30 const int rhs)
31 {
32 return lhs % 2 == rhs % 2;
33 }
34 };
35
swap_test()36 void swap_test()
37 {
38 using boost::phoenix::swap;
39 using boost::phoenix::ref;
40 using boost::phoenix::arg_names::_1;
41 using boost::phoenix::arg_names::_2;
42 int a = 123;
43 int b = 456;
44 swap(ref(a), ref(b))();
45 BOOST_TEST(a == 456 && b == 123);
46 swap(ref(a), _1)(b);
47 BOOST_TEST(a == 123 && b == 456);
48 swap(_1, _2)(a, b);
49 BOOST_TEST(a == 456 && b == 123);
50 return;
51 }
52
copy_test()53 void copy_test()
54 {
55 using boost::phoenix::copy;
56 using boost::phoenix::arg_names::arg1;
57 using boost::phoenix::arg_names::arg2;
58 int array[] = {1,2,3};
59 int output[4];
60 BOOST_TEST(
61 copy(arg1, arg2)(array, output) == output + 3);
62 BOOST_TEST(output[0] == 1);
63 BOOST_TEST(output[1] == 2);
64 BOOST_TEST(output[2] == 3);
65 return;
66 }
67
copy_backward_test()68 void copy_backward_test()
69 {
70 using boost::phoenix::copy_backward;
71 using boost::phoenix::arg_names::arg1;
72 using boost::phoenix::arg_names::arg2;
73 int array[] = {1,2,3};
74 int output[4];
75 int* output_end = output + 3;
76 BOOST_TEST(
77 copy_backward(arg1, arg2)(array, output_end) == output);
78 BOOST_TEST(output[0] == 1);
79 BOOST_TEST(output[1] == 2);
80 BOOST_TEST(output[2] == 3);
81 return;
82 }
83
84 struct increment
85 {
operator ()__anon2a5be80c0111::increment86 int operator()(
87 int i) const
88 {
89 return i+1;
90 }
91 };
92
transform_test()93 void transform_test()
94 {
95 using boost::phoenix::transform;
96 using boost::phoenix::arg_names::arg1;
97 using boost::phoenix::arg_names::arg2;
98 using boost::phoenix::arg_names::arg3;
99 int array[] = {1,2,3};
100 BOOST_TEST(
101 transform(arg1, arg2, increment())(array, array) ==
102 array + 3);
103 BOOST_TEST(array[0] == 2);
104 BOOST_TEST(array[1] == 3);
105 BOOST_TEST(array[2] == 4);
106
107 int array2[] = {1,2,3};
108 BOOST_TEST(
109 boost::phoenix::transform(arg1, arg2, arg3, std::plus<int>())(array, array2, array) ==
110 array +3);
111 BOOST_TEST(array[0] == 2 + 1);
112 BOOST_TEST(array[1] == 3 + 2);
113 BOOST_TEST(array[2] == 4 + 3);
114 return;
115 }
116
replace_test()117 void replace_test()
118 {
119 using boost::phoenix::replace;
120 using boost::phoenix::arg_names::arg1;
121 int array[] = {1,2,3};
122 replace(arg1,2,4)(array);
123 BOOST_TEST(array[0] == 1);
124 BOOST_TEST(array[1] == 4);
125 BOOST_TEST(array[2] == 3);
126 return;
127 }
128
replace_if_test()129 void replace_if_test()
130 {
131 using boost::phoenix::replace_if;
132 using boost::phoenix::arg_names::arg1;
133 int array[] = {1,2,3};
134 replace_if(arg1, even(), 4)(array);
135 BOOST_TEST(array[0] == 1);
136 BOOST_TEST(array[1] == 4);
137 BOOST_TEST(array[2] == 3);
138 return;
139 }
140
replace_copy_test()141 void replace_copy_test()
142 {
143 using boost::phoenix::replace_copy;
144 using boost::phoenix::arg_names::arg1;
145 using boost::phoenix::arg_names::arg2;
146 int input[] = {1,2,3};
147 int output[3];
148 replace_copy(arg1, arg2, 2, 4)(input, output);
149 BOOST_TEST(output[0] == 1);
150 BOOST_TEST(output[1] == 4);
151 BOOST_TEST(output[2] == 3);
152 return;
153 }
154
replace_copy_if_test()155 void replace_copy_if_test()
156 {
157 using boost::phoenix::replace_copy_if;
158 using boost::phoenix::arg_names::arg1;
159 using boost::phoenix::arg_names::arg2;
160 int input[] = {1,2,3};
161 int output[3];
162 replace_copy_if(arg1, arg2, even(), 4)(input, output);
163 BOOST_TEST(output[0] == 1);
164 BOOST_TEST(output[1] == 4);
165 BOOST_TEST(output[2] == 3);
166 return;
167 }
168
fill_test()169 void fill_test()
170 {
171 using boost::phoenix::fill;
172 using boost::phoenix::arg_names::arg1;
173 int array[] = {0,0,0};
174 fill(arg1, 1)(array);
175 BOOST_TEST(array[0] == 1);
176 BOOST_TEST(array[1] == 1);
177 BOOST_TEST(array[2] == 1);
178 return;
179 }
180
fill_n_test()181 void fill_n_test()
182 {
183 using boost::phoenix::fill_n;
184 using boost::phoenix::arg_names::arg1;
185 int array[] = {0,0,0};
186 fill_n(arg1, 2, 1)(array);
187 BOOST_TEST(array[0] == 1);
188 BOOST_TEST(array[1] == 1);
189 BOOST_TEST(array[2] == 0);
190 return;
191 }
192
193 class int_seq
194 {
195 public:
int_seq()196 int_seq() : val_(0) { }
197
operator ()()198 int operator()()
199 {
200 return val_++;
201 }
202 private:
203 int val_;
204 };
205
generate_test()206 void generate_test()
207 {
208 using boost::phoenix::generate;
209 using boost::phoenix::arg_names::arg1;
210 int array[3];
211 generate(arg1, int_seq())(array);
212 BOOST_TEST(array[0] == 0);
213 BOOST_TEST(array[1] == 1);
214 BOOST_TEST(array[2] == 2);
215 return;
216 }
217
generate_n_test()218 void generate_n_test()
219 {
220 using boost::phoenix::generate_n;
221 using boost::phoenix::arg_names::arg1;
222 int array[] = {0,0,1};
223 generate_n(arg1, 2, int_seq())(array);
224 BOOST_TEST(array[0] == 0);
225 BOOST_TEST(array[1] == 1);
226 BOOST_TEST(array[2] == 1);
227 return;
228 }
229
230
remove_test()231 void remove_test()
232 {
233 using boost::phoenix::remove;
234 using boost::phoenix::arg_names::arg1;
235 int array[] = {1,2,3};
236 std::list<int> test_list(array, array + 3);
237 BOOST_TEST(boost::phoenix::remove(arg1, 2)(array) == array + 2);
238 BOOST_TEST(array[0] == 1);
239 BOOST_TEST(array[1] == 3);
240 BOOST_TEST(boost::phoenix::remove(arg1, 2)(test_list) == test_list.end());
241 std::list<int>::const_iterator it(test_list.begin());
242 BOOST_TEST(*it++ == 1);
243 BOOST_TEST(*it++ == 3);
244 return;
245 }
246
remove_if_test()247 void remove_if_test()
248 {
249 using boost::phoenix::remove_if;
250 using boost::phoenix::arg_names::arg1;
251 int array[] = {1,2,3};
252 std::list<int> test_list(array, array + 3);
253 BOOST_TEST(boost::phoenix::remove_if(arg1, even())(array) == array + 2);
254 BOOST_TEST(array[0] == 1);
255 BOOST_TEST(array[1] == 3);
256 BOOST_TEST(boost::phoenix::remove_if(arg1, even())(test_list) == test_list.end());
257 std::list<int>::const_iterator it(test_list.begin());
258 BOOST_TEST(*it++ == 1);
259 BOOST_TEST(*it++ == 3);
260 return;
261 }
262
remove_copy_test()263 void remove_copy_test()
264 {
265 using boost::phoenix::remove_copy;
266 using boost::phoenix::arg_names::arg1;
267 using boost::phoenix::arg_names::arg2;
268 int array[] = {1,2,3};
269 int array2[2];
270 BOOST_TEST(boost::phoenix::remove_copy(arg1, arg2, 2)(array, array2) == array2 + 2);
271 BOOST_TEST(array2[0] == 1);
272 BOOST_TEST(array2[1] == 3);
273 return;
274 }
275
remove_copy_if_test()276 void remove_copy_if_test()
277 {
278 using boost::phoenix::remove_copy_if;
279 using boost::phoenix::arg_names::arg1;
280 using boost::phoenix::arg_names::arg2;
281 int array[] = {1,2,3};
282 int array2[2];
283 BOOST_TEST(boost::phoenix::remove_copy_if(arg1, arg2, even())(array, array2) == array2 + 2);
284 BOOST_TEST(array2[0] == 1);
285 BOOST_TEST(array2[1] == 3);
286 return;
287 }
288
unique_test()289 void unique_test()
290 {
291 using boost::phoenix::unique;
292 using boost::phoenix::arg_names::arg1;
293 int array[] = {1,2,2,3};
294 std::list<int> test_list(array, array + 4);
295 BOOST_TEST(unique(arg1)(array) == array + 3);
296 BOOST_TEST(array[0] == 1);
297 BOOST_TEST(array[1] == 2);
298 BOOST_TEST(array[2] == 3);
299
300 BOOST_TEST(unique(arg1)(test_list) == test_list.end());
301 std::list<int>::const_iterator it(test_list.begin());
302 BOOST_TEST(*it++ == 1);
303 BOOST_TEST(*it++ == 2);
304 BOOST_TEST(*it++ == 3);
305
306 int array2[] = {1,3,2};
307 std::list<int> test_list2(array2, array2 + 3);
308 BOOST_TEST(unique(arg1, mod_2_comparison())(array2) == array2 + 2);
309 BOOST_TEST(array2[0] == 1);
310 BOOST_TEST(array2[1] == 2);
311
312 BOOST_TEST(unique(arg1, mod_2_comparison())(test_list2) == test_list2.end());
313 std::list<int>::const_iterator jt(test_list2.begin());
314 BOOST_TEST(*jt++ == 1);
315 BOOST_TEST(*jt++ == 2);
316
317 return;
318 }
319
unique_copy_test()320 void unique_copy_test()
321 {
322 using boost::phoenix::unique_copy;
323 using boost::phoenix::arg_names::arg1;
324 using boost::phoenix::arg_names::arg2;
325 int array[] = {1,2,2,3};
326 int out[3];
327 BOOST_TEST(unique_copy(arg1, arg2)(array, out) == out + 3);
328 BOOST_TEST(out[0] == 1);
329 BOOST_TEST(out[1] == 2);
330 BOOST_TEST(out[2] == 3);
331
332 int array2[] = {1,3,2};
333 int out2[2];
334 BOOST_TEST(unique_copy(arg1, arg2, mod_2_comparison())(array2, out2) == out2 + 2);
335 BOOST_TEST(out2[0] == 1);
336 BOOST_TEST(out2[1] == 2);
337
338 return;
339 }
340
reverse_test()341 void reverse_test()
342 {
343 using boost::phoenix::reverse;
344 using boost::phoenix::arg_names::arg1;
345 int array[] = {1,2,3};
346 std::list<int> test_list(array, array + 3);
347 reverse(arg1)(array);
348 BOOST_TEST(array[0] == 3);
349 BOOST_TEST(array[1] == 2);
350 BOOST_TEST(array[2] == 1);
351
352 reverse(arg1)(test_list);
353 std::list<int>::iterator it(test_list.begin());
354 BOOST_TEST(*it++ == 3);
355 BOOST_TEST(*it++ == 2);
356 BOOST_TEST(*it++ == 1);
357 return;
358 }
359
reverse_copy_test()360 void reverse_copy_test()
361 {
362 using boost::phoenix::reverse_copy;
363 using boost::phoenix::arg_names::arg1;
364 using boost::phoenix::arg_names::arg2;
365 int array[] = {1,2,3};
366 int array2[3];
367 reverse_copy(arg1, arg2)(array, array2);
368 BOOST_TEST(array[0] == 1);
369 BOOST_TEST(array[1] == 2);
370 BOOST_TEST(array[2] == 3);
371
372 BOOST_TEST(array2[0] == 3);
373 BOOST_TEST(array2[1] == 2);
374 BOOST_TEST(array2[2] == 1);
375
376 return;
377 }
378 }
379
main()380 int main()
381 {
382 swap_test();
383 copy_test();
384 copy_backward_test();
385 transform_test();
386 replace_test();
387 replace_if_test();
388 replace_copy_test();
389 replace_copy_if_test();
390 fill_test();
391 fill_n_test();
392 generate_test();
393 generate_n_test();
394 remove_test();
395 remove_if_test();
396 remove_copy_test();
397 remove_copy_if_test();
398 unique_test();
399 unique_copy_test();
400 reverse_test();
401 reverse_copy_test();
402 boost::report_errors();
403 }
404