1 // Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
22 #include <testsuite_hooks.h>
27 using I
= std::counted_iterator
<int*>;
28 static_assert( std::is_default_constructible_v
<I
> );
29 static_assert( std::is_copy_constructible_v
<I
> );
30 static_assert( std::is_copy_assignable_v
<I
> );
31 static_assert( ! std::is_constructible_v
<I
, int*> );
32 static_assert( std::is_constructible_v
<I
, int*, std::ptrdiff_t> );
34 using J
= std::counted_iterator
<const int*>;
35 static_assert( std::is_constructible_v
<J
, const I
&> );
36 static_assert( std::is_convertible_v
<const I
&, J
> );
42 int in
[3] = { 1, 2, 3 };
43 std::counted_iterator
<const int*> in_iter(std::begin(in
), std::ssize(in
));
44 VERIFY( in_iter
.base() == in
);
45 VERIFY( (std::default_sentinel
- in_iter
) == 3 );
46 VERIFY( (in_iter
- std::default_sentinel
) == -3 );
49 std::counted_iterator
<int*> out_iter(std::begin(out
), std::ssize(out
));
50 VERIFY( out_iter
.base() == out
);
51 VERIFY( (std::default_sentinel
- out_iter
) == 4 );
52 VERIFY( (out_iter
- std::default_sentinel
) == -4 );
54 while (in_iter
!= std::default_sentinel
&& out_iter
!= std::default_sentinel
)
55 *out_iter
++ = *in_iter
++;
57 VERIFY(in_iter
== std::default_sentinel
);
58 VERIFY(out_iter
!= std::default_sentinel
);
59 VERIFY( out
[0] == 1 );
60 VERIFY( out
[1] == 2 );
61 VERIFY( out
[2] == 3 );
62 VERIFY( out
[3] == 0 );
66 VERIFY( out2
== std::default_sentinel
);
67 VERIFY( (out2
<=> out_iter
) == std::strong_ordering::greater
);
69 VERIFY( (out_iter
- out2
) == 2 );
70 VERIFY( (out2
<=> out_iter
) == std::strong_ordering::less
);
79 X(X
&& x
) : i(x
.i
) { x
.i
= -1; }
80 X
& operator=(X
&& x
) { i
= x
.i
; x
.i
= 0; return *this; }
85 std::counted_iterator
<X
*> i(arr
, 2), j(arr
+ 1, 1);
86 std::ranges::iter_swap(i
, j
);
87 VERIFY( arr
[0].i
== 2 );
88 VERIFY( arr
[1].i
== 1 );
90 X x
= std::ranges::iter_move(i
);
91 VERIFY( arr
[0].i
== -1 );