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 // UNSUPPORTED: c++03, c++11, c++14
11 
12 #include "support/pstl_test_config.h"
13 
14 #include <iterator>
15 #include <execution>
16 #include <algorithm>
17 
18 #include "support/utils.h"
19 
20 using namespace TestUtils;
21 
22 struct test_one_policy
23 {
24 #if _PSTL_ICC_18_VC141_TEST_SIMD_LAMBDA_RELEASE_BROKEN || _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||       \
25     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration
26     template <typename Iterator1, typename Iterator2>
27     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
operator ()test_one_policy28     operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
29                Iterator2 actual_e)
30     {
31     }
32     template <typename Iterator1, typename Iterator2>
33     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
operator ()test_one_policy34     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
35                Iterator2 actual_e)
36     {
37     }
38 #endif
39 
40     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2>
41     typename std::enable_if<!is_same_iterator_category<Iterator1, std::forward_iterator_tag>::value>::type
operator ()test_one_policy42     operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e)
43     {
44         using namespace std;
45 
46         copy(data_b, data_e, actual_b);
47 
48         reverse(exec, actual_b, actual_e);
49 
50         bool check = equal(data_b, data_e, reverse_iterator<Iterator2>(actual_e));
51 
52         EXPECT_TRUE(check, "wrong result of reverse");
53     }
54 
55     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2>
56     typename std::enable_if<is_same_iterator_category<Iterator1, std::forward_iterator_tag>::value>::type
operator ()test_one_policy57     operator()(ExecutionPolicy&&, Iterator1, Iterator1, Iterator2, Iterator2)
58     {
59     }
60 };
61 
62 template <typename T>
63 void
test()64 test()
65 {
66     const std::size_t max_len = 100000;
67 
68     Sequence<T> actual(max_len);
69 
70     Sequence<T> data(max_len, [](std::size_t i) { return T(i); });
71 
72     for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
73     {
74         invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(),
75                                actual.begin() + len);
76     }
77 }
78 
79 template <typename T>
80 struct wrapper
81 {
82     T t;
wrapperwrapper83     wrapper() {}
wrapperwrapper84     explicit wrapper(T t_) : t(t_) {}
85     bool
operator ==wrapper86     operator==(const wrapper<T>& a) const
87     {
88         return t == a.t;
89     }
90 };
91 
92 int
main()93 main()
94 {
95     test<int32_t>();
96     test<uint16_t>();
97     test<float64_t>();
98 #if !_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN
99     test<wrapper<float64_t>>();
100 #endif
101 
102     std::cout << done() << std::endl;
103     return 0;
104 }
105