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>
26 template<class Container1
, class Iterator
, class Container2
>
28 Container1
& reference
,
29 Iterator reference_result
,
30 Container2
& test_cont
,
35 std::distance
<Iterator
>(reference
.begin(), reference_result
),
36 std::distance
<Iterator
>(test_cont
.begin(), test_result
)
39 BOOST_CHECK_EQUAL_COLLECTIONS(
40 reference
.begin(), reference
.end(),
41 test_cont
.begin(), test_cont
.end()
45 template<class Container1
, class Container2
>
46 void test(Container1
& cont1
, Container2
& cont2
)
48 typedef BOOST_DEDUCED_TYPENAME
Container1::value_type value_t
;
49 typedef BOOST_DEDUCED_TYPENAME
std::vector
<value_t
>::iterator iterator_t
;
51 std::vector
<value_t
> reference(cont1
.size() + cont2
.size());
52 std::vector
<value_t
> test_cont(reference
);
54 iterator_t reference_result
55 = std::set_union(cont1
.begin(), cont1
.end(),
56 cont2
.begin(), cont2
.end(),
59 iterator_t test_result
60 = boost::set_union(cont1
, cont2
, test_cont
.begin());
62 check_result(reference
, reference_result
,
63 test_cont
, test_result
);
65 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
66 cont2
, test_cont
.begin());
68 check_result(reference
, reference_result
,
69 test_cont
, test_result
);
71 test_result
= boost::set_union(cont1
,
72 boost::make_iterator_range(cont2
),
75 check_result(reference
, reference_result
,
76 test_cont
, test_result
);
78 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
79 boost::make_iterator_range(cont2
),
82 check_result(reference
, reference_result
,
83 test_cont
, test_result
);
86 template<class Container
, class BinaryPredicate
>
87 void sort_container(Container
& cont
, BinaryPredicate pred
)
89 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
91 std::vector
<value_t
> temp(cont
.begin(), cont
.end());
92 std::sort(temp
.begin(), temp
.end(), pred
);
93 cont
.assign(temp
.begin(), temp
.end());
96 template<class Container1
,
98 class BinaryPredicate
>
99 void test_pred(Container1 cont1
, Container2 cont2
,
100 BinaryPredicate pred
)
102 typedef BOOST_DEDUCED_TYPENAME
Container1::value_type value_t
;
103 typedef BOOST_DEDUCED_TYPENAME
std::vector
<value_t
>::iterator iterator_t
;
105 sort_container(cont1
, pred
);
106 sort_container(cont2
, pred
);
108 std::vector
<value_t
> reference(cont1
.size() + cont2
.size());
109 std::vector
<value_t
> test_cont(reference
);
111 iterator_t reference_result
112 = std::set_union(cont1
.begin(), cont1
.end(),
113 cont2
.begin(), cont2
.end(),
117 iterator_t test_result
118 = boost::set_union(cont1
, cont2
, test_cont
.begin(), pred
);
120 check_result(reference
, reference_result
,
121 test_cont
, test_result
);
123 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
124 cont2
, test_cont
.begin(), pred
);
126 check_result(reference
, reference_result
,
127 test_cont
, test_result
);
129 test_result
= boost::set_union(cont1
,
130 boost::make_iterator_range(cont2
),
131 test_cont
.begin(), pred
);
133 check_result(reference
, reference_result
,
134 test_cont
, test_result
);
136 test_result
= boost::set_union(boost::make_iterator_range(cont1
),
137 boost::make_iterator_range(cont2
),
138 test_cont
.begin(), pred
);
140 check_result(reference
, reference_result
,
141 test_cont
, test_result
);
144 template<class Container1
, class Container2
>
145 void test_set_union_impl(
151 test_pred(cont1
, cont2
, std::less
<int>());
152 test_pred(cont1
, cont2
, std::greater
<int>());
155 template<class Container1
, class Container2
>
156 void test_set_union_impl()
158 using namespace boost::assign
;
163 test_set_union_impl(cont1
, cont2
);
168 test_set_union_impl(cont1
, cont2
);
173 test_set_union_impl(cont1
, cont2
);
177 cont1
+= 1,2,3,4,5,6,7,8,9;
179 test_set_union_impl(cont1
, cont2
);
184 cont2
+= 1,2,3,4,5,6,7,8,9;
185 test_set_union_impl(cont1
, cont2
);
188 void test_set_union()
190 test_set_union_impl
< std::vector
<int>, std::vector
<int> >();
191 test_set_union_impl
< std::list
<int>, std::list
<int> >();
192 test_set_union_impl
< std::deque
<int>, std::deque
<int> >();
193 test_set_union_impl
< std::vector
<int>, std::list
<int> >();
194 test_set_union_impl
< std::list
<int>, std::vector
<int> >();
200 boost::unit_test::test_suite
*
201 init_unit_test_suite(int argc
, char* argv
[])
203 boost::unit_test::test_suite
* test
204 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.set_union" );
206 test
->add( BOOST_TEST_CASE( &boost::test_set_union
) );