xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/remove_copy.pass.cpp (revision 3491119f9ba466ed871a789eb91e3b7577e36272)
1 // -*- C++ -*-
2 //===-- remove_copy.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 #include "support/pstl_test_config.h"
11 
12 #include <execution>
13 #include <algorithm>
14 
15 #include "support/utils.h"
16 
17 using namespace TestUtils;
18 
19 struct run_remove_copy
20 {
21     template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size,
22               typename T>
23     void
24     operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
25                OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n,
26                const T& value, T trash)
27     {
28         // Cleaning
29         std::fill_n(expected_first, n, trash);
30         std::fill_n(out_first, n, trash);
31 
32         // Run copy_if
33         auto i = std::remove_copy(first, last, expected_first, value);
34         (void)i;
35         auto k = std::remove_copy(exec, first, last, out_first, value);
36         EXPECT_EQ_N(expected_first, out_first, n, "wrong remove_copy effect");
37         for (size_t j = 0; j < GuardSize; ++j)
38         {
39             ++k;
40         }
41         EXPECT_TRUE(out_last == k, "wrong return value from remove_copy");
42     }
43 };
44 
45 template <typename T, typename Convert>
46 void
47 test(T trash, const T& value, Convert convert, bool check_weakness = true)
48 {
49     // Try sequences of various lengths.
50     for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
51     {
52         // count is number of output elements, plus a handful
53         // more for sake of detecting buffer overruns.
54         size_t count = GuardSize;
55         Sequence<T> in(n, [&](size_t k) -> T {
56             T x = convert(n ^ k);
57             count += !(x == value) ? 1 : 0;
58             return x;
59         });
60         using namespace std;
61 
62         Sequence<T> out(count, [=](size_t) { return trash; });
63         Sequence<T> expected(count, [=](size_t) { return trash; });
64         if (check_weakness)
65         {
66             auto expected_result = remove_copy(in.cfbegin(), in.cfend(), expected.begin(), value);
67             size_t m = expected_result - expected.begin();
68             EXPECT_TRUE(n / 4 <= m && m <= 3 * (n + 1) / 4, "weak test for remove_copy");
69         }
70         invoke_on_all_policies(run_remove_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(),
71                                expected.end(), count, value, trash);
72         invoke_on_all_policies(run_remove_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(),
73                                expected.end(), count, value, trash);
74     }
75 }
76 
77 int32_t
78 main()
79 {
80 
81     test<float64_t>(-666.0, 8.5, [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); });
82 
83     test<int32_t>(-666, 42, [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 42 : -1 - int32_t(j); });
84 
85     test<Number>(Number(42, OddTag()), Number(2001, OddTag()),
86                  [](int32_t j) { return ((j + 1) % 3 & 2) != 0 ? Number(2001, OddTag()) : Number(j, OddTag()); });
87     std::cout << done() << std::endl;
88     return 0;
89 }
90