| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // 25.3.9 [lib.alg.permutation.generators] |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_iterators.h> |
| 25 | #include <testsuite_rvalref.h> |
| 26 | |
| 27 | using __gnu_test::test_container; |
| 28 | using __gnu_test::bidirectional_iterator_wrapper; |
| 29 | using __gnu_test::rvalstruct; |
| 30 | using std::next_permutation; |
| 31 | |
| 32 | typedef test_container<rvalstruct, bidirectional_iterator_wrapper> Container; |
| 33 | |
| 34 | void |
| 35 | test1() |
| 36 | { |
| 37 | // Note: The standard is unclear on what should happen in this case. |
| 38 | // This seems the only really sensible behaviour, and what is done. |
| 39 | rvalstruct array[] = {0}; |
| 40 | Container con(array, array); |
| 41 | VERIFY( !next_permutation(con.begin(), con.end()) ); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | test2() |
| 46 | { |
| 47 | rvalstruct array[] = {0}; |
| 48 | Container con(array, array + 1); |
| 49 | VERIFY( !next_permutation(con.begin(), con.end()) ); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | test3() |
| 54 | { |
| 55 | rvalstruct array[] = {0, 3}; |
| 56 | Container con(array, array + 2); |
| 57 | VERIFY( next_permutation(con.begin(), con.end()) ); |
| 58 | VERIFY( array[0] == 3 && array[1] == 0 ); |
| 59 | VERIFY( !next_permutation(con.begin(), con.end()) ); |
| 60 | VERIFY( array[0] == 0 && array[1] == 3 ); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | test4() |
| 65 | { |
| 66 | int array[6] = {0, 1, 2, 3, 4, 5}; |
| 67 | for(int i = 0 ; i < 719; ++i) |
| 68 | { |
| 69 | rvalstruct temp_array[6]; |
| 70 | std::copy(array, array + 6, temp_array); |
| 71 | Container con(temp_array, temp_array + 6); |
| 72 | VERIFY( next_permutation(array, array + 6) ); |
| 73 | |
| 74 | // XXX FIXME: parallel-mode should deal correctly with moveable-only types |
| 75 | // per C++0x, at minimum smoothly fall back to serial. |
| 76 | #ifndef _GLIBCXX_PARALLEL |
| 77 | VERIFY( std::lexicographical_compare(temp_array, temp_array + 6, |
| 78 | array, array + 6) ); |
| 79 | #endif |
| 80 | } |
| 81 | VERIFY( !next_permutation(array,array + 6) ); |
| 82 | for(int i = 0; i < 6; ++i) |
| 83 | VERIFY( array[i] == i ); |
| 84 | } |
| 85 | |
| 86 | bool |
| 87 | are_ordered(const rvalstruct& lhs, const rvalstruct& rhs) |
| 88 | { return lhs < rhs; } |
| 89 | |
| 90 | void |
| 91 | test5() |
| 92 | { |
| 93 | int array[6] = {0, 1, 2, 3, 4, 5}; |
| 94 | for(int i = 0 ; i < 719; ++i) |
| 95 | { |
| 96 | rvalstruct temp_array[6]; |
| 97 | std::copy(array, array + 6, temp_array); |
| 98 | Container con(temp_array, temp_array + 6); |
| 99 | VERIFY( next_permutation(array, array + 6, are_ordered) ); |
| 100 | |
| 101 | // XXX FIXME: parallel-mode should deal correctly with moveable-only types |
| 102 | // per C++0x, at minimum smoothly fall back to serial. |
| 103 | #ifndef _GLIBCXX_PARALLEL |
| 104 | VERIFY( std::lexicographical_compare(temp_array, temp_array + 6, |
| 105 | array, array + 6, are_ordered) ); |
| 106 | #endif |
| 107 | } |
| 108 | VERIFY( !next_permutation(array,array + 6, are_ordered) ); |
| 109 | for(int i = 0; i < 6; ++i) |
| 110 | VERIFY( array[i] == i ); |
| 111 | } |
| 112 | |
| 113 | int main() |
| 114 | { |
| 115 | test1(); |
| 116 | test2(); |
| 117 | test3(); |
| 118 | test4(); |
| 119 | test5(); |
| 120 | return 0; |
| 121 | } |