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 | |
14 | #include <boost/assign.hpp> |
15 | #include <algorithm> |
16 | #include <functional> |
17 | #include <list> |
18 | #include <numeric> |
19 | #include <deque> |
20 | #include <vector> |
21 | |
22 | namespace boost |
23 | { |
24 | namespace |
25 | { |
26 | template<class Container1, class Iterator, class Container2> |
27 | void check_result( |
28 | Container1& reference, |
29 | Iterator reference_result, |
30 | Container2& test_cont, |
31 | Iterator test_result |
32 | ) |
33 | { |
34 | BOOST_CHECK_EQUAL( |
35 | std::distance<Iterator>(reference.begin(), reference_result), |
36 | std::distance<Iterator>(test_cont.begin(), test_result) |
37 | ); |
38 | |
39 | BOOST_CHECK_EQUAL_COLLECTIONS( |
40 | reference.begin(), reference.end(), |
41 | test_cont.begin(), test_cont.end() |
42 | ); |
43 | } |
44 | |
45 | template<class Container1, class Container2> |
46 | void test(Container1& cont1, Container2& cont2) |
47 | { |
48 | typedef BOOST_DEDUCED_TYPENAME Container1::value_type value_t; |
49 | typedef BOOST_DEDUCED_TYPENAME std::vector<value_t>::iterator iterator_t; |
50 | |
51 | std::vector<value_t> reference(cont1.size() + cont2.size()); |
52 | std::vector<value_t> test_cont(reference); |
53 | |
54 | iterator_t reference_result |
55 | = std::set_symmetric_difference(cont1.begin(), cont1.end(), |
56 | cont2.begin(), cont2.end(), |
57 | reference.begin()); |
58 | |
59 | iterator_t test_result |
60 | = boost::set_symmetric_difference(cont1, cont2, |
61 | test_cont.begin()); |
62 | |
63 | check_result(reference, reference_result, |
64 | test_cont, test_result); |
65 | |
66 | test_result = boost::set_symmetric_difference( |
67 | boost::make_iterator_range(cont1), cont2, |
68 | test_cont.begin()); |
69 | |
70 | check_result(reference, reference_result, |
71 | test_cont, test_result); |
72 | |
73 | test_result = boost::set_symmetric_difference( |
74 | cont1, boost::make_iterator_range(cont2), |
75 | test_cont.begin()); |
76 | |
77 | check_result(reference, reference_result, |
78 | test_cont, test_result); |
79 | |
80 | test_result = boost::set_symmetric_difference( |
81 | boost::make_iterator_range(cont1), |
82 | boost::make_iterator_range(cont2), |
83 | test_cont.begin()); |
84 | |
85 | check_result(reference, reference_result, |
86 | test_cont, test_result); |
87 | } |
88 | |
89 | template<class Container, class BinaryPredicate> |
90 | void sort_container(Container& cont, BinaryPredicate pred) |
91 | { |
92 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; |
93 | |
94 | std::vector<value_t> temp(cont.begin(), cont.end()); |
95 | std::sort(temp.begin(), temp.end(), pred); |
96 | cont.assign(temp.begin(), temp.end()); |
97 | } |
98 | |
99 | template<class Container1, |
100 | class Container2, |
101 | class BinaryPredicate> |
102 | void test_pred(Container1 cont1, Container2 cont2, |
103 | BinaryPredicate pred) |
104 | { |
105 | typedef BOOST_DEDUCED_TYPENAME Container1::value_type value_t; |
106 | typedef BOOST_DEDUCED_TYPENAME std::vector<value_t>::iterator iterator_t; |
107 | |
108 | sort_container(cont1, pred); |
109 | sort_container(cont2, pred); |
110 | |
111 | std::vector<value_t> reference(cont1.size() + cont2.size()); |
112 | std::vector<value_t> test_cont(reference); |
113 | |
114 | iterator_t reference_result |
115 | = std::set_symmetric_difference(cont1.begin(), cont1.end(), |
116 | cont2.begin(), cont2.end(), |
117 | reference.begin(), |
118 | pred); |
119 | |
120 | iterator_t test_result |
121 | = boost::set_symmetric_difference(cont1, cont2, |
122 | test_cont.begin(), pred); |
123 | |
124 | check_result(reference, reference_result, |
125 | test_cont, test_result); |
126 | |
127 | test_result = boost::set_symmetric_difference( |
128 | boost::make_iterator_range(cont1), cont2, |
129 | test_cont.begin(), pred); |
130 | |
131 | check_result(reference, reference_result, |
132 | test_cont, test_result); |
133 | |
134 | test_result = boost::set_symmetric_difference( |
135 | cont1, boost::make_iterator_range(cont2), |
136 | test_cont.begin(), pred); |
137 | |
138 | check_result(reference, reference_result, |
139 | test_cont, test_result); |
140 | |
141 | test_result = boost::set_symmetric_difference( |
142 | boost::make_iterator_range(cont1), |
143 | boost::make_iterator_range(cont2), |
144 | test_cont.begin(), pred); |
145 | |
146 | check_result(reference, reference_result, |
147 | test_cont, test_result); |
148 | } |
149 | |
150 | template<class Container1, class Container2> |
151 | void test_set_symmetric_difference_impl( |
152 | Container1& cont1, |
153 | Container2& cont2 |
154 | ) |
155 | { |
156 | test(cont1, cont2); |
157 | test_pred(cont1, cont2, std::less<int>()); |
158 | test_pred(cont1, cont2, std::greater<int>()); |
159 | } |
160 | |
161 | template<class Container1, class Container2> |
162 | void test_set_symmetric_difference_impl() |
163 | { |
164 | using namespace boost::assign; |
165 | |
166 | Container1 cont1; |
167 | Container2 cont2; |
168 | |
169 | test_set_symmetric_difference_impl(cont1, cont2); |
170 | |
171 | cont1.clear(); |
172 | cont2.clear(); |
173 | cont1 += 1; |
174 | test_set_symmetric_difference_impl(cont1, cont2); |
175 | |
176 | cont1.clear(); |
177 | cont2.clear(); |
178 | cont2 += 1; |
179 | test_set_symmetric_difference_impl(cont1, cont2); |
180 | |
181 | cont1.clear(); |
182 | cont2.clear(); |
183 | cont1 += 1,2,3,4,5,6,7,8,9; |
184 | cont2 += 2,3,4; |
185 | test_set_symmetric_difference_impl(cont1, cont2); |
186 | |
187 | cont1.clear(); |
188 | cont2.clear(); |
189 | cont1 += 2,3,4; |
190 | cont2 += 1,2,3,4,5,6,7,8,9; |
191 | test_set_symmetric_difference_impl(cont1, cont2); |
192 | } |
193 | |
194 | void test_set_symmetric_difference() |
195 | { |
196 | test_set_symmetric_difference_impl< std::vector<int>, std::vector<int> >(); |
197 | test_set_symmetric_difference_impl< std::list<int>, std::list<int> >(); |
198 | test_set_symmetric_difference_impl< std::deque<int>, std::deque<int> >(); |
199 | test_set_symmetric_difference_impl< std::vector<int>, std::list<int> >(); |
200 | test_set_symmetric_difference_impl< std::list<int>, std::vector<int> >(); |
201 | } |
202 | } |
203 | } |
204 | |
205 | |
206 | boost::unit_test::test_suite* |
207 | init_unit_test_suite(int argc, char* argv[]) |
208 | { |
209 | boost::unit_test::test_suite* test |
210 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.set_symmetric_difference" ); |
211 | |
212 | test->add( BOOST_TEST_CASE( &boost::test_set_symmetric_difference ) ); |
213 | |
214 | return test; |
215 | } |
216 | |