xref: /llvm-project/pstl/test/std/algorithms/alg.modifying.operations/unique.pass.cpp (revision 3dee12e4a57241e4a53687f4a4e463b581dabf46)
1 // -*- C++ -*-
2 //===-- unique.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 // Test for unique
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
26 {
27 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
28     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
29     template <typename ForwardIt, typename Generator>
30     void
31     operator()(pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2,
32                ForwardIt last2, Generator generator)
33     {
34     }
35 
36     template <typename ForwardIt, typename Generator>
37     void
38     operator()(pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2,
39                ForwardIt last2, Generator generator)
40     {
41     }
42 
43     template <typename ForwardIt, typename BinaryPred, typename Generator>
44     void
45     operator()(pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2,
46                ForwardIt last2, BinaryPred pred, Generator generator)
47     {
48     }
49 
50     template <typename ForwardIt, typename BinaryPred, typename Generator>
51     void
52     operator()(pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2,
53                ForwardIt last2, BinaryPred pred, Generator generator)
54     {
55     }
56 #endif
57 
58     template <typename Policy, typename ForwardIt, typename Generator>
59     void
60     operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, Generator generator)
61     {
62         using namespace std;
63 
64         // Preparation
65         fill_data(first1, last1, generator);
66         fill_data(first2, last2, generator);
67 
68         ForwardIt i = unique(first1, last1);
69         ForwardIt k = unique(exec, first2, last2);
70 
71         auto n = std::distance(first1, i);
72         EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique without predicate");
73         EXPECT_EQ_N(first1, first2, n, "wrong effect from unique without predicate");
74     }
75 
76     template <typename Policy, typename ForwardIt, typename BinaryPred, typename Generator>
77     void
78     operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, BinaryPred pred,
79                Generator generator)
80     {
81         using namespace std;
82 
83         // Preparation
84         fill_data(first1, last1, generator);
85         fill_data(first2, last2, generator);
86 
87         ForwardIt i = unique(first1, last1, pred);
88         ForwardIt k = unique(exec, first2, last2, pred);
89 
90         auto n = std::distance(first1, i);
91         EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique with predicate");
92         EXPECT_EQ_N(first1, first2, n, "wrong effect from unique with predicate");
93     }
94 };
95 
96 template <typename T, typename Generator, typename Predicate>
97 void
98 test(Generator generator, Predicate pred)
99 {
100     const std::size_t max_size = 1000000;
101     Sequence<T> in(max_size, [](size_t v) { return T(v); });
102     Sequence<T> exp(max_size, [](size_t v) { return T(v); });
103 
104     for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
105     {
106         invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, generator);
107         invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, pred, generator);
108     }
109 }
110 
111 template <typename T>
112 struct LocalWrapper
113 {
114     T my_val;
115 
116     explicit LocalWrapper(T k) : my_val(k) {}
117     LocalWrapper(LocalWrapper&& input) : my_val(std::move(input.my_val)) {}
118     LocalWrapper&
119     operator=(LocalWrapper&& input)
120     {
121         my_val = std::move(input.my_val);
122         return *this;
123     }
124     friend bool
125     operator==(const LocalWrapper<T>& x, const LocalWrapper<T>& y)
126     {
127         return x.my_val == y.my_val;
128     }
129 };
130 
131 template <typename T>
132 struct test_non_const
133 {
134     template <typename Policy, typename Iterator>
135     void
136     operator()(Policy&& exec, Iterator iter)
137     {
138         invoke_if(exec, [&]() { unique(exec, iter, iter, non_const(std::equal_to<T>())); });
139     }
140 };
141 
142 int32_t
143 main()
144 {
145 #if !_PSTL_ICC_16_17_18_TEST_UNIQUE_MASK_RELEASE_BROKEN
146     test<int32_t>([](size_t j) { return j / 3; },
147                   [](const int32_t& val1, const int32_t& val2) { return val1 * val1 == val2 * val2; });
148     test<float64_t>([](size_t) { return float64_t(1); },
149                     [](const float64_t& val1, const float64_t& val2) { return val1 != val2; });
150 #endif
151     test<LocalWrapper<uint32_t>>([](size_t j) { return LocalWrapper<uint32_t>(j); },
152                                  [](const LocalWrapper<uint32_t>& val1, const LocalWrapper<uint32_t>& val2) {
153                                      return val1.my_val != val2.my_val;
154                                  });
155 
156     test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
157 
158     std::cout << done() << std::endl;
159     return 0;
160 }
161