1 // Copyright (C) 2020-2025 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-do run { target c++20 } }
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
24 using __gnu_test::test_container
;
25 using __gnu_test::test_range
;
26 using __gnu_test::bidirectional_iterator_wrapper
;
28 namespace ranges
= std::ranges
;
33 int x
[] = {1, 2, 3, 4, 5};
34 int y
[] = {1, 2, 3, 4, 5};
36 for (int i
= 0; i
<= 5; i
++)
38 test_container
<int, bidirectional_iterator_wrapper
> cx(x
, x
+i
);
39 test_container
<int, bidirectional_iterator_wrapper
> cy(y
, y
+i
);
40 for (int j
= 0; ; j
++)
42 auto found1
= std::next_permutation(cx
.begin(), cx
.end());
43 auto [last
,found2
] = ranges::next_permutation(cy
.begin(), cy
.end());
44 VERIFY( found1
== found2
);
45 VERIFY( ranges::equal(cx
, cy
) );
55 int x
[] = {5, 4, 3, 2, 1};
56 test_range
<int, bidirectional_iterator_wrapper
> rx(x
);
57 auto [last
,found
] = ranges::next_permutation(rx
, ranges::greater
{});
58 VERIFY( found
&& last
== rx
.end() );
59 VERIFY( last
== rx
.end() );
60 VERIFY( ranges::equal(rx
, (int[]){5,4,3,1,2}) );
61 ranges::next_permutation(rx
, {}, [] (int a
) { return -a
; });
62 VERIFY( ranges::equal(rx
, (int[]){5,4,2,3,1}) );
64 VERIFY( !ranges::next_permutation(x
, x
).found
);
65 VERIFY( !ranges::next_permutation(x
, x
+1).found
);
72 ranges::next_permutation(x
);
73 return ranges::equal(x
, (int[]){1,3,2});
81 static_assert(test03());