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