| 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 <iterator> |
| 22 |
#include <ranges> |
| 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::input_iterator_wrapper; |
| 29 |
using __gnu_test::forward_iterator_wrapper; |
| 30 |
using __gnu_test::bidirectional_iterator_wrapper; |
| 31 |
|
| 32 |
namespace ranges = std::ranges; |
| 33 |
|
| 34 |
struct X { int i; }; |
| 35 |
|
| 36 |
void |
| 37 |
test01() |
| 38 |
{ |
| 39 |
int x[] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 40 |
int y[] = { {2}, {6}, {8}, {10}, {11}, {2} }; |
| 41 |
int z[] = { {2}, {6}, {8}, {10}, {2}, {2} }; |
| 42 |
|
| 43 |
VERIFY( ranges::is_permutation(x, x+6, y, y+6) ); |
| 44 |
VERIFY( !ranges::is_permutation(x, x+6, y, y+5) ); |
| 45 |
|
| 46 |
test_container<int, forward_iterator_wrapper> cx(x), cy(y), cz(z); |
| 47 |
test_range<int, forward_iterator_wrapper> rx(x), ry(y), rz(z); |
| 48 |
VERIFY( ranges::is_permutation(cx, ry) ); |
| 49 |
VERIFY( !ranges::is_permutation(rx, cz) ); |
| 50 |
VERIFY( ranges::is_permutation(rx, cy) ); |
| 51 |
VERIFY( !ranges::is_permutation(cx, rz) ); |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
test02() |
| 56 |
{ |
| 57 |
static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} }; |
| 58 |
static constexpr X y[] = { {2}, {6}, {8}, {10}, {11}, {2} }; |
| 59 |
static constexpr int z[] = { {2}, {6}, {8}, {10}, {2}, {2} }; |
| 60 |
static_assert(ranges::is_permutation(x, y, {}, &X::i, &X::i)); |
| 61 |
static_assert(!ranges::is_permutation(x, z, {}, &X::i)); |
| 62 |
static_assert(!ranges::is_permutation(z, y, {}, {}, &X::i)); |
| 63 |
} |
| 64 |
|
| 65 |
void |
| 66 |
test03() |
| 67 |
{ |
| 68 |
int x[] = { 1, 2, 3, 4 }; |
| 69 |
int y[] = { 1, 2, 3, 3 }; |
| 70 |
test_container<int, bidirectional_iterator_wrapper> cx(x); |
| 71 |
do |
| 72 |
do |
| 73 |
{ |
| 74 |
VERIFY( ranges::is_permutation(cx, x) ); |
| 75 |
VERIFY( !ranges::is_permutation(y, cx) ); |
| 76 |
} while (std::next_permutation(y, y+4)); |
| 77 |
while (std::next_permutation(std::begin(cx), std::end(cx))); |
| 78 |
} |
| 79 |
|
| 80 |
constexpr |
| 81 |
bool |
| 82 |
test04() // PR118160, do not create dangling references |
| 83 |
{ |
| 84 |
int x[] = { 4, 3, 2, 1 }; |
| 85 |
auto y = std::views::iota(1, 5); |
| 86 |
return ranges::is_permutation(x, y) && ranges::is_permutation(y, x); |
| 87 |
} |
| 88 |
|
| 89 |
static_assert(test04()); |
| 90 |
|
| 91 |
int |
| 92 |
main() |
| 93 |
{ |
| 94 |
test01(); |
| 95 |
test02(); |
| 96 |
test03(); |
| 97 |
VERIFY( test04() ); |
| 98 |
} |