| 1 | // -*- C++ -*- |
| 2 | // { dg-options "-ltbb" } |
| 3 | // { dg-do run { target c++17 } } |
| 4 | // { dg-timeout-factor 3 } |
| 5 | // { dg-require-effective-target tbb_backend } |
| 6 | |
| 7 | //===-- replace.pass.cpp --------------------------------------------------===// |
| 8 | // |
| 9 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 | // See https://llvm.org/LICENSE.txt for license information. |
| 11 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "pstl/pstl_test_config.h" |
| 16 | |
| 17 | #ifdef PSTL_STANDALONE_TESTS |
| 18 | #include "pstl/execution" |
| 19 | #include "pstl/algorithm" |
| 20 | #else |
| 21 | #include <execution> |
| 22 | #include <algorithm> |
| 23 | #endif // PSTL_STANDALONE_TESTS |
| 24 | |
| 25 | #include "pstl/test_utils.h" |
| 26 | |
| 27 | using namespace TestUtils; |
| 28 | |
| 29 | // This class is needed to check the self-copying |
| 30 | struct copy_int |
| 31 | { |
| 32 | int32_t value; |
| 33 | int32_t copied_times = 0; |
| 34 | constexpr explicit copy_int(int32_t val = 0) : value(val) {} |
| 35 | constexpr copy_int(copy_int const& other) : value(other.value), copied_times(other.copied_times) { } |
| 36 | |
| 37 | constexpr copy_int& |
| 38 | operator=(const copy_int& other) |
| 39 | { |
| 40 | if (&other == this) |
| 41 | copied_times++; |
| 42 | else |
| 43 | { |
| 44 | value = other.value; |
| 45 | copied_times = other.copied_times; |
| 46 | } |
| 47 | return *this; |
| 48 | } |
| 49 | |
| 50 | constexpr bool |
| 51 | operator==(const copy_int& other) const |
| 52 | { |
| 53 | return (value == other.value); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | template <typename Iterator> |
| 58 | struct test_one_policy |
| 59 | { |
| 60 | std::size_t len; |
| 61 | Iterator data_b; |
| 62 | Iterator data_e; |
| 63 | test_one_policy(Iterator data_, std::size_t len_) |
| 64 | { |
| 65 | len = len_; |
| 66 | data_b = data_; |
| 67 | data_e = std::next(data_b, len); |
| 68 | } |
| 69 | template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T, typename Predicate> |
| 70 | void |
| 71 | operator()(ExecutionPolicy&& exec, Iterator1 expected_b, Iterator1 expected_e, Iterator2 actual_b, |
| 72 | Iterator2 actual_e, Predicate pred, const T& value, const T& old_value) |
| 73 | { |
| 74 | using namespace std; |
| 75 | |
| 76 | copy(data_b, data_e, expected_b); |
| 77 | copy(data_b, data_e, actual_b); |
| 78 | |
| 79 | replace(expected_b, expected_e, old_value, value); |
| 80 | replace(exec, actual_b, actual_e, old_value, value); |
| 81 | |
| 82 | EXPECT_TRUE((check<T, Iterator2>(actual_b, actual_e)), "wrong result of self assignment check"); |
| 83 | EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace"); |
| 84 | |
| 85 | copy(data_b, data_e, expected_b); |
| 86 | copy(data_b, data_e, actual_b); |
| 87 | |
| 88 | replace_if(expected_b, expected_e, pred, value); |
| 89 | replace_if(exec, actual_b, actual_e, pred, value); |
| 90 | EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace_if"); |
| 91 | } |
| 92 | |
| 93 | template <typename T, typename Iterator1> |
| 94 | bool |
| 95 | check(Iterator1 b, Iterator1 e) |
| 96 | { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | template <typename T, typename Iterator1> |
| 101 | typename std::enable_if<std::is_same<T, copy_int>::value, bool>::type_t |
| 102 | check(Iterator1 b, Iterator1 e) |
| 103 | { |
| 104 | return std::all_of(b, e, [](const copy_int& elem) { return elem.copied_times == 0; }); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | template <typename T1, typename T2, typename Pred> |
| 109 | void |
| 110 | test(Pred pred) |
| 111 | { |
| 112 | typedef typename Sequence<T2>::iterator iterator_type; |
| 113 | |
| 114 | const std::size_t max_len = 100000; |
| 115 | |
| 116 | static constexpr T1 value = T1(0); |
| 117 | static constexpr T1 new_value = T1(666); |
| 118 | |
| 119 | Sequence<T2> expected(max_len); |
| 120 | Sequence<T2> actual(max_len); |
| 121 | |
| 122 | Sequence<T2> data(max_len, [](std::size_t i) { |
| 123 | if (i % 3 == 2) |
| 124 | { |
| 125 | return T1(i); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | return value; |
| 130 | } |
| 131 | }); |
| 132 | |
| 133 | for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) |
| 134 | { |
| 135 | test_one_policy<iterator_type> temp(data.begin(), len); |
| 136 | |
| 137 | invoke_on_all_policies(temp, expected.begin(), expected.begin() + len, actual.begin(), actual.begin() + len, |
| 138 | pred, new_value, value); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | template <typename T> |
| 143 | struct test_non_const |
| 144 | { |
| 145 | template <typename Policy, typename Iterator> |
| 146 | void |
| 147 | operator()(Policy&& exec, Iterator iter) |
| 148 | { |
| 149 | auto is_even = [&](float64_t v) { |
| 150 | uint32_t i = (uint32_t)v; |
| 151 | return i % 2 == 0; |
| 152 | }; |
| 153 | invoke_if(exec, [&]() { replace_if(exec, iter, iter, non_const(is_even), T(0)); }); |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | int |
| 158 | main() |
| 159 | { |
| 160 | test<int32_t, float32_t>(__pstl::__internal::__equal_value<int32_t>(666)); |
| 161 | test<uint16_t, uint8_t>([](const uint16_t& elem) { return elem % 3 < 2; }); |
| 162 | test<float64_t, int64_t>([](const float64_t& elem) { return elem * elem - 3.5 * elem > 10; }); |
| 163 | test<copy_int, copy_int>([](const copy_int& val) { return val.value / 5 > 2; }); |
| 164 | |
| 165 | test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); |
| 166 | |
| 167 | std::cout << done() << std::endl; |
| 168 | return 0; |
| 169 | } |