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