xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp (revision 3b62047b8b2209bed57d239f581bdbfc91a10b94)
1 // -*- C++ -*-
2 //===-- reverse.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 #ifdef PSTL_STANDALONE_TESTS
13 #include <iterator>
14 
15 #include "pstl/execution"
16 #include "pstl/algorithm"
17 #else
18 #include <execution>
19 #include <algorithm>
20 #endif // PSTL_STANDALONE_TESTS
21 
22 #include "support/utils.h"
23 
24 using namespace TestUtils;
25 
26 struct test_one_policy
27 {
28 #if __PSTL_ICC_18_VC141_TEST_SIMD_LAMBDA_RELEASE_BROKEN || __PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||     \
29     __PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration
30     template <typename Iterator1, typename Iterator2>
31     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
32     operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
33                Iterator2 actual_e)
34     {
35     }
36     template <typename Iterator1, typename Iterator2>
37     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
38     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
39                Iterator2 actual_e)
40     {
41     }
42 #endif
43 
44     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2>
45     typename std::enable_if<!is_same_iterator_category<Iterator1, std::forward_iterator_tag>::value>::type
46     operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e)
47     {
48         using namespace std;
49 
50         copy(data_b, data_e, actual_b);
51 
52         reverse(exec, actual_b, actual_e);
53 
54         bool check = equal(data_b, data_e, reverse_iterator<Iterator2>(actual_e));
55 
56         EXPECT_TRUE(check, "wrong result of reverse");
57     }
58 
59     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2>
60     typename std::enable_if<is_same_iterator_category<Iterator1, std::forward_iterator_tag>::value>::type
61     operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e)
62     {
63     }
64 };
65 
66 template <typename T>
67 void
68 test()
69 {
70     const std::size_t max_len = 100000;
71 
72     Sequence<T> actual(max_len);
73 
74     Sequence<T> data(max_len, [](std::size_t i) { return T(i); });
75 
76     for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
77     {
78         invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(),
79                                actual.begin() + len);
80     }
81 }
82 
83 template <typename T>
84 struct wrapper
85 {
86     T t;
87     wrapper() {}
88     explicit wrapper(T t_) : t(t_) {}
89     bool
90     operator==(const wrapper<T>& a) const
91     {
92         return t == a.t;
93     }
94 };
95 
96 int32_t
97 main()
98 {
99     test<int32_t>();
100     test<uint16_t>();
101     test<float64_t>();
102 #if !__PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN
103     test<wrapper<float64_t>>();
104 #endif
105 
106     std::cout << done() << std::endl;
107     return 0;
108 }
109