transformation4.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <vector>
  11. #include <functional>
  12. #include <algorithm>
  13. namespace
  14. {
  15. void heap_test()
  16. {
  17. using boost::phoenix::make_heap;
  18. using boost::phoenix::pop_heap;
  19. using boost::phoenix::push_heap;
  20. using boost::phoenix::sort_heap;
  21. using boost::phoenix::arg_names::arg1;
  22. int array[] = {1,2,3};
  23. std::vector<int> vec(array, array + 3);
  24. boost::phoenix::make_heap(arg1)(vec);
  25. vec.push_back(5);
  26. boost::phoenix::push_heap(arg1)(vec);
  27. vec.push_back(4);
  28. boost::phoenix::push_heap(arg1)(vec);
  29. boost::phoenix::pop_heap(arg1)(vec);
  30. BOOST_TEST(vec.back() == 5);
  31. vec.pop_back();
  32. boost::phoenix::sort_heap(arg1)(vec);
  33. int expected_result[] = {1,2,3,4};
  34. BOOST_TEST(std::equal(vec.begin(), vec.end(), expected_result));
  35. int array2[] = {3,2,1};
  36. std::vector<int> vec2(array2, array2 + 3);
  37. boost::phoenix::make_heap(arg1, std::greater<int>())(vec2);
  38. vec2.push_back(5);
  39. boost::phoenix::push_heap(arg1, std::greater<int>())(vec2);
  40. vec2.push_back(4);
  41. boost::phoenix::push_heap(arg1, std::greater<int>())(vec2);
  42. boost::phoenix::pop_heap(arg1, std::greater<int>())(vec2);
  43. BOOST_TEST(vec2.back() == 1);
  44. vec2.pop_back();
  45. boost::phoenix::sort_heap(arg1, std::greater<int>())(vec2);
  46. int expected_result2[] = {5,4,3,2};
  47. BOOST_TEST(std::equal(vec2.begin(), vec2.end(), expected_result2));
  48. return;
  49. }
  50. void next_permutation_test()
  51. {
  52. using boost::phoenix::next_permutation;
  53. using boost::phoenix::arg_names::arg1;
  54. int array[] = {1,2};
  55. int expected_result[] = {2,1};
  56. int expected_result2[] = {1,2};
  57. BOOST_TEST(next_permutation(arg1)(array));
  58. BOOST_TEST(std::equal(array, array + 2, expected_result));
  59. BOOST_TEST(!next_permutation(arg1)(array));
  60. BOOST_TEST(std::equal(array, array + 2, expected_result2));
  61. std::reverse(array, array + 2);
  62. BOOST_TEST(boost::phoenix::next_permutation(arg1, std::greater<int>())(array));
  63. BOOST_TEST(std::equal(array, array + 2, expected_result2));
  64. BOOST_TEST(!boost::phoenix::next_permutation(arg1, std::greater<int>())(array));
  65. BOOST_TEST(std::equal(array, array + 2, expected_result));
  66. return;
  67. }
  68. void prev_permutation_test()
  69. {
  70. using boost::phoenix::prev_permutation;
  71. using boost::phoenix::arg_names::arg1;
  72. int array[] = {2,1};
  73. int expected_result[] = {1,2};
  74. int expected_result2[] = {2,1};
  75. BOOST_TEST(prev_permutation(arg1)(array));
  76. BOOST_TEST(std::equal(array, array + 2, expected_result));
  77. BOOST_TEST(!prev_permutation(arg1)(array));
  78. BOOST_TEST(std::equal(array, array + 2, expected_result2));
  79. std::reverse(array, array + 2);
  80. BOOST_TEST(boost::phoenix::prev_permutation(arg1, std::greater<int>())(array));
  81. BOOST_TEST(std::equal(array, array + 2, expected_result2));
  82. BOOST_TEST(!boost::phoenix::prev_permutation(arg1, std::greater<int>())(array));
  83. BOOST_TEST(std::equal(array, array + 2, expected_result));
  84. return;
  85. }
  86. void inner_product_test()
  87. {
  88. using boost::phoenix::inner_product;
  89. using boost::phoenix::arg_names::arg1;
  90. using boost::phoenix::arg_names::arg2;
  91. int lhs[] = {1,2,3};
  92. int rhs[] = {4,5,6};
  93. BOOST_TEST(inner_product(arg1, arg2, 0)
  94. (lhs, rhs) == 1*4 + 2*5 + 3*6);
  95. BOOST_TEST(boost::phoenix::inner_product(arg1, arg2, 1, std::multiplies<int>(), std::minus<int>())
  96. (lhs, rhs) == (1 - 4) * (2 - 5) * (3 - 6));
  97. return;
  98. }
  99. void partial_sum_test()
  100. {
  101. using boost::phoenix::partial_sum;
  102. using boost::phoenix::arg_names::arg1;
  103. using boost::phoenix::arg_names::arg2;
  104. int array[] = {1,2,3};
  105. int output[3];
  106. BOOST_TEST(partial_sum(arg1, arg2)(array, output) == output + 3);
  107. int expected_result[] = {1, 3, 6};
  108. BOOST_TEST(std::equal(output, output + 3, expected_result));
  109. BOOST_TEST(boost::phoenix::partial_sum(arg1, arg2, std::multiplies<int>())
  110. (array, output) == output + 3);
  111. int expected_result2[] = {1, 2, 6};
  112. BOOST_TEST(std::equal(output, output + 3, expected_result2));
  113. return;
  114. }
  115. void adjacent_difference_test()
  116. {
  117. using boost::phoenix::adjacent_difference;
  118. using boost::phoenix::arg_names::arg1;
  119. using boost::phoenix::arg_names::arg2;
  120. int array[] = {1,2,3};
  121. int output[3];
  122. BOOST_TEST(adjacent_difference(arg1, arg2)(array, output) == output + 3);
  123. int expected_result[] = {1, 1, 1};
  124. BOOST_TEST(std::equal(output, output + 3, expected_result));
  125. BOOST_TEST(boost::phoenix::adjacent_difference(arg1, arg2, std::plus<int>())
  126. (array, output) == output + 3);
  127. int expected_result2[] = {1, 3, 5};
  128. BOOST_TEST(std::equal(output, output + 3, expected_result2));
  129. return;
  130. }
  131. }
  132. int main()
  133. {
  134. heap_test();
  135. next_permutation_test();
  136. prev_permutation_test();
  137. inner_product_test();
  138. partial_sum_test();
  139. adjacent_difference_test();
  140. return boost::report_errors();
  141. }