Debian Codegraph @

libstdc++-v3/testsuite/util/testsuite_iterators.h

raw
1 // -*- C++ -*-
2 // Iterator Wrappers for the C++ library testsuite.
3 //
4 // Copyright (C) 2004-2025 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.
11 //
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.
16 //
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/>.
20 //
21
22 // This file provides the following:
23 //
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.
29
30 #include <testsuite_hooks.h>
31 #include <bits/stl_iterator_base_types.h>
32
33 #if __cplusplus >= 201103L
34 #include <bits/move.h>
35 #endif
36
37 #if __cplusplus > 201703L
38 #include <bits/max_size_type.h>
39 #endif
40
41 #ifndef _TESTSUITE_ITERATORS
42 #define _TESTSUITE_ITERATORS
43
44 #ifdef DISABLE_ITERATOR_DEBUG
45 #define ITERATOR_VERIFY(x)
46 #else
47 #define ITERATOR_VERIFY(x) VERIFY(x)
48 #endif
49
50 namespace __gnu_test
51 {
52 /**
53 * @brief Simple container for holding two pointers.
54 *
55 * Note that input_iterator_wrapper changes first to denote
56 * how the valid range of == , ++, etc. change as the iterators are used.
57 */
58 template<typename T>
59 struct BoundsContainer
60 {
61 T* first;
62 T* last;
63
64 BoundsContainer(T* _first, T* _last) : first(_first), last(_last)
65 { }
66
67 std::size_t size() const { return last - first; }
68 };
69
70 // Simple container for holding state of a set of output iterators.
71 template<typename T>
72 struct OutputContainer : public BoundsContainer<T>
73 {
74 T* incrementedto;
75 bool* writtento;
76
77 OutputContainer(T* _first, T* _last)
78 : BoundsContainer<T>(_first, _last), incrementedto(_first),
79 writtento(new bool[this->size()]())
80 { }
81
82 ~OutputContainer()
83 { delete[] writtento; }
84 };
85
86 // Produced by output_iterator to allow limited writing to pointer
87 template<class T>
88 class WritableObject
89 {
90 T* ptr;
91
92 public:
93 OutputContainer<T>* SharedInfo;
94
95 WritableObject(T* ptr_in, OutputContainer<T>* SharedInfo_in):
96 ptr(ptr_in), SharedInfo(SharedInfo_in)
97 { }
98
99 #if __cplusplus >= 201103L
100 template<class U>
101 typename std::enable_if<std::is_assignable<T&, U>::value>::type
102 operator=(U&& new_val) const
103 {
104 ITERATOR_VERIFY(SharedInfo->writtento[ptr - SharedInfo->first] == 0);
105 SharedInfo->writtento[ptr - SharedInfo->first] = 1;
106 *ptr = std::forward<U>(new_val);
107 }
108 #else
109 template<class U>
110 void
111 operator=(const U& new_val)
112 {
113 ITERATOR_VERIFY(SharedInfo->writtento[ptr - SharedInfo->first] == 0);
114 SharedInfo->writtento[ptr - SharedInfo->first] = 1;
115 *ptr = new_val;
116 }
117 #endif
118 };
119
120 /**
121 * @brief output_iterator wrapper for pointer
122 *
123 * This class takes a pointer and wraps it to provide exactly
124 * the requirements of a output_iterator. It should not be
125 * instantiated directly, but generated from a test_container
126 */
127 template<class T>
128 struct output_iterator_wrapper
129 {
130 protected:
131 output_iterator_wrapper() : ptr(0), SharedInfo(0)
132 { }
133
134 public:
135 typedef std::output_iterator_tag iterator_category;
136 typedef T value_type;
137 typedef std::ptrdiff_t difference_type;
138 typedef T* pointer;
139 typedef T& reference;
140
141 typedef OutputContainer<T> ContainerType;
142 T* ptr;
143 ContainerType* SharedInfo;
144
145 output_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
146 : ptr(_ptr), SharedInfo(SharedInfo_in)
147 {
148 ITERATOR_VERIFY(ptr >= SharedInfo->first && ptr <= SharedInfo->last);
149 }
150
151 #if __cplusplus >= 201103L
152 output_iterator_wrapper(const output_iterator_wrapper&) = default;
153
154 output_iterator_wrapper&
155 operator=(const output_iterator_wrapper&) = default;
156 #endif
157
158 WritableObject<T>
159 operator*() const
160 {
161 ITERATOR_VERIFY(ptr < SharedInfo->last);
162 ITERATOR_VERIFY(SharedInfo->writtento[ptr - SharedInfo->first] == false);
163 return WritableObject<T>(ptr, SharedInfo);
164 }
165
166 output_iterator_wrapper&
167 operator++()
168 {
169 ITERATOR_VERIFY(SharedInfo && ptr < SharedInfo->last);
170 ITERATOR_VERIFY(ptr>=SharedInfo->incrementedto);
171 ptr++;
172 SharedInfo->incrementedto=ptr;
173 return *this;
174 }
175
176 output_iterator_wrapper
177 operator++(int)
178 {
179 output_iterator_wrapper<T> tmp = *this;
180 ++*this;
181 return tmp;
182 }
183
184 #if __cplusplus >= 201103L
185 template<typename U>
186 void operator,(const U&) const = delete;
187
188 void operator&() const = delete;
189 #else
190 private:
191 template<typename U>
192 void operator,(const U&) const;
193
194 void operator&() const;
195 #endif
196 };
197
198 #if __cplusplus >= 201103L
199 template<typename T, typename U>
200 void operator,(const T&, const output_iterator_wrapper<U>&) = delete;
201 #endif
202
203 #if __cplusplus >= 201103L
204 using std::remove_cv;
205 #else
206 template<typename T> struct remove_cv { typedef T type; };
207 template<typename T> struct remove_cv<const T> { typedef T type; };
208 template<typename T> struct remove_cv<volatile T> { typedef T type; };
209 template<typename T> struct remove_cv<const volatile T> { typedef T type; };
210 #endif
211
212 /**
213 * @brief input_iterator wrapper for pointer
214 *
215 * This class takes a pointer and wraps it to provide exactly
216 * the requirements of a input_iterator. It should not be
217 * instantiated directly, but generated from a test_container
218 */
219 template<class T>
220 class input_iterator_wrapper
221 {
222 struct post_inc_proxy
223 {
224 struct deref_proxy
225 {
226 T* ptr;
227 operator const T&() const { return *ptr; }
228 } p;
229
230 deref_proxy operator*() const { return p; }
231 };
232
233 protected:
234 input_iterator_wrapper() : ptr(0), SharedInfo(0)
235 { }
236
237 public:
238 typedef std::input_iterator_tag iterator_category;
239 typedef typename remove_cv<T>::type value_type;
240 typedef std::ptrdiff_t difference_type;
241 typedef T* pointer;
242 typedef T& reference;
243
244 typedef BoundsContainer<T> ContainerType;
245 T* ptr;
246 ContainerType* SharedInfo;
247
248 input_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
249 : ptr(_ptr), SharedInfo(SharedInfo_in)
250 { ITERATOR_VERIFY(ptr >= SharedInfo->first && ptr <= SharedInfo->last); }
251
252 #if __cplusplus >= 201103L
253 input_iterator_wrapper(const input_iterator_wrapper&) = default;
254
255 input_iterator_wrapper&
256 operator=(const input_iterator_wrapper&) = default;
257 #endif
258
259 bool
260 operator==(const input_iterator_wrapper& in) const
261 {
262 ITERATOR_VERIFY(SharedInfo && SharedInfo == in.SharedInfo);
263 ITERATOR_VERIFY(ptr>=SharedInfo->first && in.ptr>=SharedInfo->first);
264 return ptr == in.ptr;
265 }
266
267 bool
268 operator!=(const input_iterator_wrapper& in) const
269 {
270 return !(*this == in);
271 }
272
273 T&
274 operator*() const
275 {
276 ITERATOR_VERIFY(SharedInfo && ptr < SharedInfo->last);
277 ITERATOR_VERIFY(ptr >= SharedInfo->first);
278 return *ptr;
279 }
280
281 T*
282 operator->() const
283 {
284 return &**this;
285 }
286
287 input_iterator_wrapper&
288 operator++()
289 {
290 ITERATOR_VERIFY(SharedInfo && ptr < SharedInfo->last);
291 ITERATOR_VERIFY(ptr>=SharedInfo->first);
292 ptr++;
293 SharedInfo->first=ptr;
294 return *this;
295 }
296
297 post_inc_proxy
298 operator++(int)
299 {
300 post_inc_proxy tmp = { { ptr } };
301 ++*this;
302 return tmp;
303 }
304
305 #if __cplusplus >= 201103L
306 template<typename U>
307 void operator,(const U&) const = delete;
308
309 void operator&() const = delete;
310 #else
311 private:
312 template<typename U>
313 void operator,(const U&) const;
314
315 void operator&() const;
316 #endif
317 };
318
319 #if __cplusplus >= 201103L
320 template<typename T, typename U>
321 void operator,(const T&, const input_iterator_wrapper<U>&) = delete;
322 #endif
323
324 /**
325 * @brief forward_iterator wrapper for pointer
326 *
327 * This class takes a pointer and wraps it to provide exactly
328 * the requirements of a forward_iterator. It should not be
329 * instantiated directly, but generated from a test_container
330 */
331 template<class T>
332 struct forward_iterator_wrapper : public input_iterator_wrapper<T>
333 {
334 typedef BoundsContainer<T> ContainerType;
335 typedef std::forward_iterator_tag iterator_category;
336
337 forward_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
338 : input_iterator_wrapper<T>(_ptr, SharedInfo_in)
339 { }
340
341 forward_iterator_wrapper()
342 { }
343
344 #if __cplusplus >= 201103L
345 forward_iterator_wrapper(const forward_iterator_wrapper&) = default;
346
347 forward_iterator_wrapper&
348 operator=(const forward_iterator_wrapper&) = default;
349 #endif
350
351 T&
352 operator*() const
353 {
354 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
355 return *(this->ptr);
356 }
357
358 T*
359 operator->() const
360 { return &**this; }
361
362 forward_iterator_wrapper&
363 operator++()
364 {
365 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
366 this->ptr++;
367 return *this;
368 }
369
370 forward_iterator_wrapper
371 operator++(int)
372 {
373 forward_iterator_wrapper<T> tmp = *this;
374 ++*this;
375 return tmp;
376 }
377
378 #if __cplusplus >= 201402L
379 bool
380 operator==(const forward_iterator_wrapper& it) const noexcept
381 {
382 // Since C++14 value-initialized forward iterators are comparable.
383 if (this->SharedInfo == nullptr || it.SharedInfo == nullptr)
384 return this->SharedInfo == it.SharedInfo && this->ptr == it.ptr;
385
386 const input_iterator_wrapper<T>& base_this = *this;
387 const input_iterator_wrapper<T>& base_that = it;
388 return base_this == base_that;
389 }
390
391 bool
392 operator!=(const forward_iterator_wrapper& it) const noexcept
393 {
394 return !(*this == it);
395 }
396 #endif
397 };
398
399 /**
400 * @brief bidirectional_iterator wrapper for pointer
401 *
402 * This class takes a pointer and wraps it to provide exactly
403 * the requirements of a bidirectional_iterator. It should not be
404 * instantiated directly, but generated from a test_container
405 */
406 template<class T>
407 struct bidirectional_iterator_wrapper : public forward_iterator_wrapper<T>
408 {
409 typedef BoundsContainer<T> ContainerType;
410 typedef std::bidirectional_iterator_tag iterator_category;
411
412 bidirectional_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
413 : forward_iterator_wrapper<T>(_ptr, SharedInfo_in)
414 { }
415
416 bidirectional_iterator_wrapper()
417 : forward_iterator_wrapper<T>()
418 { }
419
420 #if __cplusplus >= 201103L
421 bidirectional_iterator_wrapper(
422 const bidirectional_iterator_wrapper&) = default;
423
424 bidirectional_iterator_wrapper&
425 operator=(const bidirectional_iterator_wrapper&) = default;
426 #endif
427
428 bidirectional_iterator_wrapper&
429 operator++()
430 {
431 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
432 this->ptr++;
433 return *this;
434 }
435
436 bidirectional_iterator_wrapper
437 operator++(int)
438 {
439 bidirectional_iterator_wrapper<T> tmp = *this;
440 ++*this;
441 return tmp;
442 }
443
444 bidirectional_iterator_wrapper&
445 operator--()
446 {
447 ITERATOR_VERIFY(this->SharedInfo && this->ptr > this->SharedInfo->first);
448 this->ptr--;
449 return *this;
450 }
451
452 bidirectional_iterator_wrapper
453 operator--(int)
454 {
455 bidirectional_iterator_wrapper<T> tmp = *this;
456 --*this;
457 return tmp;
458 }
459 };
460
461 /**
462 * @brief random_access_iterator wrapper for pointer
463 *
464 * This class takes a pointer and wraps it to provide exactly
465 * the requirements of a random_access_iterator. It should not be
466 * instantiated directly, but generated from a test_container
467 */
468 template<class T>
469 struct random_access_iterator_wrapper
470 : public bidirectional_iterator_wrapper<T>
471 {
472 typedef BoundsContainer<T> ContainerType;
473 typedef std::random_access_iterator_tag iterator_category;
474
475 random_access_iterator_wrapper(T* _ptr, ContainerType* SharedInfo_in)
476 : bidirectional_iterator_wrapper<T>(_ptr, SharedInfo_in)
477 { }
478
479 random_access_iterator_wrapper()
480 : bidirectional_iterator_wrapper<T>()
481 { }
482
483 #if __cplusplus >= 201103L
484 random_access_iterator_wrapper(
485 const random_access_iterator_wrapper&) = default;
486
487 random_access_iterator_wrapper&
488 operator=(const random_access_iterator_wrapper&) = default;
489 #endif
490
491 random_access_iterator_wrapper&
492 operator++()
493 {
494 ITERATOR_VERIFY(this->SharedInfo && this->ptr < this->SharedInfo->last);
495 this->ptr++;
496 return *this;
497 }
498
499 random_access_iterator_wrapper
500 operator++(int)
501 {
502 random_access_iterator_wrapper<T> tmp = *this;
503 ++*this;
504 return tmp;
505 }
506
507 random_access_iterator_wrapper&
508 operator--()
509 {
510 ITERATOR_VERIFY(this->SharedInfo && this->ptr > this->SharedInfo->first);
511 this->ptr--;
512 return *this;
513 }
514
515 random_access_iterator_wrapper
516 operator--(int)
517 {
518 random_access_iterator_wrapper<T> tmp = *this;
519 --*this;
520 return tmp;
521 }
522
523 random_access_iterator_wrapper&
524 operator+=(std::ptrdiff_t n)
525 {
526 if(n > 0)
527 {
528 ITERATOR_VERIFY(n <= this->SharedInfo->last - this->ptr);
529 this->ptr += n;
530 }
531 else
532 {
533 ITERATOR_VERIFY(-n <= this->ptr - this->SharedInfo->first);
534 this->ptr += n;
535 }
536 return *this;
537 }
538
539 random_access_iterator_wrapper&
540 operator-=(std::ptrdiff_t n)
541 { return *this += -n; }
542
543 random_access_iterator_wrapper
544 operator-(std::ptrdiff_t n) const
545 {
546 random_access_iterator_wrapper<T> tmp = *this;
547 return tmp -= n;
548 }
549
550 std::ptrdiff_t
551 operator-(const random_access_iterator_wrapper<T>& in) const
552 {
553 ITERATOR_VERIFY(this->SharedInfo == in.SharedInfo);
554 return this->ptr - in.ptr;
555 }
556
557 T&
558 operator[](std::ptrdiff_t n) const
559 { return *(*this + n); }
560
561 bool
562 operator<(const random_access_iterator_wrapper<T>& in) const
563 {
564 ITERATOR_VERIFY(this->SharedInfo == in.SharedInfo);
565 return this->ptr < in.ptr;
566 }
567
568 bool
569 operator>(const random_access_iterator_wrapper<T>& in) const
570 {
571 return in < *this;
572 }
573
574 bool
575 operator>=(const random_access_iterator_wrapper<T>& in) const
576 {
577 return !(*this < in);
578 }
579
580 bool
581 operator<=(const random_access_iterator_wrapper<T>& in) const
582 {
583 return !(*this > in);
584 }
585 };
586
587 template<typename T>
588 random_access_iterator_wrapper<T>
589 operator+(random_access_iterator_wrapper<T> it, std::ptrdiff_t n)
590 { return it += n; }
591
592 template<typename T>
593 random_access_iterator_wrapper<T>
594 operator+(std::ptrdiff_t n, random_access_iterator_wrapper<T> it)
595 { return it += n; }
596
597
598 /**
599 * @brief A container-type class for holding iterator wrappers
600 * test_container takes two parameters, a class T and an iterator
601 * wrapper templated by T (for example forward_iterator_wrapper<T>.
602 * It takes two pointers representing a range and presents them as
603 * a container of iterators.
604 */
605 template <class T, template<class TT> class ItType>
606 struct test_container
607 {
608 typename ItType<T>::ContainerType bounds;
609
610 test_container(T* _first, T* _last) : bounds(_first, _last)
611 { }
612
613 template<std::size_t N>
614 explicit
615 test_container(T (&arr)[N]) : bounds(arr, arr+N)
616 { }
617
618 ItType<T>
619 it(int pos)
620 {
621 ITERATOR_VERIFY(pos >= 0 && (unsigned)pos <= size());
622 return ItType<T>(bounds.first + pos, &bounds);
623 }
624
625 ItType<T>
626 it(T* pos)
627 {
628 ITERATOR_VERIFY(pos >= bounds.first && pos <= bounds.last);
629 return ItType<T>(pos, &bounds);
630 }
631
632 const T&
633 val(int pos)
634 { return (bounds.first)[pos]; }
635
636 ItType<T>
637 begin()
638 { return it(bounds.first); }
639
640 ItType<T>
641 end()
642 { return it(bounds.last); }
643
644 std::size_t
645 size() const
646 { return bounds.size(); }
647 };
648
649 #if __cplusplus >= 201103L
650 template<typename T>
651 using output_container
652 = test_container<T, output_iterator_wrapper>;
653
654 template<typename T>
655 using input_container
656 = test_container<T, input_iterator_wrapper>;
657
658 template<typename T>
659 using forward_container
660 = test_container<T, forward_iterator_wrapper>;
661
662 template<typename T>
663 using bidirectional_container
664 = test_container<T, bidirectional_iterator_wrapper>;
665
666 template<typename T>
667 using random_access_container
668 = test_container<T, random_access_iterator_wrapper>;
669 #endif
670
671 #if __cplusplus > 201703L
672 template<typename T>
673 struct contiguous_iterator_wrapper
674 : random_access_iterator_wrapper<T>
675 {
676 using random_access_iterator_wrapper<T>::random_access_iterator_wrapper;
677
678 using iterator_concept = std::contiguous_iterator_tag;
679
680 // Use an integer-class type to try and break the library code.
681 using difference_type = std::ranges::__detail::__max_diff_type;
682
683 contiguous_iterator_wrapper&
684 operator++()
685 {
686 random_access_iterator_wrapper<T>::operator++();
687 return *this;
688 }
689
690 contiguous_iterator_wrapper&
691 operator--()
692 {
693 random_access_iterator_wrapper<T>::operator--();
694 return *this;
695 }
696
697 contiguous_iterator_wrapper
698 operator++(int)
699 {
700 auto tmp = *this;
701 ++*this;
702 return tmp;
703 }
704
705 contiguous_iterator_wrapper
706 operator--(int)
707 {
708 auto tmp = *this;
709 --*this;
710 return tmp;
711 }
712
713 contiguous_iterator_wrapper&
714 operator+=(difference_type n)
715 {
716 auto d = static_cast<std::ptrdiff_t>(n);
717 random_access_iterator_wrapper<T>::operator+=(d);
718 return *this;
719 }
720
721 friend contiguous_iterator_wrapper
722 operator+(contiguous_iterator_wrapper iter, difference_type n)
723 { return iter += n; }
724
725 friend contiguous_iterator_wrapper
726 operator+(difference_type n, contiguous_iterator_wrapper iter)
727 { return iter += n; }
728
729 contiguous_iterator_wrapper&
730 operator-=(difference_type n)
731 { return *this += -n; }
732
733 friend contiguous_iterator_wrapper
734 operator-(contiguous_iterator_wrapper iter, difference_type n)
735 { return iter -= n; }
736
737 friend difference_type
738 operator-(contiguous_iterator_wrapper l, contiguous_iterator_wrapper r)
739 {
740 const random_access_iterator_wrapper<T>& lbase = l;
741 const random_access_iterator_wrapper<T>& rbase = r;
742 return static_cast<difference_type>(lbase - rbase);
743 }
744
745 decltype(auto) operator[](difference_type n) const
746 {
747 auto d = static_cast<std::ptrdiff_t>(n);
748 return random_access_iterator_wrapper<T>::operator[](d);
749 }
750 };
751
752 template<typename T>
753 using contiguous_container
754 = test_container<T, contiguous_iterator_wrapper>;
755
756 // A move-only input iterator type.
757 template<typename T>
758 struct input_iterator_wrapper_nocopy : input_iterator_wrapper<T>
759 {
760 using input_iterator_wrapper<T>::input_iterator_wrapper;
761
762 input_iterator_wrapper_nocopy()
763 : input_iterator_wrapper<T>(nullptr, nullptr)
764 { }
765
766 input_iterator_wrapper_nocopy(const input_iterator_wrapper_nocopy&) = delete;
767 input_iterator_wrapper_nocopy&
768 operator=(const input_iterator_wrapper_nocopy&) = delete;
769
770 input_iterator_wrapper_nocopy(input_iterator_wrapper_nocopy&&) = default;
771 input_iterator_wrapper_nocopy&
772 operator=(input_iterator_wrapper_nocopy&&) = default;
773
774 using input_iterator_wrapper<T>::operator++;
775
776 input_iterator_wrapper_nocopy&
777 operator++()
778 {
779 input_iterator_wrapper<T>::operator++();
780 return *this;
781 }
782 };
783
784 // An input iterator type with an rvalue reference type.
785 template<typename T>
786 struct input_iterator_wrapper_rval : input_iterator_wrapper<T>
787 {
788 using input_iterator_wrapper<T>::input_iterator_wrapper;
789
790 using input_iterator_wrapper<T>::operator++;
791
792 input_iterator_wrapper_rval&
793 operator++()
794 {
795 input_iterator_wrapper<T>::operator++();
796 return *this;
797 }
798
799 T&&
800 operator*() const
801 { return std::move(input_iterator_wrapper<T>::operator*()); }
802 };
803
804 // A type meeting the minimum std::range requirements
805 template<typename T, template<typename> class Iter>
806 class test_range
807 {
808 // Exposes the protected default constructor of Iter<T> if needed. This
809 // is needed only when Iter is input_iterator_wrapper or
810 // output_iterator_wrapper, because legacy forward iterators and beyond
811 // are already default constructible.
812 struct iterator : Iter<T>
813 {
814 using Iter<T>::Iter;
815
816 using Iter<T>::operator++;
817
818 iterator& operator++() { Iter<T>::operator++(); return *this; }
819 };
820
821 template<typename I>
822 struct sentinel
823 {
824 T* end;
825
826 friend bool operator==(const sentinel& s, const I& i) noexcept
827 { return s.end == i.ptr; }
828
829 friend auto operator-(const sentinel& s, const I& i) noexcept
830 requires std::random_access_iterator<I>
831 { return std::iter_difference_t<I>(s.end - i.ptr); }
832
833 friend auto operator-(const I& i, const sentinel& s) noexcept
834 requires std::random_access_iterator<I>
835 { return std::iter_difference_t<I>(i.ptr - s.end); }
836 };
837
838 protected:
839 auto
840 get_iterator(T* p)
841 {
842 if constexpr (std::default_initializable<Iter<T>>)
843 return Iter<T>(p, &bounds);
844 else
845 return iterator(p, &bounds);
846 }
847
848 public:
849 test_range(T* first, T* last) : bounds(first, last)
850 { }
851
852 template<std::size_t N>
853 explicit
854 test_range(T (&arr)[N]) : test_range(arr, arr+N)
855 { }
856
857 auto begin() & { return get_iterator(bounds.first); }
858
859 auto end() &
860 {
861 using I = decltype(get_iterator(bounds.last));
862 return sentinel<I>{bounds.last};
863 }
864
865 typename Iter<T>::ContainerType bounds;
866 };
867
868 // A move-only type meeting the minimum std::range requirements
869 template<typename T, template<typename> class Iter>
870 struct test_range_nocopy : test_range<T, Iter>
871 {
872 test_range_nocopy(T* first, T* last) : test_range<T, Iter>(first, last)
873 {}
874
875 test_range_nocopy(test_range_nocopy&&) = default;
876 test_range_nocopy& operator=(test_range_nocopy&&) = default;
877 };
878
879 template<typename T>
880 using test_contiguous_range
881 = test_range<T, contiguous_iterator_wrapper>;
882 template<typename T>
883 using test_random_access_range
884 = test_range<T, random_access_iterator_wrapper>;
885 template<typename T>
886 using test_bidirectional_range
887 = test_range<T, bidirectional_iterator_wrapper>;
888 template<typename T>
889 using test_forward_range
890 = test_range<T, forward_iterator_wrapper>;
891 template<typename T>
892 using test_input_range
893 = test_range<T, input_iterator_wrapper>;
894 template<typename T>
895 using test_input_range_nocopy
896 = test_range_nocopy<T, input_iterator_wrapper_nocopy>;
897 template<typename T>
898 using test_output_range
899 = test_range<T, output_iterator_wrapper>;
900
901 // A type meeting the minimum std::sized_range requirements
902 template<typename T, template<typename> class Iter>
903 struct test_sized_range : test_range<T, Iter>
904 {
905 using test_range<T, Iter>::test_range;
906
907 std::size_t size() const noexcept
908 { return this->bounds.size(); }
909 };
910
911 template<typename T>
912 using test_contiguous_sized_range
913 = test_sized_range<T, contiguous_iterator_wrapper>;
914 template<typename T>
915 using test_random_access_sized_range
916 = test_sized_range<T, random_access_iterator_wrapper>;
917 template<typename T>
918 using test_bidirectional_sized_range
919 = test_sized_range<T, bidirectional_iterator_wrapper>;
920 template<typename T>
921 using test_forward_sized_range
922 = test_sized_range<T, forward_iterator_wrapper>;
923 template<typename T>
924 using test_input_sized_range
925 = test_sized_range<T, input_iterator_wrapper>;
926 template<typename T>
927 using test_output_sized_range
928 = test_sized_range<T, output_iterator_wrapper>;
929
930 // A type meeting the minimum std::sized_range requirements, and whose end()
931 // returns a sized sentinel.
932 template<typename T, template<typename> class Iter>
933 struct test_sized_range_sized_sent : test_sized_range<T, Iter>
934 {
935 using test_sized_range<T, Iter>::test_sized_range;
936
937 template<typename I>
938 struct sentinel
939 {
940 T* end;
941
942 friend bool operator==(const sentinel& s, const I& i) noexcept
943 { return s.end == i.ptr; }
944
945 friend std::iter_difference_t<I>
946 operator-(const sentinel& s, const I& i) noexcept
947 { return std::iter_difference_t<I>(s.end - i.ptr); }
948
949 friend std::iter_difference_t<I>
950 operator-(const I& i, const sentinel& s) noexcept
951 { return std::iter_difference_t<I>(i.ptr - s.end); }
952 };
953
954 auto end() &
955 {
956 using I = decltype(this->get_iterator(this->bounds.last));
957 return sentinel<I>{this->bounds.last};
958 }
959 };
960
961 // test_range and test_sized_range do not own their elements, so they model
962 // std::ranges::borrowed_range. This file does not define specializations of
963 // std::ranges::enable_borrowed_range, so that individual tests can decide
964 // whether or not to do so.
965 // This is also true for test_container, although only when it has forward
966 // iterators (because output_iterator_wrapper and input_iterator_wrapper are
967 // not default constructible so do not model std::input_or_output_iterator).
968
969
970 // Test for basic properties of C++20 16.3.3.6 [customization.point.object].
971 template<typename T>
972 constexpr bool
973 is_customization_point_object(T&) noexcept
974 {
975 // A [CPO] is a function object with a literal class type.
976 static_assert( std::is_class_v<T> || std::is_union_v<T> );
977 static_assert( __is_literal_type(T) );
978 // The type of a [CPO], ignoring cv-qualifiers, shall model semiregular.
979 static_assert( std::semiregular<std::remove_cv_t<T>> );
980
981 return true;
982 }
983
984 #endif // C++20
985 } // namespace __gnu_test
986 #endif // _TESTSUITE_ITERATORS