transformation1.cpp 11 KB

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