~ubuntu-branches/ubuntu/wily/bombono-dvd/wily

« back to all changes in this revision

Viewing changes to src/mlib/tests/range/adaptor_test/filtered.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-11-04 11:46:25 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20101104114625-8xfdhvhpsm51i0nu
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <mlib/tests/_pc_.h>
 
12
 
 
13
#include <mlib/range/adaptor/filtered.hpp>
 
14
#include <mlib/range/algorithm_ext.hpp>
 
15
 
 
16
#include <boost/assign.hpp>
 
17
 
 
18
#include <algorithm>
 
19
#include <list>
 
20
#include <set>
 
21
#include <string>
 
22
#include <vector>
 
23
 
 
24
namespace boost
 
25
{
 
26
    namespace
 
27
    {
 
28
        struct always_false_pred
 
29
        {
 
30
            template< class T1 >
 
31
            bool operator()(T1) const { return false; }
 
32
        };
 
33
 
 
34
        struct always_true_pred
 
35
        {
 
36
            template< class T1 >
 
37
            bool operator()(T1) const { return true; }
 
38
        };
 
39
 
 
40
        struct is_even
 
41
        {
 
42
            template< class IntegerT >
 
43
            bool operator()( IntegerT x ) const { return x % 2 == 0; }
 
44
        };
 
45
 
 
46
        struct is_odd
 
47
        {
 
48
            template< class IntegerT >
 
49
            bool operator()( IntegerT x ) const { return x % 2 != 0; }
 
50
        };
 
51
 
 
52
        template< class Container, class Pred >
 
53
        void filtered_test_impl( Container& c, Pred pred )
 
54
        {
 
55
            using namespace boost::adaptors;
 
56
 
 
57
            typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
 
58
 
 
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));
 
62
 
 
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));
 
66
 
 
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)
 
71
            {
 
72
                if (pred(*it))
 
73
                    reference_result.push_back(*it);
 
74
            }
 
75
 
 
76
            BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
 
77
                                           reference_result.end(),
 
78
                                           test_result1.begin(),
 
79
                                           test_result1.end() );
 
80
 
 
81
            BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
 
82
                                           reference_result.end(),
 
83
                                           test_result2.begin(),
 
84
                                           test_result2.end() );
 
85
        }
 
86
 
 
87
        template< class Container, class Pred >
 
88
        void filtered_test_impl()
 
89
        {
 
90
            using namespace boost::assign;
 
91
 
 
92
            Container c;
 
93
 
 
94
            // test empty container
 
95
            filtered_test_impl(c, Pred());
 
96
 
 
97
            // test one element
 
98
            c += 1;
 
99
            filtered_test_impl(c, Pred());
 
100
 
 
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());
 
104
        }
 
105
 
 
106
        template< class Container >
 
107
        void filtered_test_all_predicates()
 
108
        {
 
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 >();
 
113
        }
 
114
 
 
115
        void filtered_test()
 
116
        {
 
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 > >();
 
121
        }
 
122
    }
 
123
}
 
124
 
 
125
BOOST_AUTO_TEST_CASE( test_range_filtered_test )
 
126
{
 
127
    boost::filtered_test();
 
128
}
 
129