Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_modifying_operations/partition.cc

raw
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 {
36 explicit DataType(int32_t k) : my_val(k) {}
37 DataType(DataType&& input) { my_val = std::move(input.my_val); }
38 DataType&
39 operator=(DataType&& input)
40 {
41 my_val = std::move(input.my_val);
42 return *this;
43 }
44 T
45 get_val() const
46 {
47 return my_val;
48 }
49
50 friend std::ostream&
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 #pragma GCC diagnostic push
61 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
62 // is_trivial is deprecated in C++26
63 template <typename Iterator>
64 using iterator_value_type_is_trivial = std::is_trivial<
65 typename std::iterator_traits<Iterator>::value_type
66 >;
67 #pragma GCC diagnostic pop
68
69 template <typename Iterator>
70 typename std::enable_if<iterator_value_type_is_trivial<Iterator>::value, bool>::type
71 is_equal(Iterator first, Iterator last, Iterator d_first)
72 {
73 return std::equal(first, last, d_first);
74 }
75
76 template <typename Iterator>
77 typename std::enable_if<!iterator_value_type_is_trivial<Iterator>::value, bool>::type
78 is_equal(Iterator, Iterator, Iterator)
79 {
80 return true;
81 }
82
83 struct test_one_policy
84 {
85 #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \
86 defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specializations to skip testing in case of broken configuration
87 template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
88 void
89 operator()(__pstl::execution::unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last,
90 Size n, UnaryOp unary_op, Generator generator)
91 {
92 }
93
94 template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
95 void
96 operator()(__pstl::execution::parallel_unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first,
97 BiDirIt exp_last, Size n, UnaryOp unary_op, Generator generator)
98 {
99 }
100 #elif defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) //dummy specializations to skip testing in case of broken configuration
101 template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
102 void
103 operator()(__pstl::execution::parallel_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last,
104 Size n, UnaryOp unary_op, Generator generator)
105 {
106 }
107
108 template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
109 void
110 operator()(__pstl::execution::parallel_unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first,
111 BiDirIt exp_last, Size n, UnaryOp unary_op, Generator generator)
112 {
113 }
114 #endif
115
116 template <typename Policy, typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
117 typename std::enable_if<!is_same_iterator_category<BiDirIt, std::forward_iterator_tag>::value, void>::type
118 operator()(Policy&& exec, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last, Size, UnaryOp unary_op,
119 Generator generator)
120 {
121 // partition
122 {
123 fill_data(first, last, generator);
124 BiDirIt actual_ret = std::partition(exec, first, last, unary_op);
125 EXPECT_TRUE(std::all_of(first, actual_ret, unary_op) && !std::any_of(actual_ret, last, unary_op),
126 "wrong effect from partition");
127 }
128 // stable_partition
129 {
130 fill_data(exp_first, exp_last, generator);
131 BiDirIt exp_ret = std::stable_partition(exp_first, exp_last, unary_op);
132 fill_data(first, last, generator);
133 BiDirIt actual_ret = std::stable_partition(exec, first, last, unary_op);
134
135 EXPECT_TRUE(std::distance(first, actual_ret) == std::distance(exp_first, exp_ret),
136 "wrong result from stable_partition");
137 EXPECT_TRUE((is_equal<BiDirIt>(exp_first, exp_last, first)), "wrong effect from stable_partition");
138 }
139 }
140 template <typename Policy, typename BiDirIt, typename Size, typename UnaryOp, typename Generator>
141 typename std::enable_if<is_same_iterator_category<BiDirIt, std::forward_iterator_tag>::value, void>::type
142 operator()(Policy&&, BiDirIt, BiDirIt, BiDirIt, BiDirIt, Size, UnaryOp, Generator)
143 {
144 }
145 };
146
147 template <typename T, typename Generator, typename UnaryPred>
148 void
149 test_by_type(Generator generator, UnaryPred pred)
150 {
151
152 using namespace std;
153 size_t max_size = 100000;
154 Sequence<T> in(max_size, [](size_t v) { return T(v); });
155 Sequence<T> exp(max_size, [](size_t v) { return T(v); });
156
157 for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
158 {
159 invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n, exp.begin(), exp.begin() + n, n, pred,
160 generator);
161 }
162 }
163
164 struct test_non_const
165 {
166 template <typename Policy, typename Iterator>
167 void
168 operator()(Policy&& exec, Iterator iter)
169 {
170 auto is_even = [&](float64_t v) {
171 uint32_t i = (uint32_t)v;
172 return i % 2 == 0;
173 };
174 invoke_if(exec, [&]() {
175 partition(exec, iter, iter, non_const(is_even));
176 stable_partition(exec, iter, iter, non_const(is_even));
177 });
178 }
179 };
180
181 int
182 main()
183 {
184 #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN)
185 test_by_type<int32_t>([](int32_t i) { return i; }, [](int32_t) { return true; });
186 #endif
187 test_by_type<float64_t>([](int32_t i) { return -i; }, [](const float64_t x) { return x < 0; });
188 test_by_type<int64_t>([](int32_t i) { return i + 1; }, [](int64_t x) { return x % 3 == 0; });
189 test_by_type<DataType<float32_t>>([](int32_t i) { return DataType<float32_t>(2 * i + 1); },
190 [](const DataType<float32_t>& x) { return x.get_val() < 0; });
191
192 test_algo_basic_single<int32_t>(run_for_rnd_bi<test_non_const>());
193
194 std::cout << done() << std::endl;
195 return 0;
196 }