1 // -*- C++ -*-
2 // { dg-options "-ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-timeout-factor 3 }
5 // { dg-require-effective-target tbb_backend }
6 
7 //===-- partition.pass.cpp ------------------------------------------------===//
8 //
9 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10 // See https://llvm.org/LICENSE.txt for license information.
11 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12 //
13 //===----------------------------------------------------------------------===//
14 
15 // Tests for stable_partition and partition
16 #include "pstl/pstl_test_config.h"
17 
18 #ifdef PSTL_STANDALONE_TESTS
19 #include "pstl/execution"
20 #include "pstl/algorithm"
21 #else
22 #include <execution>
23 #include <algorithm>
24 #endif // PSTL_STANDALONE_TESTS
25 
26 #include "pstl/test_utils.h"
27 
28 #include <iterator>
29 #include <type_traits>
30 
31 using namespace TestUtils;
32 
33 template <typename T>
34 struct DataType
35 {
DataTypeDataType36     explicit DataType(int32_t k) : my_val(k) {}
DataTypeDataType37     DataType(DataType&& input) { my_val = std::move(input.my_val); }
38     DataType&
operator =DataType39     operator=(DataType&& input)
40     {
41         my_val = std::move(input.my_val);
42         return *this;
43     }
44     T
get_valDataType45     get_val() const
46     {
47         return my_val;
48     }
49 
50     friend std::ostream&
operator <<(std::ostream & stream,const DataType<T> & input)51     operator<<(std::ostream& stream, const DataType<T>& input)
52     {
53         return stream << input.my_val;
54     }
55 
56   private:
57     T my_val;
58 };
59 
60 template <typename Iterator>
61 typename std::enable_if<std::is_trivial<typename std::iterator_traits<Iterator>::value_type>::value, bool>::type
is_equal(Iterator first,Iterator last,Iterator d_first)62 is_equal(Iterator first, Iterator last, Iterator d_first)
63 {
64     return std::equal(first, last, d_first);
65 }
66 
67 template <typename Iterator>
68 typename std::enable_if<!std::is_trivial<typename std::iterator_traits<Iterator>::value_type>::value, bool>::type
is_equal(Iterator first,Iterator last,Iterator d_first)69 is_equal(Iterator first, Iterator last, Iterator d_first)
70 {
71     return true;
72 }
73 
74 struct test_one_policy
75 {
76 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
77     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specializations to skip testing in case of broken configuration
78     template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
79     void
operator ()test_one_policy80     operator()(__pstl::execution::unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last,
81                Size n, UnaryOp unary_op, Generator generator)
82     {
83     }
84 
85     template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
86     void
operator ()test_one_policy87     operator()(__pstl::execution::parallel_unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first,
88                BiDirIt exp_last, Size n, UnaryOp unary_op, Generator generator)
89     {
90     }
91 #elif _PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN //dummy specializations to skip testing in case of broken configuration
92     template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
93     void
94     operator()(__pstl::execution::parallel_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last,
95                Size n, UnaryOp unary_op, Generator generator)
96     {
97     }
98 
99     template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
100     void
101     operator()(__pstl::execution::parallel_unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first,
102                BiDirIt exp_last, Size n, UnaryOp unary_op, Generator generator)
103     {
104     }
105 #endif
106 
107     template <typename Policy, typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
108     typename std::enable_if<!is_same_iterator_category<BiDirIt, std::forward_iterator_tag>::value, void>::type
operator ()test_one_policy109     operator()(Policy&& exec, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last, Size n,
110                UnaryOp unary_op, Generator generator)
111     {
112         // partition
113         {
114             fill_data(first, last, generator);
115             BiDirIt actual_ret = std::partition(exec, first, last, unary_op);
116             EXPECT_TRUE(std::all_of(first, actual_ret, unary_op) && !std::any_of(actual_ret, last, unary_op),
117                         "wrong effect from partition");
118         }
119         // stable_partition
120         {
121             fill_data(exp_first, exp_last, generator);
122             BiDirIt exp_ret = std::stable_partition(exp_first, exp_last, unary_op);
123             fill_data(first, last, generator);
124             BiDirIt actual_ret = std::stable_partition(exec, first, last, unary_op);
125 
126             EXPECT_TRUE(std::distance(first, actual_ret) == std::distance(exp_first, exp_ret),
127                         "wrong result from stable_partition");
128             EXPECT_TRUE((is_equal<BiDirIt>(exp_first, exp_last, first)), "wrong effect from stable_partition");
129         }
130     }
131     template <typename Policy, typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
132     typename std::enable_if<is_same_iterator_category<BiDirIt, std::forward_iterator_tag>::value, void>::type
operator ()test_one_policy133     operator()(Policy&& exec, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last, Size n,
134                UnaryOp unary_op, Generator generator)
135     {
136     }
137 };
138 
139 template <typename T, typename Generator, typename UnaryPred>
140 void
test_by_type(Generator generator,UnaryPred pred)141 test_by_type(Generator generator, UnaryPred pred)
142 {
143 
144     using namespace std;
145     size_t max_size = 100000;
146     Sequence<T> in(max_size, [](size_t v) { return T(v); });
147     Sequence<T> exp(max_size, [](size_t v) { return T(v); });
148 
149     for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
150     {
151         invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n, exp.begin(), exp.begin() + n, n, pred,
152                                generator);
153     }
154 }
155 
156 struct test_non_const
157 {
158     template <typename Policy, typename Iterator>
159     void
operator ()test_non_const160     operator()(Policy&& exec, Iterator iter)
161     {
162         auto is_even = [&](float64_t v) {
163             uint32_t i = (uint32_t)v;
164             return i % 2 == 0;
165         };
166         invoke_if(exec, [&]() {
167             partition(exec, iter, iter, non_const(is_even));
168             stable_partition(exec, iter, iter, non_const(is_even));
169         });
170     }
171 };
172 
173 int32_t
main()174 main()
175 {
176 #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN
177     test_by_type<int32_t>([](int32_t i) { return i; }, [](int32_t) { return true; });
178 #endif
179     test_by_type<float64_t>([](int32_t i) { return -i; }, [](const float64_t x) { return x < 0; });
180     test_by_type<int64_t>([](int32_t i) { return i + 1; }, [](int64_t x) { return x % 3 == 0; });
181     test_by_type<DataType<float32_t>>([](int32_t i) { return DataType<float32_t>(2 * i + 1); },
182                                       [](const DataType<float32_t>& x) { return x.get_val() < 0; });
183 
184     test_algo_basic_single<int32_t>(run_for_rnd_bi<test_non_const>());
185 
186     std::cout << done() << std::endl;
187     return 0;
188 }
189