xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/rotate_copy.pass.cpp (revision 3b62047b8b2209bed57d239f581bdbfc91a10b94)
1 // -*- C++ -*-
2 //===-- rotate_copy.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 template <typename T>
30 bool
31 compare(const wrapper<T>& a, const wrapper<T>& b)
32 {
33     return a.t == b.t;
34 }
35 
36 template <typename T>
37 bool
38 compare(const T& a, const T& b)
39 {
40     return a == b;
41 }
42 
43 template <typename T>
44 struct wrapper
45 {
46     explicit wrapper(T t_) : t(t_) {}
47     wrapper&
48     operator=(const T& t_)
49     {
50         t = t_;
51         return *this;
52     }
53     friend bool
54     compare<T>(const wrapper<T>& a, const wrapper<T>& b);
55 
56   private:
57     T t;
58 };
59 
60 template <typename T, typename It1, typename It2>
61 struct comparator
62 {
63     using T1 = typename std::iterator_traits<It1>::value_type;
64     using T2 = typename std::iterator_traits<It2>::value_type;
65     bool
66     operator()(T1 a, T2 b)
67     {
68         T temp = a;
69         return compare(temp, b);
70     }
71 };
72 
73 struct test_one_policy
74 {
75 
76 #if __PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
77     __PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration
78     template <typename Iterator1, typename Iterator2>
79     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
80     operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
81                Iterator2 actual_e, std::size_t shift)
82     {
83     }
84     template <typename Iterator1, typename Iterator2>
85     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
86     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
87                Iterator2 actual_e, std::size_t shift)
88     {
89     }
90 #endif
91 
92     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2>
93     void
94     operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e,
95                std::size_t shift)
96     {
97         using namespace std;
98         using T = typename iterator_traits<Iterator2>::value_type;
99         Iterator1 data_m = std::next(data_b, shift);
100 
101         fill(actual_b, actual_e, T(-123));
102         Iterator2 actual_return = rotate_copy(exec, data_b, data_m, data_e, actual_b);
103 
104         EXPECT_TRUE(actual_return == actual_e, "wrong result of rotate_copy");
105         auto comparer = comparator<T, Iterator1, Iterator2>();
106         bool check = std::equal(data_m, data_e, actual_b, comparer);
107         check = check && std::equal(data_b, data_m, std::next(actual_b, std::distance(data_m, data_e)), comparer);
108 
109         EXPECT_TRUE(check, "wrong effect of rotate_copy");
110     }
111 };
112 
113 template <typename T1, typename T2>
114 void
115 test()
116 {
117 
118     const std::size_t max_len = 100000;
119 
120     Sequence<T2> actual(max_len, [](std::size_t i) { return T1(i); });
121 
122     Sequence<T1> data(max_len, [](std::size_t i) { return T1(i); });
123 
124     for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
125     {
126         std::size_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1};
127         for (std::size_t shift : shifts)
128         {
129             if (shift > 0 && shift < len)
130             {
131                 invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(),
132                                        actual.begin() + len, shift);
133                 invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(),
134                                        actual.begin() + len, shift);
135             }
136         }
137     }
138 }
139 
140 int32_t
141 main()
142 {
143     test<int32_t, int8_t>();
144     test<uint16_t, float32_t>();
145     test<float64_t, int64_t>();
146     test<wrapper<float64_t>, wrapper<float64_t>>();
147 
148     std::cout << done() << std::endl;
149     return 0;
150 }
151