• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 namespace boost
20 {
21     namespace
22     {
23         template<class Container>
test_next_permutation_impl(const Container & cont)24         void test_next_permutation_impl(const Container& cont)
25         {
26             Container reference(cont);
27             Container test(cont);
28 
29             const bool reference_ret
30                 = std::next_permutation(reference.begin(), reference.end());
31 
32             const bool test_ret = boost::next_permutation(test);
33 
34             BOOST_CHECK( reference_ret == test_ret );
35 
36             BOOST_CHECK_EQUAL_COLLECTIONS(
37                 reference.begin(), reference.end(),
38                 test.begin(), test.end()
39                 );
40 
41             test = cont;
42 
43             BOOST_CHECK( test_ret == boost::next_permutation(boost::make_iterator_range(test)) );
44 
45             BOOST_CHECK_EQUAL_COLLECTIONS(
46                 reference.begin(), reference.end(),
47                 test.begin(), test.end()
48                 );
49         }
50 
51         template<class Container, class BinaryPredicate>
test_next_permutation_pred_impl(const Container & cont,BinaryPredicate pred)52         void test_next_permutation_pred_impl(const Container& cont,
53                                              BinaryPredicate  pred)
54         {
55             Container reference(cont);
56             Container test(cont);
57 
58             const bool reference_ret
59                 = std::next_permutation(reference.begin(), reference.end(),
60                                         pred);
61 
62             const bool test_ret
63                 = boost::next_permutation(test, pred);
64 
65             BOOST_CHECK( reference_ret == test_ret );
66 
67             BOOST_CHECK_EQUAL_COLLECTIONS(
68                 reference.begin(), reference.end(),
69                 test.begin(), test.end()
70                 );
71 
72             test = cont;
73 
74             BOOST_CHECK( test_ret == boost::next_permutation(boost::make_iterator_range(test), pred) );
75 
76             BOOST_CHECK_EQUAL_COLLECTIONS(
77                 reference.begin(), reference.end(),
78                 test.begin(), test.end()
79                 );
80         }
81 
82         template<class Container>
test_next_permutation_(const Container & cont)83         void test_next_permutation_(const Container& cont)
84         {
85             test_next_permutation_impl(cont);
86             test_next_permutation_pred_impl(cont, std::less<int>());
87             test_next_permutation_pred_impl(cont, std::greater<int>());
88         }
89 
90         template<class Container>
run_tests()91         void run_tests()
92         {
93             using namespace boost::assign;
94 
95             Container cont;
96             test_next_permutation_(cont);
97 
98             cont.clear();
99             cont += 1;
100             test_next_permutation_(cont);
101 
102             cont.clear();
103             cont += 1,2,3,4,5,6,7,8,9;
104             test_next_permutation_(cont);
105         }
106 
test_next_permutation()107         void test_next_permutation()
108         {
109             run_tests< std::vector<int> >();
110             run_tests< std::list<int> >();
111             run_tests< std::deque<int> >();
112         }
113     }
114 }
115 
116 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])117 init_unit_test_suite(int argc, char* argv[])
118 {
119     boost::unit_test::test_suite* test
120         = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.next_permutation" );
121 
122     test->add( BOOST_TEST_CASE( &boost::test_next_permutation ) );
123 
124     return test;
125 }
126