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