File: mismatch.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (242 lines) | stat: -rw-r--r-- 11,502 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Boost.Range library
//
//  Copyright Neil Groves 2009. Use, modification and
//  distribution is subject to the Boost Software License, Version
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
//
// For more information, see http://www.boost.org/libs/range/
//
#include <boost/range/algorithm/mismatch.hpp>

#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>

#include <boost/assign.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <vector>

namespace boost
{
    namespace
    {
        template< class Container1, class Container2 >
        void eval_mismatch(Container1& cont1,
                           Container2& cont2,
                           BOOST_DEDUCED_TYPENAME range_iterator<Container1>::type ref_it1,
                           BOOST_DEDUCED_TYPENAME range_iterator<Container2>::type ref_it2
                           )
        {
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container1>::type iter1_t;
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container2>::type iter2_t;
            typedef std::pair<iter1_t, iter2_t> result_pair_t;
            
            result_pair_t result = boost::mismatch(cont1, cont2);
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
            
            result = boost::mismatch(boost::make_iterator_range(cont1),
                                     cont2);
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
            
            result = boost::mismatch(cont1,
                                     boost::make_iterator_range(cont2));
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
            
            result = boost::mismatch(boost::make_iterator_range(cont1),
                                     boost::make_iterator_range(cont2));
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
        }
        
        template< class Container1, class Container2, class Pred >
        void eval_mismatch(Container1& cont1,
                           Container2& cont2,
                           Pred pred,
                           BOOST_DEDUCED_TYPENAME range_iterator<Container1>::type ref_it1,
                           BOOST_DEDUCED_TYPENAME range_iterator<Container2>::type ref_it2
                           )
        {
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container1>::type iter1_t;
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container2>::type iter2_t;
            typedef std::pair<iter1_t, iter2_t> result_pair_t;
            
            result_pair_t result = boost::mismatch(cont1, cont2, pred);
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
            
            result = boost::mismatch(boost::make_iterator_range(cont1),
                                     cont2, pred);
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
            
            result = boost::mismatch(cont1,
                                     boost::make_iterator_range(cont2), pred);
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
            
            result = boost::mismatch(boost::make_iterator_range(cont1),
                                     boost::make_iterator_range(cont2),
                                     pred);
            BOOST_CHECK( result.first == ref_it1 );
            BOOST_CHECK( result.second == ref_it2 );
        }
        
        template< class Container1, class Container2 >
        void eval_mismatch(Container1& cont1,
                           Container2& cont2,
                           const int ref1,
                           const int ref2)
        {
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container1>::type iter1_t;
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container2>::type iter2_t;
            typedef std::pair<iter1_t, iter2_t> result_pair_t;
            
            result_pair_t result = boost::mismatch(cont1, cont2);
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
            
            result = boost::mismatch(boost::make_iterator_range(cont1), cont2);
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
            
            result = boost::mismatch(cont1, boost::make_iterator_range(cont2));
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
            
            result = boost::mismatch(boost::make_iterator_range(cont1),
                                     boost::make_iterator_range(cont2));
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
        }
        
        template< class Container1, class Container2, class Pred >
        void eval_mismatch(Container1& cont1,
                           Container2& cont2,
                           Pred pred,
                           const int ref1,
                           const int ref2)
        {
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container1>::type iter1_t;
            typedef BOOST_DEDUCED_TYPENAME range_iterator<Container2>::type iter2_t;
            typedef std::pair<iter1_t, iter2_t> result_pair_t;
            
            result_pair_t result = boost::mismatch(cont1, cont2, pred);
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
            
            result = boost::mismatch(boost::make_iterator_range(cont1),
                                     cont2, pred);
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
            
            result = boost::mismatch(cont1, boost::make_iterator_range(cont2),
                                     pred);
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
            
            result = boost::mismatch(boost::make_iterator_range(cont1),
                                     boost::make_iterator_range(cont2),
                                     pred);
            BOOST_CHECK_EQUAL( ref1, *result.first );
            BOOST_CHECK_EQUAL( ref2, *result.second );
        }
        
