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