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