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