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