next_permutation.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_next_permutation_impl(const Container& cont)
  24. {
  25. Container reference(cont);
  26. Container test(cont);
  27. const bool reference_ret
  28. = std::next_permutation(reference.begin(), reference.end());
  29. const bool test_ret = boost::next_permutation(test);
  30. BOOST_CHECK( reference_ret == test_ret );
  31. BOOST_CHECK_EQUAL_COLLECTIONS(
  32. reference.begin(), reference.end(),
  33. test.begin(), test.end()
  34. );
  35. test = cont;
  36. BOOST_CHECK( test_ret == boost::next_permutation(boost::make_iterator_range(test)) );
  37. BOOST_CHECK_EQUAL_COLLECTIONS(
  38. reference.begin(), reference.end(),
  39. test.begin(), test.end()
  40. );
  41. }
  42. template<class Container, class BinaryPredicate>
  43. void test_next_permutation_pred_impl(const Container& cont,
  44. BinaryPredicate pred)
  45. {
  46. Container reference(cont);
  47. Container test(cont);
  48. const bool reference_ret
  49. = std::next_permutation(reference.begin(), reference.end(),
  50. pred);
  51. const bool test_ret
  52. = boost::next_permutation(test, pred);
  53. BOOST_CHECK( reference_ret == test_ret );
  54. BOOST_CHECK_EQUAL_COLLECTIONS(
  55. reference.begin(), reference.end(),
  56. test.begin(), test.end()
  57. );
  58. test = cont;
  59. BOOST_CHECK( test_ret == boost::next_permutation(boost::make_iterator_range(test), pred) );
  60. BOOST_CHECK_EQUAL_COLLECTIONS(
  61. reference.begin(), reference.end(),
  62. test.begin(), test.end()
  63. );
  64. }
  65. template<class Container>
  66. void test_next_permutation_(const Container& cont)
  67. {
  68. test_next_permutation_impl(cont);
  69. test_next_permutation_pred_impl(cont, std::less<int>());
  70. test_next_permutation_pred_impl(cont, std::greater<int>());
  71. }
  72. template<class Container>
  73. void run_tests()
  74. {
  75. using namespace boost::assign;
  76. Container cont;
  77. test_next_permutation_(cont);
  78. cont.clear();
  79. cont += 1;
  80. test_next_permutation_(cont);
  81. cont.clear();
  82. cont += 1,2,3,4,5,6,7,8,9;
  83. test_next_permutation_(cont);
  84. }
  85. void test_next_permutation()
  86. {
  87. run_tests< std::vector<int> >();
  88. run_tests< std::list<int> >();
  89. run_tests< std::deque<int> >();
  90. }
  91. }
  92. }
  93. boost::unit_test::test_suite*
  94. init_unit_test_suite(int argc, char* argv[])
  95. {
  96. boost::unit_test::test_suite* test
  97. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.next_permutation" );
  98. test->add( BOOST_TEST_CASE( &boost::test_next_permutation ) );
  99. return test;
  100. }