| 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 <class InputIterator, class Predicate> |
| 12 | // constexpr bool // constexpr after C++17 |
| 13 | // is_partitioned(InputIterator first, InputIterator last, Predicate pred); |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <functional> |
| 17 | #include <cstddef> |
| 18 | #include <cassert> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | #include "test_iterators.h" |
| 22 | #include "counting_predicates.h" |
| 23 | |
| 24 | struct is_odd { |
| 25 | TEST_CONSTEXPR bool operator()(const int &i) const { return i & 1; } |
| 26 | }; |
| 27 | |
| 28 | #if TEST_STD_VER > 17 |
| 29 | TEST_CONSTEXPR bool test_constexpr() { |
| 30 | int ia[] = {1, 3, 5, 2, 4, 6}; |
| 31 | int ib[] = {1, 2, 3, 4, 5, 6}; |
| 32 | return std::is_partitioned(std::begin(ia), std::end(ia), is_odd()) |
| 33 | && !std::is_partitioned(std::begin(ib), std::end(ib), is_odd()); |
| 34 | } |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | int main(int, char**) { |
| 39 | { |
| 40 | const int ia[] = {1, 2, 3, 4, 5, 6}; |
| 41 | unary_counting_predicate<is_odd, int> pred((is_odd())); |
| 42 | assert(!std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)), |
| 43 | cpp17_input_iterator<const int *>(std::end(ia)), |
| 44 | std::ref(pred))); |
| 45 | assert(static_cast<std::ptrdiff_t>(pred.count()) <= |
| 46 | std::distance(std::begin(ia), std::end(ia))); |
| 47 | } |
| 48 | { |
| 49 | const int ia[] = {1, 3, 5, 2, 4, 6}; |
| 50 | unary_counting_predicate<is_odd, int> pred((is_odd())); |
| 51 | assert(std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)), |
| 52 | cpp17_input_iterator<const int *>(std::end(ia)), |
| 53 | std::ref(pred))); |
| 54 | assert(static_cast<std::ptrdiff_t>(pred.count()) <= |
| 55 | std::distance(std::begin(ia), std::end(ia))); |
| 56 | } |
| 57 | { |
| 58 | const int ia[] = {2, 4, 6, 1, 3, 5}; |
| 59 | unary_counting_predicate<is_odd, int> pred((is_odd())); |
| 60 | assert(!std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)), |
| 61 | cpp17_input_iterator<const int *>(std::end(ia)), |
| 62 | std::ref(pred))); |
| 63 | assert(static_cast<std::ptrdiff_t>(pred.count()) <= |
| 64 | std::distance(std::begin(ia), std::end(ia))); |
| 65 | } |
| 66 | { |
| 67 | const int ia[] = {1, 3, 5, 2, 4, 6, 7}; |
| 68 | unary_counting_predicate<is_odd, int> pred((is_odd())); |
| 69 | assert(!std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)), |
| 70 | cpp17_input_iterator<const int *>(std::end(ia)), |
| 71 | std::ref(pred))); |
| 72 | assert(static_cast<std::ptrdiff_t>(pred.count()) <= |
| 73 | std::distance(std::begin(ia), std::end(ia))); |
| 74 | } |
| 75 | { |
| 76 | const int ia[] = {1, 3, 5, 2, 4, 6, 7}; |
| 77 | unary_counting_predicate<is_odd, int> pred((is_odd())); |
| 78 | assert(std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)), |
| 79 | cpp17_input_iterator<const int *>(std::begin(ia)), |
| 80 | std::ref(pred))); |
| 81 | assert(static_cast<std::ptrdiff_t>(pred.count()) <= |
| 82 | std::distance(std::begin(ia), std::begin(ia))); |
| 83 | } |
| 84 | { |
| 85 | const int ia[] = {1, 3, 5, 7, 9, 11, 2}; |
| 86 | unary_counting_predicate<is_odd, int> pred((is_odd())); |
| 87 | assert(std::is_partitioned(cpp17_input_iterator<const int *>(std::begin(ia)), |
| 88 | cpp17_input_iterator<const int *>(std::end(ia)), |
| 89 | std::ref(pred))); |
| 90 | assert(static_cast<std::ptrdiff_t>(pred.count()) <= |
| 91 | std::distance(std::begin(ia), std::end(ia))); |
| 92 | } |
| 93 | |
| 94 | #if TEST_STD_VER > 17 |
| 95 | static_assert(test_constexpr()); |
| 96 | #endif |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |