2 // Iterator Wrappers for the C++ library testsuite.
4 // Copyright (C) 2004-2014 Free Software Foundation, Inc.
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)
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>
37 #ifndef _TESTSUITE_ITERATORS
38 #define _TESTSUITE_ITERATORS
40 #ifdef DISABLE_ITERATOR_DEBUG
41 #define ITERATOR_VERIFY(x)
43 #define ITERATOR_VERIFY(x) VERIFY(x)
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.
55 struct BoundsContainer
59 BoundsContainer(T
* _first
, T
* _last
) : first(_first
), last(_last
)
63 // Simple container for holding state of a set of output iterators.
65 struct OutputContainer
: public BoundsContainer
<T
>
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
++)
78 { delete[] writtento
; }
81 // Produced by output_iterator to allow limited writing to pointer
88 OutputContainer
<T
>* SharedInfo
;
89 WritableObject(T
* ptr_in
,OutputContainer
<T
>* SharedInfo_in
):
90 ptr(ptr_in
), SharedInfo(SharedInfo_in
)
93 #if __cplusplus >= 201103L
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
);
105 operator=(const U
& new_val
)
107 ITERATOR_VERIFY(SharedInfo
->writtento
[ptr
- SharedInfo
->first
] == 0);
108 SharedInfo
->writtento
[ptr
- SharedInfo
->first
] = 1;
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 * instantiated directly, but generated from a test_container
122 struct output_iterator_wrapper
123 : public std::iterator
<std::output_iterator_tag
, T
, std::ptrdiff_t, T
*, T
&>
125 typedef OutputContainer
<T
> ContainerType
;
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
)
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
)
151 SharedInfo
= in
.SharedInfo
;
155 output_iterator_wrapper
&
158 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
159 ITERATOR_VERIFY(ptr
>=SharedInfo
->incrementedto
);
161 SharedInfo
->incrementedto
=ptr
;
165 output_iterator_wrapper
168 output_iterator_wrapper
<T
> tmp
= *this;
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 * instantiated directly, but generated from a test_container
183 class input_iterator_wrapper
184 : public std::iterator
<std::input_iterator_tag
, T
, std::ptrdiff_t, T
*, T
&>
187 input_iterator_wrapper()
191 typedef BoundsContainer
<T
> ContainerType
;
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
)
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
;
212 operator!=(const input_iterator_wrapper
& in
) const
214 return !(*this == in
);
220 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
221 ITERATOR_VERIFY(ptr
>= SharedInfo
->first
);
231 input_iterator_wrapper
&
232 operator=(const input_iterator_wrapper
& in
)
235 SharedInfo
= in
.SharedInfo
;
239 input_iterator_wrapper
&
242 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
243 ITERATOR_VERIFY(ptr
>=SharedInfo
->first
);
245 SharedInfo
->first
=ptr
;
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 * instantiated directly, but generated from a test_container
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()
280 this->SharedInfo
= 0;
286 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
294 forward_iterator_wrapper
&
297 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
302 forward_iterator_wrapper
305 forward_iterator_wrapper
<T
> tmp
= *this;
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 * instantiated directly, but generated from a test_container
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
)
338 this->SharedInfo
= in
.SharedInfo
;
342 bidirectional_iterator_wrapper
&
345 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
350 bidirectional_iterator_wrapper
353 bidirectional_iterator_wrapper
<T
> tmp
= *this;
358 bidirectional_iterator_wrapper
&
361 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
366 bidirectional_iterator_wrapper
369 bidirectional_iterator_wrapper
<T
> tmp
= *this;
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 * instantiated directly, but generated from a test_container
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
)
403 this->SharedInfo
= in
.SharedInfo
;
407 random_access_iterator_wrapper
&
410 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
415 random_access_iterator_wrapper
418 random_access_iterator_wrapper
<T
> tmp
= *this;
423 random_access_iterator_wrapper
&
426 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
431 random_access_iterator_wrapper
434 random_access_iterator_wrapper
<T
> tmp
= *this;
439 random_access_iterator_wrapper
&
440 operator+=(std::ptrdiff_t n
)
444 ITERATOR_VERIFY(n
<= this->SharedInfo
->last
- this->ptr
);
449 ITERATOR_VERIFY(n
<= this->ptr
- this->SharedInfo
->first
);
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;
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
); }
478 operator<(const random_access_iterator_wrapper
<T
>& in
) const
480 ITERATOR_VERIFY(this->SharedInfo
== in
.SharedInfo
);
481 return this->ptr
< in
.ptr
;
485 operator>(const random_access_iterator_wrapper
<T
>& in
) const
491 operator>=(const random_access_iterator_wrapper
<T
>& in
) const
493 return !(*this < in
);
497 operator<=(const random_access_iterator_wrapper
<T
>& in
) const
499 return !(*this > in
);
504 random_access_iterator_wrapper
<T
>
505 operator+(random_access_iterator_wrapper
<T
> it
, std::ptrdiff_t n
)
509 random_access_iterator_wrapper
<T
>
510 operator+(std::ptrdiff_t n
, random_access_iterator_wrapper
<T
> it
)
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 TT
> class ItType
>
522 struct test_container
524 typename ItType
<T
>::ContainerType bounds
;
525 test_container(T
* _first
, T
* _last
):bounds(_first
, _last
)
531 ITERATOR_VERIFY(pos
>= 0 && pos
<= (bounds
.last
- bounds
.first
));
532 return ItType
<T
>(bounds
.first
+ pos
, &bounds
);
538 ITERATOR_VERIFY(pos
>= bounds
.first
&& pos
<= bounds
.last
);
539 return ItType
<T
>(pos
, &bounds
);
544 { return (bounds
.first
)[pos
]; }
548 { return it(bounds
.first
); }
552 { return it(bounds
.last
); }