2 //===-- copy_move.pass.cpp ------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++03, c++11, c++14
12 // Tests for copy, move and copy_n
13 #include "support/pstl_test_config.h"
18 #include "support/utils.h"
20 using namespace TestUtils
;
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
>
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
,
35 template <typename InputIterator
, typename OutputIterator
, typename OutputIterator2
, typename Size
, typename T
>
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
)
44 template <typename Policy
, typename InputIterator
, typename OutputIterator
, typename OutputIterator2
, typename Size
,
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
)
51 std::fill_n(expected_first
, size
, trash
);
52 std::fill_n(out_first
, size
, trash
);
55 copy(first
, last
, expected_first
);
56 auto k
= copy(exec
, first
, last
, out_first
);
57 for (size_t j
= 0; j
< GuardSize
; ++j
)
59 EXPECT_EQ_N(expected_first
, out_first
, size
, "wrong effect from copy");
60 EXPECT_TRUE(out_last
== k
, "wrong return value from copy");
63 std::fill_n(out_first
, size
, trash
);
65 k
= copy_n(exec
, first
, n
, out_first
);
66 for (size_t j
= 0; j
< GuardSize
; ++j
)
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");
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
>
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
,
87 template <typename InputIterator
, typename OutputIterator
, typename OutputIterator2
, typename Size
>
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
)
96 template <typename Policy
, typename InputIterator
, typename OutputIterator
, typename OutputIterator2
, typename Size
>
98 operator()(Policy
&& exec
, InputIterator first
, InputIterator last
, OutputIterator out_first
,
99 OutputIterator out_last
, OutputIterator2 expected_first
, OutputIterator2
, Size size
, Size
, T trash
)
102 std::fill_n(expected_first
, size
, trash
);
103 std::fill_n(out_first
, size
, trash
);
106 move(first
, last
, expected_first
);
107 auto k
= move(exec
, first
, last
, out_first
);
108 for (size_t j
= 0; j
< GuardSize
; ++j
)
110 EXPECT_EQ_N(expected_first
, out_first
, size
, "wrong effect from move");
111 EXPECT_TRUE(out_last
== k
, "wrong return value from move");
115 template <typename T
>
116 struct run_move
<Wrapper
<T
>>
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
>
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
)
129 template <typename InputIterator
, typename OutputIterator
, typename OutputIterator2
, typename Size
>
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
)
138 template <typename Policy
, typename InputIterator
, typename OutputIterator
, typename OutputIterator2
, typename Size
>
140 operator()(Policy
&& exec
, InputIterator first
, InputIterator last
, OutputIterator out_first
,
141 OutputIterator out_last
, OutputIterator2
, OutputIterator2
, Size size
, Size
, Wrapper
<T
> trash
)
144 std::fill_n(out_first
, size
, trash
);
145 Wrapper
<T
>::SetMoveCount(0);
148 auto k
= move(exec
, first
, last
, out_first
);
149 for (size_t j
= 0; j
< GuardSize
; ++j
)
151 EXPECT_TRUE(Wrapper
<T
>::MoveCount() == size
, "wrong effect from move");
152 EXPECT_TRUE(out_last
== k
, "wrong return value from move");
156 template <typename T
, typename Convert
>
158 test(T trash
, Convert convert
)
160 // Try sequences of various lengths.
161 for (size_t n
= 0; n
<= 100000; n
= n
<= 16 ? n
+ 1 : size_t(3.1415 * n
))
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
);
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
);
180 // For this test const iterator isn't suitable
181 // because const rvalue-reference call copy assignment operator
188 test
<int32_t>(-666, [](size_t j
) { return int32_t(j
); });
189 test
<Wrapper
<float64_t
>>(Wrapper
<float64_t
>(-666.0), [](int32_t j
) { return Wrapper
<float64_t
>(j
); });
191 #if !defined(_PSTL_ICC_16_17_TEST_64_TIMEOUT)
192 test
<float64_t
>(-666.0, [](size_t j
) { return float64_t(j
); });
193 test
<Number
>(Number(42, OddTag()), [](int32_t j
) { return Number(j
, OddTag()); });
195 std::cout
<< done() << std::endl
;