File: strided.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (313 lines) | stat: -rw-r--r-- 10,733 bytes parent folder | download | duplicates (11)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 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/
//
// The strided_defect_Trac5014 test case is a modified version of a test case
// contributed by Maxim Yanchenko as part of the trac ticket.
//
// The deque test case has been removed due to erroneous standard library
// implementations causing test failures.
//
#include <boost/range/adaptor/strided.hpp>

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

#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>

#include <algorithm>
#include <vector>

namespace boost
{
    namespace
    {
        template< class Container >
        void strided_test_impl( Container& c, int stride_size )
        {
            using namespace boost::adaptors;

            // Rationale:
            // This requirement was too restrictive. It makes the use of the
            // strided adaptor too dangerous, and a simple solution existed
            // to make it safe, hence the strided adaptor has been modified
            // and this restriction no longer applies.
            //BOOST_ASSERT( c.size() % STRIDE_SIZE == 0 );

            Container reference;

            {
                typedef BOOST_DEDUCED_TYPENAME Container::const_iterator
                            iterator_t BOOST_RANGE_UNUSED;
                typedef BOOST_DEDUCED_TYPENAME Container::difference_type
                            diff_t BOOST_RANGE_UNUSED;
                typedef BOOST_DEDUCED_TYPENAME Container::size_type
                            size_type BOOST_RANGE_UNUSED;
                iterator_t it = c.begin();

                iterator_t last = c.end();
                for (; it != last; )
                {
                    reference.push_back(*it);

                    for (int i = 0; (it != last) && (i < stride_size); ++i)
                        ++it;
                }
            }

            Container test;
            boost::push_back( test, c | strided(stride_size) );

            BOOST_CHECK_EQUAL_COLLECTIONS( test.begin(), test.end(),
                reference.begin(), reference.end() );

            Container test2;
            boost::push_back( test2, adaptors::stride(c, stride_size) );

            BOOST_CHECK_EQUAL_COLLECTIONS( test2.begin(), test2.end(),
                reference.begin(), reference.end() );

            // Test the const versions:
            const Container& cc = c;
            Container test3;
            boost::push_back( test3, cc | strided(stride_size) );

            BOOST_CHECK_EQUAL_COLLECTIONS( test3.begin(), test3.end(),
                reference.begin(), reference.end() );

            Container test4;
            boost::push_back( test4, adaptors::stride(cc, stride_size) );

            BOOST_CHECK_EQUAL_COLLECTIONS( test4.begin(), test4.end(),
                reference.begin(), reference.end() );
        }

        template< class Container >
        void strided_test_impl(int stride_size)
        {
            using namespace boost::assign;

            Container c;

            // Test empty
            strided_test_impl(c, stride_size);

            // Test two elements
            c += 1,2;
            strided_test_impl(c, stride_size);

            // Test many elements
            c += 1,1,1,2,2,3,4,5,6,6,6,7,8,9;
            strided_test_impl(c, stride_size);

            // Test an odd number of elements to determine that the relaxation
            // of the requirements has been successful
            // Test a sequence of length 1 with a stride of 2
            c.clear();
            c += 1;
            strided_test_impl(c, stride_size);

            // Test a sequence of length 2 with a stride of 2
            c.clear();
            c += 1,2;
            strided_test_impl(c, stride_size);

            // Test a sequence of length 3 with a stride of 2
            c.clear();
            c += 1,2,3;
            strided_test_impl(c, stride_size);
        }

        template<typename Container>
        void strided_test_zero_stride()
        {
            Container c;
            c.push_back(1);

            typedef boost::strided_range<Container> strided_range_t;
            strided_range_t rng( boost::adaptors::stride(c, 0) );
            boost::ignore_unused_variable_warning(rng);
            typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<strided_range_t>::type iter_t;

            typedef BOOST_DEDUCED_TYPENAME boost::iterator_traversal<
                        BOOST_DEDUCED_TYPENAME Container::const_iterator
            >::type container_traversal_tag;

            iter_t first = boost::range_detail::make_begin_strided_iterator(
                c, 0, container_traversal_tag());

            iter_t last = boost::range_detail::make_end_strided_iterator(
                c, 0, container_traversal_tag());

            iter_t it = first;
            for (int i = 0; i < 10; ++i, ++it)
            {
                BOOST_CHECK(it == first);
            }
        }

