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/range/algorithm/set_algorithm.hpp> |
10 | |
11 | #include <boost/test/test_tools.hpp> |
12 | #include <boost/test/unit_test.hpp> |
13 | #include <boost/assign.hpp> |
14 | #include <algorithm> |
15 | #include <functional> |
16 | #include <list> |
17 | #include <numeric> |
18 | #include <deque> |
19 | #include <vector> |
20 | |
21 | namespace boost |
22 | { |
23 | namespace |
24 | { |
25 | template<class Container1, class Container2> |
26 | void test(Container1& cont1, Container2& cont2) |
27 | { |
28 | Container1 old_cont1(cont1); |
29 | Container2 old_cont2(cont2); |
30 | |
31 | bool reference_result |
32 | = std::includes(cont1.begin(), cont1.end(), |
33 | cont2.begin(), cont2.end()); |
34 | |
35 | bool test_result = boost::includes(cont1, cont2); |
36 | |
37 | BOOST_CHECK( reference_result == test_result ); |
38 | |
39 | BOOST_CHECK_EQUAL_COLLECTIONS( |
40 | old_cont1.begin(), old_cont1.end(), |
41 | cont1.begin(), cont1.end() |
42 | ); |
43 | |
44 | BOOST_CHECK_EQUAL_COLLECTIONS( |
45 | old_cont2.begin(), old_cont2.end(), |
46 | cont2.begin(), cont2.end() |
47 | ); |
48 | |
49 | BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), cont2) ); |
50 | BOOST_CHECK( test_result == boost::includes(cont1, boost::make_iterator_range(cont2)) ); |
51 | BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2)) ); |
52 | } |
53 | |
54 | template<class Container, class BinaryPredicate> |
55 | void sort_container(Container& cont, BinaryPredicate pred) |
56 | { |
57 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; |
58 | |
59 | std::vector<value_t> temp(cont.begin(), cont.end()); |
60 | std::sort(temp.begin(), temp.end(), pred); |
61 | cont.assign(temp.begin(), temp.end()); |
62 | } |
63 | |
64 | template<class Container1, |
65 | class Container2, |
66 | class BinaryPredicate> |
67 | void test_pred(Container1 cont1, Container2 cont2, |
68 | BinaryPredicate pred) |
69 | { |
70 | sort_container(cont1, pred); |
71 | sort_container(cont2, pred); |
72 | |
73 | Container1 old_cont1(cont1); |
74 | Container2 old_cont2(cont2); |
75 | |
76 | bool reference_result |
77 | = std::includes(cont1.begin(), cont1.end(), |
78 | cont2.begin(), cont2.end(), |
79 | pred); |
80 | |
81 | bool test_result = boost::includes(cont1, cont2, pred); |
82 | |
83 | BOOST_CHECK( reference_result == test_result ); |
84 | |
85 | BOOST_CHECK_EQUAL_COLLECTIONS( |
86 | old_cont1.begin(), old_cont1.end(), |
87 | cont1.begin(), cont1.end() |
88 | ); |
89 | |
90 | BOOST_CHECK_EQUAL_COLLECTIONS( |
91 | old_cont2.begin(), old_cont2.end(), |
92 | cont2.begin(), cont2.end() |
93 | ); |
94 | |
95 | BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), cont2, pred) ); |
96 | BOOST_CHECK( test_result == boost::includes(cont1, boost::make_iterator_range(cont2), pred) ); |
97 | BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), pred) ); |
98 | } |
99 | |
100 | template<class Container1, class Container2> |
101 | void test_includes_impl( |
102 | Container1& cont1, |
103 | Container2& cont2 |
104 | ) |
105 | { |
106 | test(cont1, cont2); |
107 | test_pred(cont1, cont2, std::less<int>()); |
108 | test_pred(cont1, cont2, std::greater<int>()); |
109 | } |
110 | |
111 | template<class Container1, class Container2> |
112 | void test_includes_impl() |
113 | { |
114 | using namespace boost::assign; |
115 | |
116 | Container1 cont1; |
117 | Container2 cont2; |
118 | |
119 | test_includes_impl(cont1, cont2); |
120 | |
121 | cont1.clear(); |
122 | cont2.clear(); |
123 | cont1 += 1; |
124 | test_includes_impl(cont1, cont2); |
125 | |
126 | cont1.clear(); |
127 | cont2.clear(); |
128 | cont2 += 1; |
129 | test_includes_impl(cont1, cont2); |
130 | |
131 | cont1.clear(); |
132 | cont2.clear(); |
133 | cont1 += 1,2,3,4,5,6,7,8,9; |
134 | cont2 += 2,3,4; |
135 | test_includes_impl(cont1, cont2); |
136 | |
137 | cont1.clear(); |
138 | cont2.clear(); |
139 | cont1 += 2,3,4; |
140 | cont2 += 1,2,3,4,5,6,7,8,9; |
141 | test_includes_impl(cont1, cont2); |
142 | } |
143 | |
144 | void test_includes() |
145 | { |
146 | test_includes_impl< std::vector<int>, std::vector<int> >(); |
147 | test_includes_impl< std::list<int>, std::list<int> >(); |
148 | test_includes_impl< std::deque<int>, std::deque<int> >(); |
149 | test_includes_impl< std::vector<int>, std::list<int> >(); |
150 | test_includes_impl< std::list<int>, std::vector<int> >(); |
151 | } |
152 | } |
153 | } |
154 | |
155 | boost::unit_test::test_suite* |
156 | init_unit_test_suite(int argc, char* argv[]) |
157 | { |
158 | boost::unit_test::test_suite* test |
159 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.includes" ); |
160 | |
161 | test->add( BOOST_TEST_CASE( &boost::test_includes ) ); |
162 | |
163 | return test; |
164 | } |
165 | |