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