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_union(cont1
.begin(), cont1
.end(),
57 cont2
.begin(), cont2
.end(),
60 iterator_t test_result
61 = boost::set_union(cont1
, cont2
, test_cont
.begin());
63 check_result(reference
, reference_result
,
64 test_cont
, test_result
);
66 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
67 cont2
, test_cont
.begin());
69 check_result(reference
, reference_result
,
70 test_cont
, test_result
);
72 test_result
= boost::set_union(cont1
,
73 boost::make_iterator_range(cont2
),
76 check_result(reference
, reference_result
,
77 test_cont
, test_result
);
79 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
80 boost::make_iterator_range(cont2
),
83 check_result(reference
, reference_result
,
84 test_cont
, test_result
);
87 template<class Container
, class BinaryPredicate
>
88 void sort_container(Container
& cont
, BinaryPredicate pred
)
90 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
92 std::vector
<value_t
> temp(cont
.begin(), cont
.end());
93 std::sort(temp
.begin(), temp
.end(), pred
);
94 cont
.assign(temp
.begin(), temp
.end());
97 template<class Container1
,
99 class BinaryPredicate
>
100 void test_pred(Container1 cont1
, Container2 cont2
,
101 BinaryPredicate pred
)
103 typedef BOOST_DEDUCED_TYPENAME
Container1::value_type value_t
;
104 typedef BOOST_DEDUCED_TYPENAME
std::vector
<value_t
>::iterator iterator_t
;
106 sort_container(cont1
, pred
);
107 sort_container(cont2
, pred
);
109 std::vector
<value_t
> reference(cont1
.size() + cont2
.size());
110 std::vector
<value_t
> test_cont(reference
);
112 iterator_t reference_result
113 = std::set_union(cont1
.begin(), cont1
.end(),
114 cont2
.begin(), cont2
.end(),
118 iterator_t test_result
119 = boost::set_union(cont1
, cont2
, test_cont
.begin(), pred
);
121 check_result(reference
, reference_result
,
122 test_cont
, test_result
);
124 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
125 cont2
, test_cont
.begin(), pred
);
127 check_result(reference
, reference_result
,
128 test_cont
, test_result
);
130 test_result
= boost::set_union(cont1
,
131 boost::make_iterator_range(cont2
),
132 test_cont
.begin(), pred
);
134 check_result(reference
, reference_result
,
135 test_cont
, test_result
);
137 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
138 boost::make_iterator_range(cont2
),
139 test_cont
.begin(), pred
);
141 check_result(reference
, reference_result
,
142 test_cont
, test_result
);
145 template<class Container1
, class Container2
>
146 void test_set_union_impl(
152 test_pred(cont1
, cont2
, std::less
<int>());
153 test_pred(cont1
, cont2
, std::greater
<int>());
156 template<class Container1
, class Container2
>
157 void test_set_union_impl()
159 using namespace boost::assign
;
164 test_set_union_impl(cont1
, cont2
);
169 test_set_union_impl(cont1
, cont2
);
174 test_set_union_impl(cont1
, cont2
);
178 cont1
+= 1,2,3,4,5,6,7,8,9;
180 test_set_union_impl(cont1
, cont2
);
185 cont2
+= 1,2,3,4,5,6,7,8,9;
186 test_set_union_impl(cont1
, cont2
);
189 void test_set_union()
191 test_set_union_impl
< std::vector
<int>, std::vector
<int> >();
192 test_set_union_impl
< std::list
<int>, std::list
<int> >();
193 test_set_union_impl
< std::deque
<int>, std::deque
<int> >();
194 test_set_union_impl
< std::vector
<int>, std::list
<int> >();
195 test_set_union_impl
< std::list
<int>, std::vector
<int> >();
201 boost::unit_test::test_suite
*
202 init_unit_test_suite(int argc
, char* argv
[])
204 boost::unit_test::test_suite
* test
205 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.set_union" );
207 test
->add( BOOST_TEST_CASE( &boost::test_set_union
) );