]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 24_iterators / common_iterator / 1.cc
1 // Copyright (C) 2019-2021 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::common_iterator<int*, const 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, const int*> );
33
34 struct sentinel { operator int*() const { return nullptr; } };
35 using K = std::common_iterator<int*, sentinel>;
36 static_assert( std::is_constructible_v<I, const K&> );
37 static_assert( std::is_assignable_v<I, const K&> );
38
39 struct sentinel2
40 {
41 const int* p;
42 sentinel2(const int* p = 0) : p(p) { }
43 bool operator==(const int* p) const { return p == this->p; }
44 };
45
46 using J = std::common_iterator<const int*, sentinel2>;
47 static_assert( std::is_constructible_v<J, const I&> );
48 static_assert( std::is_convertible_v<const I&, J> );
49 }
50
51 void
52 test02()
53 {
54 struct sentinel { int limit; };
55
56 struct iterator
57 {
58 using iterator_category = std::input_iterator_tag;
59 using value_type = int;
60 using difference_type = std::ptrdiff_t;
61 using reference = const int&;
62
63 const int& operator*() const { return counter; }
64
65 iterator& operator++() { ++counter; return *this; }
66
67 iterator operator++(int) { auto i = *this; ++counter; return i; }
68
69 bool operator==(sentinel s) const { return counter == s.limit; }
70
71 int counter = 0;
72 };
73
74 static_assert( std::sentinel_for<sentinel, iterator> );
75
76 int out[5] = { };
77 std::common_iterator<int*, const int*> obegin = std::begin(out);
78 std::common_iterator<int*, const int*> oend = std::cend(out);
79
80 iterator i;
81 sentinel s{5};
82 std::common_iterator<iterator, sentinel> begin = i, end = s;
83 while (begin != end)
84 *obegin++ = *begin++;
85
86 VERIFY(obegin == oend);
87 for (int& i : out)
88 VERIFY( i == (&i - out) );
89 }
90
91 void
92 test03()
93 {
94 int arr[2] = { 1, 2 };
95 std::common_iterator<int*, const int*> i = std::ranges::begin(arr);
96 std::common_iterator<int*, const int*> end = std::ranges::cend(arr);
97 VERIFY( i != end );
98 VERIFY( (end - i) == 2 );
99 VERIFY( (i - end) == -2 );
100 auto j = i;
101 VERIFY( j == i );
102 VERIFY( (j - i) == 0 );
103 j = end;
104 VERIFY( j != i );
105 VERIFY( j == end );
106 j = std::ranges::next(i);
107 VERIFY( j != i );
108 VERIFY( j != end );
109 VERIFY( (end - j) == 1 );
110 VERIFY( (j - i) == 1 );
111 VERIFY( (i - j) == -1 );
112 ++j;
113 VERIFY( j == end );
114 VERIFY( (end - j) == 0 );
115 j = i;
116 VERIFY( j == i );
117 VERIFY( (j - end) == -2 );
118 VERIFY( (j - i) == 0 );
119
120 try
121 {
122 struct S { operator const int*() const { throw 1; } };
123 i = std::common_iterator<int*, S>(S{});
124 VERIFY( false );
125 }
126 catch (int)
127 {
128 }
129 }
130
131 void
132 test04()
133 {
134 struct X
135 {
136 X(int i) : i(i) { }
137 X(X&& x) : i(x.i) { x.i = -1; }
138 X& operator=(X&& x) { i = x.i; x.i = 0; return *this; }
139 int i;
140 };
141
142 X arr[] = { 1, 2 };
143 std::common_iterator<X*, const X*> i(arr), j(arr+1);
144 std::ranges::iter_swap(i, j);
145 VERIFY( arr[0].i == 2 );
146 VERIFY( arr[1].i == 1 );
147
148 X x = std::ranges::iter_move(i);
149 VERIFY( arr[0].i == -1 );
150 VERIFY( x.i == 2 );
151 }
152
153 int
154 main()
155 {
156 test01();
157 test02();
158 test03();
159 test04();
160 }