summaryrefslogtreecommitdiff
path: root/libs/utility/iterators_test.cpp
blob: 7ac1191bd7590cfc08246f39708bb474dc1af644 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
//  Demonstrate and test boost/operators.hpp on std::iterators  --------------//

//  (C) Copyright Jeremy Siek 1999.
// Distributed under 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)

//  See http://www.boost.org for most recent version including documentation.

//  Revision History
//  29 May 01 Factored implementation, added comparison tests, use Test Tools
//            library (Daryle Walker)
//  12 Dec 99 Initial version with iterator operators (Jeremy Siek)

#define  BOOST_INCLUDE_MAIN
#include <boost/test/test_tools.hpp>  // for main

#include <boost/config.hpp>     // for BOOST_STATIC_CONSTANT
#include <boost/cstdlib.hpp>    // for boost::exit_success
#include <boost/operators.hpp>  // for boost::random_access_iterator_helper

#include <cstddef>    // for std::ptrdiff_t, std::size_t
#include <cstring>    // for std::strcmp
#include <iostream>   // for std::cout (std::endl, ends, and flush indirectly)
#include <string>     // for std::string
#include <sstream>    // for std::stringstream

# ifdef BOOST_NO_STDC_NAMESPACE
    namespace std { using ::strcmp; }
# endif


// Iterator test class
template <class T, class R, class P>
struct test_iter
  : public boost::random_access_iterator_helper<
     test_iter<T,R,P>, T, std::ptrdiff_t, P, R>
{
  typedef test_iter self;
  typedef R Reference;
  typedef std::ptrdiff_t Distance;

public:
  explicit test_iter(T* i =0) : _i(i) { }
  test_iter(const self& x) : _i(x._i) { }
  self& operator=(const self& x) { _i = x._i; return *this; }
  Reference operator*() const { return *_i; }
  self& operator++() { ++_i; return *this; }
  self& operator--() { --_i; return *this; }
  self& operator+=(Distance n) { _i += n; return *this; }
  self& operator-=(Distance n) { _i -= n; return *this; }
  bool operator==(const self& x) const { return _i == x._i; }
  bool operator<(const self& x) const { return _i < x._i; }
  friend Distance operator-(const self& x, const self& y) {
    return x._i - y._i; 
  }
protected:
  P _i;
};

// Iterator operator testing classes
class test_opr_base
{
protected:
    // Test data and types
    BOOST_STATIC_CONSTANT( std::size_t, fruit_length = 6u );

    typedef std::string  fruit_array_type[ fruit_length ];

    static  fruit_array_type    fruit;

};  // test_opr_base

#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
//  A definition is required even for integral static constants
const std::size_t test_opr_base::fruit_length;
#endif

template <typename T, typename R = T&, typename P = T*>
class test_opr
    : public test_opr_base
{
    typedef test_opr<T, R, P>  self_type;

public:
    // Types
    typedef T  value_type;
    typedef R  reference;
    typedef P  pointer;

    typedef test_iter<T, R, P>  iter_type;

    // Test controller
    static  void  master_test( char const name[] );

private:
    // Test data
    static iter_type const  fruit_begin;
    static iter_type const  fruit_end;

    // Test parts
    static  void  post_increment_test();
    static  void  post_decrement_test();
    static  void  indirect_referral_test();
    static  void  offset_addition_test();
    static  void  reverse_offset_addition_test();
    static  void  offset_subtraction_test();
    static  void  comparison_test();
    static  void  indexing_test();

};  // test_opr


// Class-static data definitions
test_opr_base::fruit_array_type
 test_opr_base::fruit = { "apple", "orange", "pear", "peach", "grape", "plum" };

template <typename T, typename R, typename P>
  typename test_opr<T, R, P>::iter_type const
 test_opr<T, R, P>::fruit_begin = test_iter<T,R,P>( fruit );

template <typename T, typename R, typename P>
typename test_opr<T, R, P>::iter_type const
 test_opr<T, R, P>::fruit_end = test_iter<T,R,P>( fruit + fruit_length );


// Main testing function
int
test_main( int , char * [] )
{
    using std::string;

    typedef test_opr<string, string &, string *>              test1_type;
    typedef test_opr<string, string const &, string const *>  test2_type;

    test1_type::master_test( "non-const string" );
    test2_type::master_test( "const string" );

    return boost::exit_success;
}

