Cleanup release tags.
[boost.git] / Version_1_18_3 / boost / libs / utility / iterators_test.cpp
blobe264affbb096371ffa961e9b4ca32b1ad77fca43
1 // Demonstrate and test boost/operators.hpp on std::iterators --------------//
3 // (C) Copyright Jeremy Siek 1999. Permission to copy, use, modify,
4 // sell and distribute this software is granted provided this
5 // copyright notice appears in all copies. This software is provided
6 // "as is" without express or implied warranty, and with no claim as
7 // to its suitability for any purpose.
9 // See http://www.boost.org for most recent version including documentation.
11 // Revision History
12 // 12 Dec 99 Initial version with iterator operators (Jeremy Siek)
14 #include <string>
15 #include <iostream>
16 using namespace std;
18 #include <boost/operators.hpp>
19 using namespace boost;
22 template <class T, class R, class P>
23 struct test_iter
24 : public boost::random_access_iterator_helper<
25 test_iter<T,R,P>, T, std::ptrdiff_t, P, R>
27 typedef test_iter self;
28 typedef R Reference;
29 typedef std::ptrdiff_t Distance;
31 public:
32 test_iter(T* i) : _i(i) { }
33 test_iter(const self& x) : _i(x._i) { }
34 self& operator=(const self& x) { _i = x._i; return *this; }
35 Reference operator*() const { return *_i; }
36 self& operator++() { ++_i; return *this; }
37 self& operator--() { --_i; return *this; }
38 self& operator+=(Distance n) { _i += n; return *this; }
39 self& operator-=(Distance n) { _i -= n; return *this; }
40 bool operator==(const self& x) const { return _i == x._i; }
41 bool operator<(const self& x) const { return _i < x._i; }
42 friend Distance operator-(const self& x, const self& y) {
43 return x._i - y._i;
45 protected:
46 T* _i;
50 int
51 main()
53 string array[] = { "apple", "orange", "pear", "peach", "grape", "plum" };
55 test_iter<string,string&,string*> i = array,
56 ie = array + sizeof(array)/sizeof(string);
58 // Tests for all of the operators added by random_access_iterator_helper
60 // test i++
61 while (i != ie)
62 cout << *i++ << " ";
63 cout << endl;
64 i = array;
66 // test i--
67 while (ie != i) {
68 ie--;
69 cout << *ie << " ";
71 cout << endl;
72 ie = array + sizeof(array)/sizeof(string);
74 // test i->m
75 while (i != ie) {
76 cout << i->size() << " ";
77 ++i;
79 cout << endl;
80 i = array;
82 // test i + n
83 while (i < ie) {
84 cout << *i << " ";
85 i = i + 2;
87 cout << endl;
88 i = array;
90 // test n + i
91 while (i < ie) {
92 cout << *i << " ";
93 i = ptrdiff_t(2) + i;
95 cout << endl;
96 i = array;
98 // test i - n
99 while (ie > i) {
100 ie = ie - 2;
101 cout << *ie << " ";
103 cout << endl;
104 ie = array + sizeof(array)/sizeof(string);
106 // test i[n]
107 for (std::size_t j = 0; j < sizeof(array)/sizeof(string); ++j)
108 cout << i[j] << " ";
109 cout << endl;
112 test_iter<string, const string&, const string*> i = array,
113 ie = array + sizeof(array)/sizeof(string);
115 // Tests for all of the operators added by random_access_iterator_helper
117 // test i++
118 while (i != ie)
119 cout << *i++ << " ";
120 cout << endl;
121 i = array;
123 // test i--
124 while (ie != i) {
125 ie--;
126 cout << *ie << " ";
128 cout << endl;
129 ie = array + sizeof(array)/sizeof(string);
131 // test i->m
132 while (i != ie) {
133 cout << i->size() << " ";
134 ++i;
136 cout << endl;
137 i = array;
139 // test i + n
140 while (i < ie) {
141 cout << *i << " ";
142 i = i + 2;
144 cout << endl;
145 i = array;
147 // test n + i
148 while (i < ie) {
149 cout << *i << " ";
150 i = ptrdiff_t(2) + i;
152 cout << endl;
153 i = array;
155 // test i - n
156 while (ie > i) {
157 ie = ie - 2;
158 cout << *ie << " ";
160 cout << endl;
161 ie = array + sizeof(array)/sizeof(string);
163 // test i[n]
164 for (std::size_t j = 0; j < sizeof(array)/sizeof(string); ++j)
165 cout << i[j] << " ";
166 cout << endl;
168 return 0;