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