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++98, 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 86 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 93 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 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 138 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 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