2 // Iterator Wrappers for the C++ library testsuite.
4 // Copyright (C) 2004-2017 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;
184 * @brief input_iterator wrapper for pointer
186 * This class takes a pointer and wraps it to provide exactly
187 * the requirements of a input_iterator. It should not be
188 * instantiated directly, but generated from a test_container
191 class input_iterator_wrapper
192 : public std::iterator
<std::input_iterator_tag
, T
, std::ptrdiff_t, T
*, T
&>
195 input_iterator_wrapper()
199 typedef BoundsContainer
<T
> ContainerType
;
201 ContainerType
* SharedInfo
;
203 input_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
204 : ptr(_ptr
), SharedInfo(SharedInfo_in
)
205 { ITERATOR_VERIFY(ptr
>= SharedInfo
->first
&& ptr
<= SharedInfo
->last
); }
207 input_iterator_wrapper(const input_iterator_wrapper
& in
)
208 : ptr(in
.ptr
), SharedInfo(in
.SharedInfo
)
212 operator==(const input_iterator_wrapper
& in
) const
214 ITERATOR_VERIFY(SharedInfo
&& SharedInfo
== in
.SharedInfo
);
215 ITERATOR_VERIFY(ptr
>=SharedInfo
->first
&& in
.ptr
>=SharedInfo
->first
);
216 return ptr
== in
.ptr
;
220 operator!=(const input_iterator_wrapper
& in
) const
222 return !(*this == in
);
228 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
229 ITERATOR_VERIFY(ptr
>= SharedInfo
->first
);
239 input_iterator_wrapper
&
240 operator=(const input_iterator_wrapper
& in
)
243 SharedInfo
= in
.SharedInfo
;
247 input_iterator_wrapper
&
250 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
251 ITERATOR_VERIFY(ptr
>=SharedInfo
->first
);
253 SharedInfo
->first
=ptr
;
263 #if __cplusplus >= 201103L
265 void operator,(const U
&) const = delete;
269 void operator,(const U
&) const;
275 * @brief forward_iterator wrapper for pointer
277 * This class takes a pointer and wraps it to provide exactly
278 * the requirements of a forward_iterator. It should not be
279 * instantiated directly, but generated from a test_container
282 struct forward_iterator_wrapper
: public input_iterator_wrapper
<T
>
284 typedef BoundsContainer
<T
> ContainerType
;
285 typedef std::forward_iterator_tag iterator_category
;
286 forward_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
287 : input_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
290 forward_iterator_wrapper(const forward_iterator_wrapper
& in
)
291 : input_iterator_wrapper
<T
>(in
)
294 forward_iterator_wrapper()
297 this->SharedInfo
= 0;
303 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
311 forward_iterator_wrapper
&
314 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
319 forward_iterator_wrapper
322 forward_iterator_wrapper
<T
> tmp
= *this;
329 * @brief bidirectional_iterator wrapper for pointer
331 * This class takes a pointer and wraps it to provide exactly
332 * the requirements of a forward_iterator. It should not be
333 * instantiated directly, but generated from a test_container
336 struct bidirectional_iterator_wrapper
: public forward_iterator_wrapper
<T
>
338 typedef BoundsContainer
<T
> ContainerType
;
339 typedef std::bidirectional_iterator_tag iterator_category
;
340 bidirectional_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
341 : forward_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
344 bidirectional_iterator_wrapper(const bidirectional_iterator_wrapper
& in
)
345 : forward_iterator_wrapper
<T
>(in
)
348 bidirectional_iterator_wrapper(): forward_iterator_wrapper
<T
>()
351 bidirectional_iterator_wrapper
&
352 operator=(const bidirectional_iterator_wrapper
& in
)
355 this->SharedInfo
= in
.SharedInfo
;
359 bidirectional_iterator_wrapper
&
362 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
367 bidirectional_iterator_wrapper
370 bidirectional_iterator_wrapper
<T
> tmp
= *this;
375 bidirectional_iterator_wrapper
&
378 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
383 bidirectional_iterator_wrapper
386 bidirectional_iterator_wrapper
<T
> tmp
= *this;
393 * @brief random_access_iterator wrapper for pointer
395 * This class takes a pointer and wraps it to provide exactly
396 * the requirements of a forward_iterator. It should not be
397 * instantiated directly, but generated from a test_container
400 struct random_access_iterator_wrapper
401 : public bidirectional_iterator_wrapper
<T
>
403 typedef BoundsContainer
<T
> ContainerType
;
404 typedef std::random_access_iterator_tag iterator_category
;
405 random_access_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
406 : bidirectional_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
409 random_access_iterator_wrapper(const random_access_iterator_wrapper
<T
>& in
)
410 : bidirectional_iterator_wrapper
<T
>(in
)
413 random_access_iterator_wrapper():bidirectional_iterator_wrapper
<T
>()
416 random_access_iterator_wrapper
&
417 operator=(const random_access_iterator_wrapper
& in
)
420 this->SharedInfo
= in
.SharedInfo
;
424 random_access_iterator_wrapper
&
427 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
432 random_access_iterator_wrapper
435 random_access_iterator_wrapper
<T
> tmp
= *this;
440 random_access_iterator_wrapper
&
443 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
448 random_access_iterator_wrapper
451 random_access_iterator_wrapper
<T
> tmp
= *this;
456 random_access_iterator_wrapper
&
457 operator+=(std::ptrdiff_t n
)
461 ITERATOR_VERIFY(n
<= this->SharedInfo
->last
- this->ptr
);
466 ITERATOR_VERIFY(n
<= this->ptr
- this->SharedInfo
->first
);
472 random_access_iterator_wrapper
&
473 operator-=(std::ptrdiff_t n
)
474 { return *this += -n
; }
476 random_access_iterator_wrapper
477 operator-(std::ptrdiff_t n
) const
479 random_access_iterator_wrapper
<T
> tmp
= *this;
484 operator-(const random_access_iterator_wrapper
<T
>& in
) const
486 ITERATOR_VERIFY(this->SharedInfo
== in
.SharedInfo
);
487 return this->ptr
- in
.ptr
;
491 operator[](std::ptrdiff_t n
) const
492 { return *(*this + n
); }
495 operator<(const random_access_iterator_wrapper
<T
>& in
) const
497 ITERATOR_VERIFY(this->SharedInfo
== in
.SharedInfo
);
498 return this->ptr
< in
.ptr
;
502 operator>(const random_access_iterator_wrapper
<T
>& in
) const
508 operator>=(const random_access_iterator_wrapper
<T
>& in
) const
510 return !(*this < in
);
514 operator<=(const random_access_iterator_wrapper
<T
>& in
) const
516 return !(*this > in
);
521 random_access_iterator_wrapper
<T
>
522 operator+(random_access_iterator_wrapper
<T
> it
, std::ptrdiff_t n
)
526 random_access_iterator_wrapper
<T
>
527 operator+(std::ptrdiff_t n
, random_access_iterator_wrapper
<T
> it
)
532 * @brief A container-type class for holding iterator wrappers
533 * test_container takes two parameters, a class T and an iterator
534 * wrapper templated by T (for example forward_iterator_wrapper<T>.
535 * It takes two pointers representing a range and presents them as
536 * a container of iterators.
538 template <class T
, template<class TT
> class ItType
>
539 struct test_container
541 typename ItType
<T
>::ContainerType bounds
;
542 test_container(T
* _first
, T
* _last
):bounds(_first
, _last
)
545 #if __cplusplus >= 201103L
546 template<std::size_t N
>
548 test_container(T (&arr
)[N
]) : test_container(arr
, arr
+N
)
555 ITERATOR_VERIFY(pos
>= 0 && pos
<= (bounds
.last
- bounds
.first
));
556 return ItType
<T
>(bounds
.first
+ pos
, &bounds
);
562 ITERATOR_VERIFY(pos
>= bounds
.first
&& pos
<= bounds
.last
);
563 return ItType
<T
>(pos
, &bounds
);
568 { return (bounds
.first
)[pos
]; }
572 { return it(bounds
.first
); }
576 { return it(bounds
.last
); }