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 } }
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
25 using __gnu_test::test_container
;
26 using __gnu_test::test_range
;
27 using __gnu_test::bidirectional_iterator_wrapper
;
29 namespace ranges
= std::ranges
;
34 int x
[] = {1, 2, 3, 4, 5};
35 int y
[] = {1, 2, 3, 4, 5};
37 for (int i
= 0; i
<= 5; i
++)
39 test_container
<int, bidirectional_iterator_wrapper
> cx(x
, x
+i
);
40 test_container
<int, bidirectional_iterator_wrapper
> cy(y
, y
+i
);
41 for (int j
= 0; ; j
++)
43 auto found1
= std::next_permutation(cx
.begin(), cx
.end());
44 auto [last
,found2
] = ranges::next_permutation(cy
.begin(), cy
.end());
45 VERIFY( found1
== found2
);
46 VERIFY( ranges::equal(cx
, cy
) );
56 int x
[] = {5, 4, 3, 2, 1};
57 test_range
<int, bidirectional_iterator_wrapper
> rx(x
);
58 auto [last
,found
] = ranges::next_permutation(rx
, ranges::greater
{});
59 VERIFY( found
&& last
== rx
.end() );
60 VERIFY( last
== rx
.end() );
61 VERIFY( ranges::equal(rx
, (int[]){5,4,3,1,2}) );
62 ranges::next_permutation(rx
, {}, [] (int a
) { return -a
; });
63 VERIFY( ranges::equal(rx
, (int[]){5,4,2,3,1}) );
65 VERIFY( !ranges::next_permutation(x
, x
).found
);
66 VERIFY( !ranges::next_permutation(x
, x
+1).found
);
73 ranges::next_permutation(x
);
74 return ranges::equal(x
, (int[]){1,3,2});
82 static_assert(test03());