Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_modifying_operations/rotate.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 //===-- rotate.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 #include "pstl/pstl_test_config.h"
16
17 #ifdef PSTL_STANDALONE_TESTS
18 #include <iterator>
19
20 #include "pstl/execution"
21 #include "pstl/algorithm"
22 #else
23 #include <execution>
24 #include <algorithm>
25 #endif // PSTL_STANDALONE_TESTS
26
27 #include "pstl/test_utils.h"
28
29 using namespace TestUtils;
30
31 template <typename T>
32 struct wrapper
33 {
34 T t;
35 int move_count;
36 explicit wrapper(T t_) : t(t_), move_count(0) {}
37 wrapper&
38 operator=(const T& t_)
39 {
40 t = t_;
41 return *this;
42 }
43
44 wrapper(const wrapper<T>& a) : move_count(0) { t = a.t; }
45
46 wrapper<T>&
47 operator=(wrapper<T>& a)
48 {
49 t = a.t;
50 return *this;
51 }
52
53 wrapper<T>&
54 operator=(wrapper<T>&& a)
55 {
56 t = a.t;
57 move_count += 1;
58 return *this;
59 }
60 };
61
62 template <typename T>
63 struct compare
64 {
65 bool
66 operator()(const T& a, const T& b)
67 {
68 return a == b;
69 }
70 };
71
72 template <typename T>
73 struct compare<wrapper<T>>
74 {
75 bool
76 operator()(const wrapper<T>& a, const wrapper<T>& b)
77 {
78 return a.t == b.t;
79 }
80 };
81 #include <typeinfo>
82
83 struct test_one_policy
84 {
85
86 #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \
87 defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specializations to skip testing in case of broken configuration
88 template <typename Iterator, typename Size>
89 void
90 operator()(__pstl::execution::unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b,
91 Iterator actual_e, Size shift)
92 {
93 }
94 template <typename Iterator, typename Size>
95 void
96 operator()(__pstl::execution::parallel_unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b,
97 Iterator actual_e, Size shift)
98 {
99 }
100 #endif
101
102 template <typename ExecutionPolicy, typename Iterator, typename Size>
103 void
104 operator()(ExecutionPolicy&& exec, Iterator data_b, Iterator data_e, Iterator actual_b, Iterator actual_e,
105 Size shift)
106 {
107 using namespace std;
108 using T = typename iterator_traits<Iterator>::value_type;
109 Iterator actual_m = std::next(actual_b, shift);
110
111 copy(data_b, data_e, actual_b);
112 Iterator actual_return = rotate(exec, actual_b, actual_m, actual_e);
113
114 EXPECT_TRUE(actual_return == std::next(actual_b, std::distance(actual_m, actual_e)), "wrong result of rotate");
115 auto comparator = compare<T>();
116 bool check = std::equal(actual_return, actual_e, data_b, comparator);
117 check = check && std::equal(actual_b, actual_return, std::next(data_b, shift), comparator);
118
119 EXPECT_TRUE(check, "wrong effect of rotate");
120 EXPECT_TRUE(check_move(exec, actual_b, actual_e, shift), "wrong move test of rotate");
121 }
122
123 template <typename ExecutionPolicy, typename Iterator, typename Size>
124 typename std::enable_if<
125 is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value &&
126 !std::is_same<ExecutionPolicy, std::execution::sequenced_policy>::value &&
127 std::is_same<typename std::iterator_traits<Iterator>::value_type, wrapper<float32_t>>::value,
128 bool>::type
129 check_move(ExecutionPolicy&&, Iterator b, Iterator e, Size shift)
130 {
131 bool result = all_of(b, e, [](wrapper<float32_t>& a) {
132 bool temp = a.move_count > 0;
133 a.move_count = 0;
134 return temp;
135 });
136 return shift == 0 || result;
137 }
138
139 template <typename ExecutionPolicy, typename Iterator, typename Size>
140 typename std::enable_if<
141 !(is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value &&
142 !std::is_same<ExecutionPolicy, std::execution::sequenced_policy>::value &&
143 std::is_same<typename std::iterator_traits<Iterator>::value_type, wrapper<float32_t>>::value),
144 bool>::type
145 check_move(ExecutionPolicy&&, Iterator, Iterator, Size)
146 {
147 return true;
148 }
149 };
150
151 template <typename T>
152 void
153 test()
154 {
155 const int32_t max_len = 100000;
156
157 Sequence<T> actual(max_len, [](std::size_t i) { return T(i); });
158 Sequence<T> data(max_len, [](std::size_t i) { return T(i); });
159
160 for (int32_t len = 0; len < max_len; len = len <= 16 ? len + 1 : int32_t(3.1415 * len))
161 {
162 int32_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1};
163 for (auto shift : shifts)
164 {
165 if (shift >= 0 && shift < len)
166 {
167 invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(),
168 actual.begin() + len, shift);
169 }
170 }
171 }
172 }
173
174 int
175 main()
176 {
177 test<int32_t>();
178 test<wrapper<float64_t>>();
179
180 std::cout << done() << std::endl;
181 return 0;
182 }