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