| 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_copy.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 | // Tests for replace_copy and replace_copy_if |
| 16 | |
| 17 | #include "pstl/pstl_test_config.h" |
| 18 | |
| 19 | #ifdef PSTL_STANDALONE_TESTS |
| 20 | #include "pstl/execution" |
| 21 | #include "pstl/algorithm" |
| 22 | #else |
| 23 | #include <execution> |
| 24 | #include <algorithm> |
| 25 | #endif // PSTL_STANDALONE_TESTS |
| 26 | |
| 27 | #include "pstl/test_utils.h" |
| 28 | |
| 29 | using namespace TestUtils; |
| 30 | |
| 31 | struct test_replace_copy |
| 32 | { |
| 33 | template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, |
| 34 | typename Predicate, typename T> |
| 35 | void |
| 36 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, |
| 37 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, Predicate pred, |
| 38 | const T& old_value, const T& new_value, T trash) |
| 39 | { |
| 40 | // Cleaning |
| 41 | std::fill_n(expected_first, n, trash); |
| 42 | std::fill_n(out_first, n, trash); |
| 43 | // Run replace_copy |
| 44 | auto i = std::replace_copy(first, last, expected_first, old_value, new_value); |
| 45 | auto k = std::replace_copy(exec, first, last, out_first, old_value, new_value); |
| 46 | EXPECT_EQ_N(expected_first, out_first, n, "wrong replace_copy effect"); |
| 47 | EXPECT_TRUE(out_last == k, "wrong return value from replace_copy"); |
| 48 | |
| 49 | // Cleaning |
| 50 | std::fill_n(expected_first, n, trash); |
| 51 | std::fill_n(out_first, n, trash); |
| 52 | // Run replace_copy_if |
| 53 | i = replace_copy_if(first, last, expected_first, pred, new_value); |
| 54 | k = replace_copy_if(exec, first, last, out_first, pred, new_value); |
| 55 | EXPECT_EQ_N(expected_first, out_first, n, "wrong replace_copy_if effect"); |
| 56 | EXPECT_TRUE(out_last == k, "wrong return value from replace_copy_if"); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | template <typename T, typename Convert, typename Predicate> |
| 61 | void |
| 62 | test(T trash, const T& old_value, const T& new_value, Predicate pred, Convert convert) |
| 63 | { |
| 64 | // Try sequences of various lengths. |
| 65 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 66 | { |
| 67 | Sequence<T> in(n, [&](size_t k) -> T { return convert(n ^ k); }); |
| 68 | Sequence<T> out(n, [=](size_t) { return trash; }); |
| 69 | Sequence<T> expected(n, [=](size_t) { return trash; }); |
| 70 | |
| 71 | invoke_on_all_policies(test_replace_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), |
| 72 | expected.end(), out.size(), pred, old_value, new_value, trash); |
| 73 | invoke_on_all_policies(test_replace_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), |
| 74 | expected.end(), out.size(), pred, old_value, new_value, trash); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | template <typename T> |
| 79 | struct test_non_const |
| 80 | { |
| 81 | template <typename Policy, typename InputIterator, typename OutputInterator> |
| 82 | void |
| 83 | operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) |
| 84 | { |
| 85 | auto is_even = [&](float64_t v) { |
| 86 | uint32_t i = (uint32_t)v; |
| 87 | return i % 2 == 0; |
| 88 | }; |
| 89 | |
| 90 | invoke_if(exec, [&]() { replace_copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even), T(0)); }); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | int |
| 95 | main() |
| 96 | { |
| 97 | |
| 98 | test<float64_t>(-666.0, 8.5, 0.33, [](const float64_t& x) { return x * x <= 1024; }, |
| 99 | [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); }); |
| 100 | |
| 101 | test<int32_t>(-666, 42, 99, [](const int32_t& x) { return x != 42; }, |
| 102 | [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 42 : -1 - int32_t(j); }); |
| 103 | |
| 104 | #if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN) |
| 105 | test<Number>(Number(42, OddTag()), Number(2001, OddTag()), Number(2017, OddTag()), IsMultiple(3, OddTag()), |
| 106 | [](int32_t j) { return ((j + 1) % 3 & 2) != 0 ? Number(2001, OddTag()) : Number(j, OddTag()); }); |
| 107 | #endif |
| 108 | |
| 109 | test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); |
| 110 | |
| 111 | std::cout << done() << std::endl; |
| 112 | return 0; |
| 113 | } |