Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_modifying_operations/partition_copy.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 //===-- partition_copy.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 // Tests for stable_partition and partition_copy
16 #include "pstl/pstl_test_config.h"
17
18 #ifdef PSTL_STANDALONE_TESTS
19 #include "pstl/execution"
20 #include "pstl/algorithm"
21 #else
22 #include <execution>
23 #include <algorithm>
24 #endif // PSTL_STANDALONE_TESTS
25
26 #include "pstl/test_utils.h"
27
28 #include <cstdlib>
29 #include <iterator>
30
31 using namespace TestUtils;
32
33 struct test_partition_copy
34 {
35 template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2,
36 typename UnaryOp>
37 void
38 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator true_first, OutputIterator,
39 OutputIterator2 false_first, OutputIterator2, UnaryOp unary_op)
40 {
41
42 auto actual_ret = std::partition_copy(exec, first, last, true_first, false_first, unary_op);
43
44 EXPECT_TRUE(std::distance(true_first, actual_ret.first) == std::count_if(first, last, unary_op),
45 "partition_copy has wrong effect from true sequence");
46 EXPECT_TRUE(std::distance(false_first, actual_ret.second) == std::count_if(first, last, std::not_fn(unary_op)),
47 "partition_copy has wrong effect from false sequence");
48 }
49
50 //dummy specialization by iterator type and policy type, in case of broken configuration
51 #if defined(_PSTL_ICC_1800_TEST_MONOTONIC_RELEASE_64_BROKEN)
52 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename UnaryOp>
53 void
54 operator()(__pstl::execution::unsequenced_policy, std::reverse_iterator<InputIterator> first,
55 std::reverse_iterator<InputIterator> last, std::reverse_iterator<OutputIterator> true_first,
56 std::reverse_iterator<OutputIterator> true_last, std::reverse_iterator<OutputIterator2> false_first,
57 OutputIterator2 false_last, UnaryOp unary_op)
58 {
59 }
60 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename UnaryOp>
61 void
62 operator()(__pstl::execution::parallel_unsequenced_policy, std::reverse_iterator<InputIterator> first,
63 std::reverse_iterator<InputIterator> last, std::reverse_iterator<OutputIterator> true_first,
64 std::reverse_iterator<OutputIterator> true_last, std::reverse_iterator<OutputIterator2> false_first,
65 OutputIterator2 false_last, UnaryOp unary_op)
66 {
67 }
68 #endif
69 };
70
71 template <typename T, typename UnaryPred>
72 void
73 test(UnaryPred pred)
74 {
75
76 const std::size_t max_size = 100000;
77 Sequence<T> in(max_size, [](std::size_t v) -> T { return T(v); });
78 Sequence<T> actual_true(max_size);
79 Sequence<T> actual_false(max_size);
80 for (std::size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : std::size_t(3.1415 * n))
81 {
82
83 // for non-const input iterators
84 invoke_on_all_policies(test_partition_copy(), in.begin(), in.begin() + n, actual_true.begin(),
85 actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred);
86
87 // for const input iterators
88 invoke_on_all_policies(test_partition_copy(), in.cbegin(), in.cbegin() + n, actual_true.begin(),
89 actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred);
90 }
91 }
92
93 struct test_non_const
94 {
95 template <typename Policy, typename InputIterator, typename OutputInterator>
96 void
97 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter)
98 {
99 auto is_even = [&](float64_t v) {
100 uint32_t i = (uint32_t)v;
101 return i % 2 == 0;
102 };
103
104 partition_copy(exec, input_iter, input_iter, out_iter, out_iter, non_const(is_even));
105 }
106 };
107
108 int
109 main()
110 {
111 test<int32_t>([](const int32_t value) { return value % 2; });
112
113 #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN)
114 test<int32_t>([](const int32_t) { return true; });
115 #endif
116
117 test<float64_t>([](const float64_t value) { return value > 2 << 6; });
118 test<Wrapper<float64_t>>([](const Wrapper<float64_t>& value) -> bool { return value.get_my_field() != nullptr; });
119
120 test_algo_basic_double<int32_t>(run_for_rnd_bi<test_non_const>());
121
122 std::cout << done() << std::endl;
123 return 0;
124 }