1/*
2 Copyright (c) Marshall Clow 2013.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8*/
9
10#ifndef ITERATOR_TEST_H
11#define ITERATOR_TEST_H
12
13/*
14 A set of iterator adapters for constructing test cases
15 From an iterator (or a pointer), you can make any class of iterator.
16 Assuming you want to degrade the capabilities.
17
18 Modeled closely on work that Howard Hinnant did for libc++.
19*/
20
21#include <iterator>
22
23// == Input Iterator ==
24template <typename It>
25class input_iterator {
26public:
27 typedef std::input_iterator_tag iterator_category;
28 typedef typename std::iterator_traits<It>::value_type value_type;
29 typedef typename std::iterator_traits<It>::difference_type difference_type;
30 typedef It pointer;
31 typedef typename std::iterator_traits<It>::reference reference;
32
33 It base() const {return it_;}
34
35 input_iterator() : it_() {}
36 explicit input_iterator(It it) : it_(it) {}
37
38 template <typename U>
39 input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
40
41 reference operator*() const {return *it_;}
42 pointer operator->() const {return it_;}
43
44 input_iterator& operator++() {++it_; return *this;}
45 input_iterator operator++(int) {input_iterator tmp(*this); ++(*this); return tmp;}
46
47 friend bool operator==(const input_iterator& x, const input_iterator& y)
48 {return x.it_ == y.it_;}
49 friend bool operator!=(const input_iterator& x, const input_iterator& y)
50 {return !(x == y);}
51
52private:
53 It it_;
54 template <typename U> friend class input_iterator;
55};
56
57template <typename T, typename U>
58inline bool
59operator==(const input_iterator<T>& x, const input_iterator<U>& y)
60{
61 return x.base() == y.base();
62}
63
64template <typename T, typename U>
65inline bool
66operator!=(const input_iterator<T>& x, const input_iterator<U>& y)
67{
68 return !(x == y);
69}
70
71
72// == Forward Iterator ==
73template <typename It>
74class forward_iterator {
75public:
76 typedef std::forward_iterator_tag iterator_category;
77 typedef typename std::iterator_traits<It>::value_type value_type;
78 typedef typename std::iterator_traits<It>::difference_type difference_type;
79 typedef It pointer;
80 typedef typename std::iterator_traits<It>::reference reference;
81
82 It base() const {return it_;}
83
84 forward_iterator() : it_() {}
85 explicit forward_iterator(It it) : it_(it) {}
86 template <typename U>
87 forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
88
89 reference operator*() const {return *it_;}
90 pointer operator->() const {return it_;}
91
92 forward_iterator& operator++() {++it_; return *this;}
93 forward_iterator operator++(int) {forward_iterator tmp(*this); ++(*this); return tmp;}
94
95 friend bool operator==(const forward_iterator& x, const forward_iterator& y)
96 {return x.it_ == y.it_;}
97 friend bool operator!=(const forward_iterator& x, const forward_iterator& y)
98 {return !(x == y);}
99private:
100 It it_;
101
102 template <typename U> friend class forward_iterator;
103};
104
105template <typename T, typename U>
106inline bool
107operator==(const forward_iterator<T>& x, const forward_iterator<U>& y)
108{
109 return x.base() == y.base();
110}
111
112template <typename T, typename U>
113inline bool
114operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y)
115{
116 return !(x == y);
117}
118
119// == Bidirectional Iterator ==
120template <typename It>
121class bidirectional_iterator
122{
123public:
124 typedef std::bidirectional_iterator_tag iterator_category;
125 typedef typename std::iterator_traits<It>::value_type value_type;
126 typedef typename std::iterator_traits<It>::difference_type difference_type;
127 typedef It pointer;
128 typedef typename std::iterator_traits<It>::reference reference;
129
130 It base() const {return it_;}
131
132 bidirectional_iterator() : it_() {}
133 explicit bidirectional_iterator(It it) : it_(it) {}
134 template <typename U>
135 bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
136
137 reference operator*() const {return *it_;}
138 pointer operator->() const {return it_;}
139
140 bidirectional_iterator& operator++() {++it_; return *this;}
141 bidirectional_iterator operator++(int) {bidirectional_iterator tmp(*this); ++(*this); return tmp;}
142
143 bidirectional_iterator& operator--() {--it_; return *this;}
144 bidirectional_iterator operator--(int) {bidirectional_iterator tmp(*this); --(*this); return tmp;}
145private:
146 It it_;
147 template <typename U> friend class bidirectional_iterator;
148};
149
150template <typename T, typename U>
151inline bool
152operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
153{
154 return x.base() == y.base();
155}
156
157template <typename T, typename U>
158inline bool
159operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
160{
161 return !(x == y);
162}
163
164
165// == Random Access Iterator ==
166template <typename It>
167class random_access_iterator {
168public:
169 typedef std::random_access_iterator_tag iterator_category;
170 typedef typename std::iterator_traits<It>::value_type value_type;
171 typedef typename std::iterator_traits<It>::difference_type difference_type;
172 typedef It pointer;
173 typedef typename std::iterator_traits<It>::reference reference;
174
175 It base() const {return it_;}
176
177 random_access_iterator() : it_() {}
178 explicit random_access_iterator(It it) : it_(it) {}
179 template <typename U>
180 random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
181
182 reference operator*() const {return *it_;}
183 pointer operator->() const {return it_;}
184
185 random_access_iterator& operator++() {++it_; return *this;}
186 random_access_iterator operator++(int) {random_access_iterator tmp(*this); ++(*this); return tmp;}
187
188 random_access_iterator& operator--() {--it_; return *this;}
189 random_access_iterator operator--(int) {random_access_iterator tmp(*this); --(*this); return tmp;}
190
191 random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
192 random_access_iterator operator+ (difference_type n) const {random_access_iterator tmp(*this); tmp += n; return tmp;}
193 friend random_access_iterator operator+(difference_type n, random_access_iterator x) {x += n; return x;}
194
195 random_access_iterator& operator-=(difference_type n) {return *this += -n;}
196 random_access_iterator operator- (difference_type n) const {random_access_iterator tmp(*this); tmp -= n; return tmp;}
197
198 reference operator[](difference_type n) const {return it_[n];}
199private:
200 It it_;
201
202 template <typename U> friend class random_access_iterator;
203};
204
205template <typename T, typename U>
206inline bool
207operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
208{
209 return x.base() == y.base();
210}
211
212template <typename T, typename U>
213inline bool
214operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
215{
216 return !(x == y);
217}
218
219template <typename T, typename U>
220inline bool
221operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
222{
223 return x.base() < y.base();
224}
225
226template <typename T, typename U>
227inline bool
228operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
229{
230 return !(y < x);
231}
232
233template <typename T, typename U>
234inline bool
235operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
236{
237 return y < x;
238}
239
240template <typename T, typename U>
241inline bool
242operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
243{
244 return !(x < y);
245}
246
247template <typename T, typename U>
248inline typename std::iterator_traits<T>::difference_type
249operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
250{
251 return x.base() - y.base();
252}
253
254
255// == Output Iterator ==
256template <typename It>
257class output_iterator {
258public:
259 typedef std::output_iterator_tag iterator_category;
260 typedef void value_type;
261 typedef typename std::iterator_traits<It>::difference_type difference_type;
262 typedef It pointer;
263 typedef typename std::iterator_traits<It>::reference reference;
264
265 It base() const {return it_;}
266
267 output_iterator () {}
268 explicit output_iterator(It it) : it_(it) {}
269
270 template <typename U>
271 output_iterator(const output_iterator<U>& u) :it_(u.it_) {}
272
273 reference operator*() const {return *it_;}
274
275 output_iterator& operator++() {++it_; return *this;}
276 output_iterator operator++(int) {output_iterator tmp(*this); ++(*this); return tmp;}
277
278private:
279 It it_;
280 template <typename U> friend class output_iterator;
281 };
282
283// No comparison operators for output iterators
284
285
286// == Get the base of an iterator; used for comparisons ==
287template <typename Iter>
288inline Iter base(output_iterator<Iter> i) { return i.base(); }
289
290template <typename Iter>
291inline Iter base(input_iterator<Iter> i) { return i.base(); }
292
293template <typename Iter>
294inline Iter base(forward_iterator<Iter> i) { return i.base(); }
295
296template <typename Iter>
297inline Iter base(bidirectional_iterator<Iter> i) { return i.base(); }
298
299template <typename Iter>
300inline Iter base(random_access_iterator<Iter> i) { return i.base(); }
301
302template <typename Iter> // everything else
303inline Iter base(Iter i) { return i; }
304
305#endif // ITERATORS_H
306