]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/range/test/adaptor_test/filtered.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / range / test / adaptor_test / filtered.cpp
1 // Boost.Range library
2 //
3 // Copyright Neil Groves 2009. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 //
9 // For more information, see http://www.boost.org/libs/range/
10 //
11 #include <boost/range/adaptor/filtered.hpp>
12
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15
16 #include <boost/assign.hpp>
17 #include <boost/range/algorithm_ext.hpp>
18
19 #include <algorithm>
20 #include <list>
21 #include <set>
22 #include <string>
23 #include <vector>
24 #include <sstream>
25
26 namespace boost
27 {
28 namespace
29 {
30 struct always_false_pred
31 {
32 template< class T1 >
33 bool operator()(T1) const { return false; }
34 };
35
36 struct always_true_pred
37 {
38 template< class T1 >
39 bool operator()(T1) const { return true; }
40 };
41
42 struct is_even
43 {
44 template< class IntegerT >
45 bool operator()( IntegerT x ) const { return x % 2 == 0; }
46 };
47
48 struct is_odd
49 {
50 template< class IntegerT >
51 bool operator()( IntegerT x ) const { return x % 2 != 0; }
52 };
53
54 template< class Container, class Pred >
55 void filtered_test_impl( Container& c, Pred pred )
56 {
57 using namespace boost::adaptors;
58
59 typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
60
61 // This is my preferred syntax using the | operator.
62 std::vector< value_t > test_result1;
63 boost::push_back(test_result1, c | filtered(pred));
64
65 // This is an alternative syntax preferred by some.
66 std::vector< value_t > test_result2;
67 boost::push_back(test_result2, adaptors::filter(c, pred));
68
69 // Calculate the reference result.
70 std::vector< value_t > reference_result;
71 typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
72 for (iter_t it = c.begin(); it != c.end(); ++it)
73 {
74 if (pred(*it))
75 reference_result.push_back(*it);
76 }
77
78 BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
79 reference_result.end(),
80 test_result1.begin(),
81 test_result1.end() );
82
83 BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
84 reference_result.end(),
85 test_result2.begin(),
86 test_result2.end() );
87 }
88
89 template< class Container, class Pred >
90 void filtered_test_impl()
91 {
92 using namespace boost::assign;
93
94 Container c;
95
96 // test empty container
97 filtered_test_impl(c, Pred());
98
99 // test one element
100 c += 1;
101 filtered_test_impl(c, Pred());
102
103 // test many elements
104 c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
105 filtered_test_impl(c, Pred());
106 }
107
108 template< class Container >
109 void filtered_test_all_predicates()
110 {
111 filtered_test_impl< Container, always_false_pred >();
112 filtered_test_impl< Container, always_true_pred >();
113 filtered_test_impl< Container, is_odd >();
114 filtered_test_impl< Container, is_even >();
115 }
116
117 void ticket_10988_single_pass()
118 {
119 std::vector<int> v;
120 std::string str("0 1 2 3 4 5");
121 std::istringstream in(str);
122
123 boost::push_back(v,
124 boost::make_iterator_range(
125 std::istream_iterator<int>(in),
126 std::istream_iterator<int>())
127 | boost::adaptors::filtered(is_even()));
128
129 std::vector<int> reference;
130 for (int i = 0; i < 6; i += 2)
131 {
132 reference.push_back(i);
133 }
134 BOOST_CHECK_EQUAL_COLLECTIONS(
135 reference.begin(), reference.end(),
136 v.begin(), v.end());
137 }
138
139 void filtered_test()
140 {
141 filtered_test_all_predicates< std::vector< int > >();
142 filtered_test_all_predicates< std::list< int > >();
143 filtered_test_all_predicates< std::set< int > >();
144 filtered_test_all_predicates< std::multiset< int > >();
145 ticket_10988_single_pass();
146 }
147 }
148 }
149
150 boost::unit_test::test_suite*
151 init_unit_test_suite(int argc, char* argv[])
152 {
153 boost::unit_test::test_suite* test
154 = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.filtered" );
155
156 test->add( BOOST_TEST_CASE( &boost::filtered_test ) );
157
158 return test;
159 }