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