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