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