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