]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/24_iterators/counted_iterator/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 24_iterators / counted_iterator / 1.cc
1 // Copyright (C) 2019-2022 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <iterator>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
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> );
33
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> );
37 }
38
39 void
40 test02()
41 {
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 );
47
48 int out[4] = { };
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 );
53
54 while (in_iter != std::default_sentinel && out_iter != std::default_sentinel)
55 *out_iter++ = *in_iter++;
56
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 );
63
64 auto out2 = out_iter;
65 out2 += 1;
66 VERIFY( out2 == std::default_sentinel );
67 VERIFY( (out2 <=> out_iter) == std::strong_ordering::greater );
68 out2 -= 3;
69 VERIFY( (out_iter - out2) == 2 );
70 VERIFY( (out2 <=> out_iter) == std::strong_ordering::less );
71 }
72
73 void
74 test03()
75 {
76 struct X
77 {
78 X(int i) : i(i) { }
79 X(X&& x) : i(x.i) { x.i = -1; }
80 X& operator=(X&& x) { i = x.i; x.i = 0; return *this; }
81 int i;
82 };
83
84 X arr[] = { 1, 2 };
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 );
89
90 X x = std::ranges::iter_move(i);
91 VERIFY( arr[0].i == -1 );
92 VERIFY( x.i == 2 );
93 }
94
95 int
96 main()
97 {
98 test01();
99 test02();
100 test03();
101 }