| 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 | //===-- reverse_copy.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 | #include "pstl/pstl_test_config.h" |
| 16 | |
| 17 | #ifdef PSTL_STANDALONE_TESTS |
| 18 | #include <iterator> |
| 19 | |
| 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 | template <typename T> |
| 32 | struct wrapper |
| 33 | { |
| 34 | T t; |
| 35 | wrapper() {} |
| 36 | explicit wrapper(T t_) : t(t_) {} |
| 37 | wrapper& |
| 38 | operator=(const T& t_) |
| 39 | { |
| 40 | t = t_; |
| 41 | return *this; |
| 42 | } |
| 43 | bool |
| 44 | operator==(const wrapper& t_) const |
| 45 | { |
| 46 | return t == t_.t; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | template <typename T1, typename T2> |
| 51 | bool |
| 52 | eq(const wrapper<T1>& a, const wrapper<T2>& b) |
| 53 | { |
| 54 | return a.t == b.t; |
| 55 | } |
| 56 | |
| 57 | template <typename T1, typename T2> |
| 58 | bool |
| 59 | eq(const T1& a, const T2& b) |
| 60 | { |
| 61 | return a == b; |
| 62 | } |
| 63 | |
| 64 | // we need to save state here, because we need to test with different types of iterators |
| 65 | // due to the caller invoke_on_all_policies does forcing modification passed iterator type to cover additional usage cases. |
| 66 | template <typename Iterator> |
| 67 | struct test_one_policy |
| 68 | { |
| 69 | Iterator data_b; |
| 70 | Iterator data_e; |
| 71 | test_one_policy(Iterator b, Iterator e) : data_b(b), data_e(e) {} |
| 72 | |
| 73 | #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ |
| 74 | defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration |
| 75 | template <typename Iterator1> |
| 76 | typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type |
| 77 | operator()(__pstl::execution::unsequenced_policy, Iterator1 actual_b, Iterator1 actual_e) |
| 78 | { |
| 79 | } |
| 80 | template <typename Iterator1> |
| 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 actual_b, Iterator1 actual_e) |
| 83 | { |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | template <typename ExecutionPolicy, typename Iterator1> |
| 88 | void |
| 89 | operator()(ExecutionPolicy&& exec, Iterator1 actual_b, Iterator1 actual_e) |
| 90 | { |
| 91 | using namespace std; |
| 92 | using T = typename iterator_traits<Iterator1>::value_type; |
| 93 | |
| 94 | fill(actual_b, actual_e, T(-123)); |
| 95 | Iterator1 actual_return = reverse_copy(exec, data_b, data_e, actual_b); |
| 96 | |
| 97 | EXPECT_TRUE(actual_return == actual_e, "wrong result of reverse_copy"); |
| 98 | |
| 99 | const auto n = std::distance(data_b, data_e); |
| 100 | Sequence<T> res(n); |
| 101 | std::copy(std::reverse_iterator<Iterator>(data_e), std::reverse_iterator<Iterator>(data_b), res.begin()); |
| 102 | |
| 103 | EXPECT_EQ_N(res.begin(), actual_b, n, "wrong effect of reverse_copy"); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | template <typename T1, typename T2> |
| 108 | void |
| 109 | test() |
| 110 | { |
| 111 | typedef typename Sequence<T1>::iterator iterator_type; |
| 112 | typedef typename Sequence<T1>::const_bidirectional_iterator cbi_iterator_type; |
| 113 | |
| 114 | const std::size_t max_len = 100000; |
| 115 | |
| 116 | Sequence<T2> actual(max_len); |
| 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 | invoke_on_all_policies(test_one_policy<iterator_type>(data.begin(), data.begin() + len), actual.begin(), |
| 123 | actual.begin() + len); |
| 124 | invoke_on_all_policies(test_one_policy<cbi_iterator_type>(data.cbibegin(), std::next(data.cbibegin(), len)), |
| 125 | actual.begin(), actual.begin() + len); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | int |
| 130 | main() |
| 131 | { |
| 132 | test<int16_t, int8_t>(); |
| 133 | test<uint16_t, float32_t>(); |
| 134 | test<float64_t, int64_t>(); |
| 135 | test<wrapper<float64_t>, wrapper<float64_t>>(); |
| 136 | |
| 137 | std::cout << done() << std::endl; |
| 138 | return 0; |
| 139 | } |