1 | // -*- C++ -*- |
2 | //===-- copy_move.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 | // Tests for copy, move and copy_n |
13 | #include "support/pstl_test_config.h" |
14 | |
15 | #include <execution> |
16 | #include <algorithm> |
17 | |
18 | #include "support/utils.h" |
19 | |
20 | using namespace TestUtils; |
21 | |
22 | struct run_copy |
23 | { |
24 | |
25 | #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ |
26 | defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration |
27 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, typename T> |
28 | void |
29 | operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, |
30 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size size, |
31 | Size n, T trash) |
32 | { |
33 | } |
34 | |
35 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, typename T> |
36 | void |
37 | operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, |
38 | OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, |
39 | OutputIterator2 expected_last, Size size, Size n, T trash) |
40 | { |
41 | } |
42 | #endif |
43 | |
44 | template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, |
45 | typename T> |
46 | void |
47 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, |
48 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size size, Size n, T trash) |
49 | { |
50 | // Cleaning |
51 | std::fill_n(expected_first, size, trash); |
52 | std::fill_n(out_first, size, trash); |
53 | |
54 | // Run copy |
55 | copy(first, last, expected_first); |
56 | auto k = copy(exec, first, last, out_first); |
57 | for (size_t j = 0; j < GuardSize; ++j) |
58 | ++k; |
59 | EXPECT_EQ_N(expected_first, out_first, size, "wrong effect from copy" ); |
60 | EXPECT_TRUE(out_last == k, "wrong return value from copy" ); |
61 | |
62 | // Cleaning |
63 | std::fill_n(out_first, size, trash); |
64 | // Run copy_n |
65 | k = copy_n(exec, first, n, out_first); |
66 | for (size_t j = 0; j < GuardSize; ++j) |
67 | ++k; |
68 | EXPECT_EQ_N(expected_first, out_first, size, "wrong effect from copy_n" ); |
69 | EXPECT_TRUE(out_last == k, "wrong return value from copy_n" ); |
70 | } |
71 | }; |
72 | |
73 | template <typename T> |
74 | struct run_move |
75 | { |
76 | |
77 | #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ |
78 | defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration |
79 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> |
80 | void |
81 | operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, |
82 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size size, |
83 | Size n, T trash) |
84 | { |
85 | } |
86 | |
87 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> |
88 | void |
89 | operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, |
90 | OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, |
91 | OutputIterator2 expected_last, Size size, Size n, T trash) |
92 | { |
93 | } |
94 | #endif |
95 | |
96 | template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> |
97 | void |
98 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, |
99 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size size, Size, T trash) |
100 | { |
101 | // Cleaning |
102 | std::fill_n(expected_first, size, trash); |
103 | std::fill_n(out_first, size, trash); |
104 | |
105 | // Run move |
106 | move(first, last, expected_first); |
107 | auto k = move(exec, first, last, out_first); |
108 | for (size_t j = 0; j < GuardSize; ++j) |
109 | ++k; |
110 | EXPECT_EQ_N(expected_first, out_first, size, "wrong effect from move" ); |
111 | EXPECT_TRUE(out_last == k, "wrong return value from move" ); |
112 | } |
113 | }; |
114 | |
115 | template <typename T> |
116 | struct run_move<Wrapper<T>> |
117 | { |
118 | |
119 | #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ |
120 | defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration |
121 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> |
122 | void |
123 | operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, |
124 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size size, |
125 | Size n, Wrapper<T> trash) |
126 | { |
127 | } |
128 | |
129 | template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> |
130 | void |
131 | operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, |
132 | OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, |
133 | OutputIterator2 expected_last, Size size, Size n, Wrapper<T> trash) |
134 | { |
135 | } |
136 | #endif |
137 | |
138 | template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> |
139 | void |
140 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, |
141 | OutputIterator out_last, OutputIterator2, OutputIterator2, Size size, Size, Wrapper<T> trash) |
142 | { |
143 | // Cleaning |
144 | std::fill_n(out_first, size, trash); |
145 | Wrapper<T>::SetMoveCount(0); |
146 | |
147 | // Run move |
148 | auto k = move(exec, first, last, out_first); |
149 | for (size_t j = 0; j < GuardSize; ++j) |
150 | ++k; |
151 | EXPECT_TRUE(Wrapper<T>::MoveCount() == size, "wrong effect from move" ); |
152 | EXPECT_TRUE(out_last == k, "wrong return value from move" ); |
153 | } |
154 | }; |
155 | |
156 | template <typename T, typename Convert> |
157 | void |
158 | test(T trash, Convert convert) |
159 | { |
160 | // Try sequences of various lengths. |
161 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
162 | { |
163 | // count is number of output elements, plus a handful |
164 | // more for sake of detecting buffer overruns. |
165 | Sequence<T> in(n, [&](size_t k) -> T { |
166 | T val = convert(n ^ k); |
167 | return val; |
168 | }); |
169 | |
170 | const size_t outN = n + GuardSize; |
171 | Sequence<T> out(outN, [=](size_t) { return trash; }); |
172 | Sequence<T> expected(outN, [=](size_t) { return trash; }); |
173 | invoke_on_all_policies(run_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), |
174 | expected.end(), outN, n, trash); |
175 | invoke_on_all_policies(run_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), |
176 | expected.end(), outN, n, trash); |
177 | invoke_on_all_policies(run_move<T>(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), |
178 | expected.end(), n, n, trash); |
179 | |
180 | // For this test const iterator isn't suitable |
181 | // because const rvalue-reference call copy assignment operator |
182 | } |
183 | } |
184 | |
185 | int |
186 | main() |
187 | { |
188 | test<int32_t>(trash: -666, convert: [](size_t j) { return int32_t(j); }); |
189 | test<Wrapper<float64_t>>(trash: Wrapper<float64_t>(-666.0), convert: [](int32_t j) { return Wrapper<float64_t>(j); }); |
190 | |
191 | #if !defined(_PSTL_ICC_16_17_TEST_64_TIMEOUT) |
192 | test<float64_t>(trash: -666.0, convert: [](size_t j) { return float64_t(j); }); |
193 | test<Number>(trash: Number(42, OddTag()), convert: [](int32_t j) { return Number(j, OddTag()); }); |
194 | #endif |
195 | std::cout << done() << std::endl; |
196 | return 0; |
197 | } |
198 | |