Debian Codegraph @

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