1 // Copyright (C) 2020-2023 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 } }
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
26 using __gnu_test::test_container
;
27 using __gnu_test::test_range
;
28 using __gnu_test::bidirectional_iterator_wrapper
;
30 namespace ranges
= std::ranges
;
37 constexpr X() : i(0) { }
38 constexpr X(int a
) : i(a
) { }
40 constexpr X(const X
&) = delete;
41 constexpr X
& operator=(const X
&) = delete;
43 constexpr X(X
&& other
)
45 *this = std::move(other
);
57 operator==(const X
& a
, const X
& b
)
58 { return a
.i
== b
.i
; }
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
) );
74 int x
[3] = { 1, 2, 3 };
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
) );
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
) );
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
) );
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
) );
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
) );
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) );
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 );
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);
158 ok
&= ranges::equal(x
, z
);
159 ok
&= ranges::count(x
, 1, &X::moved
) == 5;
160 ok
&= ranges::count(y
, 0, &X::moved
) == 6;
169 static_assert(test03());