2 // Iterator Wrappers for the C++ library testsuite.
4 // Copyright (C) 2004-2018 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;
173 #if __cplusplus >= 201103L
175 void operator,(const U
&) const = delete;
179 void operator,(const U
&) const;
183 #if __cplusplus >= 201103L
184 template<typename T
, typename U
>
185 void operator,(const T
&, const output_iterator_wrapper
<U
>&) = delete;
189 * @brief input_iterator wrapper for pointer
191 * This class takes a pointer and wraps it to provide exactly
192 * the requirements of a input_iterator. It should not be
193 * instantiated directly, but generated from a test_container
196 class input_iterator_wrapper
197 : public std::iterator
<std::input_iterator_tag
, T
, std::ptrdiff_t, T
*, T
&>
200 input_iterator_wrapper()
204 typedef BoundsContainer
<T
> ContainerType
;
206 ContainerType
* SharedInfo
;
208 input_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
209 : ptr(_ptr
), SharedInfo(SharedInfo_in
)
210 { ITERATOR_VERIFY(ptr
>= SharedInfo
->first
&& ptr
<= SharedInfo
->last
); }
212 input_iterator_wrapper(const input_iterator_wrapper
& in
)
213 : ptr(in
.ptr
), SharedInfo(in
.SharedInfo
)
217 operator==(const input_iterator_wrapper
& in
) const
219 ITERATOR_VERIFY(SharedInfo
&& SharedInfo
== in
.SharedInfo
);
220 ITERATOR_VERIFY(ptr
>=SharedInfo
->first
&& in
.ptr
>=SharedInfo
->first
);
221 return ptr
== in
.ptr
;
225 operator!=(const input_iterator_wrapper
& in
) const
227 return !(*this == in
);
233 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
234 ITERATOR_VERIFY(ptr
>= SharedInfo
->first
);
244 input_iterator_wrapper
&
245 operator=(const input_iterator_wrapper
& in
)
248 SharedInfo
= in
.SharedInfo
;
252 input_iterator_wrapper
&
255 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
256 ITERATOR_VERIFY(ptr
>=SharedInfo
->first
);
258 SharedInfo
->first
=ptr
;
268 #if __cplusplus >= 201103L
270 void operator,(const U
&) const = delete;
274 void operator,(const U
&) const;
278 #if __cplusplus >= 201103L
279 template<typename T
, typename U
>
280 void operator,(const T
&, const input_iterator_wrapper
<U
>&) = delete;
284 * @brief forward_iterator wrapper for pointer
286 * This class takes a pointer and wraps it to provide exactly
287 * the requirements of a forward_iterator. It should not be
288 * instantiated directly, but generated from a test_container
291 struct forward_iterator_wrapper
: public input_iterator_wrapper
<T
>
293 typedef BoundsContainer
<T
> ContainerType
;
294 typedef std::forward_iterator_tag iterator_category
;
295 forward_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
296 : input_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
299 forward_iterator_wrapper(const forward_iterator_wrapper
& in
)
300 : input_iterator_wrapper
<T
>(in
)
303 forward_iterator_wrapper()
306 this->SharedInfo
= 0;
312 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
320 forward_iterator_wrapper
&
323 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
328 forward_iterator_wrapper
331 forward_iterator_wrapper
<T
> tmp
= *this;
338 * @brief bidirectional_iterator wrapper for pointer
340 * This class takes a pointer and wraps it to provide exactly
341 * the requirements of a forward_iterator. It should not be
342 * instantiated directly, but generated from a test_container
345 struct bidirectional_iterator_wrapper
: public forward_iterator_wrapper
<T
>
347 typedef BoundsContainer
<T
> ContainerType
;
348 typedef std::bidirectional_iterator_tag iterator_category
;
349 bidirectional_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
350 : forward_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
353 bidirectional_iterator_wrapper(const bidirectional_iterator_wrapper
& in
)
354 : forward_iterator_wrapper
<T
>(in
)
357 bidirectional_iterator_wrapper(): forward_iterator_wrapper
<T
>()
360 bidirectional_iterator_wrapper
&
361 operator=(const bidirectional_iterator_wrapper
& in
)
364 this->SharedInfo
= in
.SharedInfo
;
368 bidirectional_iterator_wrapper
&
371 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
376 bidirectional_iterator_wrapper
379 bidirectional_iterator_wrapper
<T
> tmp
= *this;
384 bidirectional_iterator_wrapper
&
387 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
392 bidirectional_iterator_wrapper
395 bidirectional_iterator_wrapper
<T
> tmp
= *this;
402 * @brief random_access_iterator wrapper for pointer
404 * This class takes a pointer and wraps it to provide exactly
405 * the requirements of a forward_iterator. It should not be
406 * instantiated directly, but generated from a test_container
409 struct random_access_iterator_wrapper
410 : public bidirectional_iterator_wrapper
<T
>
412 typedef BoundsContainer
<T
> ContainerType
;
413 typedef std::random_access_iterator_tag iterator_category
;
414 random_access_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
415 : bidirectional_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
418 random_access_iterator_wrapper(const random_access_iterator_wrapper
<T
>& in
)
419 : bidirectional_iterator_wrapper
<T
>(in
)
422 random_access_iterator_wrapper():bidirectional_iterator_wrapper
<T
>()
425 random_access_iterator_wrapper
&
426 operator=(const random_access_iterator_wrapper
& in
)
429 this->SharedInfo
= in
.SharedInfo
;
433 random_access_iterator_wrapper
&
436 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
441 random_access_iterator_wrapper
444 random_access_iterator_wrapper
<T
> tmp
= *this;
449 random_access_iterator_wrapper
&
452 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
457 random_access_iterator_wrapper
460 random_access_iterator_wrapper
<T
> tmp
= *this;
465 random_access_iterator_wrapper
&
466 operator+=(std::ptrdiff_t n
)
470 ITERATOR_VERIFY(n
<= this->SharedInfo
->last
- this->ptr
);
475 ITERATOR_VERIFY(n
<= this->ptr
- this->SharedInfo
->first
);
481 random_access_iterator_wrapper
&
482 operator-=(std::ptrdiff_t n
)
483 { return *this += -n
; }
485 random_access_iterator_wrapper
486 operator-(std::ptrdiff_t n
) const
488 random_access_iterator_wrapper
<T
> tmp
= *this;
493 operator-(const random_access_iterator_wrapper
<T
>& in
) const
495 ITERATOR_VERIFY(this->SharedInfo
== in
.SharedInfo
);
496 return this->ptr
- in
.ptr
;
500 operator[](std::ptrdiff_t n
) const
501 { return *(*this + n
); }
504 operator<(const random_access_iterator_wrapper
<T
>& in
) const
506 ITERATOR_VERIFY(this->SharedInfo
== in
.SharedInfo
);
507 return this->ptr
< in
.ptr
;
511 operator>(const random_access_iterator_wrapper
<T
>& in
) const
517 operator>=(const random_access_iterator_wrapper
<T
>& in
) const
519 return !(*this < in
);
523 operator<=(const random_access_iterator_wrapper
<T
>& in
) const
525 return !(*this > in
);
530 random_access_iterator_wrapper
<T
>
531 operator+(random_access_iterator_wrapper
<T
> it
, std::ptrdiff_t n
)
535 random_access_iterator_wrapper
<T
>
536 operator+(std::ptrdiff_t n
, random_access_iterator_wrapper
<T
> it
)
541 * @brief A container-type class for holding iterator wrappers
542 * test_container takes two parameters, a class T and an iterator
543 * wrapper templated by T (for example forward_iterator_wrapper<T>.
544 * It takes two pointers representing a range and presents them as
545 * a container of iterators.
547 template <class T
, template<class TT
> class ItType
>
548 struct test_container
550 typename ItType
<T
>::ContainerType bounds
;
551 test_container(T
* _first
, T
* _last
) : bounds(_first
, _last
)
554 #if __cplusplus >= 201103L
555 template<std::size_t N
>
557 test_container(T (&arr
)[N
]) : test_container(arr
, arr
+N
)
564 ITERATOR_VERIFY(pos
>= 0 && pos
<= (bounds
.last
- bounds
.first
));
565 return ItType
<T
>(bounds
.first
+ pos
, &bounds
);
571 ITERATOR_VERIFY(pos
>= bounds
.first
&& pos
<= bounds
.last
);
572 return ItType
<T
>(pos
, &bounds
);
577 { return (bounds
.first
)[pos
]; }
581 { return it(bounds
.first
); }
585 { return it(bounds
.last
); }