| 1 |
// -*- C++ -*- |
| 2 |
// { dg-options "-ltbb" } |
| 3 |
// { dg-do run { target c++17 } } |
| 4 |
// { dg-timeout-factor 3 } |
| 5 |
// { dg-require-effective-target tbb_backend } |
| 6 |
|
| 7 |
//===-- merge.pass.cpp ----------------------------------------------------===// |
| 8 |
// |
| 9 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 |
// See https://llvm.org/LICENSE.txt for license information. |
| 11 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
#include "pstl/pstl_test_config.h" |
| 16 |
|
| 17 |
#ifdef PSTL_STANDALONE_TESTS |
| 18 |
#include <algorithm> |
| 19 |
#include <functional> |
| 20 |
#include "pstl/execution" |
| 21 |
#include "pstl/algorithm" |
| 22 |
|
| 23 |
#else |
| 24 |
#include <execution> |
| 25 |
#include <algorithm> |
| 26 |
#include <functional> |
| 27 |
#endif // PSTL_STANDALONE_TESTS |
| 28 |
|
| 29 |
#include "pstl/test_utils.h" |
| 30 |
|
| 31 |
using namespace TestUtils; |
| 32 |
|
| 33 |
struct test_merge |
| 34 |
{ |
| 35 |
template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator, |
| 36 |
typename Compare> |
| 37 |
void |
| 38 |
operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, |
| 39 |
OutputIterator out_first, OutputIterator out_last, Compare comp) |
| 40 |
{ |
| 41 |
using namespace std; |
| 42 |
{ |
| 43 |
const auto res = merge(exec, first1, last1, first2, last2, out_first, comp); |
| 44 |
EXPECT_TRUE(res == out_last, "wrong return result from merge with predicate"); |
| 45 |
EXPECT_TRUE(is_sorted(out_first, res, comp), "wrong result from merge with predicate"); |
| 46 |
EXPECT_TRUE(includes(out_first, res, first1, last1, comp), "first sequence is not a part of result"); |
| 47 |
EXPECT_TRUE(includes(out_first, res, first2, last2, comp), "second sequence is not a part of result"); |
| 48 |
} |
| 49 |
{ |
| 50 |
const auto res = merge(exec, first1, last1, first2, last2, out_first); |
| 51 |
EXPECT_TRUE(res == out_last, "wrong return result from merge"); |
| 52 |
EXPECT_TRUE(is_sorted(out_first, res), "wrong result from merge"); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// for reverse iterators |
| 57 |
template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator, |
| 58 |
typename Compare> |
| 59 |
void |
| 60 |
operator()(Policy&& exec, std::reverse_iterator<InputIterator1> first1, std::reverse_iterator<InputIterator1> last1, |
| 61 |
std::reverse_iterator<InputIterator2> first2, std::reverse_iterator<InputIterator2> last2, |
| 62 |
std::reverse_iterator<OutputIterator> out_first, std::reverse_iterator<OutputIterator> out_last, Compare) |
| 63 |
{ |
| 64 |
using namespace std; |
| 65 |
typedef typename std::iterator_traits<std::reverse_iterator<InputIterator1>>::value_type T; |
| 66 |
const auto res = merge(exec, first1, last1, first2, last2, out_first, std::greater<T>()); |
| 67 |
|
| 68 |
EXPECT_TRUE(res == out_last, "wrong return result from merge with predicate"); |
| 69 |
EXPECT_TRUE(is_sorted(out_first, res, std::greater<T>()), "wrong result from merge with predicate"); |
| 70 |
EXPECT_TRUE(includes(out_first, res, first1, last1, std::greater<T>()), |
| 71 |
"first sequence is not a part of result"); |
| 72 |
EXPECT_TRUE(includes(out_first, res, first2, last2, std::greater<T>()), |
| 73 |
"second sequence is not a part of result"); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
template <typename T, typename Generator1, typename Generator2> |
| 78 |
void |
| 79 |
test_merge_by_type(Generator1 generator1, Generator2 generator2) |
| 80 |
{ |
| 81 |
using namespace std; |
| 82 |
size_t max_size = 100000; |
| 83 |
Sequence<T> in1(max_size, generator1); |
| 84 |
Sequence<T> in2(max_size / 2, generator2); |
| 85 |
Sequence<T> out(in1.size() + in2.size()); |
| 86 |
std::sort(in1.begin(), in1.end()); |
| 87 |
std::sort(in2.begin(), in2.end()); |
| 88 |
|
| 89 |
for (size_t size = 0; size <= max_size; size = size <= 16 ? size + 1 : size_t(3.1415 * size)) |
| 90 |
{ |
| 91 |
invoke_on_all_policies(test_merge(), in1.cbegin(), in1.cbegin() + size, in2.data(), in2.data() + size / 2, |
| 92 |
out.begin(), out.begin() + 1.5 * size, std::less<T>()); |
| 93 |
invoke_on_all_policies(test_merge(), in1.data(), in1.data() + size, in2.cbegin(), in2.cbegin() + size / 2, |
| 94 |
out.begin(), out.begin() + 3 * size / 2, std::less<T>()); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
template <typename T> |
| 99 |
struct test_non_const |
| 100 |
{ |
| 101 |
template <typename Policy, typename InputIterator, typename OutputIterator> |
| 102 |
void |
| 103 |
operator()(Policy&& exec, InputIterator input_iter, OutputIterator out_iter) |
| 104 |
{ |
| 105 |
merge(exec, input_iter, input_iter, input_iter, input_iter, out_iter, non_const(std::less<T>())); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
int |
| 110 |
main() |
| 111 |
{ |
| 112 |
test_merge_by_type<int32_t>([](size_t v) { return (v % 2 == 0 ? v : -v) * 3; }, [](size_t v) { return v * 2; }); |
| 113 |
test_merge_by_type<float64_t>([](size_t v) { return float64_t(v); }, [](size_t v) { return float64_t(v - 100); }); |
| 114 |
|
| 115 |
#if !defined(_PSTL_ICC_16_17_TEST_64_TIMEOUT) |
| 116 |
test_merge_by_type<Wrapper<int16_t>>([](size_t v) { return Wrapper<int16_t>(v % 100); }, |
| 117 |
[](size_t v) { return Wrapper<int16_t>(v % 10); }); |
| 118 |
#endif |
| 119 |
|
| 120 |
test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); |
| 121 |
|
| 122 |
std::cout << done() << std::endl; |
| 123 |
return 0; |
| 124 |
} |