1 // -*- C++ -*- 2 //===-- copy_if.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 copy_if and remove_copy_if 11 #include "support/pstl_test_config.h" 12 13 #include <execution> 14 #include <algorithm> 15 16 #include "support/utils.h" 17 18 using namespace TestUtils; 19 20 struct run_copy_if 21 { 22 #if _PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN // dummy specializations to skip testing in case of broken configuration 23 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, 24 typename Predicate, typename T> 25 void 26 operator()(pstl::execution::parallel_policy, InputIterator first, InputIterator last, OutputIterator out_first, 27 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n, 28 Predicate pred, T trash) 29 { 30 } 31 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, 32 typename Predicate, typename T> 33 void 34 operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, 35 OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, 36 OutputIterator2 expected_last, Size n, Predicate pred, T trash) 37 { 38 } 39 #endif 40 41 template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, 42 typename Predicate, typename T> 43 void 44 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, 45 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, 46 Predicate pred, T trash) 47 { 48 // Cleaning 49 std::fill_n(expected_first, n, trash); 50 std::fill_n(out_first, n, trash); 51 52 // Run copy_if 53 auto i = copy_if(first, last, expected_first, pred); 54 auto k = copy_if(exec, first, last, out_first, pred); 55 EXPECT_EQ_N(expected_first, out_first, n, "wrong copy_if effect"); 56 for (size_t j = 0; j < GuardSize; ++j) 57 { 58 ++k; 59 } 60 EXPECT_TRUE(out_last == k, "wrong return value from copy_if"); 61 62 // Cleaning 63 std::fill_n(expected_first, n, trash); 64 std::fill_n(out_first, n, trash); 65 // Run remove_copy_if 66 i = remove_copy_if(first, last, expected_first, [=](const T& x) { return !pred(x); }); 67 k = remove_copy_if(exec, first, last, out_first, [=](const T& x) { return !pred(x); }); 68 EXPECT_EQ_N(expected_first, out_first, n, "wrong remove_copy_if effect"); 69 for (size_t j = 0; j < GuardSize; ++j) 70 { 71 ++k; 72 } 73 EXPECT_TRUE(out_last == k, "wrong return value from remove_copy_if"); 74 } 75 }; 76 77 template <typename T, typename Predicate, typename Convert> 78 void 79 test(T trash, Predicate pred, Convert convert, bool check_weakness = true) 80 { 81 // Try sequences of various lengths. 82 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 83 { 84 // count is number of output elements, plus a handful 85 // more for sake of detecting buffer overruns. 86 size_t count = GuardSize; 87 Sequence<T> in(n, [&](size_t k) -> T { 88 T val = convert(n ^ k); 89 count += pred(val) ? 1 : 0; 90 return val; 91 }); 92 93 Sequence<T> out(count, [=](size_t) { return trash; }); 94 Sequence<T> expected(count, [=](size_t) { return trash; }); 95 if (check_weakness) 96 { 97 auto expected_result = copy_if(in.cfbegin(), in.cfend(), expected.begin(), pred); 98 size_t m = expected_result - expected.begin(); 99 EXPECT_TRUE(n / 4 <= m && m <= 3 * (n + 1) / 4, "weak test for copy_if"); 100 } 101 invoke_on_all_policies(run_copy_if(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), 102 expected.end(), count, pred, trash); 103 invoke_on_all_policies(run_copy_if(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), 104 expected.end(), count, pred, trash); 105 } 106 } 107 108 struct test_non_const 109 { 110 template <typename Policy, typename InputIterator, typename OutputInterator> 111 void 112 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) 113 { 114 auto is_even = [&](float64_t v) { 115 uint32_t i = (uint32_t)v; 116 return i % 2 == 0; 117 }; 118 copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even)); 119 120 invoke_if(exec, [&]() { remove_copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even)); }); 121 } 122 }; 123 124 int32_t 125 main() 126 { 127 test<float64_t>(-666.0, [](const float64_t& x) { return x * x <= 1024; }, 128 [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? float64_t(j % 32) : float64_t(j % 33 + 34); }); 129 130 test<int32_t>(-666, [](const int32_t& x) { return x != 42; }, 131 [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? int32_t(j + 1) : 42; }); 132 133 #if !_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN 134 test<Number>(Number(42, OddTag()), IsMultiple(3, OddTag()), [](int32_t j) { return Number(j, OddTag()); }); 135 #endif 136 137 #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN 138 test<int32_t>(-666, [](const int32_t&) { return true; }, [](size_t j) { return j; }, false); 139 #endif 140 141 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const>()); 142 143 std::cout << done() << std::endl; 144 return 0; 145 } 146