xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp (revision ed27a4edb03809f0fe38f780765acfb97346c127)
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 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
12 
13 // <algorithm>
14 
15 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
16 //   bool equal(ExecutionPolicy&& exec,
17 //              ForwardIterator1 first1, ForwardIterator1 last1,
18 //              ForwardIterator2 first2);
19 //
20 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
21 //          class BinaryPredicate>
22 //   bool equal(ExecutionPolicy&& exec,
23 //              ForwardIterator1 first1, ForwardIterator1 last1,
24 //              ForwardIterator2 first2, BinaryPredicate pred);
25 //
26 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
27 //   bool equal(ExecutionPolicy&& exec,
28 //              ForwardIterator1 first1, ForwardIterator1 last1,
29 //              ForwardIterator2 first2, ForwardIterator2 last2);
30 //
31 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
32 //          class BinaryPredicate>
33 //   bool equal(ExecutionPolicy&& exec,
34 //              ForwardIterator1 first1, ForwardIterator1 last1,
35 //              ForwardIterator2 first2, ForwardIterator2 last2,
36 //              BinaryPredicate pred);
37 
38 #include <algorithm>
39 #include <cassert>
40 #include <iterator>
41 
42 #include "test_execution_policies.h"
43 #include "test_iterators.h"
44 #include "test_macros.h"
45 
46 template <class It1, class It2>
47 struct Test {
48   template <class Policy>
operator ()Test49   void operator()(Policy&& policy) {
50     { // 3 iter overloads
51       // check with equal ranges
52       {
53         int a[] = {1, 2, 3, 4};
54         int b[] = {1, 2, 3, 4};
55         assert(std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b))));
56       }
57 
58       // check with an empty range
59       {
60         int a[] = {999};
61         int b[] = {1, 2, 3};
62         assert(std::equal(policy, It1(std::begin(a)), It1(std::begin(a)), It2(std::begin(b))));
63       }
64 
65       // check with different ranges
66       {
67         int a[] = {1, 2, 3};
68         int b[] = {3, 2, 1};
69         assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b))));
70       }
71 
72       // check that the predicate is used
73       {
74         int a[] = {2, 4, 6, 8, 10};
75         int b[] = {12, 14, 16, 18, 20};
76         assert(std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), [](int lhs, int rhs) {
77           return lhs % 2 == rhs % 2;
78         }));
79       }
80     }
81 
82     { // 4 iter overloads
83       // check with equal ranges of equal size
84       {
85         int a[] = {1, 2, 3, 4};
86         int b[] = {1, 2, 3, 4};
87         assert(std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
88       }
89 
90       // check with unequal ranges of equal size
91       {
92         int a[] = {1, 2, 3, 4};
93         int b[] = {4, 3, 2, 1};
94         assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
95       }
96 
97       // check with equal ranges of unequal size
98       {
99         {
100           int a[] = {1, 2, 3, 4};
101           int b[] = {1, 2, 3, 4, 5};
102           assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
103         }
104         {
105           int a[] = {1, 2, 3, 4, 5};
106           int b[] = {1, 2, 3, 4};
107           assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
108         }
109       }
110 
111       // check empty ranges
112       {
113         // empty/empty
114         {
115           int a[] = {888};
116           int b[] = {999};
117           assert(std::equal(policy, It1(std::begin(a)), It1(std::begin(a)), It2(std::begin(b)), It2(std::begin(b))));
118         }
119         // empty/non-empty
120         {
121           int a[] = {999};
122           int b[] = {999};
123           assert(!std::equal(policy, It1(std::begin(a)), It1(std::begin(a)), It2(std::begin(b)), It2(std::end(b))));
124         }
125         // non-empty/empty
126         {
127           int a[] = {999};
128           int b[] = {999};
129           assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::begin(b))));
130         }
131       }
132 
133       // check that the predicate is used
134       {
135         int a[] = {2, 4, 6, 8, 10};
136         int b[] = {12, 14, 16, 18, 20};
137         assert(std::equal(
138             policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b)), [](int lhs, int rhs) {
139               return lhs % 2 == rhs % 2;
140             }));
141       }
142     }
143   }
144 };
145 
main(int,char **)146 int main(int, char**) {
147   types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v) {
148                     using It1 = typename decltype(v)::type;
149                     types::for_each(
150                         types::forward_iterator_list<int*>{},
151                         TestIteratorWithPolicies<types::partial_instantiation<Test, It1>::template apply>{});
152                   }});
153   return 0;
154 }
155