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