1 // -*- C++ -*-
2 //===-- replace.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 #include "support/pstl_test_config.h"
13
14 #include <execution>
15 #include <algorithm>
16
17 #include "support/utils.h"
18
19 using namespace TestUtils;
20
21 // This class is needed to check the self-copying
22 struct copy_int
23 {
24 int32_t value;
25 int32_t copied_times = 0;
copy_intcopy_int26 constexpr explicit copy_int(int32_t val = 0) : value(val) {}
copy_intcopy_int27 constexpr copy_int(copy_int const& other) : value(other.value), copied_times(other.copied_times) { }
28
29 constexpr copy_int&
operator =copy_int30 operator=(const copy_int& other)
31 {
32 if (&other == this)
33 copied_times++;
34 else
35 {
36 value = other.value;
37 copied_times = other.copied_times;
38 }
39 return *this;
40 }
41
42 constexpr bool
operator ==copy_int43 operator==(const copy_int& other) const
44 {
45 return (value == other.value);
46 }
47 };
48
49 template <typename Iterator>
50 struct test_one_policy
51 {
52 std::size_t len;
53 Iterator data_b;
54 Iterator data_e;
test_one_policytest_one_policy55 test_one_policy(Iterator data_, std::size_t len_)
56 {
57 len = len_;
58 data_b = data_;
59 data_e = std::next(data_b, len);
60 }
61 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T, typename Predicate>
62 void
operator ()test_one_policy63 operator()(ExecutionPolicy&& exec, Iterator1 expected_b, Iterator1 expected_e, Iterator2 actual_b,
64 Iterator2 actual_e, Predicate pred, const T& value, const T& old_value)
65 {
66 using namespace std;
67
68 copy(data_b, data_e, expected_b);
69 copy(data_b, data_e, actual_b);
70
71 replace(expected_b, expected_e, old_value, value);
72 replace(exec, actual_b, actual_e, old_value, value);
73
74 EXPECT_TRUE((check<T, Iterator2>(actual_b, actual_e)), "wrong result of self assignment check");
75 EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace");
76
77 copy(data_b, data_e, expected_b);
78 copy(data_b, data_e, actual_b);
79
80 replace_if(expected_b, expected_e, pred, value);
81 replace_if(exec, actual_b, actual_e, pred, value);
82 EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace_if");
83 }
84
85 template <typename T, typename Iterator1>
checktest_one_policy86 bool check(Iterator1, Iterator1)
87 {
88 return true;
89 }
90
91 template <typename T, typename Iterator1>
92 typename std::enable_if<std::is_same<T, copy_int>::value, bool>::type_t
checktest_one_policy93 check(Iterator1 b, Iterator1 e)
94 {
95 return std::all_of(b, e, [](const copy_int& elem) { return elem.copied_times == 0; });
96 }
97 };
98
99 template <typename T1, typename T2, typename Pred>
100 void
test(Pred pred)101 test(Pred pred)
102 {
103 typedef typename Sequence<T2>::iterator iterator_type;
104
105 const std::size_t max_len = 100000;
106
107 static constexpr T1 value = T1(0);
108 static constexpr T1 new_value = T1(666);
109
110 Sequence<T2> expected(max_len);
111 Sequence<T2> actual(max_len);
112
113 Sequence<T2> data(max_len, [](std::size_t i) {
114 if (i % 3 == 2)
115 {
116 return T1(i);
117 }
118 else
119 {
120 return value;
121 }
122 });
123
124 for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
125 {
126 test_one_policy<iterator_type> temp(data.begin(), len);
127
128 invoke_on_all_policies(temp, expected.begin(), expected.begin() + len, actual.begin(), actual.begin() + len,
129 pred, new_value, value);
130 }
131 }
132
133 template <typename T>
134 struct test_non_const
135 {
136 template <typename Policy, typename Iterator>
137 void
operator ()test_non_const138 operator()(Policy&& exec, Iterator iter)
139 {
140 auto is_even = [&](float64_t v) {
141 uint32_t i = (uint32_t)v;
142 return i % 2 == 0;
143 };
144 invoke_if(exec, [&]() { replace_if(exec, iter, iter, non_const(is_even), T(0)); });
145 }
146 };
147
148 int
main()149 main()
150 {
151 test<int32_t, float32_t>(__pstl::__internal::__equal_value<int32_t>(666));
152 test<uint16_t, uint8_t>([](const uint16_t& elem) { return elem % 3 < 2; });
153 test<float64_t, int64_t>([](const float64_t& elem) { return elem * elem - 3.5 * elem > 10; });
154 test<copy_int, copy_int>([](const copy_int& val) { return val.value / 5 > 2; });
155
156 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
157
158 std::cout << done() << std::endl;
159 return 0;
160 }
161