| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // 2011-01-13 Paolo Carlini <[email protected]> |
| 4 | // |
| 5 | // Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 6 | // |
| 7 | // This file is part of the GNU ISO C++ Library. This library is free |
| 8 | // software; you can redistribute it and/or modify it under the |
| 9 | // terms of the GNU General Public License as published by the |
| 10 | // Free Software Foundation; either version 3, or (at your option) |
| 11 | // any later version. |
| 12 | |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License along |
| 19 | // with this library; see the file COPYING3. If not see |
| 20 | // <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | // 25.2.12 [alg.is_permutation] Is permutation |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <functional> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | struct my_equal_to |
| 29 | { |
| 30 | bool |
| 31 | operator()(int __x, int __y) const |
| 32 | { return __x % 10 == __y % 10; } |
| 33 | }; |
| 34 | |
| 35 | const int arr0[] = { 11, 22, 33, 44, 55 }; |
| 36 | |
| 37 | void |
| 38 | do_test(int arr1[5], bool np = true) |
| 39 | { |
| 40 | do |
| 41 | VERIFY( std::is_permutation(arr1, arr1 + 5, arr0) == np ); |
| 42 | while (std::next_permutation(arr1, arr1 + 5)); |
| 43 | } |
| 44 | |
| 45 | template<typename Predicate> |
| 46 | void |
| 47 | do_test(int arr1[5], Predicate pred, bool np = true) |
| 48 | { |
| 49 | do |
| 50 | VERIFY( std::is_permutation(arr1, arr1 + 5, arr0, pred) == np ); |
| 51 | while (std::next_permutation(arr1, arr1 + 5)); |
| 52 | } |
| 53 | |
| 54 | void test01() |
| 55 | { |
| 56 | int arr1[] = { 11, 22, 33, 44, 55 }; |
| 57 | do_test(arr1); |
| 58 | |
| 59 | int arr2[] = { 11, 33, 33, 44, 55 }; |
| 60 | do_test(arr2, false); |
| 61 | |
| 62 | int arr3[] = { 33, 33, 33, 44, 44 }; |
| 63 | do_test(arr3, false); |
| 64 | |
| 65 | int arr4[] = { 11, 22, 33, 44, 55 }; |
| 66 | do_test(arr4, std::equal_to<int>()); |
| 67 | |
| 68 | int arr5[] = { 11, 33, 33, 44, 55 }; |
| 69 | do_test(arr5, std::equal_to<int>(), false); |
| 70 | |
| 71 | int arr6[] = { 33, 33, 33, 44, 44 }; |
| 72 | do_test(arr6, std::equal_to<int>(), false); |
| 73 | |
| 74 | int arr7[] = { 1, 2, 3, 4, 5 }; |
| 75 | do_test(arr7, my_equal_to()); |
| 76 | |
| 77 | int arr8[] = { 1, 3, 3, 4, 5 }; |
| 78 | do_test(arr8, my_equal_to(), false); |
| 79 | |
| 80 | int arr9[] = { 3, 3, 3, 4, 4 }; |
| 81 | do_test(arr9, my_equal_to(), false); |
| 82 | |
| 83 | int arr10[] = { 111, 222, 333, 444, 555 }; |
| 84 | do_test(arr10, my_equal_to()); |
| 85 | |
| 86 | int arr11[] = { 1, 222, 33, 4, 55 }; |
| 87 | do_test(arr11, my_equal_to()); |
| 88 | |
| 89 | int arr12[] = { 111, 333, 333, 444, 555 }; |
| 90 | do_test(arr12, my_equal_to(), false); |
| 91 | |
| 92 | int arr13[] = { 333, 333, 333, 444, 444 }; |
| 93 | do_test(arr13, my_equal_to(), false); |
| 94 | } |
| 95 | |
| 96 | int main() |
| 97 | { |
| 98 | test01(); |
| 99 | return 0; |
| 100 | } |