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