Synchronize libstdc++ PSTL with upstream LLVM PSTL
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / pstl / alg_modifying_operations / remove.cc
1 // -*- C++ -*-
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-require-effective-target tbb-backend }
5
6 //===-- remove.pass.cpp ---------------------------------------------------===//
7 //
8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9 // See https://llvm.org/LICENSE.txt for license information.
10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //
12 //===----------------------------------------------------------------------===//
13
14 // Test for remove, remove_if
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 struct run_remove
30 {
31 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \
32 _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
33 template <typename InputIterator, typename OutputIterator, typename Size, typename T>
34 void
35 operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first,
36 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n,
37 const T& value)
38 {
39 }
40 template <typename InputIterator, typename OutputIterator, typename Size, typename T>
41 void
42 operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
43 OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first,
44 OutputIterator expected_last, Size n, const T& value)
45 {
46 }
47 #endif
48
49 template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename T>
50 void
51 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
52 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size,
53 const T& value)
54 {
55 // Cleaning
56 std::copy(first, last, expected_first);
57 std::copy(first, last, out_first);
58
59 // Run remove
60 OutputIterator i = remove(expected_first, expected_last, value);
61 OutputIterator k = remove(exec, out_first, out_last, value);
62 EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k), "wrong return value from remove");
63 EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove effect");
64 }
65 };
66
67 struct run_remove_if
68 {
69 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \
70 _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
71 template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
72 void
73 operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first,
74 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n,
75 Predicate pred)
76 {
77 }
78 template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
79 void
80 operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
81 OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first,
82 OutputIterator expected_last, Size n, Predicate pred)
83 {
84 }
85 #endif
86
87 template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
88 void
89 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
90 OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size,
91 Predicate pred)
92 {
93 // Cleaning
94 std::copy(first, last, expected_first);
95 std::copy(first, last, out_first);
96
97 // Run remove_if
98 OutputIterator i = remove_if(expected_first, expected_last, pred);
99 OutputIterator k = remove_if(exec, out_first, out_last, pred);
100 EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k),
101 "wrong return value from remove_if");
102 EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove_if effect");
103 }
104 };
105
106 template <typename T, typename Predicate, typename Convert>
107 void
108 test(T trash, const T& value, Predicate pred, Convert convert)
109 {
110 const std::size_t max_size = 100000;
111 Sequence<T> out(max_size, [trash](size_t) { return trash; });
112 Sequence<T> expected(max_size, [trash](size_t) { return trash; });
113
114 for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
115 {
116 Sequence<T> data(n, [&](size_t k) -> T { return convert(k); });
117
118 invoke_on_all_policies(run_remove(), data.begin(), data.end(), out.begin(), out.begin() + n, expected.begin(),
119 expected.begin() + n, n, value);
120 invoke_on_all_policies(run_remove_if(), data.begin(), data.end(), out.begin(), out.begin() + n,
121 expected.begin(), expected.begin() + n, n, pred);
122 }
123 }
124
125 struct test_non_const
126 {
127 template <typename Policy, typename Iterator>
128 void
129 operator()(Policy&& exec, Iterator iter)
130 {
131 auto is_even = [&](float64_t v) {
132 uint32_t i = (uint32_t)v;
133 return i % 2 == 0;
134 };
135
136 invoke_if(exec, [&]() { remove_if(exec, iter, iter, non_const(is_even)); });
137 }
138 };
139
140 int32_t
141 main()
142 {
143 #if !_PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN
144 test<int32_t>(666, 42, [](int32_t val) { return true; }, [](size_t j) { return j; });
145 #endif
146
147 test<int32_t>(666, 2001, [](const int32_t& val) { return val != 2001; },
148 [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 2001 : -1 - int32_t(j); });
149 test<float64_t>(-666.0, 8.5, [](const float64_t& val) { return val != 8.5; },
150 [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); });
151
152 #if !_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN
153 test<Number>(Number(-666, OddTag()), Number(42, OddTag()), IsMultiple(3, OddTag()),
154 [](int32_t j) { return Number(j, OddTag()); });
155 #endif
156
157 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
158
159 std::cout << done() << std::endl;
160 return 0;
161 }