Debian Codegraph @

libstdc++-v3/testsuite/26_numerics/pstl/numeric_ops/adjacent_difference.cc

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