1 // -*- C++ -*- 2 //===-- remove_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 #include "support/pstl_test_config.h" 11 12 #include <execution> 13 #include <algorithm> 14 15 #include "support/utils.h" 16 17 using namespace TestUtils; 18 19 struct run_remove_copy 20 { 21 template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, 22 typename T> 23 void 24 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, 25 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n, 26 const T& value, T trash) 27 { 28 // Cleaning 29 std::fill_n(expected_first, n, trash); 30 std::fill_n(out_first, n, trash); 31 32 // Run copy_if 33 auto i = remove_copy(first, last, expected_first, value); 34 auto k = remove_copy(exec, first, last, out_first, value); 35 EXPECT_EQ_N(expected_first, out_first, n, "wrong remove_copy effect"); 36 for (size_t j = 0; j < GuardSize; ++j) 37 { 38 ++k; 39 } 40 EXPECT_TRUE(out_last == k, "wrong return value from remove_copy"); 41 } 42 }; 43 44 template <typename T, typename Convert> 45 void 46 test(T trash, const T& value, Convert convert, bool check_weakness = true) 47 { 48 // Try sequences of various lengths. 49 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 50 { 51 // count is number of output elements, plus a handful 52 // more for sake of detecting buffer overruns. 53 size_t count = GuardSize; 54 Sequence<T> in(n, [&](size_t k) -> T { 55 T x = convert(n ^ k); 56 count += !(x == value) ? 1 : 0; 57 return x; 58 }); 59 using namespace std; 60 61 Sequence<T> out(count, [=](size_t) { return trash; }); 62 Sequence<T> expected(count, [=](size_t) { return trash; }); 63 if (check_weakness) 64 { 65 auto expected_result = remove_copy(in.cfbegin(), in.cfend(), expected.begin(), value); 66 size_t m = expected_result - expected.begin(); 67 EXPECT_TRUE(n / 4 <= m && m <= 3 * (n + 1) / 4, "weak test for remove_copy"); 68 } 69 invoke_on_all_policies(run_remove_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), 70 expected.end(), count, value, trash); 71 invoke_on_all_policies(run_remove_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), 72 expected.end(), count, value, trash); 73 } 74 } 75 76 int32_t 77 main() 78 { 79 80 test<float64_t>(-666.0, 8.5, [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); }); 81 82 test<int32_t>(-666, 42, [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 42 : -1 - int32_t(j); }); 83 84 test<Number>(Number(42, OddTag()), Number(2001, OddTag()), 85 [](int32_t j) { return ((j + 1) % 3 & 2) != 0 ? Number(2001, OddTag()) : Number(j, OddTag()); }); 86 std::cout << done() << std::endl; 87 return 0; 88 } 89