Synchronize libstdc++ PSTL with upstream LLVM PSTL
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / pstl / alg_modifying_operations / unique_copy_equal.cc
1 // -*- C++ -*-
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-require-effective-target tbb-backend }
5
6 //===-- unique_copy_equal.pass.cpp ----------------------------------------===//
7 //
8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9 // See https://llvm.org/LICENSE.txt for license information.
10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //
12 //===----------------------------------------------------------------------===//
13
14 // Tests for unique_copy
15 #include "pstl/pstl_test_config.h"
16
17 #ifdef PSTL_STANDALONE_TESTS
18 #include "pstl/execution"
19 #include "pstl/algorithm"
20 #else
21 #include <execution>
22 #include <algorithm>
23 #endif // PSTL_STANDALONE_TESTS
24
25 #include "pstl/test_utils.h"
26
27 using namespace TestUtils;
28
29 struct run_unique_copy
30 {
31 #if _PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN // dummy specializations to skip testing in case of broken configuration
32 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size,
33 typename Predicate, typename T>
34 void
35 operator()(__pstl::execution::parallel_policy, InputIterator first, InputIterator last, OutputIterator out_first,
36 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n,
37 Predicate pred, T trash)
38 {
39 }
40
41 template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size,
42 typename Predicate, typename T>
43 void
44 operator()(__pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
45 OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first,
46 OutputIterator2 expected_last, Size n, Predicate pred, T trash)
47 {
48 }
49 #endif
50
51 template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size,
52 typename Predicate, typename T>
53 void
54 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
55 OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n,
56 Predicate pred, T trash)
57 {
58 // Cleaning
59 std::fill_n(expected_first, n, trash);
60 std::fill_n(out_first, n, trash);
61
62 // Run unique_copy
63 auto i = unique_copy(first, last, expected_first);
64 auto k = unique_copy(exec, first, last, out_first);
65 EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy effect");
66 for (size_t j = 0; j < GuardSize; ++j)
67 {
68 ++k;
69 }
70 EXPECT_TRUE(out_last == k, "wrong return value from unique_copy");
71
72 // Cleaning
73 std::fill_n(expected_first, n, trash);
74 std::fill_n(out_first, n, trash);
75 // Run unique_copy with predicate
76 i = unique_copy(first, last, expected_first, pred);
77 k = unique_copy(exec, first, last, out_first, pred);
78 EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy with predicate effect");
79 for (size_t j = 0; j < GuardSize; ++j)
80 {
81 ++k;
82 }
83 EXPECT_TRUE(out_last == k, "wrong return value from unique_copy with predicate");
84 }
85 };
86
87 template <typename T, typename BinaryPredicate, typename Convert>
88 void
89 test(T trash, BinaryPredicate pred, Convert convert, bool check_weakness = true)
90 {
91 // Try sequences of various lengths.
92 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
93 {
94 // count is number of output elements, plus a handful
95 // more for sake of detecting buffer overruns.
96 Sequence<T> in(n, [&](size_t k) -> T { return convert(k ^ n); });
97 using namespace std;
98 size_t count = GuardSize;
99 for (size_t k = 0; k < in.size(); ++k)
100 count += k == 0 || !pred(in[k], in[k - 1]) ? 1 : 0;
101 Sequence<T> out(count, [=](size_t) { return trash; });
102 Sequence<T> expected(count, [=](size_t) { return trash; });
103 if (check_weakness)
104 {
105 auto expected_result = unique_copy(in.begin(), in.end(), expected.begin(), pred);
106 size_t m = expected_result - expected.begin();
107 EXPECT_TRUE(n / (n < 10000 ? 4 : 6) <= m && m <= (3 * n + 1) / 4, "weak test for unique_copy");
108 }
109 invoke_on_all_policies(run_unique_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(),
110 expected.end(), count, pred, trash);
111 }
112 }
113
114 template <typename T>
115 struct test_non_const
116 {
117 template <typename Policy, typename InputIterator, typename OutputInterator>
118 void
119 operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter)
120 {
121 unique_copy(exec, input_iter, input_iter, out_iter, non_const(std::equal_to<T>()));
122 }
123 };
124
125 int32_t
126 main(int32_t argc, char* argv[])
127 {
128 test<Number>(Number(42, OddTag()), std::equal_to<Number>(),
129 [](int32_t j) { return Number(3 * j / 13 ^ (j & 8), OddTag()); });
130
131 test<float32_t>(float32_t(42), std::equal_to<float32_t>(),
132 [](int32_t j) { return float32_t(5 * j / 23 ^ (j / 7)); });
133 #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN
134 test<float32_t>(float32_t(42), [](float32_t x, float32_t y) { return false; },
135 [](int32_t j) { return float32_t(j); }, false);
136 #endif
137
138 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
139
140 std::cout << done() << std::endl;
141 return 0;
142 }