prev_permutation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #include <boost/assign.hpp>
  10. #include <boost/range/algorithm/permutation.hpp>
  11. #include <boost/test/test_tools.hpp>
  12. #include <boost/test/unit_test.hpp>
  13. #include <algorithm>
  14. #include <deque>
  15. #include <functional>
  16. #include <list>
  17. #include <vector>
  18. namespace boost
  19. {
  20. namespace
  21. {
  22. template<class Container>
  23. void test_prev_permutation_impl(const Container& cont)
  24. {
  25. Container reference(cont);
  26. Container test(cont);
  27. Container test2(cont);
  28. const bool reference_ret
  29. = std::prev_permutation(reference.begin(), reference.end());
  30. const bool test_ret = boost::prev_permutation(test);
  31. BOOST_CHECK( reference_ret == test_ret );
  32. BOOST_CHECK_EQUAL_COLLECTIONS(
  33. reference.begin(), reference.end(),
  34. test.begin(), test.end()
  35. );
  36. BOOST_CHECK( test_ret == boost::prev_permutation(
  37. boost::make_iterator_range(test2)) );
  38. BOOST_CHECK_EQUAL_COLLECTIONS(
  39. reference.begin(), reference.end(),
  40. test2.begin(), test2.end()
  41. );
  42. }
  43. template<class Container, class BinaryPredicate>
  44. void test_prev_permutation_pred_impl(const Container& cont,
  45. BinaryPredicate pred)
  46. {
  47. Container reference(cont);
  48. Container test(cont);
  49. Container test2(cont);
  50. const bool reference_ret
  51. = std::prev_permutation(reference.begin(), reference.end(),
  52. pred);
  53. const bool test_ret = boost::prev_permutation(test, pred);
  54. BOOST_CHECK( reference_ret == test_ret );
  55. BOOST_CHECK_EQUAL_COLLECTIONS(
  56. reference.begin(), reference.end(),
  57. test.begin(), test.end()
  58. );
  59. BOOST_CHECK( test_ret == boost::prev_permutation(
  60. boost::make_iterator_range(test2), pred) );
  61. BOOST_CHECK_EQUAL_COLLECTIONS(
  62. reference.begin(), reference.end(),
  63. test2.begin(), test2.end()
  64. );
  65. }
  66. template<class Container>
  67. void test_prev_permutation_(const Container& cont)
  68. {
  69. test_prev_permutation_impl(cont);
  70. test_prev_permutation_pred_impl(cont, std::less<int>());
  71. test_prev_permutation_pred_impl(cont, std::greater<int>());
  72. }
  73. template<class Container>
  74. void run_tests()
  75. {
  76. using namespace boost::assign;
  77. Container cont;
  78. test_prev_permutation_(cont);
  79. cont.clear();
  80. cont += 1;
  81. test_prev_permutation_(cont);
  82. cont.clear();
  83. cont += 1,2,3,4,5,6,7,8,9;
  84. test_prev_permutation_(cont);
  85. }
  86. void test_prev_permutation()
  87. {
  88. run_tests< std::vector<int> >();
  89. run_tests< std::list<int> >();
  90. run_tests< std::deque<int> >();
  91. }
  92. }
  93. }
  94. boost::unit_test::test_suite*
  95. init_unit_test_suite(int argc, char* argv[])
  96. {
  97. boost::unit_test::test_suite* test
  98. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.prev_permutation" );
  99. test->add( BOOST_TEST_CASE( &boost::test_prev_permutation ) );
  100. return test;
  101. }