Merged revisions 195034,195219,195245,195357,195374,195428,195599,195673,195809 via...
[official-gcc.git] / main / libstdc++-v3 / testsuite / util / testsuite_iterators.h
blob757282bd014a189c3aba73b87eb426b4b34ea48b
1 // -*- C++ -*-
2 // Iterator Wrappers for the C++ library testsuite.
3 //
4 // Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 // This file provides the following:
24 // input_iterator_wrapper, output_iterator_wrapper
25 // forward_iterator_wrapper, bidirectional_iterator_wrapper and
26 // random_access_wrapper, which attempt to exactly perform the requirements
27 // of these types of iterators. These are constructed from the class
28 // test_container, which is given two pointers to T and an iterator type.
30 #include <testsuite_hooks.h>
31 #include <bits/stl_iterator_base_types.h>
33 #if __cplusplus >= 201103L
34 #include <bits/move.h>
35 #endif
37 #ifndef _TESTSUITE_ITERATORS
38 #define _TESTSUITE_ITERATORS
40 #ifdef DISABLE_ITERATOR_DEBUG
41 #define ITERATOR_VERIFY(x)
42 #else
43 #define ITERATOR_VERIFY(x) VERIFY(x)
44 #endif
46 namespace __gnu_test
48 /**
49 * @brief Simple container for holding two pointers.
51 * Note that input_iterator_wrapper changes first to denote
52 * how the valid range of == , ++, etc. change as the iterators are used.
54 template<typename T>
55 struct BoundsContainer
57 T* first;
58 T* last;
59 BoundsContainer(T* _first, T* _last) : first(_first), last(_last)
60 { }
63 // Simple container for holding state of a set of output iterators.
64 template<typename T>
65 struct OutputContainer : public BoundsContainer<T>
67 T* incrementedto;
68 bool* writtento;
69 OutputContainer(T* _first, T* _last)
70 : BoundsContainer<T>(_first, _last), incrementedto(_first)
72 writtento = new bool[this->last - this->first];
73 for(int i = 0; i < this->last - this->first; i++)
74 writtento[i] = false;
77 ~OutputContainer()
78 { delete[] writtento; }
81 // Produced by output_iterator to allow limited writing to pointer
82 template<class T>
83 class WritableObject
85 T* ptr;
87 public:
88 OutputContainer<T>* SharedInfo;
89 WritableObject(T* ptr_in,OutputContainer<T>* SharedInfo_in):
90 ptr(ptr_in), SharedInfo(SharedInfo_in)
91 { }
93 #if __cplusplus >= 201103L
94 template<class U>
95 void
96 operator=(U&& new_val)
98 ITERATOR_VERIFY(SharedInfo->writtento[ptr - SharedInfo->first] == 0);
99 SharedInfo->writtento[ptr - SharedInfo->first] = 1;
100 *ptr = std::forward<U>(new_val);
102 #else
103 template<class U>
104 void
105 operator=(const U& new_val)
107 ITERATOR_VERIFY(SharedInfo->writtento[ptr - SharedInfo->first] == 0);
108 SharedInfo->writtento[ptr - SharedInfo->first] = 1;
109 *ptr = new_val;
111 #endif
115 * @brief output_iterator wrapper for pointer
117 * This class takes a pointer and wraps it to provide exactly
118 * the requirements of a output_iterator. It should not be
119 * instansiated directly, but generated from a test_container
121 template<class T>
122 struct output_iterator_wrapper
123 : public std::iterator<std::output_iterator_tag, T, std::ptrdiff_t, T*, T&>
125 typedef OutputContainer<T> ContainerType;
126 T* ptr;
127 ContainerType* SharedInfo;
129 output_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
130 : ptr(_ptr), SharedInfo(SharedInfo_in)
132 ITERATOR_VERIFY(ptr >= SharedInfo->first && ptr <= SharedInfo->last);
135 output_iterator_wrapper(const output_iterator_wrapper& in)
136 : ptr(in.ptr), SharedInfo(in.SharedInfo)
139 WritableObject<T>
140 operator*() const
142 ITERATOR_VERIFY(ptr < SharedInfo->last);
143 ITERATOR_VERIFY(SharedInfo->writtento[ptr - SharedInfo->first] == false);
144 return WritableObject<T>(ptr, SharedInfo);
147 output_iterator_wrapper&
148 operator=(const output_iterator_wrapper& in)
150 ptr = in.ptr;
151 SharedInfo = in.SharedInfo;
152 return *this;
155 output_iterator_wrapper&
156 operator++()
158 ITERATOR_VERIFY(SharedInfo && ptr < SharedInfo->last);
159 ITERATOR_VERIFY(ptr>=SharedInfo->incrementedto);
160 ptr++;
161 SharedInfo->incrementedto=ptr;
162 return *this;
165 output_iterator_wrapper
166 operator++(int)
168 output_iterator_wrapper<T> tmp = *this;
169 ++*this;
170 return tmp;
176 * @brief input_iterator wrapper for pointer
178 * This class takes a pointer and wraps it to provide exactly
179 * the requirements of a input_iterator. It should not be
180 * instansiated directly, but generated from a test_container
182 template<class T>
183 class input_iterator_wrapper
184 : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T&>
186 protected:
187 input_iterator_wrapper()
190 public:
191 typedef BoundsContainer<T> ContainerType;
192 T* ptr;
193 ContainerType* SharedInfo;
195 input_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
196 : ptr(_ptr), SharedInfo(SharedInfo_in)
197 { ITERATOR_VERIFY(ptr >= SharedInfo->first && ptr <= SharedInfo->last); }
199 input_iterator_wrapper(const input_iterator_wrapper& in)
200 : ptr(in.ptr), SharedInfo(in.SharedInfo)
203 bool
204 operator==(const input_iterator_wrapper& in) const
206 ITERATOR_VERIFY(SharedInfo && SharedInfo == in.SharedInfo);
207 ITERATOR_VERIFY(ptr>=SharedInfo->first && in.ptr>=SharedInfo->first);
208 return ptr == in.ptr;
211 bool
212 operator!=(const input_iterator_wrapper& in) const
214 return !(*this == in);
218 operator*() const
220 ITERATOR_VERIFY(SharedInfo && ptr < SharedInfo->last);
221 ITERATOR_VERIFY(ptr >= SharedInfo->first);
222 return *ptr;
226 operator->() const
228 return &**this;
231 input_iterator_wrapper&
232 operator=(const input_iterator_wrapper& in)
234 ptr = in.ptr;
235 SharedInfo = in.SharedInfo;
236 return *this;
239 input_iterator_wrapper&
240 operator++()
242 ITERATOR_VERIFY(SharedInfo && ptr < SharedInfo->last);
243 ITERATOR_VERIFY(ptr>=SharedInfo->first);
244 ptr++;
245 SharedInfo->first=ptr;
246 return *this;
249 void
250 operator++(int)
252 ++*this;
258 * @brief forward_iterator wrapper for pointer
260 * This class takes a pointer and wraps it to provide exactly
261 * the requirements of a forward_iterator. It should not be
262 * instansiated directly, but generated from a test_container
264 template<class T>
265 struct forward_iterator_wrapper : public input_iterator_wrapper<T>
267 typedef BoundsContainer<T> ContainerType;
268 typedef std::forward_iterator_tag iterator_category;
269 forward_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
270 : input_iterator_wrapper<T>(_ptr, SharedInfo_in)
273 forward_iterator_wrapper(const forward_iterator_wrapper& in)
274 : input_iterator_wrapper<T>(in)
277 forward_iterator_wrapper()
279 this->ptr = 0;
280 this->SharedInfo = 0;
284 operator*() const
286 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
287 return *(this->ptr);
291 operator->() const
292 { return &**this; }
294 forward_iterator_wrapper&
295 operator++()
297 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
298 this->ptr++;
299 return *this;
302 forward_iterator_wrapper
303 operator++(int)
305 forward_iterator_wrapper<T> tmp = *this;
306 ++*this;
307 return tmp;
312 * @brief bidirectional_iterator wrapper for pointer
314 * This class takes a pointer and wraps it to provide exactly
315 * the requirements of a forward_iterator. It should not be
316 * instansiated directly, but generated from a test_container
318 template<class T>
319 struct bidirectional_iterator_wrapper : public forward_iterator_wrapper<T>
321 typedef BoundsContainer<T> ContainerType;
322 typedef std::bidirectional_iterator_tag iterator_category;
323 bidirectional_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
324 : forward_iterator_wrapper<T>(_ptr, SharedInfo_in)
327 bidirectional_iterator_wrapper(const bidirectional_iterator_wrapper& in)
328 : forward_iterator_wrapper<T>(in)
331 bidirectional_iterator_wrapper(): forward_iterator_wrapper<T>()
334 bidirectional_iterator_wrapper&
335 operator=(const bidirectional_iterator_wrapper& in)
337 this->ptr = in.ptr;
338 this->SharedInfo = in.SharedInfo;
339 return *this;
342 bidirectional_iterator_wrapper&
343 operator++()
345 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
346 this->ptr++;
347 return *this;
350 bidirectional_iterator_wrapper
351 operator++(int)
353 bidirectional_iterator_wrapper<T> tmp = *this;
354 ++*this;
355 return tmp;
358 bidirectional_iterator_wrapper&
359 operator--()
361 ITERATOR_VERIFY(this->SharedInfo && this->ptr > this->SharedInfo->first);
362 this->ptr--;
363 return *this;
366 bidirectional_iterator_wrapper
367 operator--(int)
369 bidirectional_iterator_wrapper<T> tmp = *this;
370 --*this;
371 return tmp;
376 * @brief random_access_iterator wrapper for pointer
378 * This class takes a pointer and wraps it to provide exactly
379 * the requirements of a forward_iterator. It should not be
380 * instansiated directly, but generated from a test_container
382 template<class T>
383 struct random_access_iterator_wrapper
384 : public bidirectional_iterator_wrapper<T>
386 typedef BoundsContainer<T> ContainerType;
387 typedef std::random_access_iterator_tag iterator_category;
388 random_access_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
389 : bidirectional_iterator_wrapper<T>(_ptr, SharedInfo_in)
392 random_access_iterator_wrapper(const random_access_iterator_wrapper<T>& in)
393 : bidirectional_iterator_wrapper<T>(in)
396 random_access_iterator_wrapper():bidirectional_iterator_wrapper<T>()
399 random_access_iterator_wrapper&
400 operator=(const random_access_iterator_wrapper& in)
402 this->ptr = in.ptr;
403 this->SharedInfo = in.SharedInfo;
404 return *this;
407 random_access_iterator_wrapper&
408 operator++()
410 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
411 this->ptr++;
412 return *this;
415 random_access_iterator_wrapper
416 operator++(int)
418 random_access_iterator_wrapper<T> tmp = *this;
419 ++*this;
420 return tmp;
423 random_access_iterator_wrapper&
424 operator--()
426 ITERATOR_VERIFY(this->SharedInfo && this->ptr > this->SharedInfo->first);
427 this->ptr--;
428 return *this;
431 random_access_iterator_wrapper
432 operator--(int)
434 random_access_iterator_wrapper<T> tmp = *this;
435 --*this;
436 return tmp;
439 random_access_iterator_wrapper&
440 operator+=(std::ptrdiff_t n)
442 if(n > 0)
444 ITERATOR_VERIFY(n <= this->SharedInfo->last - this->ptr);
445 this->ptr += n;
447 else
449 ITERATOR_VERIFY(n <= this->ptr - this->SharedInfo->first);
450 this->ptr += n;
452 return *this;
455 random_access_iterator_wrapper&
456 operator-=(std::ptrdiff_t n)
457 { return *this += -n; }
459 random_access_iterator_wrapper
460 operator-(std::ptrdiff_t n) const
462 random_access_iterator_wrapper<T> tmp = *this;
463 return tmp -= n;
466 std::ptrdiff_t
467 operator-(const random_access_iterator_wrapper<T>& in) const
469 ITERATOR_VERIFY(this->SharedInfo == in.SharedInfo);
470 return this->ptr - in.ptr;
474 operator[](std::ptrdiff_t n) const
475 { return *(*this + n); }
477 bool
478 operator<(const random_access_iterator_wrapper<T>& in) const
480 ITERATOR_VERIFY(this->SharedInfo == in.SharedInfo);
481 return this->ptr < in.ptr;
484 bool
485 operator>(const random_access_iterator_wrapper<T>& in) const
487 return in < *this;
490 bool
491 operator>=(const random_access_iterator_wrapper<T>& in) const
493 return !(*this < in);
496 bool
497 operator<=(const random_access_iterator_wrapper<T>& in) const
499 return !(*this > in);
503 template<typename T>
504 random_access_iterator_wrapper<T>
505 operator+(random_access_iterator_wrapper<T> it, std::ptrdiff_t n)
506 { return it += n; }
508 template<typename T>
509 random_access_iterator_wrapper<T>
510 operator+(std::ptrdiff_t n, random_access_iterator_wrapper<T> it)
511 { return it += n; }
514 /**
515 * @brief A container-type class for holding iterator wrappers
516 * test_container takes two parameters, a class T and an iterator
517 * wrapper templated by T (for example forward_iterator_wrapper<T>.
518 * It takes two pointers representing a range and presents them as
519 * a container of iterators.
521 template <class T, template<class T> class ItType>
522 struct test_container
524 typename ItType<T>::ContainerType bounds;
525 test_container(T* _first, T* _last):bounds(_first, _last)
528 ItType<T>
529 it(int pos)
531 ITERATOR_VERIFY(pos >= 0 && pos <= (bounds.last - bounds.first));
532 return ItType<T>(bounds.first + pos, &bounds);
535 ItType<T>
536 it(T* pos)
538 ITERATOR_VERIFY(pos >= bounds.first && pos <= bounds.last);
539 return ItType<T>(pos, &bounds);
542 ItType<T>
543 begin()
544 { return it(bounds.first); }
546 ItType<T>
547 end()
548 { return it(bounds.last); }
551 #endif