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