        template<typename Container>
        void strided_test_impl()
        {
            strided_test_zero_stride< Container >();

            const int MAX_STRIDE_SIZE = 10;
            for (int stride_size = 1; stride_size <= MAX_STRIDE_SIZE; ++stride_size)
            {
                strided_test_impl< Container >(stride_size);
            }
        }

        void strided_test()
        {
            strided_test_impl< std::vector<int> >();
            strided_test_impl< std::list<int> >();
        }

        void strided_defect_Trac5014()
        {
            using namespace boost::assign;

            std::vector<int> v;
            for (int i = 0; i < 30; ++i)
                v.push_back(i);

            std::vector<int> reference;
            reference += 0,4,8,12,16,20,24,28;

            std::vector<int> output;
            boost::push_back(output, v | boost::adaptors::strided(4));

            BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
                                           output.begin(), output.end() );

            BOOST_CHECK_EQUAL( output.back(), 28 );
        }

        template<typename BaseIterator, typename Category>
        class strided_mock_iterator
            : public boost::iterator_adaptor<
                strided_mock_iterator<BaseIterator,Category>
              , BaseIterator
              , boost::use_default
              , Category
            >
        {
            typedef boost::iterator_adaptor<
                        strided_mock_iterator
                      , BaseIterator
                      , boost::use_default
                      , Category
                    > super_t;
        public:
            explicit strided_mock_iterator(BaseIterator it)
                : super_t(it)
            {
            }

        private:
            void increment()
            {
                ++(this->base_reference());
            }

            friend class boost::iterator_core_access;
        };

        template<typename Category, typename Range>
        boost::iterator_range<strided_mock_iterator<BOOST_DEDUCED_TYPENAME boost::range_iterator<Range>::type, Category> >
        as_mock_range(Range& rng)
        {
            typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Range>::type range_iter_t;
            typedef strided_mock_iterator<range_iter_t, Category> mock_iter_t;

            return boost::iterator_range<mock_iter_t>(
                      mock_iter_t(boost::begin(rng)),
                      mock_iter_t(boost::end(rng)));
        }

        void strided_test_traversal()
        {
            using namespace boost::assign;

            std::vector<int> v;
            for (int i = 0; i < 30; ++i)
                v.push_back(i);

            std::vector<int> reference;
            reference += 0,4,8,12,16,20,24,28;

            std::vector<int> output;
            boost::push_back(output, as_mock_range<boost::forward_traversal_tag>(v) | boost::adaptors::strided(4));

            BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
                                           output.begin(), output.end() );

            output.clear();
            boost::push_back(output, as_mock_range<boost::bidirectional_traversal_tag>(v) | boost::adaptors::strided(4));

            BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
                                           output.begin(), output.end() );

            output.clear();
            boost::push_back(output, as_mock_range<boost::random_access_traversal_tag>(v) | boost::adaptors::strided(4));

            BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
                                           output.begin(), output.end() );
        }

        template<typename Range>
        void strided_test_ticket_5236_check_bidirectional(const Range& rng)
        {
            BOOST_CHECK_EQUAL( boost::distance(rng), 1 );
            BOOST_CHECK_EQUAL( std::distance(boost::begin(rng), boost::prior(boost::end(rng))), 0 );
        }
        
        template<typename Range>
        void strided_test_ticket_5236_check(const Range& rng)
        {
            strided_test_ticket_5236_check_bidirectional(rng);
                        
            typename boost::range_iterator<const Range>::type it = boost::end(rng);
            it = it - 1;
            BOOST_CHECK_EQUAL( std::distance(boost::begin(rng), it), 0 );
        }
        
        void strided_test_ticket_5236()
        {
            std::vector<int> v;
            v.push_back(1);
            strided_test_ticket_5236_check( v | boost::adaptors::strided(2) );                
            
            // Ensure that there is consistency between the random-access implementation
            // and the bidirectional.
            
            std::list<int> l;
            l.push_back(1);
            strided_test_ticket_5236_check_bidirectional( l | boost::adaptors::strided(2) );
        }
        
    }
}

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

    test->add( BOOST_TEST_CASE( &boost::strided_test ) );
    test->add( BOOST_TEST_CASE( &boost::strided_defect_Trac5014 ) );
    test->add( BOOST_TEST_CASE( &boost::strided_test_traversal ) );
    test->add( BOOST_TEST_CASE( &boost::strided_test_ticket_5236 ) );

    return test;
}