2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-require-effective-target tbb-backend }
6 //===-- partition.pass.cpp ------------------------------------------------===//
8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9 // See https://llvm.org/LICENSE.txt for license information.
10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12 //===----------------------------------------------------------------------===//
14 // Tests for stable_partition and partition
15 #include "pstl/pstl_test_config.h"
17 #ifdef PSTL_STANDALONE_TESTS
18 #include "pstl/execution"
19 #include "pstl/algorithm"
23 #endif // PSTL_STANDALONE_TESTS
25 #include "pstl/test_utils.h"
28 #include <type_traits>
30 using namespace TestUtils
;
35 explicit DataType(int32_t k
) : my_val(k
) {}
36 DataType(DataType
&& input
) { my_val
= std::move(input
.my_val
); }
38 operator=(DataType
&& input
)
40 my_val
= std::move(input
.my_val
);
50 operator<<(std::ostream
& stream
, const DataType
<T
>& input
)
52 return stream
<< input
.my_val
;
59 template <typename Iterator
>
60 typename
std::enable_if
<std::is_trivial
<typename
std::iterator_traits
<Iterator
>::value_type
>::value
, bool>::type
61 is_equal(Iterator first
, Iterator last
, Iterator d_first
)
63 return std::equal(first
, last
, d_first
);
66 template <typename Iterator
>
67 typename
std::enable_if
<!std::is_trivial
<typename
std::iterator_traits
<Iterator
>::value_type
>::value
, bool>::type
68 is_equal(Iterator first
, Iterator last
, Iterator d_first
)
73 struct test_one_policy
75 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \
76 _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specializations to skip testing in case of broken configuration
77 template <typename BiDirIt
, typename Size
, typename UnaryOp
, typename Generator
>
79 operator()(__pstl::execution::unsequenced_policy
, BiDirIt first
, BiDirIt last
, BiDirIt exp_first
, BiDirIt exp_last
,
80 Size n
, UnaryOp unary_op
, Generator generator
)
84 template <typename BiDirIt
, typename Size
, typename UnaryOp
, typename Generator
>
86 operator()(__pstl::execution::parallel_unsequenced_policy
, BiDirIt first
, BiDirIt last
, BiDirIt exp_first
,
87 BiDirIt exp_last
, Size n
, UnaryOp unary_op
, Generator generator
)
90 #elif _PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN //dummy specializations to skip testing in case of broken configuration
91 template <typename BiDirIt
, typename Size
, typename UnaryOp
, typename Generator
>
93 operator()(__pstl::execution::parallel_policy
, BiDirIt first
, BiDirIt last
, BiDirIt exp_first
, BiDirIt exp_last
,
94 Size n
, UnaryOp unary_op
, Generator generator
)
98 template <typename BiDirIt
, typename Size
, typename UnaryOp
, typename Generator
>
100 operator()(__pstl::execution::parallel_unsequenced_policy
, BiDirIt first
, BiDirIt last
, BiDirIt exp_first
,
101 BiDirIt exp_last
, Size n
, UnaryOp unary_op
, Generator generator
)
106 template <typename Policy
, typename BiDirIt
, typename Size
, typename UnaryOp
, typename Generator
>
107 typename
std::enable_if
<!is_same_iterator_category
<BiDirIt
, std::forward_iterator_tag
>::value
, void>::type
108 operator()(Policy
&& exec
, BiDirIt first
, BiDirIt last
, BiDirIt exp_first
, BiDirIt exp_last
, Size n
,
109 UnaryOp unary_op
, Generator generator
)
113 fill_data(first
, last
, generator
);
114 BiDirIt actual_ret
= std::partition(exec
, first
, last
, unary_op
);
115 EXPECT_TRUE(std::all_of(first
, actual_ret
, unary_op
) && !std::any_of(actual_ret
, last
, unary_op
),
116 "wrong effect from partition");
120 fill_data(exp_first
, exp_last
, generator
);
121 BiDirIt exp_ret
= std::stable_partition(exp_first
, exp_last
, unary_op
);
122 fill_data(first
, last
, generator
);
123 BiDirIt actual_ret
= std::stable_partition(exec
, first
, last
, unary_op
);
125 EXPECT_TRUE(std::distance(first
, actual_ret
) == std::distance(exp_first
, exp_ret
),
126 "wrong result from stable_partition");
127 EXPECT_TRUE((is_equal
<BiDirIt
>(exp_first
, exp_last
, first
)), "wrong effect from stable_partition");
130 template <typename Policy
, typename BiDirIt
, typename Size
, typename UnaryOp
, typename Generator
>
131 typename
std::enable_if
<is_same_iterator_category
<BiDirIt
, std::forward_iterator_tag
>::value
, void>::type
132 operator()(Policy
&& exec
, BiDirIt first
, BiDirIt last
, BiDirIt exp_first
, BiDirIt exp_last
, Size n
,
133 UnaryOp unary_op
, Generator generator
)
138 template <typename T
, typename Generator
, typename UnaryPred
>
140 test_by_type(Generator generator
, UnaryPred pred
)
144 size_t max_size
= 100000;
145 Sequence
<T
> in(max_size
, [](size_t v
) { return T(v
); });
146 Sequence
<T
> exp(max_size
, [](size_t v
) { return T(v
); });
148 for (size_t n
= 0; n
<= max_size
; n
= n
<= 16 ? n
+ 1 : size_t(3.1415 * n
))
150 invoke_on_all_policies(test_one_policy(), in
.begin(), in
.begin() + n
, exp
.begin(), exp
.begin() + n
, n
, pred
,
155 struct test_non_const
157 template <typename Policy
, typename Iterator
>
159 operator()(Policy
&& exec
, Iterator iter
)
161 auto is_even
= [&](float64_t v
) {
162 uint32_t i
= (uint32_t)v
;
165 invoke_if(exec
, [&]() {
166 partition(exec
, iter
, iter
, non_const(is_even
));
167 stable_partition(exec
, iter
, iter
, non_const(is_even
));
175 #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN
176 test_by_type
<int32_t>([](int32_t i
) { return i
; }, [](int32_t) { return true; });
178 test_by_type
<float64_t
>([](int32_t i
) { return -i
; }, [](const float64_t x
) { return x
< 0; });
179 test_by_type
<int64_t>([](int32_t i
) { return i
+ 1; }, [](int64_t x
) { return x
% 3 == 0; });
180 test_by_type
<DataType
<float32_t
>>([](int32_t i
) { return DataType
<float32_t
>(2 * i
+ 1); },
181 [](const DataType
<float32_t
>& x
) { return x
.get_val() < 0; });
183 test_algo_basic_single
<int32_t>(run_for_rnd_bi
<test_non_const
>());
185 std::cout
<< done() << std::endl
;