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 Iter1, InputIterator Iter2,
12// Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
13// requires CopyConstructible<Pred>
14// constexpr bool // constexpr after c++17
15// equal(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred);
16//
17// Introduced in C++14:
18// template<InputIterator Iter1, InputIterator Iter2,
19// Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
20// requires CopyConstructible<Pred>
21// constexpr bool // constexpr after c++17
22// equal(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred);
23
24
25#include <algorithm>
26#include <functional>
27#include <cassert>
28
29#include "test_macros.h"
30#include "test_iterators.h"
31
32#if TEST_STD_VER > 17
33TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
34
35TEST_CONSTEXPR bool test_constexpr() {
36 int ia[] = {1, 3, 6, 7};
37 int ib[] = {1, 3};
38 int ic[] = {1, 3, 5, 7};
39 typedef cpp17_input_iterator<int*> II;
40 typedef bidirectional_iterator<int*> BI;
41
42 return !std::equal(std::begin(ia), std::end(ia), std::begin(ic) , eq)
43 && !std::equal(std::begin(ia), std::end(ia), std::begin(ic), std::end(ic), eq)
44 && std::equal(std::begin(ib), std::end(ib), std::begin(ic) , eq)
45 && !std::equal(std::begin(ib), std::end(ib), std::begin(ic), std::end(ic), eq)
46
47 && std::equal(II(std::begin(ib)), II(std::end(ib)), II(std::begin(ic)) , eq)
48 && !std::equal(BI(std::begin(ib)), BI(std::end(ib)), BI(std::begin(ic)), BI(std::end(ic)), eq)
49 ;
50 }
51#endif
52
53
54int comparison_count = 0;
55template <typename T>
56bool counting_equals ( const T &a, const T &b ) {
57 ++comparison_count;
58 return a == b;
59}
60
61int main(int, char**)
62{
63 int ia[] = {0, 1, 2, 3, 4, 5};
64 const unsigned s = sizeof(ia)/sizeof(ia[0]);
65 int ib[s] = {0, 1, 2, 5, 4, 5};
66 assert(std::equal(cpp17_input_iterator<const int*>(ia),
67 cpp17_input_iterator<const int*>(ia+s),
68 cpp17_input_iterator<const int*>(ia),
69 std::equal_to<int>()));
70#if TEST_STD_VER >= 14
71 assert(std::equal(cpp17_input_iterator<const int*>(ia),
72 cpp17_input_iterator<const int*>(ia+s),
73 cpp17_input_iterator<const int*>(ia),
74 cpp17_input_iterator<const int*>(ia+s),
75 std::equal_to<int>()));
76 assert(std::equal(random_access_iterator<const int*>(ia),
77 random_access_iterator<const int*>(ia+s),
78 random_access_iterator<const int*>(ia),
79 random_access_iterator<const int*>(ia+s),
80 std::equal_to<int>()));
81
82 comparison_count = 0;
83 assert(!std::equal(cpp17_input_iterator<const int*>(ia),
84 cpp17_input_iterator<const int*>(ia+s),
85 cpp17_input_iterator<const int*>(ia),
86 cpp17_input_iterator<const int*>(ia+s-1),
87 counting_equals<int>));
88 assert(comparison_count > 0);
89 comparison_count = 0;
90 assert(!std::equal(random_access_iterator<const int*>(ia),
91 random_access_iterator<const int*>(ia+s),
92 random_access_iterator<const int*>(ia),
93 random_access_iterator<const int*>(ia+s-1),
94 counting_equals<int>));
95 assert(comparison_count == 0);
96#endif
97 assert(!std::equal(cpp17_input_iterator<const int*>(ia),
98 cpp17_input_iterator<const int*>(ia+s),
99 cpp17_input_iterator<const int*>(ib),
100 std::equal_to<int>()));
101#if TEST_STD_VER >= 14
102 assert(!std::equal(cpp17_input_iterator<const int*>(ia),
103 cpp17_input_iterator<const int*>(ia+s),
104 cpp17_input_iterator<const int*>(ib),
105 cpp17_input_iterator<const int*>(ib+s),
106 std::equal_to<int>()));
107 assert(!std::equal(random_access_iterator<const int*>(ia),
108 random_access_iterator<const int*>(ia+s),
109 random_access_iterator<const int*>(ib),
110 random_access_iterator<const int*>(ib+s),
111 std::equal_to<int>()));
112#endif
113
114#if TEST_STD_VER > 17
115 static_assert(test_constexpr());
116#endif
117
118 return 0;
119}
120

source code of libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/equal_pred.pass.cpp