[AMDGPU] Add True16 register classes.
[llvm-project.git] / pstl / test / std / algorithms / alg.modifying.operations / replace.pass.cpp
blob9c6ca1ea8c0faa7fe4bb84709252a55c903f0dbe
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 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++03, c++11, c++14
12 #include "support/pstl_test_config.h"
14 #include <execution>
15 #include <algorithm>
17 #include "support/utils.h"
19 using namespace TestUtils;
21 // This class is needed to check the self-copying
22 struct copy_int
24 int32_t value;
25 int32_t copied_times = 0;
26 constexpr explicit copy_int(int32_t val = 0) : value(val) {}
27 constexpr copy_int(copy_int const& other) : value(other.value), copied_times(other.copied_times) { }
29 constexpr copy_int&
30 operator=(const copy_int& other)
32 if (&other == this)
33 copied_times++;
34 else
36 value = other.value;
37 copied_times = other.copied_times;
39 return *this;
42 constexpr bool
43 operator==(const copy_int& other) const
45 return (value == other.value);
49 template <typename Iterator>
50 struct test_one_policy
52 std::size_t len;
53 Iterator data_b;
54 Iterator data_e;
55 test_one_policy(Iterator data_, std::size_t len_)
57 len = len_;
58 data_b = data_;
59 data_e = std::next(data_b, len);
61 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T, typename Predicate>
62 void
63 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)
66 using namespace std;
68 copy(data_b, data_e, expected_b);
69 copy(data_b, data_e, actual_b);
71 replace(expected_b, expected_e, old_value, value);
72 replace(exec, actual_b, actual_e, old_value, value);
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");
77 copy(data_b, data_e, expected_b);
78 copy(data_b, data_e, actual_b);
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");
85 template <typename T, typename Iterator1>
86 bool check(Iterator1, Iterator1)
88 return true;
91 template <typename T, typename Iterator1>
92 typename std::enable_if<std::is_same<T, copy_int>::value, bool>::type_t
93 check(Iterator1 b, Iterator1 e)
95 return std::all_of(b, e, [](const copy_int& elem) { return elem.copied_times == 0; });
99 template <typename T1, typename T2, typename Pred>
100 void
101 test(Pred pred)
103 typedef typename Sequence<T2>::iterator iterator_type;
105 const std::size_t max_len = 100000;
107 static constexpr T1 value = T1(0);
108 static constexpr T1 new_value = T1(666);
110 Sequence<T2> expected(max_len);
111 Sequence<T2> actual(max_len);
113 Sequence<T2> data(max_len, [](std::size_t i) {
114 if (i % 3 == 2)
116 return T1(i);
118 else
120 return value;
124 for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len))
126 test_one_policy<iterator_type> temp(data.begin(), len);
128 invoke_on_all_policies(temp, expected.begin(), expected.begin() + len, actual.begin(), actual.begin() + len,
129 pred, new_value, value);
133 template <typename T>
134 struct test_non_const
136 template <typename Policy, typename Iterator>
137 void
138 operator()(Policy&& exec, Iterator iter)
140 auto is_even = [&](float64_t v) {
141 uint32_t i = (uint32_t)v;
142 return i % 2 == 0;
144 invoke_if(exec, [&]() { replace_if(exec, iter, iter, non_const(is_even), T(0)); });
149 main()
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; });
156 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
158 std::cout << done() << std::endl;
159 return 0;