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