| 1 |
// Copyright (C) 2020-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 <algorithm> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
#include <testsuite_iterators.h> |
| 23 |
|
| 24 |
using __gnu_test::test_container; |
| 25 |
using __gnu_test::test_range; |
| 26 |
using __gnu_test::input_iterator_wrapper; |
| 27 |
using __gnu_test::forward_iterator_wrapper; |
| 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(int a) : i(a) { } |
| 38 |
|
| 39 |
constexpr X(const X&) = delete; |
| 40 |
constexpr X& operator=(const X&) = delete; |
| 41 |
|
| 42 |
constexpr X(X&& other) |
| 43 |
{ |
| 44 |
*this = std::move(other); |
| 45 |
} |
| 46 |
|
| 47 |
constexpr X& |
| 48 |
operator=(X&& other) |
| 49 |
{ |
| 50 |
other.moved++; |
| 51 |
i = other.i; |
| 52 |
return *this; |
| 53 |
} |
| 54 |
|
| 55 |
friend constexpr bool |
| 56 |
operator==(const X& a, const X& b) |
| 57 |
{ return a.i == b.i; } |
| 58 |
}; |
| 59 |
|
| 60 |
void |
| 61 |
test01() |
| 62 |
{ |
| 63 |
{ |
| 64 |
X x[7] = { 1, 2, 3, 4, 5, 6, 7 }; |
| 65 |
X y[7] = { 2, 4, 3, 5, 8, 9, 1 }; |
| 66 |
X z[7] = { 1, 2, 3, 4, 5, 6, 7 }; |
| 67 |
X w[7] = { 2, 4, 3, 5, 8, 9, 1 }; |
| 68 |
auto [x_iter, y_iter] = ranges::swap_ranges(x, y); |
| 69 |
VERIFY( ranges::equal(y, z) && x_iter == x+7 && y_iter == y+7 ); |
| 70 |
VERIFY( ranges::equal(x, w) ); |
| 71 |
} |
| 72 |
|
| 73 |
{ |
| 74 |
int x[3] = { 1, 2, 3 }; |
| 75 |
int y[4] = { 2, 4, 6, 0 }; |
| 76 |
int z[3] = { 1, 2, 3 }; |
| 77 |
int w[3] = { 2, 4, 6 }; |
| 78 |
test_container<int, forward_iterator_wrapper> cx(x); |
| 79 |
test_container<int, forward_iterator_wrapper> cy(y); |
| 80 |
auto [x_iter, y_iter] = ranges::swap_ranges(cx, cy); |
| 81 |
VERIFY( ranges::equal(y, y+3, z, z+3) ); |
| 82 |
VERIFY( x_iter.ptr == x+3 && y_iter.ptr == y+3 ); |
| 83 |
VERIFY( y[3] == 0 ); |
| 84 |
VERIFY( ranges::equal(x, w) ); |
| 85 |
} |
| 86 |
|
| 87 |
{ |
| 88 |
int x[3] = { 1, 2, 3 }; |
| 89 |
int y[4] = { 2, 4, 6, 0 }; |
| 90 |
int z[3] = { 1, 2, 3 }; |
| 91 |
int w[3] = { 2, 4, 6 }; |
| 92 |
test_range<int, input_iterator_wrapper> cx(x); |
| 93 |
test_range<int, input_iterator_wrapper> cy(y); |
| 94 |
auto [y_iter, x_iter] = ranges::swap_ranges(cy, cx); |
| 95 |
VERIFY( ranges::equal(y, y+3, z, z+3) ); |
| 96 |
VERIFY( x_iter.ptr == x+3 && y_iter.ptr == y+3 ); |
| 97 |
VERIFY( y[3] == 0 ); |
| 98 |
VERIFY( ranges::equal(x, w) ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
constexpr bool |
| 103 |
test02() |
| 104 |
{ |
| 105 |
bool ok = true; |
| 106 |
X x[7] = { 1, 2, 3, 4, 5, 6, 7 }; |
| 107 |
X y[7] = { 2, 4, 3, 5, 8, 9, 1 }; |
| 108 |
X z[7] = { 1, 2, 3, 4, 5, 6, 7 }; |
| 109 |
X w[7] = { 2, 4, 3, 5, 8, 9, 1 }; |
| 110 |
auto [x_iter, y_iter] = ranges::swap_ranges(x, y); |
| 111 |
ok &= ranges::equal(y, z) && x_iter == x+7 && y_iter == y+7; |
| 112 |
ok &= ranges::equal(x, w); |
| 113 |
return ok; |
| 114 |
} |
| 115 |
|
| 116 |
int |
| 117 |
main() |
| 118 |
{ |
| 119 |
test01(); |
| 120 |
static_assert(test02()); |
| 121 |
} |
| 122 |
|
| 123 |
|