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