1 // -*- C++ -*-
2 //===-- adjacent_difference.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 <numeric>
15 
16 #include "support/utils.h"
17 
18 using namespace TestUtils;
19 
20 template <typename T>
21 struct wrapper
22 {
23     T t;
24     explicit wrapper(T t_) : t(t_) {}
25     template <typename T2>
26     wrapper(const wrapper<T2>& a)
27     {
28         t = a.t;
29     }
30     template <typename T2>
31     void
32     operator=(const wrapper<T2>& a)
33     {
34         t = a.t;
35     }
36     wrapper<T>
37     operator-(const wrapper<T>& a) const
38     {
39         return wrapper<T>(t - a.t);
40     }
41 };
42 
43 template <typename T>
44 bool
45 compare(const T& a, const T& b)
46 {
47     return a == b;
48 }
49 
50 template <typename T>
51 bool
52 compare(const wrapper<T>& a, const wrapper<T>& b)
53 {
54     return a.t == b.t;
55 }
56 
57 template <typename Iterator1, typename Iterator2, typename T, typename Function>
58 typename std::enable_if<!std::is_floating_point<T>::value, bool>::type
59 compute_and_check(Iterator1 first, Iterator1 last, Iterator2 d_first, T, Function f)
60 {
61     using T2 = typename std::iterator_traits<Iterator2>::value_type;
62 
63     if (first == last)
64         return true;
65 
66     T2 temp(*first);
67     if (!compare(temp, *d_first))
68         return false;
69     Iterator1 second = std::next(first);
70 
71     ++d_first;
72     for (; second != last; ++first, ++second, ++d_first)
73     {
74         T2 temp(f(*second, *first));
75         if (!compare(temp, *d_first))
76             return false;
77     }
78 
79     return true;
80 }
81 
82 // we don't want to check equality here
83 // because we can't be sure it will be strictly equal for floating point types
84 template <typename Iterator1, typename Iterator2, typename T, typename Function>
85 typename std::enable_if<std::is_floating_point<T>::value, bool>::type
86 compute_and_check(Iterator1 first, Iterator1 last, Iterator2 d_first, T, Function)
87 {
88     return true;
89 }
90 
91 struct test_one_policy
92 {
93 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
94     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration
95     template <typename Iterator1, typename Iterator2, typename T, typename Function>
96     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
97     operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
98                Iterator2 actual_e, T trash, Function f)
99     {
100     }
101     template <typename Iterator1, typename Iterator2, typename T, typename Function>
102     typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type
103     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b,
104                Iterator2 actual_e, T trash, Function f)
105     {
106     }
107 #endif
108 
109     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T, typename Function>
110     void
111     operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e,
112                T trash, Function f)
113     {
114         using namespace std;
115         using T2 = typename std::iterator_traits<Iterator1>::value_type;
116 
117         fill(actual_b, actual_e, trash);
118 
119         Iterator2 actual_return = adjacent_difference(exec, data_b, data_e, actual_b);
120         EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), std::minus<T2>()),
121                     "wrong effect of adjacent_difference");
122         EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference");
123 
124         fill(actual_b, actual_e, trash);
125 
126         actual_return = adjacent_difference(exec, data_b, data_e, actual_b, f);
127         EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), f),
128                     "wrong effect of adjacent_difference with functor");
129         EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference with functor");
130     }
131 };
132 
133 template <typename T1, typename T2, typename Pred>
134 void
135 test(Pred pred)
136 {
137     typedef typename Sequence<T2>::iterator iterator_type;
138 
139     const std::size_t max_len = 100000;
140 
141     const T2 value = T2(77);
142     const T1 trash = T1(31);
143 
144     Sequence<T1> actual(max_len, [](std::size_t i) { return T1(i); });
145 
146     Sequence<T2> data(max_len, [&value](std::size_t i) { return i % 3 == 2 ? T2(i * i) : value; });
147 
148     for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
149     {
150         invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(),
151                                actual.begin() + len, trash, pred);
152         invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(),
153                                actual.begin() + len, trash, pred);
154     }
155 }
156 
157 int32_t
158 main()
159 {
160     test<uint8_t, uint32_t>([](uint32_t a, uint32_t b) { return a - b; });
161     test<int32_t, int64_t>([](int64_t a, int64_t b) { return a / (b + 1); });
162     test<int64_t, float32_t>([](float32_t a, float32_t b) { return (a + b) / 2; });
163     test<wrapper<int32_t>, wrapper<int64_t>>(
164         [](const wrapper<int64_t>& a, const wrapper<int64_t>& b) { return a - b; });
165 
166     std::cout << done() << std::endl;
167     return 0;
168 }
169