transformation2.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. struct even
  15. {
  16. bool operator()(const int i) const
  17. {
  18. return i % 2 == 0;
  19. }
  20. };
  21. void rotate_test()
  22. {
  23. using boost::phoenix::rotate;
  24. using boost::phoenix::arg_names::arg1;
  25. int array[] = {1,2,3};
  26. rotate(arg1, array + 1)(array);
  27. std::cout << array[0] << array[1] << array[2] << std::endl;
  28. BOOST_TEST(array[0] == 2);
  29. BOOST_TEST(array[1] == 3);
  30. BOOST_TEST(array[2] == 1);
  31. return;
  32. }
  33. void rotate_copy_test()
  34. {
  35. using boost::phoenix::rotate_copy;
  36. using boost::phoenix::arg_names::arg1;
  37. using boost::phoenix::arg_names::arg2;
  38. int array[] = {1,2,3};
  39. int array2[3];
  40. rotate_copy(arg1, array + 1, arg2)(array, array2);
  41. BOOST_TEST(array2[0] == 2);
  42. BOOST_TEST(array2[1] == 3);
  43. BOOST_TEST(array2[2] == 1);
  44. return;
  45. }
  46. void random_shuffle_test()
  47. {
  48. #ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
  49. using boost::phoenix::random_shuffle;
  50. using boost::phoenix::arg_names::arg1;
  51. int array[] = {1,2,3};
  52. random_shuffle(arg1)(array);
  53. const int first = array[0];
  54. BOOST_TEST(first == 1 || first == 2 || first == 3);
  55. const int second = array[1];
  56. BOOST_TEST(second == 1 || second == 2 || second == 3);
  57. BOOST_TEST(first != second);
  58. const int third = array[2];
  59. BOOST_TEST(third == 1 || third == 2 || third == 3);
  60. BOOST_TEST(first != third && second != third);
  61. return;
  62. #endif
  63. }
  64. void partition_test()
  65. {
  66. using boost::phoenix::partition;
  67. using boost::phoenix::arg_names::arg1;
  68. int array[] = {1,2,3};
  69. int* const end = partition(arg1, even())(array);
  70. BOOST_TEST(end == array + 1);
  71. BOOST_TEST(array[0] % 2 == 0);
  72. BOOST_TEST(array[1] % 2 != 0);
  73. BOOST_TEST(array[2] % 2 != 0);
  74. return;
  75. }
  76. void stable_partition_test()
  77. {
  78. using boost::phoenix::stable_partition;
  79. using boost::phoenix::arg_names::arg1;
  80. int array[] = {1,2,3};
  81. int* const end = stable_partition(arg1, even())(array);
  82. BOOST_TEST(end == array + 1);
  83. BOOST_TEST(array[0] == 2);
  84. BOOST_TEST(array[1] == 1);
  85. BOOST_TEST(array[2] == 3);
  86. return;
  87. }
  88. void sort_test()
  89. {
  90. using boost::phoenix::sort;
  91. using boost::phoenix::arg_names::arg1;
  92. int array[] = {3,1,2};
  93. std::list<int> test_list(array, array + 3);
  94. sort(arg1)(array);
  95. BOOST_TEST(array[0] == 1);
  96. BOOST_TEST(array[1] == 2);
  97. BOOST_TEST(array[2] == 3);
  98. sort(arg1)(test_list);
  99. std::list<int>::const_iterator it(test_list.begin());
  100. BOOST_TEST(*it++ == 1);
  101. BOOST_TEST(*it++ == 2);
  102. BOOST_TEST(*it++ == 3);
  103. boost::phoenix::sort(arg1, std::greater<int>())(array);
  104. BOOST_TEST(array[0] == 3);
  105. BOOST_TEST(array[1] == 2);
  106. BOOST_TEST(array[2] == 1);
  107. boost::phoenix::sort(arg1, std::greater<int>())(test_list);
  108. std::list<int>::const_iterator jt(test_list.begin());
  109. BOOST_TEST(*jt++ == 3);
  110. BOOST_TEST(*jt++ == 2);
  111. BOOST_TEST(*jt++ == 1);
  112. return;
  113. }
  114. void stable_sort_test()
  115. {
  116. using boost::phoenix::stable_sort;
  117. using boost::phoenix::arg_names::arg1;
  118. int array[] = {3,1,2};
  119. stable_sort(arg1)(array);
  120. BOOST_TEST(array[0] == 1);
  121. BOOST_TEST(array[1] == 2);
  122. BOOST_TEST(array[2] == 3);
  123. boost::phoenix::stable_sort(arg1, std::greater<int>())(array);
  124. BOOST_TEST(array[0] == 3);
  125. BOOST_TEST(array[1] == 2);
  126. BOOST_TEST(array[2] == 1);
  127. return;
  128. }
  129. void partial_sort_test()
  130. {
  131. using boost::phoenix::partial_sort;
  132. using boost::phoenix::arg_names::arg1;
  133. int array[] = {2,4,1,3};
  134. partial_sort(arg1, array + 2)(array);
  135. BOOST_TEST(array[0] == 1);
  136. BOOST_TEST(array[1] == 2);
  137. boost::phoenix::partial_sort(arg1, array + 2, std::greater<int>())(array);
  138. BOOST_TEST(array[0] == 4);
  139. BOOST_TEST(array[1] == 3);
  140. return;
  141. }
  142. void partial_sort_copy_test()
  143. {
  144. using boost::phoenix::partial_sort_copy;
  145. using boost::phoenix::arg_names::arg1;
  146. using boost::phoenix::arg_names::arg2;
  147. int array[] = {2,4,1,3};
  148. int array2[2];
  149. partial_sort_copy(arg1, arg2)(array, array2);
  150. BOOST_TEST(array2[0] == 1);
  151. BOOST_TEST(array2[1] == 2);
  152. boost::phoenix::partial_sort_copy(arg1, arg2, std::greater<int>())(array, array2);
  153. BOOST_TEST(array2[0] == 4);
  154. BOOST_TEST(array2[1] == 3);
  155. return;
  156. }
  157. }
  158. int main()
  159. {
  160. rotate_test();
  161. rotate_copy_test();
  162. random_shuffle_test();
  163. partition_test();
  164. stable_partition_test();
  165. sort_test();
  166. stable_sort_test();
  167. partial_sort_test();
  168. partial_sort_copy_test();
  169. return boost::report_errors();
  170. }