| 1 | // -*- C++ -*- |
| 2 | //===-- partition_copy.pass.cpp -------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // UNSUPPORTED: c++03, c++11, c++14 |
| 11 | |
| 12 | // Tests for stable_partition and partition_copy |
| 13 | #include "support/pstl_test_config.h" |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <cstdlib> |
| 17 | #include <execution> |
| 18 | #include <functional> |
| 19 | #include <iterator> |
| 20 | |
| 21 | #include "support/utils.h" |
| 22 | |
| 23 | using namespace TestUtils; |
| 24 | |
| 25 | struct test_partition_copy |
| 26 | { |
| 27 | template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, |
| 28 | typename UnaryOp> |
| 29 | void |
| 30 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator true_first, OutputIterator, |
| 31 | OutputIterator2 false_first, OutputIterator2, UnaryOp unary_op) |
| 32 | { |
| 33 | |
| 34 | auto actual_ret = std::partition_copy(exec, first, last, true_first, false_first, unary_op); |
| 35 | |
| 36 | EXPECT_TRUE(std::distance(true_first, actual_ret.first) == std::count_if(first, last, unary_op), |
| 37 | "partition_copy has wrong effect from true sequence" ); |
| 38 | EXPECT_TRUE(std::distance(false_first, actual_ret.second) == std::count_if(first, last, std::not_fn(unary_op)), |
| 39 | "partition_copy has wrong effect from false sequence" ); |
| 40 | } |
| 41 | |
| 42 | //dummy specialization by iterator type and policy type, in case of broken configuration |
| 43 | #if defined(_PSTL_ICC_1800_TEST_MONOTONIC_RELEASE_64_BROKEN) |
| 44 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename UnaryOp> |
| 45 | void |
| 46 | operator()(pstl::execution::unsequenced_policy, std::reverse_iterator<InputIterator> first, |
| 47 | std::reverse_iterator<InputIterator> last, std::reverse_iterator<OutputIterator> true_first, |
| 48 | std::reverse_iterator<OutputIterator> true_last, std::reverse_iterator<OutputIterator2> false_first, |
| 49 | OutputIterator2 false_last, UnaryOp unary_op) |
| 50 | { |
| 51 | } |
| 52 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename UnaryOp> |
| 53 | void |
| 54 | operator()(pstl::execution::parallel_unsequenced_policy, std::reverse_iterator<InputIterator> first, |
| 55 | std::reverse_iterator<InputIterator> last, std::reverse_iterator<OutputIterator> true_first, |
| 56 | std::reverse_iterator<OutputIterator> true_last, std::reverse_iterator<OutputIterator2> false_first, |
| 57 | OutputIterator2 false_last, UnaryOp unary_op) |
| 58 | { |
| 59 | } |
| 60 | #endif |
| 61 | }; |
| 62 | |
| 63 | template <typename T, typename UnaryPred> |
| 64 | void |
| 65 | test(UnaryPred pred) |
| 66 | { |
| 67 | |
| 68 | const std::size_t max_size = 100000; |
| 69 | Sequence<T> in(max_size, [](std::size_t v) -> T { return T(v); }); |
| 70 | Sequence<T> actual_true(max_size); |
| 71 | Sequence<T> actual_false(max_size); |
| 72 | for (std::size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : std::size_t(3.1415 * n)) |
| 73 | { |
| 74 | |
| 75 | // for non-const input iterators |
| 76 | invoke_on_all_policies(test_partition_copy(), in.begin(), in.begin() + n, actual_true.begin(), |
| 77 | actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred); |
| 78 | |
| 79 | // for const input iterators |
| 80 | invoke_on_all_policies(test_partition_copy(), in.cbegin(), in.cbegin() + n, actual_true.begin(), |
| 81 | actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | struct test_non_const |
| 86 | { |
| 87 | template <typename Policy, typename InputIterator, typename OutputInterator> |
| 88 | void |
| 89 | operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) |
| 90 | { |
| 91 | auto is_even = [&](float64_t v) { |
| 92 | uint32_t i = (uint32_t)v; |
| 93 | return i % 2 == 0; |
| 94 | }; |
| 95 | |
| 96 | partition_copy(exec, input_iter, input_iter, out_iter, out_iter, non_const(is_even)); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | int |
| 101 | main() |
| 102 | { |
| 103 | test<int32_t>(pred: [](const int32_t value) { return value % 2; }); |
| 104 | |
| 105 | #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) |
| 106 | test<int32_t>(pred: [](const int32_t) { return true; }); |
| 107 | #endif |
| 108 | |
| 109 | test<float64_t>(pred: [](const float64_t value) { return value > 2 << 6; }); |
| 110 | test<Wrapper<float64_t>>(pred: [](const Wrapper<float64_t>& value) -> bool { return value.get_my_field() != nullptr; }); |
| 111 | |
| 112 | test_algo_basic_double<int32_t>(f: run_for_rnd_bi<test_non_const>()); |
| 113 | |
| 114 | std::cout << done() << std::endl; |
| 115 | return 0; |
| 116 | } |
| 117 | |