]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/range/test/algorithm_test/partition.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / range / test / algorithm_test / partition.cpp
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/partition.hpp>
10
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/assign.hpp>
15 #include <boost/bind.hpp>
16 #include "../test_driver/range_return_test_driver.hpp"
17 #include <algorithm>
18 #include <functional>
19 #include <list>
20 #include <numeric>
21 #include <deque>
22 #include <vector>
23
24 namespace boost_range_test_algorithm_partition
25 {
26 struct equal_to_5
27 {
28 typedef bool result_type;
29 typedef int argument_type;
30 bool operator()(int x) const { return x == 5; }
31 };
32
33 // test the 'partition' algorithm
34 template<class UnaryPredicate>
35 class partition_test_policy
36 {
37 public:
38 template< class Container >
39 BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
40 test_iter(Container& cont)
41 {
42 typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t;
43
44 const Container old_cont(cont);
45 Container cont2(old_cont);
46 iter_t result = boost::partition(cont, UnaryPredicate());
47
48 boost::partition(cont2, UnaryPredicate());
49 cont2 = old_cont;
50 boost::partition(
51 boost::make_iterator_range(cont2), UnaryPredicate());
52
53 BOOST_CHECK_EQUAL_COLLECTIONS( cont.begin(), cont.end(),
54 cont2.begin(), cont2.end() );
55
56 return result;
57 }
58
59 UnaryPredicate pred() const { return UnaryPredicate(); }
60
61 template< boost::range_return_value return_type >
62 struct test_range
63 {
64 template< class Container, class Policy >
65 BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type
66 operator()(Policy& policy, Container& cont)
67 {
68 typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t;
69
70 const Container old_cont(cont);
71 Container cont2(old_cont);
72 result_t result = boost::partition<return_type>(cont, policy.pred());
73
74 // Test that operation a temporary created by using
75 // make_iterator_range.
76 boost::partition<return_type>(
77 boost::make_iterator_range(cont2), policy.pred());
78
79 BOOST_CHECK_EQUAL_COLLECTIONS( cont.begin(), cont.end(),
80 cont2.begin(), cont2.end() );
81
82 return result;
83 }
84 };
85
86 template< class Container >
87 BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
88 reference(Container& cont)
89 {
90 return std::partition(cont.begin(), cont.end(), UnaryPredicate());
91 }
92 };
93
94 template<class Container>
95 void test_partition_impl()
96 {
97 using namespace boost::assign;
98
99 boost::range_test::range_return_test_driver test_driver;
100
101 partition_test_policy< equal_to_5 > policy;
102
103 Container cont;
104 test_driver(cont, policy);
105
106 cont.clear();
107 cont += 1;
108 test_driver(cont, policy);
109
110 cont.clear();
111 cont += 1,2,2,2,2,2,3,4,5,6,7,8,9;
112 test_driver(cont, policy);
113
114 cont.clear();
115 cont += 1,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,5,6,7,8,9;
116 test_driver(cont, policy);
117 }
118
119 void test_partition()
120 {
121 test_partition_impl< std::vector<int> >();
122 test_partition_impl< std::list<int> >();
123 test_partition_impl< std::deque<int> >();
124 }
125 }
126
127 boost::unit_test::test_suite*
128 init_unit_test_suite(int argc, char* argv[])
129 {
130 boost::unit_test::test_suite* test
131 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.partition" );
132
133 test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_partition::test_partition ) );
134
135 return test;
136 }