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, Predicate<auto, Iter::value_type> Pred> |
12 | // requires ShuffleIterator<Iter> |
13 | // && CopyConstructible<Pred> |
14 | // constexpr Iter // constexpr in C++20 |
15 | // partition(Iter first, Iter last, Pred pred); |
16 | |
17 | #include <algorithm> |
18 | #include <cassert> |
19 | |
20 | |
21 | #include "test_macros.h" |
22 | #include "test_iterators.h" |
23 | |
24 | struct is_odd |
25 | { |
26 | TEST_CONSTEXPR bool operator()(const int& i) const {return i & 1;} |
27 | }; |
28 | |
29 | template <class Iter> |
30 | TEST_CONSTEXPR_CXX20 bool |
31 | test() |
32 | { |
33 | // check mixed |
34 | int ia[] = {1, 2, 3, 4, 5, 6, 7, 8 ,9}; |
35 | const unsigned sa = sizeof(ia)/sizeof(ia[0]); |
36 | Iter r = std::partition(Iter(ia), Iter(ia + sa), is_odd()); |
37 | assert(base(r) == ia + 5); |
38 | for (int* i = ia; i < base(r); ++i) |
39 | assert(is_odd()(*i)); |
40 | for (int* i = base(r); i < ia+sa; ++i) |
41 | assert(!is_odd()(*i)); |
42 | // check empty |
43 | r = std::partition(Iter(ia), Iter(ia), is_odd()); |
44 | assert(base(r) == ia); |
45 | // check all false |
46 | for (unsigned i = 0; i < sa; ++i) |
47 | ia[i] = 2*i; |
48 | r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); |
49 | assert(base(r) == ia); |
50 | // check all true |
51 | for (unsigned i = 0; i < sa; ++i) |
52 | ia[i] = 2*i+1; |
53 | r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); |
54 | assert(base(r) == ia+sa); |
55 | // check all true but last |
56 | for (unsigned i = 0; i < sa; ++i) |
57 | ia[i] = 2*i+1; |
58 | ia[sa-1] = 10; |
59 | r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); |
60 | assert(base(r) == ia+sa-1); |
61 | for (int* i = ia; i < base(r); ++i) |
62 | assert(is_odd()(*i)); |
63 | for (int* i = base(r); i < ia+sa; ++i) |
64 | assert(!is_odd()(*i)); |
65 | // check all true but first |
66 | for (unsigned i = 0; i < sa; ++i) |
67 | ia[i] = 2*i+1; |
68 | ia[0] = 10; |
69 | r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); |
70 | assert(base(r) == ia+sa-1); |
71 | for (int* i = ia; i < base(r); ++i) |
72 | assert(is_odd()(*i)); |
73 | for (int* i = base(r); i < ia+sa; ++i) |
74 | assert(!is_odd()(*i)); |
75 | // check all false but last |
76 | for (unsigned i = 0; i < sa; ++i) |
77 | ia[i] = 2*i; |
78 | ia[sa-1] = 11; |
79 | r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); |
80 | assert(base(r) == ia+1); |
81 | for (int* i = ia; i < base(r); ++i) |
82 | assert(is_odd()(*i)); |
83 | for (int* i = base(r); i < ia+sa; ++i) |
84 | assert(!is_odd()(*i)); |
85 | // check all false but first |
86 | for (unsigned i = 0; i < sa; ++i) |
87 | ia[i] = 2*i; |
88 | ia[0] = 11; |
89 | r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); |
90 | assert(base(r) == ia+1); |
91 | for (int* i = ia; i < base(r); ++i) |
92 | assert(is_odd()(*i)); |
93 | for (int* i = base(r); i < ia+sa; ++i) |
94 | assert(!is_odd()(*i)); |
95 | |
96 | return true; |
97 | } |
98 | |
99 | int main(int, char**) |
100 | { |
101 | test<bidirectional_iterator<int*> >(); |
102 | test<random_access_iterator<int*> >(); |
103 | test<int*>(); |
104 | |
105 | #if TEST_STD_VER >= 20 |
106 | static_assert(test<bidirectional_iterator<int*>>()); |
107 | static_assert(test<random_access_iterator<int*>>()); |
108 | static_assert(test<int*>()); |
109 | #endif |
110 | |
111 | return 0; |
112 | } |
113 | |