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)
7 // For more information, see http://www.boost.org/libs/range/
9 #include <boost/range/algorithm/set_algorithm.hpp>
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
14 #include <boost/assign.hpp>
15 #include <boost/bind.hpp>
27 template<class Container1
, class Iterator
, class Container2
>
29 Container1
& reference
,
30 Iterator reference_result
,
31 Container2
& test_cont
,
36 std::distance
<Iterator
>(reference
.begin(), reference_result
),
37 std::distance
<Iterator
>(test_cont
.begin(), test_result
)
40 BOOST_CHECK_EQUAL_COLLECTIONS(
41 reference
.begin(), reference
.end(),
42 test_cont
.begin(), test_cont
.end()
46 template<class Container1
, class Container2
>
47 void test(Container1
& cont1
, Container2
& cont2
)
49 typedef BOOST_DEDUCED_TYPENAME
Container1::value_type value_t
;
50 typedef BOOST_DEDUCED_TYPENAME
std::vector
<value_t
>::iterator iterator_t
;
52 std::vector
<value_t
> reference(cont1
.size() + cont2
.size());
53 std::vector
<value_t
> test_cont(reference
);
55 iterator_t reference_result
56 = std::set_difference(cont1
.begin(), cont1
.end(),
57 cont2
.begin(), cont2
.end(),
60 iterator_t test_result
61 = boost::set_difference(cont1
, cont2
, test_cont
.begin());
63 check_result(reference
, reference_result
,
64 test_cont
, test_result
);
66 test_result
= boost::set_difference(
67 boost::make_iterator_range(cont1
), cont2
,
70 check_result(reference
, reference_result
,
71 test_cont
, test_result
);
73 test_result
= boost::set_difference(
74 cont1
, boost::make_iterator_range(cont2
),
77 check_result(reference
, reference_result
,
78 test_cont
, test_result
);
80 test_result
= boost::set_difference(
81 boost::make_iterator_range(cont1
),
82 boost::make_iterator_range(cont2
),
85 check_result(reference
, reference_result
,
86 test_cont
, test_result
);
89 template<class Container
, class BinaryPredicate
>
90 void sort_container(Container
& cont
, BinaryPredicate pred
)
92 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
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());
99 template<class Container1
,
101 class BinaryPredicate
>
102 void test_pred(Container1 cont1
, Container2 cont2
,
103 BinaryPredicate pred
)
105 typedef BOOST_DEDUCED_TYPENAME
Container1::value_type value_t
;
106 typedef BOOST_DEDUCED_TYPENAME
std::vector
<value_t
>::iterator iterator_t
;
108 sort_container(cont1
, pred
);
109 sort_container(cont2
, pred
);
111 std::vector
<value_t
> reference(cont1
.size() + cont2
.size());
112 std::vector
<value_t
> test_cont(reference
);
114 iterator_t reference_result
115 = std::set_difference(cont1
.begin(), cont1
.end(),
116 cont2
.begin(), cont2
.end(),
120 iterator_t test_result
121 = boost::set_difference(cont1
, cont2
, test_cont
.begin(), pred
);
123 check_result(reference
, reference_result
,
124 test_cont
, test_result
);
126 test_result
= boost::set_difference(
127 boost::make_iterator_range(cont1
), cont2
,
128 test_cont
.begin(), pred
);
130 check_result(reference
, reference_result
,
131 test_cont
, test_result
);
133 test_result
= boost::set_difference(
134 cont1
, boost::make_iterator_range(cont2
),
135 test_cont
.begin(), pred
);
137 check_result(reference
, reference_result
,
138 test_cont
, test_result
);
140 test_result
= boost::set_difference(
141 boost::make_iterator_range(cont1
),
142 boost::make_iterator_range(cont2
),
143 test_cont
.begin(), pred
);
145 check_result(reference
, reference_result
,
146 test_cont
, test_result
);
149 template<class Container1
, class Container2
>
150 void test_set_difference_impl(
156 test_pred(cont1
, cont2
, std::less
<int>());
157 test_pred(cont1
, cont2
, std::greater
<int>());
160 template<class Container1
, class Container2
>
161 void test_set_difference_impl()
163 using namespace boost::assign
;
168 test_set_difference_impl(cont1
, cont2
);
173 test_set_difference_impl(cont1
, cont2
);
178 test_set_difference_impl(cont1
, cont2
);
182 cont1
+= 1,2,3,4,5,6,7,8,9;
184 test_set_difference_impl(cont1
, cont2
);
189 cont2
+= 1,2,3,4,5,6,7,8,9;
190 test_set_difference_impl(cont1
, cont2
);
193 void test_set_difference()
195 test_set_difference_impl
< std::vector
<int>, std::vector
<int> >();
196 test_set_difference_impl
< std::list
<int>, std::list
<int> >();
197 test_set_difference_impl
< std::deque
<int>, std::deque
<int> >();
198 test_set_difference_impl
< std::vector
<int>, std::list
<int> >();
199 test_set_difference_impl
< std::list
<int>, std::vector
<int> >();
205 boost::unit_test::test_suite
*
206 init_unit_test_suite(int argc
, char* argv
[])
208 boost::unit_test::test_suite
* test
209 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.set_difference" );
211 test
->add( BOOST_TEST_CASE( &boost::test_set_difference
) );