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;
wrapperwrapper35 wrapper() {}
wrapperwrapper36 explicit wrapper(T t_) : t(t_) {}
37 wrapper&
operator =wrapper38 operator=(const T& t_)
39 {
40 t = t_;
41 return *this;
42 }
43 bool
operator ==wrapper44 operator==(const wrapper& t_) const
45 {
46 return t == t_.t;
47 }
48 };
49
50 template <typename T1, typename T2>
51 bool
eq(const wrapper<T1> & a,const wrapper<T2> & b)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
eq(const T1 & a,const T2 & b)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;
test_one_policytest_one_policy71 test_one_policy(Iterator b, Iterator e) : data_b(b), data_e(e) {}
72
73 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \
74 _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
operator ()test_one_policy77 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
operator ()test_one_policy82 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
operator ()test_one_policy89 operator()(ExecutionPolicy&& exec, Iterator1 actual_b, Iterator1 actual_e)
90 {
91 using namespace std;
92 using T = typename iterator_traits<Iterator1>::value_type;
93 using DifferenceType = typename iterator_traits<Iterator1>::difference_type;
94
95 fill(actual_b, actual_e, T(-123));
96 Iterator1 actual_return = reverse_copy(exec, data_b, data_e, actual_b);
97
98 EXPECT_TRUE(actual_return == actual_e, "wrong result of reverse_copy");
99
100 const auto n = std::distance(data_b, data_e);
101 Sequence<T> res(n);
102 std::copy(std::reverse_iterator<Iterator>(data_e), std::reverse_iterator<Iterator>(data_b), res.begin());
103
104 EXPECT_EQ_N(res.begin(), actual_b, n, "wrong effect of reverse_copy");
105 }
106 };
107
108 template <typename T1, typename T2>
109 void
test()110 test()
111 {
112 typedef typename Sequence<T1>::iterator iterator_type;
113 typedef typename Sequence<T1>::const_bidirectional_iterator cbi_iterator_type;
114
115 const std::size_t max_len = 100000;
116
117 Sequence<T2> actual(max_len);
118
119 Sequence<T1> data(max_len, [](std::size_t i) { return T1(i); });
120
121 for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
122 {
123 invoke_on_all_policies(test_one_policy<iterator_type>(data.begin(), data.begin() + len), actual.begin(),
124 actual.begin() + len);
125 invoke_on_all_policies(test_one_policy<cbi_iterator_type>(data.cbibegin(), std::next(data.cbibegin(), len)),
126 actual.begin(), actual.begin() + len);
127 }
128 }
129
130 int32_t
main()131 main()
132 {
133 // clang-3.8 fails to correctly auto vectorize the loop in some cases of different types of container's elements,
134 // for example: int32_t and int8_t. This issue isn't detected for clang-3.9 and newer versions.
135 test<int16_t, int8_t>();
136 test<uint16_t, float32_t>();
137 test<float64_t, int64_t>();
138 test<wrapper<float64_t>, wrapper<float64_t>>();
139
140 std::cout << done() << std::endl;
141 return 0;
142 }
143