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