Debian Codegraph @

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

raw
1 // -*- C++ -*-
2 // { dg-options "-ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-timeout-factor 3 }
5 // { dg-require-effective-target tbb_backend }
6
7 //===-- transform_unary.pass.cpp ------------------------------------------===//
8 //
9 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10 // See https://llvm.org/LICENSE.txt for license information.
11 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "pstl/pstl_test_config.h"
16
17 #ifdef PSTL_STANDALONE_TESTS
18 #include "pstl/execution"
19 #include "pstl/algorithm"
20 #else
21 #include <execution>
22 #include <algorithm>
23 #endif // PSTL_STANDALONE_TESTS
24
25 #include "pstl/test_utils.h"
26
27 using namespace TestUtils;
28
29 template <typename InputIterator, typename OutputIterator>
30 void
31 check_and_reset(InputIterator first, InputIterator last, OutputIterator out_first)
32 {
33 typedef typename std::iterator_traits<OutputIterator>::value_type Out;
34 typename std::iterator_traits<OutputIterator>::difference_type k = 0;
35 for (; first != last; ++first, ++out_first, ++k)
36 {
37 // check
38 Out expected = 1 - *first;
39 Out actual = *out_first;
40 EXPECT_EQ(expected, actual, "wrong value in output sequence");
41 // reset
42 *out_first = k % 7 != 4 ? 7 * k - 5 : 0;
43 }
44 }
45
46 struct test_one_policy
47 {
48 template <typename Policy, typename InputIterator, typename OutputIterator, typename UnaryOp>
49 void
50 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
51 OutputIterator out_last, UnaryOp op)
52 {
53 auto orr = std::transform(exec, first, last, out_first, op);
54 EXPECT_TRUE(out_last == orr, "transform returned wrong iterator");
55 check_and_reset(first, last, out_first);
56 }
57 };
58
59 template <typename Tin, typename Tout>
60 void
61 test()
62 {
63 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
64 {
65 Sequence<Tin> in(n, [](int32_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; });
66
67 Sequence<Tout> out(n);
68
69 const auto flip = Complement<Tin, Tout>(1);
70 invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), out.begin(), out.end(), flip);
71 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), out.begin(), out.end(), flip);
72 }
73 }
74
75 template <typename T>
76 struct test_non_const
77 {
78 template <typename Policy, typename InputIterator, typename OutputInterator>
79 void
80 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter)
81 {
82 invoke_if(exec, [&]() { transform(exec, input_iter, input_iter, out_iter, non_const(std::negate<T>())); });
83 }
84 };
85
86 int
87 main()
88 {
89 test<int32_t, int32_t>();
90 test<int32_t, float32_t>();
91 test<uint16_t, float32_t>();
92 test<float32_t, float64_t>();
93 test<float64_t, float64_t>();
94
95 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
96
97 std::cout << done() << std::endl;
98 return 0;
99 }