xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp (revision 4d88b17b3f282b1023400837c3249c9f27774eca)
1 // -*- C++ -*-
2 //===-- transform_binary.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 #include <execution>
13 #include <algorithm>
14 
15 #include "support/utils.h"
16 
17 using namespace TestUtils;
18 
19 template <typename In1, typename In2, typename Out>
20 class TheOperation
21 {
22     Out val;
23 
24   public:
25     TheOperation(Out v) : val(v) {}
26     Out
27     operator()(const In1& x, const In2& y) const
28     {
29         return Out(val + x - y);
30     }
31 };
32 
33 template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
34 void
35 check_and_reset(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator out_first)
36 {
37     typedef typename std::iterator_traits<OutputIterator>::value_type Out;
38     typename std::iterator_traits<OutputIterator>::difference_type k = 0;
39     for (; first1 != last1; ++first1, ++first2, ++out_first, ++k)
40     {
41         // check
42         Out expected = Out(1.5) + *first1 - *first2;
43         Out actual = *out_first;
44         if (std::is_floating_point<Out>::value)
45         {
46             EXPECT_TRUE((expected > actual ? expected - actual : actual - expected) < 1e7,
47                         "wrong value in output sequence");
48         }
49         else
50         {
51             EXPECT_EQ(expected, actual, "wrong value in output sequence");
52         }
53         // reset
54         *out_first = k % 7 != 4 ? 7 * k - 5 : 0;
55     }
56 }
57 
58 struct test_one_policy
59 {
60     template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator,
61               typename BinaryOp>
62     void
63     operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
64                OutputIterator out_first, OutputIterator out_last, BinaryOp op)
65     {
66         auto orrr = std::transform(exec, first1, last1, first2, out_first, op);
67         check_and_reset(first1, last1, first2, out_first);
68     }
69 };
70 
71 template <typename In1, typename In2, typename Out, typename Predicate>
72 void
73 test(Predicate pred)
74 {
75     for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
76     {
77         Sequence<In1> in1(n, [](size_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; });
78         Sequence<In2> in2(n, [](size_t k) { return k % 7 != 2 ? 5 * k - 5 : 0; });
79 
80         Sequence<Out> out(n, [](size_t k) { return -1; });
81 
82         invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.begin(), in2.end(), out.begin(),
83                                out.end(), pred);
84         invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cend(), in2.cbegin(), in2.cend(), out.begin(),
85                                out.end(), pred);
86     }
87 }
88 
89 template <typename T>
90 struct test_non_const
91 {
92     template <typename Policy, typename InputIterator, typename OutputInterator>
93     void
94     operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter)
95     {
96         invoke_if(exec, [&]() {
97             InputIterator input_iter2 = input_iter;
98             transform(exec, input_iter, input_iter, input_iter2, out_iter, non_const(std::plus<T>()));
99         });
100     }
101 };
102 
103 int32_t
104 main()
105 {
106     //const operator()
107     test<int32_t, int32_t, int32_t>(TheOperation<int32_t, int32_t, int32_t>(1));
108     test<float32_t, float32_t, float32_t>(TheOperation<float32_t, float32_t, float32_t>(1.5));
109     //non-const operator()
110     test<int32_t, float32_t, float32_t>(non_const(TheOperation<int32_t, float32_t, float32_t>(1.5)));
111     test<int64_t, float64_t, float32_t>(non_const(TheOperation<int64_t, float64_t, float32_t>(1.5)));
112     //lambda
113     test<int8_t, float64_t, int8_t>([](const int8_t& x, const float64_t& y) { return int8_t(int8_t(1.5) + x - y); });
114 
115     test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
116 
117     std::cout << done() << std::endl;
118     return 0;
119 }
120