xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp (revision 4d88b17b3f282b1023400837c3249c9f27774eca)
1 // -*- C++ -*-
2 //===-- reverse_copy.pass.cpp ---------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "support/pstl_test_config.h"
11 
12 #include <iterator>
13 #include <execution>
14 #include <algorithm>
15 
16 #include "support/utils.h"
17 
18 using namespace TestUtils;
19 
20 template <typename T>
21 struct wrapper
22 {
23     T t;
24     wrapper() {}
25     explicit wrapper(T t_) : t(t_) {}
26     wrapper&
27     operator=(const T& t_)
28     {
29         t = t_;
30         return *this;
31     }
32     bool
33     operator==(const wrapper& t_) const
34     {
35         return t == t_.t;
36     }
37 };
38 
39 template <typename T1, typename T2>
40 bool
41 eq(const wrapper<T1>& a, const wrapper<T2>& b)
42 {
43     return a.t == b.t;
44 }
45 
46 template <typename T1, typename T2>
47 bool
48 eq(const T1& a, const T2& b)
49 {
50     return a == b;
51 }
52 
53 // we need to save state here, because we need to test with different types of iterators
54 // due to the caller invoke_on_all_policies does forcing modification passed iterator type to cover additional usage cases.
55 template <typename Iterator>
56 struct test_one_policy
57 {
58     Iterator data_b;
59     Iterator data_e;
60     test_one_policy(Iterator b, Iterator e) : data_b(b), data_e(e) {}
61 
62 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
63     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration
64     template <typename Iterator1>
65     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
66     operator()(pstl::execution::unsequenced_policy, Iterator1 actual_b, Iterator1 actual_e)
67     {
68     }
69     template <typename Iterator1>
70     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
71     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 actual_b, Iterator1 actual_e)
72     {
73     }
74 #endif
75 
76     template <typename ExecutionPolicy, typename Iterator1>
77     void
78     operator()(ExecutionPolicy&& exec, Iterator1 actual_b, Iterator1 actual_e)
79     {
80         using namespace std;
81         using T = typename iterator_traits<Iterator1>::value_type;
82         using DifferenceType = typename iterator_traits<Iterator1>::difference_type;
83 
84         fill(actual_b, actual_e, T(-123));
85         Iterator1 actual_return = reverse_copy(exec, data_b, data_e, actual_b);
86 
87         EXPECT_TRUE(actual_return == actual_e, "wrong result of reverse_copy");
88 
89         const auto n = std::distance(data_b, data_e);
90         Sequence<T> res(n);
91         std::copy(std::reverse_iterator<Iterator>(data_e), std::reverse_iterator<Iterator>(data_b), res.begin());
92 
93         EXPECT_EQ_N(res.begin(), actual_b, n, "wrong effect of reverse_copy");
94     }
95 };
96 
97 template <typename T1, typename T2>
98 void
99 test()
100 {
101     typedef typename Sequence<T1>::iterator iterator_type;
102     typedef typename Sequence<T1>::const_bidirectional_iterator cbi_iterator_type;
103 
104     const std::size_t max_len = 100000;
105 
106     Sequence<T2> actual(max_len);
107 
108     Sequence<T1> data(max_len, [](std::size_t i) { return T1(i); });
109 
110     for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
111     {
112         invoke_on_all_policies(test_one_policy<iterator_type>(data.begin(), data.begin() + len), actual.begin(),
113                                actual.begin() + len);
114         invoke_on_all_policies(test_one_policy<cbi_iterator_type>(data.cbibegin(), std::next(data.cbibegin(), len)),
115                                actual.begin(), actual.begin() + len);
116     }
117 }
118 
119 int32_t
120 main()
121 {
122     // clang-3.8 fails to correctly auto vectorize the loop in some cases of different types of container's elements,
123     // for example: int32_t and int8_t. This issue isn't detected for clang-3.9 and newer versions.
124     test<int16_t, int8_t>();
125     test<uint16_t, float32_t>();
126     test<float64_t, int64_t>();
127     test<wrapper<float64_t>, wrapper<float64_t>>();
128 
129     std::cout << done() << std::endl;
130     return 0;
131 }
132