1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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);
21 #include "test_macros.h"
22 #include "test_iterators.h"
26 TEST_CONSTEXPR
bool operator()(const int& i
) const {return i
& 1;}
30 TEST_CONSTEXPR_CXX20
bool
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
)
40 for (int* i
= base(r
); i
< ia
+sa
; ++i
)
41 assert(!is_odd()(*i
));
43 r
= std::partition(Iter(ia
), Iter(ia
), is_odd());
44 assert(base(r
) == ia
);
46 for (unsigned i
= 0; i
< sa
; ++i
)
48 r
= std::partition(Iter(ia
), Iter(ia
+sa
), is_odd());
49 assert(base(r
) == ia
);
51 for (unsigned i
= 0; i
< sa
; ++i
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
93 for (int* i
= base(r
); i
< ia
+sa
; ++i
)
94 assert(!is_odd()(*i
));
101 test
<bidirectional_iterator
<int*> >();
102 test
<random_access_iterator
<int*> >();
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*>());