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