Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/utility/iterators_test.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 17 years ago

added boost

File size: 8.2 KB
Line 
1//  Demonstrate and test boost/operators.hpp on std::iterators  --------------//
2
3//  (C) Copyright Jeremy Siek 1999.
4// Distributed under the Boost Software License, Version 1.0. (See
5// accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7
8//  See http://www.boost.org for most recent version including documentation.
9
10//  Revision History
11//  29 May 01 Factored implementation, added comparison tests, use Test Tools
12//            library (Daryle Walker)
13//  12 Dec 99 Initial version with iterator operators (Jeremy Siek)
14
15#define  BOOST_INCLUDE_MAIN
16#include <boost/test/test_tools.hpp>  // for main
17
18#include <boost/config.hpp>     // for BOOST_STATIC_CONSTANT
19#include <boost/cstdlib.hpp>    // for boost::exit_success
20#include <boost/operators.hpp>  // for boost::random_access_iterator_helper
21
22#include <cstddef>    // for std::ptrdiff_t, std::size_t
23#include <cstring>    // for std::strcmp
24#include <iostream>   // for std::cout (std::endl, ends, and flush indirectly)
25#include <string>     // for std::string
26#include <sstream>    // for std::stringstream
27
28# ifdef BOOST_NO_STDC_NAMESPACE
29    namespace std { using ::strcmp; }
30# endif
31
32
33// Iterator test class
34template <class T, class R, class P>
35struct test_iter
36  : public boost::random_access_iterator_helper<
37     test_iter<T,R,P>, T, std::ptrdiff_t, P, R>
38{
39  typedef test_iter self;
40  typedef R Reference;
41  typedef std::ptrdiff_t Distance;
42
43public:
44  explicit test_iter(T* i =0) : _i(i) { }
45  test_iter(const self& x) : _i(x._i) { }
46  self& operator=(const self& x) { _i = x._i; return *this; }
47  Reference operator*() const { return *_i; }
48  self& operator++() { ++_i; return *this; }
49  self& operator--() { --_i; return *this; }
50  self& operator+=(Distance n) { _i += n; return *this; }
51  self& operator-=(Distance n) { _i -= n; return *this; }
52  bool operator==(const self& x) const { return _i == x._i; }
53  bool operator<(const self& x) const { return _i < x._i; }
54  friend Distance operator-(const self& x, const self& y) {
55    return x._i - y._i; 
56  }
57protected:
58  P _i;
59};
60
61// Iterator operator testing classes
62class test_opr_base
63{
64protected:
65    // Test data and types
66    BOOST_STATIC_CONSTANT( std::size_t, fruit_length = 6u );
67
68    typedef std::string  fruit_array_type[ fruit_length ];
69
70    static  fruit_array_type    fruit;
71
72};  // test_opr_base
73
74#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
75//  A definition is required even for integral static constants
76const std::size_t test_opr_base::fruit_length;
77#endif
78
79template <typename T, typename R = T&, typename P = T*>
80class test_opr
81    : public test_opr_base
82{
83    typedef test_opr<T, R, P>  self_type;
84
85public:
86    // Types
87    typedef T  value_type;
88    typedef R  reference;
89    typedef P  pointer;
90
91    typedef test_iter<T, R, P>  iter_type;
92
93    // Test controller
94    static  void  master_test( char const name[] );
95
96private:
97    // Test data
98    static iter_type const  fruit_begin;
99    static iter_type const  fruit_end;
100
101    // Test parts
102    static  void  post_increment_test();
103    static  void  post_decrement_test();
104    static  void  indirect_referral_test();
105    static  void  offset_addition_test();
106    static  void  reverse_offset_addition_test();
107    static  void  offset_subtraction_test();
108    static  void  comparison_test();
109    static  void  indexing_test();
110
111};  // test_opr
112
113
114// Class-static data definitions
115test_opr_base::fruit_array_type
116 test_opr_base::fruit = { "apple", "orange", "pear", "peach", "grape", "plum" };
117
118template <typename T, typename R, typename P>
119  typename test_opr<T, R, P>::iter_type const
120 test_opr<T, R, P>::fruit_begin = test_iter<T,R,P>( fruit );
121
122template <typename T, typename R, typename P>
123typename test_opr<T, R, P>::iter_type const
124 test_opr<T, R, P>::fruit_end = test_iter<T,R,P>( fruit + fruit_length );
125
126
127// Main testing function
128int
129test_main( int , char * [] )
130{
131    using std::string;
132
133    typedef test_opr<string, string &, string *>              test1_type;
134    typedef test_opr<string, string const &, string const *>  test2_type;
135
136    test1_type::master_test( "non-const string" );
137    test2_type::master_test( "const string" );
138
139    return boost::exit_success;
140}
141
142// Tests for all of the operators added by random_access_iterator_helper
143template <typename T, typename R, typename P>
144void
145test_opr<T, R, P>::master_test
146(
147    char const  name[]
148)
149{
150    std::cout << "Doing test run for " << name << '.' << std::endl;
151
152    post_increment_test();
153    post_decrement_test();
154    indirect_referral_test();
155    offset_addition_test();
156    reverse_offset_addition_test();
157    offset_subtraction_test();
158    comparison_test();
159    indexing_test();
160}
161
162// Test post-increment
163template <typename T, typename R, typename P>
164void
165test_opr<T, R, P>::post_increment_test
166(
167)
168{
169    std::cout << "\tDoing post-increment test." << std::endl;
170
171    std::stringstream oss;
172    for ( iter_type i = fruit_begin ; i != fruit_end ; )
173    {
174        oss << *i++ << ' ';
175    }
176
177    BOOST_CHECK( oss.str() == "apple orange pear peach grape plum ");
178}
179
180// Test post-decrement
181template <typename T, typename R, typename P>
182void
183test_opr<T, R, P>::post_decrement_test
184(
185)
186{
187    std::cout << "\tDoing post-decrement test." << std::endl;
188
189    std::stringstream oss;
190    for ( iter_type i = fruit_end ; i != fruit_begin ; )
191    {
192        i--;
193        oss << *i << ' ';
194    }
195
196    BOOST_CHECK( oss.str() == "plum grape peach pear orange apple ");
197}
198
199// Test indirect structure referral
200template <typename T, typename R, typename P>
201void
202test_opr<T, R, P>::indirect_referral_test
203(
204)
205{
206    std::cout << "\tDoing indirect reference test." << std::endl;
207
208    std::stringstream oss;
209    for ( iter_type i = fruit_begin ; i != fruit_end ; ++i )
210    {
211        oss << i->size() << ' ';
212    }
213
214    BOOST_CHECK( oss.str() == "5 6 4 5 5 4 ");
215}
216
217// Test offset addition
218template <typename T, typename R, typename P>
219void
220test_opr<T, R, P>::offset_addition_test
221(
222)
223{
224    std::cout << "\tDoing offset addition test." << std::endl;
225
226    std::ptrdiff_t const  two = 2;
227    std::stringstream oss;
228    for ( iter_type i = fruit_begin ; i != fruit_end ; i = i + two )
229    {
230        oss << *i << ' ';
231    }
232
233    BOOST_CHECK( oss.str() == "apple pear grape ");
234}
235
236// Test offset addition, in reverse order
237template <typename T, typename R, typename P>
238void
239test_opr<T, R, P>::reverse_offset_addition_test
240(
241)
242{
243    std::cout << "\tDoing reverse offset addition test." << std::endl;
244
245    std::ptrdiff_t const  two = 2;
246    std::stringstream oss;
247    for ( iter_type i = fruit_begin ; i != fruit_end ; i = two + i )
248    {
249        oss << *i << ' ';
250    }
251
252    BOOST_CHECK( oss.str() == "apple pear grape ");
253}
254
255// Test offset subtraction
256template <typename T, typename R, typename P>
257void
258test_opr<T, R, P>::offset_subtraction_test
259(
260)
261{
262    std::cout << "\tDoing offset subtraction test." << std::endl;
263
264    std::ptrdiff_t const  two = 2;
265    std::stringstream oss;
266    for ( iter_type i = fruit_end ; fruit_begin < i ; )
267    {
268        i = i - two;
269        if ( (fruit_begin < i) || (fruit_begin == i) )
270        {
271            oss << *i << ' ';
272        }
273    }
274
275    BOOST_CHECK( oss.str() == "grape pear apple ");
276}
277
278// Test comparisons
279template <typename T, typename R, typename P>
280void
281test_opr<T, R, P>::comparison_test
282(
283)
284{
285    using std::cout;
286    using std::ptrdiff_t;
287
288    cout << "\tDoing comparison tests.\n\t\tPass:";
289
290    for ( iter_type i = fruit_begin ; i != fruit_end ; ++i )
291    {
292        ptrdiff_t const  i_offset = i - fruit_begin;
293
294        cout << ' ' << *i << std::flush;
295        for ( iter_type j = fruit_begin ; j != fruit_end ; ++j )
296        {
297            ptrdiff_t const  j_offset = j - fruit_begin;
298
299            BOOST_CHECK( (i != j) == (i_offset != j_offset) );
300            BOOST_CHECK( (i > j) == (i_offset > j_offset) );
301            BOOST_CHECK( (i <= j) == (i_offset <= j_offset) );
302            BOOST_CHECK( (i >= j) == (i_offset >= j_offset) );
303        }
304    }
305    cout << std::endl;
306}
307
308// Test indexing
309template <typename T, typename R, typename P>
310void
311test_opr<T, R, P>::indexing_test
312(
313)
314{
315    std::cout << "\tDoing indexing test." << std::endl;
316
317    std::stringstream oss;
318    for ( std::size_t k = 0u ; k < fruit_length ; ++k )
319    {
320        oss << fruit_begin[ k ] << ' ';
321    }
322
323    BOOST_CHECK( oss.str() == "apple orange pear peach grape plum ");
324}
Note: See TracBrowser for help on using the repository browser.