2 // Iterator Wrappers for the C++ library testsuite.
4 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
23 // This file provides the following:
25 // input_iterator_wrapper, output_iterator_wrapper
26 // forward_iterator_wrapper, bidirectional_iterator_wrapper and
27 // random_access_wrapper, which attempt to exactly perform the requirements
28 // of these types of iterators. These are constructed from the class
29 // test_container, which is given two pointers to T and an iterator type.
31 #include <testsuite_hooks.h>
32 #include <bits/stl_iterator_base_types.h>
33 #include <bits/move.h>
35 #ifndef _TESTSUITE_ITERATORS
36 #define _TESTSUITE_ITERATORS
38 #ifdef DISABLE_ITERATOR_DEBUG
39 #define ITERATOR_VERIFY(x)
41 #define ITERATOR_VERIFY(x) VERIFY(x)
47 * @brief Simple container for holding two pointers.
49 * Note that input_iterator_wrapper changes first to denote
50 * how the valid range of == , ++, etc. change as the iterators are used.
53 struct BoundsContainer
57 BoundsContainer(T
* _first
, T
* _last
) : first(_first
), last(_last
)
61 // Simple container for holding state of a set of output iterators.
63 struct OutputContainer
: public BoundsContainer
<T
>
67 OutputContainer(T
* _first
, T
* _last
)
68 : BoundsContainer
<T
>(_first
, _last
), incrementedto(_first
)
70 writtento
= new bool[this->last
- this->first
];
71 for(int i
= 0; i
< this->last
- this->first
; i
++)
76 { delete[] writtento
; }
79 // Produced by output_iterator to allow limited writing to pointer
86 OutputContainer
<T
>* SharedInfo
;
87 WritableObject(T
* ptr_in
,OutputContainer
<T
>* SharedInfo_in
):
88 ptr(ptr_in
), SharedInfo(SharedInfo_in
)
91 #ifdef __GXX_EXPERIMENTAL_CXX0X__
94 operator=(U
&& new_val
)
96 ITERATOR_VERIFY(SharedInfo
->writtento
[ptr
- SharedInfo
->first
] == 0);
97 SharedInfo
->writtento
[ptr
- SharedInfo
->first
] = 1;
98 *ptr
= std::forward
<U
>(new_val
);
103 operator=(const U
& new_val
)
105 ITERATOR_VERIFY(SharedInfo
->writtento
[ptr
- SharedInfo
->first
] == 0);
106 SharedInfo
->writtento
[ptr
- SharedInfo
->first
] = 1;
113 * @brief output_iterator wrapper for pointer
115 * This class takes a pointer and wraps it to provide exactly
116 * the requirements of a output_iterator. It should not be
117 * instansiated directly, but generated from a test_container
120 struct output_iterator_wrapper
121 : public std::iterator
<std::output_iterator_tag
, T
, std::ptrdiff_t, T
*, T
&>
123 typedef OutputContainer
<T
> ContainerType
;
125 ContainerType
* SharedInfo
;
127 output_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
128 : ptr(_ptr
), SharedInfo(SharedInfo_in
)
130 ITERATOR_VERIFY(ptr
>= SharedInfo
->first
&& ptr
<= SharedInfo
->last
);
133 output_iterator_wrapper(const output_iterator_wrapper
& in
)
134 : ptr(in
.ptr
), SharedInfo(in
.SharedInfo
)
140 ITERATOR_VERIFY(ptr
< SharedInfo
->last
);
141 ITERATOR_VERIFY(SharedInfo
->writtento
[ptr
- SharedInfo
->first
] == false);
142 return WritableObject
<T
>(ptr
, SharedInfo
);
145 output_iterator_wrapper
&
146 operator=(const output_iterator_wrapper
& in
)
149 SharedInfo
= in
.SharedInfo
;
153 output_iterator_wrapper
&
156 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
157 ITERATOR_VERIFY(ptr
>=SharedInfo
->incrementedto
);
159 SharedInfo
->incrementedto
=ptr
;
163 output_iterator_wrapper
166 output_iterator_wrapper
<T
> tmp
= *this;
174 * @brief input_iterator wrapper for pointer
176 * This class takes a pointer and wraps it to provide exactly
177 * the requirements of a input_iterator. It should not be
178 * instansiated directly, but generated from a test_container
181 class input_iterator_wrapper
182 : public std::iterator
<std::input_iterator_tag
, T
, std::ptrdiff_t, T
*, T
&>
185 input_iterator_wrapper()
189 typedef BoundsContainer
<T
> ContainerType
;
191 ContainerType
* SharedInfo
;
193 input_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
194 : ptr(_ptr
), SharedInfo(SharedInfo_in
)
195 { ITERATOR_VERIFY(ptr
>= SharedInfo
->first
&& ptr
<= SharedInfo
->last
); }
197 input_iterator_wrapper(const input_iterator_wrapper
& in
)
198 : ptr(in
.ptr
), SharedInfo(in
.SharedInfo
)
202 operator==(const input_iterator_wrapper
& in
) const
204 ITERATOR_VERIFY(SharedInfo
&& SharedInfo
== in
.SharedInfo
);
205 ITERATOR_VERIFY(ptr
>=SharedInfo
->first
&& in
.ptr
>=SharedInfo
->first
);
206 return ptr
== in
.ptr
;
210 operator!=(const input_iterator_wrapper
& in
) const
212 return !(*this == in
);
218 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
219 ITERATOR_VERIFY(ptr
>= SharedInfo
->first
);
229 input_iterator_wrapper
&
230 operator=(const input_iterator_wrapper
& in
)
233 SharedInfo
= in
.SharedInfo
;
237 input_iterator_wrapper
&
240 ITERATOR_VERIFY(SharedInfo
&& ptr
< SharedInfo
->last
);
241 ITERATOR_VERIFY(ptr
>=SharedInfo
->first
);
243 SharedInfo
->first
=ptr
;
256 * @brief forward_iterator wrapper for pointer
258 * This class takes a pointer and wraps it to provide exactly
259 * the requirements of a forward_iterator. It should not be
260 * instansiated directly, but generated from a test_container
263 struct forward_iterator_wrapper
: public input_iterator_wrapper
<T
>
265 typedef BoundsContainer
<T
> ContainerType
;
266 typedef std::forward_iterator_tag iterator_category
;
267 forward_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
268 : input_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
271 forward_iterator_wrapper(const forward_iterator_wrapper
& in
)
272 : input_iterator_wrapper
<T
>(in
)
275 forward_iterator_wrapper()
278 this->SharedInfo
= 0;
284 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
292 forward_iterator_wrapper
&
295 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
300 forward_iterator_wrapper
303 forward_iterator_wrapper
<T
> tmp
= *this;
310 * @brief bidirectional_iterator wrapper for pointer
312 * This class takes a pointer and wraps it to provide exactly
313 * the requirements of a forward_iterator. It should not be
314 * instansiated directly, but generated from a test_container
317 struct bidirectional_iterator_wrapper
: public forward_iterator_wrapper
<T
>
319 typedef BoundsContainer
<T
> ContainerType
;
320 typedef std::bidirectional_iterator_tag iterator_category
;
321 bidirectional_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
322 : forward_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
325 bidirectional_iterator_wrapper(const bidirectional_iterator_wrapper
& in
)
326 : forward_iterator_wrapper
<T
>(in
)
329 bidirectional_iterator_wrapper(): forward_iterator_wrapper
<T
>()
332 bidirectional_iterator_wrapper
&
333 operator=(const bidirectional_iterator_wrapper
& in
)
336 this->SharedInfo
= in
.SharedInfo
;
340 bidirectional_iterator_wrapper
&
343 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
348 bidirectional_iterator_wrapper
351 bidirectional_iterator_wrapper
<T
> tmp
= *this;
356 bidirectional_iterator_wrapper
&
359 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
364 bidirectional_iterator_wrapper
367 bidirectional_iterator_wrapper
<T
> tmp
= *this;
374 * @brief random_access_iterator wrapper for pointer
376 * This class takes a pointer and wraps it to provide exactly
377 * the requirements of a forward_iterator. It should not be
378 * instansiated directly, but generated from a test_container
381 struct random_access_iterator_wrapper
382 : public bidirectional_iterator_wrapper
<T
>
384 typedef BoundsContainer
<T
> ContainerType
;
385 typedef std::random_access_iterator_tag iterator_category
;
386 random_access_iterator_wrapper(T
* _ptr
, ContainerType
* SharedInfo_in
)
387 : bidirectional_iterator_wrapper
<T
>(_ptr
, SharedInfo_in
)
390 random_access_iterator_wrapper(const random_access_iterator_wrapper
<T
>& in
)
391 : bidirectional_iterator_wrapper
<T
>(in
)
394 random_access_iterator_wrapper():bidirectional_iterator_wrapper
<T
>()
397 random_access_iterator_wrapper
&
398 operator=(const random_access_iterator_wrapper
& in
)
401 this->SharedInfo
= in
.SharedInfo
;
405 random_access_iterator_wrapper
&
408 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
< this->SharedInfo
->last
);
413 random_access_iterator_wrapper
416 random_access_iterator_wrapper
<T
> tmp
= *this;
421 random_access_iterator_wrapper
&
424 ITERATOR_VERIFY(this->SharedInfo
&& this->ptr
> this->SharedInfo
->first
);
429 random_access_iterator_wrapper
432 random_access_iterator_wrapper
<T
> tmp
= *this;
437 random_access_iterator_wrapper
&
438 operator+=(std::ptrdiff_t n
)
442 ITERATOR_VERIFY(n
<= this->SharedInfo
->last
- this->ptr
);
447 ITERATOR_VERIFY(n
<= this->ptr
- this->SharedInfo
->first
);
453 random_access_iterator_wrapper
&
454 operator-=(std::ptrdiff_t n
)
455 { return *this += -n
; }
457 random_access_iterator_wrapper
458 operator-(std::ptrdiff_t n
) const
460 random_access_iterator_wrapper
<T
> tmp
= *this;
465 operator-(const random_access_iterator_wrapper
<T
>& in
) const
467 ITERATOR_VERIFY(this->SharedInfo
== in
.SharedInfo
);
468 return this->ptr
- in
.ptr
;
472 operator[](std::ptrdiff_t n
) const
473 { return *(*this + n
); }
476 operator<(const random_access_iterator_wrapper
<T
>& in
) const
478 ITERATOR_VERIFY(this->SharedInfo
== in
.SharedInfo
);
479 return this->ptr
< in
.ptr
;
483 operator>(const random_access_iterator_wrapper
<T
>& in
) const
489 operator>=(const random_access_iterator_wrapper
<T
>& in
) const
491 return !(*this < in
);
495 operator<=(const random_access_iterator_wrapper
<T
>& in
) const
497 return !(*this > in
);
502 random_access_iterator_wrapper
<T
>
503 operator+(random_access_iterator_wrapper
<T
> it
, std::ptrdiff_t n
)
507 random_access_iterator_wrapper
<T
>
508 operator+(std::ptrdiff_t n
, random_access_iterator_wrapper
<T
> it
)
513 * @brief A container-type class for holding iterator wrappers
514 * test_container takes two parameters, a class T and an iterator
515 * wrapper templated by T (for example forward_iterator_wrapper<T>.
516 * It takes two pointers representing a range and presents them as
517 * a container of iterators.
519 template <class T
, template<class T
> class ItType
>
520 struct test_container
522 typename ItType
<T
>::ContainerType bounds
;
523 test_container(T
* _first
, T
* _last
):bounds(_first
, _last
)
529 ITERATOR_VERIFY(pos
>= 0 && pos
<= (bounds
.last
- bounds
.first
));
530 return ItType
<T
>(bounds
.first
+ pos
, &bounds
);
536 ITERATOR_VERIFY(pos
>= bounds
.first
&& pos
<= bounds
.last
);
537 return ItType
<T
>(pos
, &bounds
);
542 { return it(bounds
.first
); }
546 { return it(bounds
.last
); }