| 1 |
// Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 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) |
| 7 |
// any later version. |
| 8 |
|
| 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. |
| 13 |
|
| 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/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++20 } } |
| 19 |
|
| 20 |
#include <iterator> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
void |
| 24 |
test01() |
| 25 |
{ |
| 26 |
using I = std::counted_iterator<int*>; |
| 27 |
static_assert( std::is_default_constructible_v<I> ); |
| 28 |
static_assert( std::is_copy_constructible_v<I> ); |
| 29 |
static_assert( std::is_copy_assignable_v<I> ); |
| 30 |
static_assert( ! std::is_constructible_v<I, int*> ); |
| 31 |
static_assert( std::is_constructible_v<I, int*, std::ptrdiff_t> ); |
| 32 |
|
| 33 |
using J = std::counted_iterator<const int*>; |
| 34 |
static_assert( std::is_constructible_v<J, const I&> ); |
| 35 |
static_assert( std::is_convertible_v<const I&, J> ); |
| 36 |
} |
| 37 |
|
| 38 |
void |
| 39 |
test02() |
| 40 |
{ |
| 41 |
int in[3] = { 1, 2, 3 }; |
| 42 |
std::counted_iterator<const int*> in_iter(std::begin(in), std::ssize(in)); |
| 43 |
VERIFY( in_iter.base() == in ); |
| 44 |
VERIFY( (std::default_sentinel - in_iter) == 3 ); |
| 45 |
VERIFY( (in_iter - std::default_sentinel) == -3 ); |
| 46 |
|
| 47 |
int out[4] = { }; |
| 48 |
std::counted_iterator<int*> out_iter(std::begin(out), std::ssize(out)); |
| 49 |
VERIFY( out_iter.base() == out ); |
| 50 |
VERIFY( (std::default_sentinel - out_iter) == 4 ); |
| 51 |
VERIFY( (out_iter - std::default_sentinel) == -4 ); |
| 52 |
|
| 53 |
while (in_iter != std::default_sentinel && out_iter != std::default_sentinel) |
| 54 |
*out_iter++ = *in_iter++; |
| 55 |
|
| 56 |
VERIFY(in_iter == std::default_sentinel); |
| 57 |
VERIFY(out_iter != std::default_sentinel); |
| 58 |
VERIFY( out[0] == 1 ); |
| 59 |
VERIFY( out[1] == 2 ); |
| 60 |
VERIFY( out[2] == 3 ); |
| 61 |
VERIFY( out[3] == 0 ); |
| 62 |
|
| 63 |
auto out2 = out_iter; |
| 64 |
out2 += 1; |
| 65 |
VERIFY( out2 == std::default_sentinel ); |
| 66 |
VERIFY( (out2 <=> out_iter) == std::strong_ordering::greater ); |
| 67 |
out2 -= 3; |
| 68 |
VERIFY( (out_iter - out2) == 2 ); |
| 69 |
VERIFY( (out2 <=> out_iter) == std::strong_ordering::less ); |
| 70 |
} |
| 71 |
|
| 72 |
void |
| 73 |
test03() |
| 74 |
{ |
| 75 |
struct X |
| 76 |
{ |
| 77 |
X(int i) : i(i) { } |
| 78 |
X(X&& x) : i(x.i) { x.i = -1; } |
| 79 |
X& operator=(X&& x) { i = x.i; x.i = 0; return *this; } |
| 80 |
int i; |
| 81 |
}; |
| 82 |
|
| 83 |
X arr[] = { 1, 2 }; |
| 84 |
std::counted_iterator<X*> i(arr, 2), j(arr + 1, 1); |
| 85 |
std::ranges::iter_swap(i, j); |
| 86 |
VERIFY( arr[0].i == 2 ); |
| 87 |
VERIFY( arr[1].i == 1 ); |
| 88 |
|
| 89 |
X x = std::ranges::iter_move(i); |
| 90 |
VERIFY( arr[0].i == -1 ); |
| 91 |
VERIFY( x.i == 2 ); |
| 92 |
} |
| 93 |
|
| 94 |
int |
| 95 |
main() |
| 96 |
{ |
| 97 |
test01(); |
| 98 |
test02(); |
| 99 |
test03(); |
| 100 |
} |