]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/move_backward/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / move_backward / constrained.cc
1 // Copyright (C) 2020-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 <algorithm>
22 #include <vector>
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
25
26 using __gnu_test::test_container;
27 using __gnu_test::test_range;
28 using __gnu_test::bidirectional_iterator_wrapper;
29
30 namespace ranges = std::ranges;
31
32 struct X
33 {
34 int i;
35 int moved = 0;
36
37 constexpr X() : i(0) { }
38 constexpr X(int a) : i(a) { }
39
40 constexpr X(const X&) = delete;
41 constexpr X& operator=(const X&) = delete;
42
43 constexpr X(X&& other)
44 {
45 *this = std::move(other);
46 }
47
48 constexpr X&
49 operator=(X&& other)
50 {
51 other.moved++;
52 i = other.i;
53 return *this;
54 }
55
56 friend constexpr bool
57 operator==(const X& a, const X& b)
58 { return a.i == b.i; }
59 };
60
61 void
62 test01()
63 {
64 {
65 X x[7] = { 1, 2, 3, 4, 5, 6, 7 };
66 X y[7] = { 0, 0, 0, 0, 0, 0, 0 };
67 X z[7] = { 1, 2, 3, 4, 5, 6, 7 };
68 auto [in, out] = ranges::move_backward(x, y+7);
69 VERIFY( ranges::equal(x, y) && in == x+7 && out == y );
70 VERIFY( ranges::equal(x, z) );
71 }
72
73 {
74 int x[3] = { 1, 2, 3 };
75 char y[4] = { 0 };
76 int z[3] = { 1, 2, 3 };
77 test_container<int, bidirectional_iterator_wrapper> cx(x);
78 test_container<char, bidirectional_iterator_wrapper> cy(y);
79 auto [in, out] = ranges::move_backward(cx, cy.end());
80 VERIFY( ranges::equal(x, x+3, y+1, y+4) && in.ptr == x+3 && out.ptr == y+1 );
81 VERIFY( ranges::equal(x, z) );
82 }
83
84 {
85 std::vector<char> x= {1,2,3};
86 std::vector<int> y(3);
87 const int z[3] = { 1, 2, 3 };
88 auto [in, out] = ranges::move_backward(x, ranges::end(y));
89 VERIFY( in == x.begin()+3 );
90 VERIFY( out == y.begin() );
91 VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
92 }
93
94
95 {
96 std::vector<int> x = {1,2,3};
97 std::vector<int> y(3);
98 const int z[3] = { 1, 2, 3 };
99 auto [in, out] = ranges::move_backward(x, ranges::end(y));
100 VERIFY( in == x.begin()+3 );
101 VERIFY( out == y.begin() );
102 VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
103 }
104
105 {
106 std::vector<int> x = {1,2,3};
107 std::vector<int> y(3);
108 const int z[3] = { 1, 2, 3 };
109 auto [in,out] = ranges::move_backward(make_reverse_iterator(x.end()),
110 make_reverse_iterator(x.begin()),
111 make_reverse_iterator(y.begin()));
112 VERIFY( in.base() == x.begin()+3 );
113 VERIFY( out.base() == y.begin()+3 );
114 VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
115 }
116
117 {
118 std::vector<char> x = {1,2,3};
119 std::vector<int> y(3);
120 const int z[3] = { 1, 2, 3 };
121 auto [in,out] = ranges::move_backward(make_reverse_iterator(x.end()),
122 make_reverse_iterator(x.begin()),
123 make_reverse_iterator(y.begin()));
124 VERIFY( in.base() == x.begin()+3 );
125 VERIFY( out.base() == y.begin()+3 );
126 VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
127 }
128 }
129
130 void
131 test02()
132 {
133 X x[] = { {2}, {2}, {6}, {8}, {10} };
134 X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
135 const X z[] = { {2}, {2}, {6}, {8}, {10} };
136 auto [in, out] = ranges::move_backward(x, ranges::end(y));
137 VERIFY( ranges::equal(x, x+5, y+1, y+6) );
138 VERIFY( in == x+5 );
139 VERIFY( out == y+1 );
140 VERIFY( y[0].i == 2 );
141 VERIFY( ranges::equal(x, z) );
142 VERIFY( ranges::count(x, 1, &X::moved) == 5 );
143 VERIFY( ranges::count(y, 0, &X::moved) == 6 );
144 }
145
146 constexpr bool
147 test03()
148 {
149 bool ok = true;
150 X x[] = { {2}, {2}, {6}, {8}, {10} };
151 X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
152 const X z[] = { {2}, {2}, {6}, {8}, {10} };
153 auto [in, out] = ranges::move_backward(x, ranges::end(y));
154 ok &= ranges::equal(x, x+5, y+1, y+6);
155 ok &= (in == x+5);
156 ok &= (out == y+1);
157 ok &= (y[0].i == 2);
158 ok &= ranges::equal(x, z);
159 ok &= ranges::count(x, 1, &X::moved) == 5;
160 ok &= ranges::count(y, 0, &X::moved) == 6;
161 return ok;
162 }
163
164 int
165 main()
166 {
167 test01();
168 test02();
169 static_assert(test03());
170 }