1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // <algorithm> |
10 | |
11 | // template<BidirectionalIterator Iter> |
12 | // requires ShuffleIterator<Iter> |
13 | // && LessThanComparable<Iter::value_type> |
14 | // constexpr bool // constexpr in C++20 |
15 | // prev_permutation(Iter first, Iter last); |
16 | |
17 | #include <algorithm> |
18 | #include <cassert> |
19 | |
20 | #include "test_macros.h" |
21 | #include "test_iterators.h" |
22 | |
23 | #include <cstdio> |
24 | |
25 | TEST_CONSTEXPR_CXX14 int factorial(int x) |
26 | { |
27 | int r = 1; |
28 | for (; x; --x) |
29 | r *= x; |
30 | return r; |
31 | } |
32 | |
33 | template <class Iter> |
34 | TEST_CONSTEXPR_CXX20 bool |
35 | test() |
36 | { |
37 | int ia[] = {6, 5, 4, 3, 2, 1}; |
38 | const int sa = sizeof(ia)/sizeof(ia[0]); |
39 | int prev[sa]; |
40 | for (int e = 0; e <= sa; ++e) |
41 | { |
42 | int count = 0; |
43 | bool x; |
44 | do |
45 | { |
46 | std::copy(ia, ia+e, prev); |
47 | x = std::prev_permutation(Iter(ia), Iter(ia+e)); |
48 | if (e > 1) |
49 | { |
50 | if (x) |
51 | assert(std::lexicographical_compare(ia, ia+e, prev, prev+e)); |
52 | else |
53 | assert(std::lexicographical_compare(prev, prev+e, ia, ia+e)); |
54 | } |
55 | ++count; |
56 | } while (x); |
57 | assert(count == factorial(e)); |
58 | } |
59 | return true; |
60 | } |
61 | |
62 | int main(int, char**) |
63 | { |
64 | test<bidirectional_iterator<int*> >(); |
65 | test<random_access_iterator<int*> >(); |
66 | test<int*>(); |
67 | |
68 | #if TEST_STD_VER >= 20 |
69 | static_assert(test<bidirectional_iterator<int*>>()); |
70 | static_assert(test<random_access_iterator<int*>>()); |
71 | static_assert(test<int*>()); |
72 | #endif |
73 | |
74 | return 0; |
75 | } |
76 | |