Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_modifying_operations/transform_binary.cc

raw
1 // -*- C++ -*-
2 // { dg-options "-ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-timeout-factor 3 }
5 // { dg-timeout-factor 5 { target debug_mode } }
6 // { dg-require-effective-target tbb_backend }
7
8 //===-- transform_binary.pass.cpp -----------------------------------------===//
9 //
10 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://llvm.org/LICENSE.txt for license information.
12 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "pstl/pstl_test_config.h"
17
18 #ifdef PSTL_STANDALONE_TESTS
19 #include "pstl/execution"
20 #include "pstl/algorithm"
21 #else
22 #include <execution>
23 #include <algorithm>
24 #endif // PSTL_STANDALONE_TESTS
25
26 #include "pstl/test_utils.h"
27
28 using namespace TestUtils;
29
30 template <typename In1, typename In2, typename Out>
31 class TheOperation
32 {
33 Out val;
34
35 public:
36 TheOperation(Out v) : val(v) {}
37 Out
38 operator()(const In1& x, const In2& y) const
39 {
40 return Out(val + x - y);
41 }
42 };
43
44 template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
45 void
46 check_and_reset(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator out_first)
47 {
48 typedef typename std::iterator_traits<OutputIterator>::value_type Out;
49 typename std::iterator_traits<OutputIterator>::difference_type k = 0;
50 for (; first1 != last1; ++first1, ++first2, ++out_first, ++k)
51 {
52 // check
53 Out expected = Out(1.5) + *first1 - *first2;
54 Out actual = *out_first;
55 if (std::is_floating_point<Out>::value)
56 {
57 EXPECT_TRUE((expected > actual ? expected - actual : actual - expected) < 1e7,
58 "wrong value in output sequence");
59 }
60 else
61 {
62 EXPECT_EQ(expected, actual, "wrong value in output sequence");
63 }
64 // reset
65 *out_first = k % 7 != 4 ? 7 * k - 5 : 0;
66 }
67 }
68
69 struct test_one_policy
70 {
71 template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator,
72 typename BinaryOp>
73 void
74 operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2,
75 OutputIterator out_first, OutputIterator, BinaryOp op)
76 {
77 auto result = std::transform(exec, first1, last1, first2, out_first, op);
78 (void)result;
79 check_and_reset(first1, last1, first2, out_first);
80 }
81 };
82
83 template <typename In1, typename In2, typename Out, typename Predicate>
84 void
85 test(Predicate pred)
86 {
87 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
88 {
89 Sequence<In1> in1(n, [](size_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; });
90 Sequence<In2> in2(n, [](size_t k) { return k % 7 != 2 ? 5 * k - 5 : 0; });
91
92 Sequence<Out> out(n, [](size_t) { return -1; });
93
94 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.begin(), in2.end(), out.begin(),
95 out.end(), pred);
96 invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cend(), in2.cbegin(), in2.cend(), out.begin(),
97 out.end(), pred);
98 }
99 }
100
101 template <typename T>
102 struct test_non_const
103 {
104 template <typename Policy, typename InputIterator, typename OutputInterator>
105 void
106 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter)
107 {
108 invoke_if(exec, [&]() {
109 InputIterator input_iter2 = input_iter;
110 transform(exec, input_iter, input_iter, input_iter2, out_iter, non_const(std::plus<T>()));
111 });
112 }
113 };
114
115 int
116 main()
117 {
118 //const operator()
119 test<int32_t, int32_t, int32_t>(TheOperation<int32_t, int32_t, int32_t>(1));
120 test<float32_t, float32_t, float32_t>(TheOperation<float32_t, float32_t, float32_t>(1.5));
121 //non-const operator()
122 test<int32_t, float32_t, float32_t>(non_const(TheOperation<int32_t, float32_t, float32_t>(1.5)));
123 test<int64_t, float64_t, float32_t>(non_const(TheOperation<int64_t, float64_t, float32_t>(1.5)));
124 //lambda
125 test<int8_t, float64_t, int8_t>([](const int8_t& x, const float64_t& y) { return int8_t(int8_t(1.5) + x - y); });
126
127 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
128
129 std::cout << done() << std::endl;
130 return 0;
131 }