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