/Src/Dependencies/Boost/libs/phoenix/test/algorithm/transformation2.cpp

http://hadesmem.googlecode.com/ · C++ · 192 lines · 161 code · 24 blank · 7 comment · 60 complexity · 207c3c0bfd87ce805bdb4103595a0225 MD5 · raw file

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