]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/next_permutation/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / next_permutation / constrained.cc
1 // Copyright (C) 2020-2023 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <algorithm>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 using __gnu_test::test_container;
26 using __gnu_test::test_range;
27 using __gnu_test::bidirectional_iterator_wrapper;
28
29 namespace ranges = std::ranges;
30
31 void
32 test01()
33 {
34 int x[] = {1, 2, 3, 4, 5};
35 int y[] = {1, 2, 3, 4, 5};
36
37 for (int i = 0; i <= 5; i++)
38 {
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++)
42 {
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) );
47 if (!found2)
48 break;
49 }
50 }
51 }
52
53 void
54 test02()
55 {
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}) );
64
65 VERIFY( !ranges::next_permutation(x, x).found );
66 VERIFY( !ranges::next_permutation(x, x+1).found );
67 }
68
69 constexpr bool
70 test03()
71 {
72 int x[] = {1,2,3};
73 ranges::next_permutation(x);
74 return ranges::equal(x, (int[]){1,3,2});
75 }
76
77 int
78 main()
79 {
80 test01();
81 test02();
82 static_assert(test03());
83 }