xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp (revision 3491119f9ba466ed871a789eb91e3b7577e36272)
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 
83         fill(actual_b, actual_e, T(-123));
84         Iterator1 actual_return = reverse_copy(exec, data_b, data_e, actual_b);
85 
86         EXPECT_TRUE(actual_return == actual_e, "wrong result of reverse_copy");
87 
88         const auto n = std::distance(data_b, data_e);
89         Sequence<T> res(n);
90         std::copy(std::reverse_iterator<Iterator>(data_e), std::reverse_iterator<Iterator>(data_b), res.begin());
91 
92         EXPECT_EQ_N(res.begin(), actual_b, n, "wrong effect of reverse_copy");
93     }
94 };
95 
96 template <typename T1, typename T2>
97 void
98 test()
99 {
100     typedef typename Sequence<T1>::iterator iterator_type;
101     typedef typename Sequence<T1>::const_bidirectional_iterator cbi_iterator_type;
102 
103     const std::size_t max_len = 100000;
104 
105     Sequence<T2> actual(max_len);
106 
107     Sequence<T1> data(max_len, [](std::size_t i) { return T1(i); });
108 
109     for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
110     {
111         invoke_on_all_policies(test_one_policy<iterator_type>(data.begin(), data.begin() + len), actual.begin(),
112                                actual.begin() + len);
113         invoke_on_all_policies(test_one_policy<cbi_iterator_type>(data.cbibegin(), std::next(data.cbibegin(), len)),
114                                actual.begin(), actual.begin() + len);
115     }
116 }
117 
118 int32_t
119 main()
120 {
121     // clang-3.8 fails to correctly auto vectorize the loop in some cases of different types of container's elements,
122     // for example: int32_t and int8_t. This issue isn't detected for clang-3.9 and newer versions.
123     test<int16_t, int8_t>();
124     test<uint16_t, float32_t>();
125     test<float64_t, int64_t>();
126     test<wrapper<float64_t>, wrapper<float64_t>>();
127 
128     std::cout << done() << std::endl;
129     return 0;
130 }
131