15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <algorithm>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template <class InputIterator, class Predicate>
12530c69e9SArthur O'Dwyer //     constexpr bool       // constexpr after C++17
135a83710eSEric Fiselier //     is_partitioned(InputIterator first, InputIterator last, Predicate pred);
145a83710eSEric Fiselier 
155a83710eSEric Fiselier #include <algorithm>
16bb1db210SMarshall Clow #include <functional>
17c71bd55bSEric Fiselier #include <cstddef>
185a83710eSEric Fiselier #include <cassert>
195a83710eSEric Fiselier 
2049c7643cSMarshall Clow #include "test_macros.h"
215a83710eSEric Fiselier #include "test_iterators.h"
22cc89063bSNico Weber #include "counting_predicates.h"
235a83710eSEric Fiselier 
24c71bd55bSEric Fiselier struct is_odd {
operator ()is_odd2549c7643cSMarshall Clow   TEST_CONSTEXPR bool operator()(const int &i) const { return i & 1; }
265a83710eSEric Fiselier };
275a83710eSEric Fiselier 
2849c7643cSMarshall Clow #if TEST_STD_VER > 17
test_constexpr()29404ee020SMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
3049c7643cSMarshall Clow     int ia[] = {1, 3, 5, 2, 4, 6};
3149c7643cSMarshall Clow     int ib[] = {1, 2, 3, 4, 5, 6};
3249c7643cSMarshall Clow     return     std::is_partitioned(std::begin(ia), std::end(ia), is_odd())
3349c7643cSMarshall Clow            && !std::is_partitioned(std::begin(ib), std::end(ib), is_odd());
3449c7643cSMarshall Clow     }
3549c7643cSMarshall Clow #endif
3649c7643cSMarshall Clow 
3749c7643cSMarshall Clow 
main(int,char **)382df59c50SJF Bastien int main(int, char**) {
395a83710eSEric Fiselier   {
405a83710eSEric Fiselier     const int ia[] = {1, 2, 3, 4, 5, 6};
41b9595b79SMarshall Clow     unary_counting_predicate<is_odd, int> pred((is_odd()));
42*773ae441SChristopher Di Bella     assert(!std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)),
43*773ae441SChristopher Di Bella                                 cpp17_input_iterator<const int *>(std::end(ia)),
44b9595b79SMarshall Clow                                 std::ref(pred)));
45c71bd55bSEric Fiselier     assert(static_cast<std::ptrdiff_t>(pred.count()) <=
46c71bd55bSEric Fiselier            std::distance(std::begin(ia), std::end(ia)));
475a83710eSEric Fiselier   }
485a83710eSEric Fiselier   {
495a83710eSEric Fiselier     const int ia[] = {1, 3, 5, 2, 4, 6};
50b9595b79SMarshall Clow     unary_counting_predicate<is_odd, int> pred((is_odd()));
51*773ae441SChristopher Di Bella     assert(std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)),
52*773ae441SChristopher Di Bella                                cpp17_input_iterator<const int *>(std::end(ia)),
53b9595b79SMarshall Clow                                std::ref(pred)));
54c71bd55bSEric Fiselier     assert(static_cast<std::ptrdiff_t>(pred.count()) <=
55c71bd55bSEric Fiselier            std::distance(std::begin(ia), std::end(ia)));
565a83710eSEric Fiselier   }
575a83710eSEric Fiselier   {
585a83710eSEric Fiselier     const int ia[] = {2, 4, 6, 1, 3, 5};
59b9595b79SMarshall Clow     unary_counting_predicate<is_odd, int> pred((is_odd()));
60*773ae441SChristopher Di Bella     assert(!std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)),
61*773ae441SChristopher Di Bella                                 cpp17_input_iterator<const int *>(std::end(ia)),
62b9595b79SMarshall Clow                                 std::ref(pred)));
63c71bd55bSEric Fiselier     assert(static_cast<std::ptrdiff_t>(pred.count()) <=
64c71bd55bSEric Fiselier            std::distance(std::begin(ia), std::end(ia)));
655a83710eSEric Fiselier   }
665a83710eSEric Fiselier   {
675a83710eSEric Fiselier     const int ia[] = {1, 3, 5, 2, 4, 6, 7};
68b9595b79SMarshall Clow     unary_counting_predicate<is_odd, int> pred((is_odd()));
69*773ae441SChristopher Di Bella     assert(!std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)),
70*773ae441SChristopher Di Bella                                 cpp17_input_iterator<const int *>(std::end(ia)),
71b9595b79SMarshall Clow                                 std::ref(pred)));
72c71bd55bSEric Fiselier     assert(static_cast<std::ptrdiff_t>(pred.count()) <=
73c71bd55bSEric Fiselier            std::distance(std::begin(ia), std::end(ia)));
745a83710eSEric Fiselier   }
755a83710eSEric Fiselier   {
765a83710eSEric Fiselier     const int ia[] = {1, 3, 5, 2, 4, 6, 7};
77b9595b79SMarshall Clow     unary_counting_predicate<is_odd, int> pred((is_odd()));
78*773ae441SChristopher Di Bella     assert(std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)),
79*773ae441SChristopher Di Bella                                cpp17_input_iterator<const int *>(std::begin(ia)),
80b9595b79SMarshall Clow                                std::ref(pred)));
81c71bd55bSEric Fiselier     assert(static_cast<std::ptrdiff_t>(pred.count()) <=
82c71bd55bSEric Fiselier            std::distance(std::begin(ia), std::begin(ia)));
83b9595b79SMarshall Clow   }
84b9595b79SMarshall Clow   {
85b9595b79SMarshall Clow     const int ia[] = {1, 3, 5, 7, 9, 11, 2};
86b9595b79SMarshall Clow     unary_counting_predicate<is_odd, int> pred((is_odd()));
87*773ae441SChristopher Di Bella     assert(std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)),
88*773ae441SChristopher Di Bella                                cpp17_input_iterator<const int *>(std::end(ia)),
89b9595b79SMarshall Clow                                std::ref(pred)));
90c71bd55bSEric Fiselier     assert(static_cast<std::ptrdiff_t>(pred.count()) <=
91c71bd55bSEric Fiselier            std::distance(std::begin(ia), std::end(ia)));
925a83710eSEric Fiselier   }
9349c7643cSMarshall Clow 
9449c7643cSMarshall Clow #if TEST_STD_VER > 17
9549c7643cSMarshall Clow     static_assert(test_constexpr());
9649c7643cSMarshall Clow #endif
972df59c50SJF Bastien 
982df59c50SJF Bastien   return 0;
995a83710eSEric Fiselier }
100