Synchronize libstdc++ PSTL with upstream LLVM PSTL
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / pstl / alg_modifying_operations / partition.cc
1 // -*- C++ -*-
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-require-effective-target tbb-backend }
5
6 //===-- partition.pass.cpp ------------------------------------------------===//
7 //
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
11 //
12 //===----------------------------------------------------------------------===//
13
14 // Tests for stable_partition and partition
15 #include "pstl/pstl_test_config.h"
16
17 #ifdef PSTL_STANDALONE_TESTS
18 #include "pstl/execution"
19 #include "pstl/algorithm"
20 #else
21 #include <execution>
22 #include <algorithm>
23 #endif // PSTL_STANDALONE_TESTS
24
25 #include "pstl/test_utils.h"
26
27 #include <iterator>
28 #include <type_traits>
29
30 using namespace TestUtils;
31
32 template <typename T>
33 struct DataType
34 {
35 explicit DataType(int32_t k) : my_val(k) {}
36 DataType(DataType&& input) { my_val = std::move(input.my_val); }
37 DataType&
38 operator=(DataType&& input)
39 {
40 my_val = std::move(input.my_val);
41 return *this;
42 }
43 T
44 get_val() const
45 {
46 return my_val;
47 }
48
49 friend std::ostream&
50 operator<<(std::ostream& stream, const DataType<T>& input)
51 {
52 return stream << input.my_val;
53 }
54
55 private:
56 T my_val;
57 };
58
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)
62 {
63 return std::equal(first, last, d_first);
64 }
65
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)
69 {
70 return true;
71 }
72
73 struct test_one_policy
74 {
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>
78 void
79 operator()(__pstl::execution::unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last,
80 Size n, UnaryOp unary_op, Generator generator)
81 {
82 }
83
84 template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
85 void
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)
88 {
89 }
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>
92 void
93 operator()(__pstl::execution::parallel_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last,
94 Size n, UnaryOp unary_op, Generator generator)
95 {
96 }
97
98 template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
99 void
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)
102 {
103 }
104 #endif
105
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)
110 {
111 // partition
112 {
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");
117 }
118 // stable_partition
119 {
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);
124
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");
128 }
129 }
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)
134 {
135 }
136 };
137
138 template <typename T, typename Generator, typename UnaryPred>
139 void
140 test_by_type(Generator generator, UnaryPred pred)
141 {
142
143 using namespace std;
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); });
147
148 for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
149 {
150 invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n, exp.begin(), exp.begin() + n, n, pred,
151 generator);
152 }
153 }
154
155 struct test_non_const
156 {
157 template <typename Policy, typename Iterator>
158 void
159 operator()(Policy&& exec, Iterator iter)
160 {
161 auto is_even = [&](float64_t v) {
162 uint32_t i = (uint32_t)v;
163 return i % 2 == 0;
164 };
165 invoke_if(exec, [&]() {
166 partition(exec, iter, iter, non_const(is_even));
167 stable_partition(exec, iter, iter, non_const(is_even));
168 });
169 }
170 };
171
172 int32_t
173 main()
174 {
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; });
177 #endif
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; });
182
183 test_algo_basic_single<int32_t>(run_for_rnd_bi<test_non_const>());
184
185 std::cout << done() << std::endl;
186 return 0;
187 }