transformation3.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <functional>
  8. #include <boost/phoenix/core.hpp>
  9. #include <boost/phoenix/stl/algorithm/transformation.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <list>
  12. namespace
  13. {
  14. void nth_element_test()
  15. {
  16. using boost::phoenix::nth_element;
  17. using boost::phoenix::arg_names::arg1;
  18. int array[] = {5,1,4,3,2};
  19. nth_element(arg1, array + 2)(array);
  20. BOOST_TEST(array[0] < 3);
  21. BOOST_TEST(array[1] < 3);
  22. BOOST_TEST(array[2] == 3);
  23. BOOST_TEST(array[3] > 3);
  24. BOOST_TEST(array[4] > 3);
  25. boost::phoenix::nth_element(arg1, array + 2, std::greater<int>())(array);
  26. BOOST_TEST(array[0] > 3);
  27. BOOST_TEST(array[1] > 3);
  28. BOOST_TEST(array[2] == 3);
  29. BOOST_TEST(array[3] < 3);
  30. BOOST_TEST(array[4] < 3);
  31. return;
  32. }
  33. void merge_test()
  34. {
  35. using boost::phoenix::merge;
  36. using boost::phoenix::arg_names::arg1;
  37. using boost::phoenix::arg_names::arg2;
  38. using boost::phoenix::arg_names::arg3;
  39. int array[] = {1,2,3};
  40. int array2[] = {2,3,4};
  41. int output[6];
  42. BOOST_TEST(merge(arg1, arg2, arg3)(array, array2, output) == output + 6);
  43. int expected_result[] = {1,2,2,3,3,4};
  44. BOOST_TEST(std::equal(output, output + 6, expected_result));
  45. int array3[] = {5,4,3};
  46. int array4[] = {3,2,1};
  47. int output2[6];
  48. BOOST_TEST(boost::phoenix::merge(arg1, arg2, arg3, std::greater<int>())(array3, array4, output2) ==
  49. output2 + 6);
  50. int expected_result2[] = {5,4,3,3,2,1};
  51. BOOST_TEST(std::equal(output2, output2 + 6, expected_result2));
  52. return;
  53. }
  54. void inplace_merge_test()
  55. {
  56. using boost::phoenix::inplace_merge;
  57. using boost::phoenix::arg_names::arg1;
  58. int array[] = {1,2,3,2,3,4};
  59. inplace_merge(arg1, array + 3)(array);
  60. int expected_result[] = {1,2,2,3,3,4};
  61. BOOST_TEST(std::equal(array, array + 6, expected_result));
  62. int array2[] = {5,4,3,4,3,2};
  63. boost::phoenix::inplace_merge(arg1, array2 + 3, std::greater<int>())(array2);
  64. int expected_result2[] = {5,4,4,3,3,2};
  65. BOOST_TEST(std::equal(array2, array2 + 6, expected_result2));
  66. return;
  67. }
  68. void set_union_test()
  69. {
  70. using boost::phoenix::set_union;
  71. using boost::phoenix::arg_names::arg1;
  72. using boost::phoenix::arg_names::arg2;
  73. using boost::phoenix::arg_names::arg3;
  74. int array[] = {1,2,3};
  75. int array2[] = {2,3,4};
  76. int output[4];
  77. BOOST_TEST(set_union(arg1, arg2, arg3)(array, array2, output) == output + 4);
  78. int expected_result[] = {1,2,3,4};
  79. BOOST_TEST(std::equal(output, output + 4, expected_result));
  80. int array3[] = {3,2,1};
  81. int array4[] = {4,3,2};
  82. int output2[4];
  83. BOOST_TEST(boost::phoenix::set_union(arg1, arg2, arg3, std::greater<int>())
  84. (array3, array4, output2) ==
  85. output2 + 4);
  86. int expected_result2[] = {4,3,2,1};
  87. BOOST_TEST(std::equal(output2, output2 + 4, expected_result2));
  88. return;
  89. }
  90. void set_intersection_test()
  91. {
  92. using boost::phoenix::set_intersection;
  93. using boost::phoenix::arg_names::arg1;
  94. using boost::phoenix::arg_names::arg2;
  95. using boost::phoenix::arg_names::arg3;
  96. int array[] = {1,2,3};
  97. int array2[] = {2,3,4};
  98. int output[2];
  99. BOOST_TEST(set_intersection(arg1, arg2, arg3)(array, array2, output) == output + 2);
  100. int expected_result[] = {2,3};
  101. BOOST_TEST(std::equal(output, output + 2, expected_result));
  102. int array3[] = {3,2,1};
  103. int array4[] = {4,3,2};
  104. int output2[2];
  105. BOOST_TEST(boost::phoenix::set_intersection(arg1, arg2, arg3, std::greater<int>())
  106. (array3, array4, output2) ==
  107. output2 + 2);
  108. int expected_result2[] = {3,2};
  109. BOOST_TEST(std::equal(output2, output2 + 2, expected_result2));
  110. return;
  111. }
  112. void set_difference_test()
  113. {
  114. using boost::phoenix::set_difference;
  115. using boost::phoenix::arg_names::arg1;
  116. using boost::phoenix::arg_names::arg2;
  117. using boost::phoenix::arg_names::arg3;
  118. int array[] = {1,2,3};
  119. int array2[] = {2,3,4};
  120. int output[1];
  121. BOOST_TEST(set_difference(arg1, arg2, arg3)(array, array2, output) == output + 1);
  122. int expected_result[] = {1};
  123. BOOST_TEST(std::equal(output, output + 1, expected_result));
  124. int array3[] = {3,2,1};
  125. int array4[] = {4,3,2};
  126. int output2[1];
  127. BOOST_TEST(boost::phoenix::set_difference(arg1, arg2, arg3, std::greater<int>())
  128. (array3, array4, output2) ==
  129. output2 + 1);
  130. int expected_result2[] = {1};
  131. BOOST_TEST(std::equal(output2, output2 + 1, expected_result2));
  132. return;
  133. }
  134. void set_symmetric_difference_test()
  135. {
  136. using boost::phoenix::set_symmetric_difference;
  137. using boost::phoenix::arg_names::arg1;
  138. using boost::phoenix::arg_names::arg2;
  139. using boost::phoenix::arg_names::arg3;
  140. int array[] = {1,2,3};
  141. int array2[] = {2,3,4};
  142. int output[2];
  143. BOOST_TEST(set_symmetric_difference(arg1, arg2, arg3)(array, array2, output) == output + 2);
  144. int expected_result[] = {1,4};
  145. BOOST_TEST(std::equal(output, output + 2, expected_result));
  146. int array3[] = {3,2,1};
  147. int array4[] = {4,3,2};
  148. int output2[2];
  149. BOOST_TEST(boost::phoenix::set_symmetric_difference(arg1, arg2, arg3, std::greater<int>())
  150. (array3, array4, output2) ==
  151. output2 + 2);
  152. int expected_result2[] = {4,1};
  153. BOOST_TEST(std::equal(output2, output2 + 2, expected_result2));
  154. return;
  155. }
  156. }
  157. int main()
  158. {
  159. nth_element_test();
  160. merge_test();
  161. inplace_merge_test();
  162. set_union_test();
  163. set_intersection_test();
  164. set_difference_test();
  165. set_symmetric_difference_test();
  166. return boost::report_errors();
  167. }