]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/is_permutation/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / is_permutation / 2.cc
CommitLineData
52066eae 1// { dg-do run { target c++14 } }
f7fbb003 2
a945c346 3// Copyright (C) 2013-2024 Free Software Foundation, Inc.
f7fbb003
JW
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.2.12 [alg.is_permutation] Is permutation
21
22#include <algorithm>
94311bf3 23#include <iterator>
f7fbb003
JW
24#include <functional>
25#include <testsuite_hooks.h>
26
27struct my_equal_to
28{
29 bool
30 operator()(int __x, int __y) const
31 { return __x % 10 == __y % 10; }
32};
33
34const int arr0[] = { 11, 22, 33, 44, 55 };
35
36void
37do_test(int arr1[5], bool np = true, unsigned N = 5)
38{
f7fbb003
JW
39 do
40 VERIFY( std::is_permutation(arr1, arr1 + 5, arr0, arr0 + N) == np );
41 while (std::next_permutation(arr1, arr1 + 5));
42}
43
44template<typename Predicate>
45 void
46 do_test(int arr1[5], Predicate pred, bool np = true, unsigned N = 5)
47 {
f7fbb003
JW
48 do
49 VERIFY( std::is_permutation(arr1, arr1 + 5, arr0, arr0 + N, pred) == np );
50 while (std::next_permutation(arr1, arr1 + 5));
51 }
52
53void test01()
54{
55 int arr1[] = { 11, 22, 33, 44, 55 };
56 do_test(arr1);
57 do_test(arr1, false, 4);
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 do_test(arr4, std::equal_to<int>(), false, 4);
68
69 int arr5[] = { 11, 33, 33, 44, 55 };
70 do_test(arr5, std::equal_to<int>(), false);
71
72 int arr6[] = { 33, 33, 33, 44, 44 };
73 do_test(arr6, std::equal_to<int>(), false);
74
75 int arr7[] = { 1, 2, 3, 4, 5 };
76 do_test(arr7, my_equal_to());
77 do_test(arr7, my_equal_to(), false, 4);
78
79 int arr8[] = { 1, 3, 3, 4, 5 };
80 do_test(arr8, my_equal_to(), false);
81
82 int arr9[] = { 3, 3, 3, 4, 4 };
83 do_test(arr9, my_equal_to(), false);
84
85 int arr10[] = { 111, 222, 333, 444, 555 };
86 do_test(arr10, my_equal_to());
87 do_test(arr10, my_equal_to(), false, 4);
88
89 int arr11[] = { 1, 222, 33, 4, 55 };
90 do_test(arr11, my_equal_to());
91
92 int arr12[] = { 111, 333, 333, 444, 555 };
93 do_test(arr12, my_equal_to(), false);
94
95 int arr13[] = { 333, 333, 333, 444, 444 };
96 do_test(arr13, my_equal_to(), false);
97}
98
99bool thrower(int, int) { throw 1; }
100
101void test02()
102{
103 int arr[] = { 11, 22, 33 };
104 using namespace std;
105 is_permutation(begin(arr0), end(arr0), begin(arr), end(arr), thrower);
106}
107
108int main()
109{
110 test01();
111 test02();
112}