1// -*- C++ -*-
2//===-- copy_if.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// UNSUPPORTED: c++03, c++11, c++14
11
12// Tests for copy_if and remove_copy_if
13#include "support/pstl_test_config.h"
14
15#include <execution>
16#include <algorithm>
17
18#include "support/utils.h"
19
20using namespace TestUtils;
21
22struct run_copy_if
23{
24#if defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) // dummy specializations to skip testing in case of broken configuration
25 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size,
26 typename Predicate, typename T>
27 void
28 operator()(pstl::execution::parallel_policy, InputIterator first, InputIterator last, OutputIterator out_first,
29 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n,
30 Predicate pred, T trash)
31 {
32 }
33 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size,
34 typename Predicate, typename T>
35 void
36 operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
37 OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first,
38 OutputIterator2 expected_last, Size n, Predicate pred, T trash)
39 {
40 }
41#endif
42
43 template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size,
44 typename Predicate, typename T>
45 void
46 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
47 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, Predicate pred,
48 T trash)
49 {
50 // Cleaning
51 std::fill_n(expected_first, n, trash);
52 std::fill_n(out_first, n, trash);
53
54 // Run copy_if
55 auto i = copy_if(first, last, expected_first, pred);
56 auto k = copy_if(exec, first, last, out_first, pred);
57 EXPECT_EQ_N(expected_first, out_first, n, "wrong copy_if effect");
58 for (size_t j = 0; j < GuardSize; ++j)
59 {
60 ++k;
61 }
62 EXPECT_TRUE(out_last == k, "wrong return value from copy_if");
63
64 // Cleaning
65 std::fill_n(expected_first, n, trash);
66 std::fill_n(out_first, n, trash);
67 // Run remove_copy_if
68 i = remove_copy_if(first, last, expected_first, [=](const T& x) { return !pred(x); });
69 k = remove_copy_if(exec, first, last, out_first, [=](const T& x) { return !pred(x); });
70 EXPECT_EQ_N(expected_first, out_first, n, "wrong remove_copy_if effect");
71 for (size_t j = 0; j < GuardSize; ++j)
72 {
73 ++k;
74 }
75 EXPECT_TRUE(out_last == k, "wrong return value from remove_copy_if");
76 }
77};
78
79template <typename T, typename Predicate, typename Convert>
80void
81test(T trash, Predicate pred, Convert convert, bool check_weakness = true)
82{
83 // Try sequences of various lengths.
84 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
85 {
86 // count is number of output elements, plus a handful
87 // more for sake of detecting buffer overruns.
88 size_t count = GuardSize;
89 Sequence<T> in(n, [&](size_t k) -> T {
90 T val = convert(n ^ k);
91 count += pred(val) ? 1 : 0;
92 return val;
93 });
94
95 Sequence<T> out(count, [=](size_t) { return trash; });
96 Sequence<T> expected(count, [=](size_t) { return trash; });
97 if (check_weakness)
98 {
99 auto expected_result = copy_if(in.cfbegin(), in.cfend(), expected.begin(), pred);
100 size_t m = expected_result - expected.begin();
101 EXPECT_TRUE(n / 4 <= m && m <= 3 * (n + 1) / 4, "weak test for copy_if");
102 }
103 invoke_on_all_policies(run_copy_if(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(),
104 expected.end(), count, pred, trash);
105 invoke_on_all_policies(run_copy_if(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(),
106 expected.end(), count, pred, trash);
107 }
108}
109
110struct test_non_const
111{
112 template <typename Policy, typename InputIterator, typename OutputInterator>
113 void
114 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter)
115 {
116 auto is_even = [&](float64_t v) {
117 uint32_t i = (uint32_t)v;
118 return i % 2 == 0;
119 };
120 copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even));
121
122 invoke_if(exec, [&]() { remove_copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even)); });
123 }
124};
125
126int
127main()
128{
129 test<float64_t>(trash: -666.0, pred: [](const float64_t& x) { return x * x <= 1024; },
130 convert: [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? float64_t(j % 32) : float64_t(j % 33 + 34); });
131
132 test<int32_t>(trash: -666, pred: [](const int32_t& x) { return x != 42; },
133 convert: [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? int32_t(j + 1) : 42; });
134
135#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN)
136 test<Number>(trash: Number(42, OddTag()), pred: IsMultiple(3, OddTag()), convert: [](int32_t j) { return Number(j, OddTag()); });
137#endif
138
139#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN)
140 test<int32_t>(trash: -666, pred: [](const int32_t&) { return true; }, convert: [](size_t j) { return j; }, check_weakness: false);
141#endif
142
143 test_algo_basic_double<int32_t>(f: run_for_rnd_fw<test_non_const>());
144
145 std::cout << done() << std::endl;
146 return 0;
147}
148

source code of pstl/test/std/algorithms/alg.modifying.operations/alg.copy/copy_if.pass.cpp