Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_modifying_operations/remove.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 //===-- remove.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 // Test for remove, remove_if
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 using namespace TestUtils;
29
30 struct run_remove
31 {
32 #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \
33 defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration
34 template <typename InputIterator, typename OutputIterator, typename Size, typename T>
35 void
36 operator()(__pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first,
37 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n,
38 const T& value)
39 {
40 }
41 template <typename InputIterator, typename OutputIterator, typename Size, typename T>
42 void
43 operator()(__pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
44 OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first,
45 OutputIterator expected_last, Size n, const T& value)
46 {
47 }
48 #endif
49
50 template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename T>
51 void
52 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
53 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size,
54 const T& value)
55 {
56 // Cleaning
57 std::copy(first, last, expected_first);
58 std::copy(first, last, out_first);
59
60 // Run remove
61 OutputIterator i = remove(expected_first, expected_last, value);
62 OutputIterator k = remove(exec, out_first, out_last, value);
63 EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k), "wrong return value from remove");
64 EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove effect");
65 }
66 };
67
68 struct run_remove_if
69 {
70 #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \
71 defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration
72 template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
73 void
74 operator()(__pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first,
75 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n,
76 Predicate pred)
77 {
78 }
79 template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
80 void
81 operator()(__pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
82 OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first,
83 OutputIterator expected_last, Size n, Predicate pred)
84 {
85 }
86 #endif
87
88 template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
89 void
90 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
91 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size,
92 Predicate pred)
93 {
94 // Cleaning
95 std::copy(first, last, expected_first);
96 std::copy(first, last, out_first);
97
98 // Run remove_if
99 OutputIterator i = remove_if(expected_first, expected_last, pred);
100 OutputIterator k = remove_if(exec, out_first, out_last, pred);
101 EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k),
102 "wrong return value from remove_if");
103 EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove_if effect");
104 }
105 };
106
107 template <typename T, typename Predicate, typename Convert>
108 void
109 test(T trash, const T& value, Predicate pred, Convert convert)
110 {
111 const std::size_t max_size = 100000;
112 Sequence<T> out(max_size, [trash](size_t) { return trash; });
113 Sequence<T> expected(max_size, [trash](size_t) { return trash; });
114
115 for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
116 {
117 Sequence<T> data(n, [&](size_t k) -> T { return convert(k); });
118
119 invoke_on_all_policies(run_remove(), data.begin(), data.end(), out.begin(), out.begin() + n, expected.begin(),
120 expected.begin() + n, n, value);
121 invoke_on_all_policies(run_remove_if(), data.begin(), data.end(), out.begin(), out.begin() + n,
122 expected.begin(), expected.begin() + n, n, pred);
123 }
124 }
125
126 struct test_non_const
127 {
128 template <typename Policy, typename Iterator>
129 void
130 operator()(Policy&& exec, Iterator iter)
131 {
132 auto is_even = [&](float64_t v) {
133 uint32_t i = (uint32_t)v;
134 return i % 2 == 0;
135 };
136
137 invoke_if(exec, [&]() { remove_if(exec, iter, iter, non_const(is_even)); });
138 }
139 };
140
141 int
142 main()
143 {
144 #if !defined(_PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN)
145 test<int32_t>(666, 42, [](int32_t) { return true; }, [](size_t j) { return j; });
146 #endif
147
148 test<int32_t>(666, 2001, [](const int32_t& val) { return val != 2001; },
149 [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 2001 : -1 - int32_t(j); });
150 test<float64_t>(-666.0, 8.5, [](const float64_t& val) { return val != 8.5; },
151 [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); });
152
153 #if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN)
154 test<Number>(Number(-666, OddTag()), Number(42, OddTag()), IsMultiple(3, OddTag()),
155 [](int32_t j) { return Number(j, OddTag()); });
156 #endif
157
158 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
159
160 test<MemoryChecker>(MemoryChecker{0}, MemoryChecker{1},
161 [](const MemoryChecker& val){ return val.value() == 1; },
162 [](std::size_t idx){ return MemoryChecker{std::int32_t(idx % 3 == 0)}; }
163 );
164 EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from remove,remove_if: number of ctors calls < num of dtors calls");
165 EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from remove,remove_if: number of ctors calls > num of dtors calls");
166
167 std::cout << done() << std::endl;
168 return 0;
169 }