1 // -*- C++ -*- 2 //===-- transform_unary.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 InputIterator, typename OutputIterator> 20 void 21 check_and_reset(InputIterator first, InputIterator last, OutputIterator out_first) 22 { 23 typedef typename std::iterator_traits<OutputIterator>::value_type Out; 24 typename std::iterator_traits<OutputIterator>::difference_type k = 0; 25 for (; first != last; ++first, ++out_first, ++k) 26 { 27 // check 28 Out expected = 1 - *first; 29 Out actual = *out_first; 30 EXPECT_EQ(expected, actual, "wrong value in output sequence"); 31 // reset 32 *out_first = k % 7 != 4 ? 7 * k - 5 : 0; 33 } 34 } 35 36 struct test_one_policy 37 { 38 template <typename Policy, typename InputIterator, typename OutputIterator, typename UnaryOp> 39 void 40 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, 41 OutputIterator out_last, UnaryOp op) 42 { 43 auto orr = std::transform(exec, first, last, out_first, op); 44 EXPECT_TRUE(out_last == orr, "transform returned wrong iterator"); 45 check_and_reset(first, last, out_first); 46 } 47 }; 48 49 template <typename Tin, typename Tout> 50 void 51 test() 52 { 53 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 54 { 55 Sequence<Tin> in(n, [](int32_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; }); 56 57 Sequence<Tout> out(n); 58 59 const auto flip = Complement<Tin, Tout>(1); 60 invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), out.begin(), out.end(), flip); 61 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), out.begin(), out.end(), flip); 62 } 63 } 64 65 template <typename T> 66 struct test_non_const 67 { 68 template <typename Policy, typename InputIterator, typename OutputInterator> 69 void 70 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) 71 { 72 invoke_if(exec, [&]() { transform(exec, input_iter, input_iter, out_iter, non_const(std::negate<T>())); }); 73 } 74 }; 75 76 int32_t 77 main() 78 { 79 test<int32_t, int32_t>(); 80 test<int32_t, float32_t>(); 81 test<uint16_t, float32_t>(); 82 test<float32_t, float64_t>(); 83 test<float64_t, float64_t>(); 84 85 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); 86 87 std::cout << done() << std::endl; 88 return 0; 89 } 90