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