// Tests for all of the operators added by random_access_iterator_helper
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::master_test
(
    char const  name[]
)
{
    std::cout << "Doing test run for " << name << '.' << std::endl;

    post_increment_test();
    post_decrement_test();
    indirect_referral_test();
    offset_addition_test();
    reverse_offset_addition_test();
    offset_subtraction_test();
    comparison_test();
    indexing_test();
}

// Test post-increment
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::post_increment_test
(
)
{
    std::cout << "\tDoing post-increment test." << std::endl;

    std::stringstream oss;
    for ( iter_type i = fruit_begin ; i != fruit_end ; )
    {
        oss << *i++ << ' ';
    }

    BOOST_CHECK( oss.str() == "apple orange pear peach grape plum ");
}

// Test post-decrement
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::post_decrement_test
(
)
{
    std::cout << "\tDoing post-decrement test." << std::endl;

    std::stringstream oss;
    for ( iter_type i = fruit_end ; i != fruit_begin ; )
    {
        i--;
        oss << *i << ' ';
    }

    BOOST_CHECK( oss.str() == "plum grape peach pear orange apple ");
}

// Test indirect structure referral
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::indirect_referral_test
(
)
{
    std::cout << "\tDoing indirect reference test." << std::endl;

    std::stringstream oss;
    for ( iter_type i = fruit_begin ; i != fruit_end ; ++i )
    {
        oss << i->size() << ' ';
    }

    BOOST_CHECK( oss.str() == "5 6 4 5 5 4 ");
}

// Test offset addition
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::offset_addition_test
(
)
{
    std::cout << "\tDoing offset addition test." << std::endl;

    std::ptrdiff_t const  two = 2;
    std::stringstream oss;
    for ( iter_type i = fruit_begin ; i != fruit_end ; i = i + two )
    {
        oss << *i << ' ';
    }

    BOOST_CHECK( oss.str() == "apple pear grape ");
}

// Test offset addition, in reverse order
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::reverse_offset_addition_test
(
)
{
    std::cout << "\tDoing reverse offset addition test." << std::endl;

    std::ptrdiff_t const  two = 2;
    std::stringstream oss;
    for ( iter_type i = fruit_begin ; i != fruit_end ; i = two + i )
    {
        oss << *i << ' ';
    }

    BOOST_CHECK( oss.str() == "apple pear grape ");
}

// Test offset subtraction
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::offset_subtraction_test
(
)
{
    std::cout << "\tDoing offset subtraction test." << std::endl;

    std::ptrdiff_t const  two = 2;
    std::stringstream oss;
    for ( iter_type i = fruit_end ; fruit_begin < i ; )
    {
        i = i - two;
        if ( (fruit_begin < i) || (fruit_begin == i) )
        {
            oss << *i << ' ';
        }
    }

    BOOST_CHECK( oss.str() == "grape pear apple ");
}

// Test comparisons
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::comparison_test
(
)
{
    using std::cout;
    using std::ptrdiff_t;

    cout << "\tDoing comparison tests.\n\t\tPass:";

    for ( iter_type i = fruit_begin ; i != fruit_end ; ++i )
    {
        ptrdiff_t const  i_offset = i - fruit_begin;

        cout << ' ' << *i << std::flush;
        for ( iter_type j = fruit_begin ; j != fruit_end ; ++j )
        {
            ptrdiff_t const  j_offset = j - fruit_begin;

            BOOST_CHECK( (i != j) == (i_offset != j_offset) );
            BOOST_CHECK( (i > j) == (i_offset > j_offset) );
            BOOST_CHECK( (i <= j) == (i_offset <= j_offset) );
            BOOST_CHECK( (i >= j) == (i_offset >= j_offset) );
        }
    }
    cout << std::endl;
}

// Test indexing
template <typename T, typename R, typename P>
void
test_opr<T, R, P>::indexing_test
(
)
{
    std::cout << "\tDoing indexing test." << std::endl;

    std::stringstream oss;
    for ( std::size_t k = 0u ; k < fruit_length ; ++k )
    {
        oss << fruit_begin[ k ] << ' ';
    }

    BOOST_CHECK( oss.str() == "apple orange pear peach grape plum ");
}