        template< class Container1, class Container2 >
        void test_mismatch_impl()
        {
            using namespace boost::assign;

            typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container1>::type MutableContainer1;
            typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container2>::type MutableContainer2;

            MutableContainer1 cont1;
            const Container1& cref_cont1 = cont1;
            MutableContainer2 cont2;
            const Container2& cref_cont2 = cont2;

            typedef BOOST_DEDUCED_TYPENAME Container1::iterator
                                        iterator1_t BOOST_RANGE_UNUSED;

            typedef BOOST_DEDUCED_TYPENAME Container1::const_iterator
                                        const_iterator1_t BOOST_RANGE_UNUSED;

            typedef BOOST_DEDUCED_TYPENAME Container2::iterator
                                        iterator2_t BOOST_RANGE_UNUSED;

            typedef BOOST_DEDUCED_TYPENAME Container2::const_iterator
                                        const_iterator2_t BOOST_RANGE_UNUSED;

            eval_mismatch(cont1, cont2, cont1.end(), cont2.end());
            eval_mismatch(cont1, cont2, std::equal_to<int>(), cont1.end(), cont2.end());
            eval_mismatch(cref_cont1, cont2, cref_cont1.end(), cont2.end());
            eval_mismatch(cref_cont1, cont2, std::equal_to<int>(), cref_cont1.end(), cont2.end());
            eval_mismatch(cont1, cref_cont2, cont1.end(), cref_cont2.end());
            eval_mismatch(cont1, cref_cont2, std::equal_to<int>(), cont1.end(), cref_cont2.end());
            eval_mismatch(cref_cont1, cref_cont2, cref_cont1.end(), cref_cont2.end());
            eval_mismatch(cref_cont1, cref_cont2, std::equal_to<int>(), cref_cont1.end(), cref_cont2.end());

            cont1 += 1,2,3,4;
            cont2 += 1,2,3,4;
            eval_mismatch(cont1, cont2, cont1.end(), cont2.end());
            eval_mismatch(cont1, cont2, std::equal_to<int>(), cont1.end(), cont2.end());
            eval_mismatch(cref_cont1, cont2, cref_cont1.end(), cont2.end());
            eval_mismatch(cref_cont1, cont2, std::equal_to<int>(), cref_cont1.end(), cont2.end());
            eval_mismatch(cont1, cref_cont2, cont1.end(), cref_cont2.end());
            eval_mismatch(cont1, cref_cont2, std::equal_to<int>(), cont1.end(), cref_cont2.end());
            eval_mismatch(cref_cont1, cref_cont2, cref_cont1.end(), cref_cont2.end());
            eval_mismatch(cref_cont1, cref_cont2, std::equal_to<int>(), cref_cont1.end(), cref_cont2.end());

            cont1.clear();
            cont2.clear();
            cont1 += 1,2,3,4;
            cont2 += 1,2,5,4;
            eval_mismatch(cont1, cont2, 3, 5);
            eval_mismatch(cont1, cont2, std::equal_to<int>(), 3, 5);
            eval_mismatch(cont1, cont2, std::not_equal_to<int>(), cont1.begin(), cont2.begin());
            eval_mismatch(cref_cont1, cont2, 3, 5);
            eval_mismatch(cref_cont1, cont2, std::equal_to<int>(), 3, 5);
            eval_mismatch(cref_cont1, cont2, std::not_equal_to<int>(), cref_cont1.begin(), cont2.begin());
            eval_mismatch(cont1, cref_cont2, 3, 5);
            eval_mismatch(cont1, cref_cont2, std::equal_to<int>(), 3, 5);
            eval_mismatch(cont1, cref_cont2, std::not_equal_to<int>(), cont1.begin(), cref_cont2.begin());
            eval_mismatch(cref_cont1, cref_cont2, 3, 5);
            eval_mismatch(cref_cont1, cref_cont2, std::equal_to<int>(), 3, 5);
            eval_mismatch(cref_cont1, cref_cont2, std::not_equal_to<int>(), cref_cont1.begin(), cref_cont2.begin());
        }

        void test_mismatch()
        {
            test_mismatch_impl< std::list<int>, std::list<int> >();
            test_mismatch_impl< const std::list<int>, std::list<int> >();
            test_mismatch_impl< std::list<int>, const std::list<int> >();
            test_mismatch_impl< const std::list<int>, const std::list<int> >();

            test_mismatch_impl< std::vector<int>, std::list<int> >();
            test_mismatch_impl< const std::vector<int>, std::list<int> >();
            test_mismatch_impl< std::vector<int>, const std::list<int> >();
            test_mismatch_impl< const std::vector<int>, const std::list<int> >();

            test_mismatch_impl< std::list<int>, std::vector<int> >();
            test_mismatch_impl< const std::list<int>, std::vector<int> >();
            test_mismatch_impl< std::list<int>, const std::vector<int> >();
            test_mismatch_impl< const std::list<int>, const std::vector<int> >();
        }
    }
}

boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
    boost::unit_test::test_suite* test
        = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.mismatch" );

    test->add( BOOST_TEST_CASE( &boost::test_mismatch ) );

    return test;
}