1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
12 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
13 // class ForwardIterator>
15 // merge(ExecutionPolicy&& exec,
16 // ForwardIterator1 first1, ForwardIterator1 last1,
17 // ForwardIterator2 first2, ForwardIterator2 last2,
18 // ForwardIterator result);
20 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
21 // class ForwardIterator, class Compare>
23 // merge(ExecutionPolicy&& exec,
24 // ForwardIterator1 first1, ForwardIterator1 last1,
25 // ForwardIterator2 first2, ForwardIterator2 last2,
26 // ForwardIterator result, Compare comp);
35 #include "type_algorithms.h"
36 #include "test_execution_policies.h"
37 #include "test_iterators.h"
39 template <class Iter1
, class Iter2
>
41 template <class Policy
>
42 void operator()(Policy
&& policy
) {
44 int a
[] = {1, 3, 5, 7, 9};
45 int b
[] = {2, 4, 6, 8, 10};
46 std::array
<int, std::size(a
) + std::size(b
)> out
;
48 policy
, Iter1(std::begin(a
)), Iter1(std::end(a
)), Iter2(std::begin(b
)), Iter2(std::end(b
)), std::begin(out
));
49 assert((out
== std::array
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
52 { // check that it works with both ranges being empty
55 std::array
<int, std::size(a
) + std::size(b
)> out
;
57 policy
, Iter1(std::begin(a
)), Iter1(std::end(a
)), Iter2(std::begin(b
)), Iter2(std::end(b
)), std::begin(out
));
59 { // check that it works with the first range being empty
61 int b
[] = {2, 4, 6, 8, 10};
62 std::array
<int, std::size(a
) + std::size(b
)> out
;
64 policy
, Iter1(std::begin(a
)), Iter1(std::end(a
)), Iter2(std::begin(b
)), Iter2(std::end(b
)), std::begin(out
));
65 assert((out
== std::array
{2, 4, 6, 8, 10}));
68 { // check that it works with the second range being empty
69 int a
[] = {2, 4, 6, 8, 10};
71 std::array
<int, std::size(a
) + std::size(b
)> out
;
73 policy
, Iter1(std::begin(a
)), Iter1(std::end(a
)), Iter2(std::begin(b
)), Iter2(std::end(b
)), std::begin(out
));
74 assert((out
== std::array
{2, 4, 6, 8, 10}));
77 { // check that it works when the ranges don't have the same length
78 int a
[] = {2, 4, 6, 8, 10};
80 std::array
<int, std::size(a
) + std::size(b
)> out
;
82 policy
, Iter1(std::begin(a
)), Iter1(std::end(a
)), Iter2(std::begin(b
)), Iter2(std::end(b
)), std::begin(out
));
83 assert((out
== std::array
{2, 3, 4, 4, 6, 8, 10}));
86 { // check that large ranges work
87 std::vector
<int> a(100);
88 std::vector
<int> b(100);
105 std::vector
<int> out(std::size(a
) + std::size(b
));
108 Iter1(a
.data() + a
.size()),
110 Iter2(b
.data() + b
.size()),
112 std::vector
<int> expected(200);
113 std::iota(expected
.begin(), expected
.end(), 0);
114 assert(std::equal(out
.begin(), out
.end(), expected
.begin()));
117 { // check that the predicate is used
118 int a
[] = {10, 9, 8, 7};
120 std::array
<int, std::size(a
) + std::size(b
)> out
;
123 Iter1(std::begin(a
)),
125 Iter2(std::begin(b
)),
129 assert((out
== std::array
{10, 9, 8, 8, 7, 4, 3}));
134 int main(int, char**) {
135 types::for_each(types::forward_iterator_list
<int*>{}, types::apply_type_identity
{[](auto v
) {
136 using Iter
= typename
decltype(v
)::type
;
138 types::forward_iterator_list
<int*>{},
139 TestIteratorWithPolicies
<types::partial_instantiation
<Test
, Iter
>::template apply
>{});