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)
9
// For more information, see http://www.boost.org/libs/range/
11
#include <mlib/tests/_pc_.h>
13
#include <mlib/range/adaptor/filtered.hpp>
14
#include <mlib/range/algorithm_ext.hpp>
16
#include <boost/assign.hpp>
28
struct always_false_pred
31
bool operator()(T1) const { return false; }
34
struct always_true_pred
37
bool operator()(T1) const { return true; }
42
template< class IntegerT >
43
bool operator()( IntegerT x ) const { return x % 2 == 0; }
48
template< class IntegerT >
49
bool operator()( IntegerT x ) const { return x % 2 != 0; }
52
template< class Container, class Pred >
53
void filtered_test_impl( Container& c, Pred pred )
55
using namespace boost::adaptors;
57
typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
59
// This is my preferred syntax using the | operator.
60
std::vector< value_t > test_result1;
61
boost::push_back(test_result1, c | filtered(pred));
63
// This is an alternative syntax preferred by some.
64
std::vector< value_t > test_result2;
65
boost::push_back(test_result2, adaptors::filter(c, pred));
67
// Calculate the reference result.
68
std::vector< value_t > reference_result;
69
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
70
for (iter_t it = c.begin(); it != c.end(); ++it)
73
reference_result.push_back(*it);
76
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
77
reference_result.end(),
81
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
82
reference_result.end(),
87
template< class Container, class Pred >
88
void filtered_test_impl()
90
using namespace boost::assign;
94
// test empty container
95
filtered_test_impl(c, Pred());
99
filtered_test_impl(c, Pred());
101
// test many elements
102
c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
103
filtered_test_impl(c, Pred());
106
template< class Container >
107
void filtered_test_all_predicates()
109
filtered_test_impl< Container, always_false_pred >();
110
filtered_test_impl< Container, always_true_pred >();
111
filtered_test_impl< Container, is_odd >();
112
filtered_test_impl< Container, is_even >();
117
filtered_test_all_predicates< std::vector< int > >();
118
filtered_test_all_predicates< std::list< int > >();
119
filtered_test_all_predicates< std::set< int > >();
120
filtered_test_all_predicates< std::multiset< int > >();
125
BOOST_AUTO_TEST_CASE( test_range_filtered_test )
127
boost::filtered_test();