1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9// <algorithm>
10
11// template<InputIterator InIter1, InputIterator InIter2, typename OutIter,
12// CopyConstructible Compare>
13// requires OutputIterator<OutIter, InIter1::reference>
14// && OutputIterator<OutIter, InIter2::reference>
15// && Predicate<Compare, InIter1::value_type, InIter2::value_type>
16// && Predicate<Compare, InIter2::value_type, InIter1::value_type>
17// constexpr OutIter // constexpr after C++17
18// merge(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2,
19// OutIter result, Compare comp);
20
21#include <algorithm>
22#include <cassert>
23
24#include "test_macros.h"
25#include "test_iterators.h"
26#include "../sortable_helpers.h"
27
28template<class T, class Iter1, class Iter2, class OutIter>
29TEST_CONSTEXPR_CXX20 void test4()
30{
31 const T a[] = {11, 33, 31, 41};
32 const T b[] = {22, 32, 43, 42, 52};
33 {
34 T result[20] = {};
35 T expected[] = {11, 22, 33, 31, 32, 41, 43, 42, 52};
36 OutIter end = std::merge(Iter1(a), Iter1(a+4), Iter2(b), Iter2(b+5), OutIter(result), typename T::Comparator());
37 assert(std::lexicographical_compare(result, base(end), expected, expected+9, T::less) == 0);
38 for (const T *it = base(end); it != result+20; ++it) {
39 assert(it->value == 0);
40 }
41 }
42 {
43 T result[20] = {};
44 T expected[] = {11, 22, 32, 33, 31, 43, 42, 41, 52};
45 OutIter end = std::merge(Iter1(b), Iter1(b+5), Iter2(a), Iter2(a+4), OutIter(result), typename T::Comparator());
46 assert(std::lexicographical_compare(result, base(end), expected, expected+9, T::less) == 0);
47 for (const T *it = base(end); it != result+20; ++it) {
48 assert(it->value == 0);
49 }
50 }
51}
52
53template<class T, class Iter1, class Iter2>
54TEST_CONSTEXPR_CXX20 void test3()
55{
56 test4<T, Iter1, Iter2, cpp17_output_iterator<T*> >();
57 test4<T, Iter1, Iter2, forward_iterator<T*> >();
58 test4<T, Iter1, Iter2, bidirectional_iterator<T*> >();
59 test4<T, Iter1, Iter2, random_access_iterator<T*> >();
60 test4<T, Iter1, Iter2, T*>();
61}
62
63template<class T, class Iter1>
64TEST_CONSTEXPR_CXX20 void test2()
65{
66 test3<T, Iter1, cpp17_input_iterator<const T*> >();
67 test3<T, Iter1, forward_iterator<const T*> >();
68 test3<T, Iter1, bidirectional_iterator<const T*> >();
69 test3<T, Iter1, random_access_iterator<const T*> >();
70 test3<T, Iter1, const T*>();
71}
72
73template<class T>
74TEST_CONSTEXPR_CXX20 void test1()
75{
76 test2<T, cpp17_input_iterator<const T*> >();
77 test2<T, forward_iterator<const T*> >();
78 test2<T, bidirectional_iterator<const T*> >();
79 test2<T, random_access_iterator<const T*> >();
80 test2<T, const T*>();
81}
82
83TEST_CONSTEXPR_CXX20 bool test()
84{
85 test1<TrivialSortableWithComp>();
86 test1<NonTrivialSortableWithComp>();
87 return true;
88}
89
90int main(int, char**)
91{
92 test();
93#if TEST_STD_VER > 17
94 static_assert(test());
95#endif
96
97 return 0;
98}
99

source code of libcxx/test/std/algorithms/alg.sorting/alg.merge/merge_comp.pass.cpp