1 // -*- C++ -*- 2 //===-- unique_copy_equal.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 unique_copy 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_unique_copy 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 32 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, 33 typename Predicate, typename T> 34 void 35 operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, 36 OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, 37 OutputIterator2 expected_last, Size n, Predicate pred, T trash) 38 { 39 } 40 #endif 41 42 template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, 43 typename Predicate, typename T> 44 void 45 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, 46 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n, 47 Predicate pred, T trash) 48 { 49 // Cleaning 50 std::fill_n(expected_first, n, trash); 51 std::fill_n(out_first, n, trash); 52 53 // Run unique_copy 54 auto i = unique_copy(first, last, expected_first); 55 auto k = unique_copy(exec, first, last, out_first); 56 EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy effect"); 57 for (size_t j = 0; j < GuardSize; ++j) 58 { 59 ++k; 60 } 61 EXPECT_TRUE(out_last == k, "wrong return value from unique_copy"); 62 63 // Cleaning 64 std::fill_n(expected_first, n, trash); 65 std::fill_n(out_first, n, trash); 66 // Run unique_copy with predicate 67 i = unique_copy(first, last, expected_first, pred); 68 k = unique_copy(exec, first, last, out_first, pred); 69 EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy with predicate effect"); 70 for (size_t j = 0; j < GuardSize; ++j) 71 { 72 ++k; 73 } 74 EXPECT_TRUE(out_last == k, "wrong return value from unique_copy with predicate"); 75 } 76 }; 77 78 template <typename T, typename BinaryPredicate, typename Convert> 79 void 80 test(T trash, BinaryPredicate pred, Convert convert, bool check_weakness = true) 81 { 82 // Try sequences of various lengths. 83 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 84 { 85 // count is number of output elements, plus a handful 86 // more for sake of detecting buffer overruns. 87 Sequence<T> in(n, [&](size_t k) -> T { return convert(k ^ n); }); 88 using namespace std; 89 size_t count = GuardSize; 90 for (size_t k = 0; k < in.size(); ++k) 91 count += k == 0 || !pred(in[k], in[k - 1]) ? 1 : 0; 92 Sequence<T> out(count, [=](size_t) { return trash; }); 93 Sequence<T> expected(count, [=](size_t) { return trash; }); 94 if (check_weakness) 95 { 96 auto expected_result = unique_copy(in.begin(), in.end(), expected.begin(), pred); 97 size_t m = expected_result - expected.begin(); 98 EXPECT_TRUE(n / (n < 10000 ? 4 : 6) <= m && m <= (3 * n + 1) / 4, "weak test for unique_copy"); 99 } 100 invoke_on_all_policies(run_unique_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), 101 expected.end(), count, pred, trash); 102 } 103 } 104 105 template <typename T> 106 struct test_non_const 107 { 108 template <typename Policy, typename InputIterator, typename OutputInterator> 109 void 110 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) 111 { 112 unique_copy(exec, input_iter, input_iter, out_iter, non_const(std::equal_to<T>())); 113 } 114 }; 115 116 int32_t 117 main(int32_t argc, char* argv[]) 118 { 119 test<Number>(Number(42, OddTag()), std::equal_to<Number>(), 120 [](int32_t j) { return Number(3 * j / 13 ^ (j & 8), OddTag()); }); 121 122 test<float32_t>(float32_t(42), std::equal_to<float32_t>(), 123 [](int32_t j) { return float32_t(5 * j / 23 ^ (j / 7)); }); 124 #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN 125 test<float32_t>(float32_t(42), [](float32_t x, float32_t y) { return false; }, 126 [](int32_t j) { return float32_t(j); }, false); 127 #endif 128 129 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); 130 131 std::cout << done() << std::endl; 132 return 0; 133 } 134