xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/remove.pass.cpp (revision 3491119f9ba466ed871a789eb91e3b7577e36272)
1 // -*- C++ -*-
2 //===-- remove.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 // Test for remove, remove_if
11 #include "support/pstl_test_config.h"
12 
13 #include <execution>
14 #include <algorithm>
15 
16 #include "support/utils.h"
17 
18 using namespace TestUtils;
19 
20 struct run_remove
21 {
22 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
23     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
24     template <typename InputIterator, typename OutputIterator, typename Size, typename T>
25     void
26     operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first,
27                OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n,
28                const T& value)
29     {
30     }
31     template <typename InputIterator, typename OutputIterator, typename Size, typename T>
32     void
33     operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
34                OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first,
35                OutputIterator expected_last, Size n, const T& value)
36     {
37     }
38 #endif
39 
40     template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename T>
41     void
42     operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
43                OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size,
44                const T& value)
45     {
46         // Cleaning
47         std::copy(first, last, expected_first);
48         std::copy(first, last, out_first);
49 
50         // Run remove
51         OutputIterator i = remove(expected_first, expected_last, value);
52         OutputIterator k = remove(exec, out_first, out_last, value);
53         EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k), "wrong return value from remove");
54         EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove effect");
55     }
56 };
57 
58 struct run_remove_if
59 {
60 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
61     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
62     template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
63     void
64     operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first,
65                OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n,
66                Predicate pred)
67     {
68     }
69     template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
70     void
71     operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
72                OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first,
73                OutputIterator expected_last, Size n, Predicate pred)
74     {
75     }
76 #endif
77 
78     template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename Predicate>
79     void
80     operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
81                OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size,
82                Predicate pred)
83     {
84         // Cleaning
85         std::copy(first, last, expected_first);
86         std::copy(first, last, out_first);
87 
88         // Run remove_if
89         OutputIterator i = remove_if(expected_first, expected_last, pred);
90         OutputIterator k = remove_if(exec, out_first, out_last, pred);
91         EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k),
92                     "wrong return value from remove_if");
93         EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove_if effect");
94     }
95 };
96 
97 template <typename T, typename Predicate, typename Convert>
98 void
99 test(T trash, const T& value, Predicate pred, Convert convert)
100 {
101     const std::size_t max_size = 100000;
102     Sequence<T> out(max_size, [trash](size_t) { return trash; });
103     Sequence<T> expected(max_size, [trash](size_t) { return trash; });
104 
105     for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
106     {
107         Sequence<T> data(n, [&](size_t k) -> T { return convert(k); });
108 
109         invoke_on_all_policies(run_remove(), data.begin(), data.end(), out.begin(), out.begin() + n, expected.begin(),
110                                expected.begin() + n, n, value);
111         invoke_on_all_policies(run_remove_if(), data.begin(), data.end(), out.begin(), out.begin() + n,
112                                expected.begin(), expected.begin() + n, n, pred);
113     }
114 }
115 
116 struct test_non_const
117 {
118     template <typename Policy, typename Iterator>
119     void
120     operator()(Policy&& exec, Iterator iter)
121     {
122         auto is_even = [&](float64_t v) {
123             uint32_t i = (uint32_t)v;
124             return i % 2 == 0;
125         };
126 
127         invoke_if(exec, [&]() { remove_if(exec, iter, iter, non_const(is_even)); });
128     }
129 };
130 
131 int32_t
132 main()
133 {
134 #if !_PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN
135     test<int32_t>(666, 42, [](int32_t) { return true; }, [](size_t j) { return j; });
136 #endif
137 
138     test<int32_t>(666, 2001, [](const int32_t& val) { return val != 2001; },
139                   [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 2001 : -1 - int32_t(j); });
140     test<float64_t>(-666.0, 8.5, [](const float64_t& val) { return val != 8.5; },
141                     [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); });
142 
143 #if !_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN
144     test<Number>(Number(-666, OddTag()), Number(42, OddTag()), IsMultiple(3, OddTag()),
145                  [](int32_t j) { return Number(j, OddTag()); });
146 #endif
147 
148     test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
149 
150     std::cout << done() << std::endl;
151     return 0;
152 }
153