15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
95a83710eSEric Fiselier // <algorithm>
105a83710eSEric Fiselier
115a83710eSEric Fiselier // template<InputIterator Iter1, InputIterator Iter2,
125a83710eSEric Fiselier // Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
135a83710eSEric Fiselier // requires CopyConstructible<Pred>
146538e28dSMarshall Clow // constexpr bool // constexpr after c++17
155a83710eSEric Fiselier // equal(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred);
166538e28dSMarshall Clow //
176538e28dSMarshall Clow // Introduced in C++14:
186538e28dSMarshall Clow // template<InputIterator Iter1, InputIterator Iter2,
196538e28dSMarshall Clow // Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
206538e28dSMarshall Clow // requires CopyConstructible<Pred>
216538e28dSMarshall Clow // constexpr bool // constexpr after c++17
226538e28dSMarshall Clow // equal(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred);
236538e28dSMarshall Clow
245a83710eSEric Fiselier
255a83710eSEric Fiselier #include <algorithm>
265a83710eSEric Fiselier #include <functional>
275a83710eSEric Fiselier #include <cassert>
285a83710eSEric Fiselier
29e58baed3SEric Fiselier #include "test_macros.h"
305a83710eSEric Fiselier #include "test_iterators.h"
315a83710eSEric Fiselier
326538e28dSMarshall Clow #if TEST_STD_VER > 17
eq(int a,int b)336538e28dSMarshall Clow TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
346538e28dSMarshall Clow
test_constexpr()356538e28dSMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
366538e28dSMarshall Clow int ia[] = {1, 3, 6, 7};
376538e28dSMarshall Clow int ib[] = {1, 3};
386538e28dSMarshall Clow int ic[] = {1, 3, 5, 7};
39*773ae441SChristopher Di Bella typedef cpp17_input_iterator<int*> II;
406538e28dSMarshall Clow typedef bidirectional_iterator<int*> BI;
416538e28dSMarshall Clow
426538e28dSMarshall Clow return !std::equal(std::begin(ia), std::end(ia), std::begin(ic) , eq)
436538e28dSMarshall Clow && !std::equal(std::begin(ia), std::end(ia), std::begin(ic), std::end(ic), eq)
446538e28dSMarshall Clow && std::equal(std::begin(ib), std::end(ib), std::begin(ic) , eq)
456538e28dSMarshall Clow && !std::equal(std::begin(ib), std::end(ib), std::begin(ic), std::end(ic), eq)
466538e28dSMarshall Clow
476538e28dSMarshall Clow && std::equal(II(std::begin(ib)), II(std::end(ib)), II(std::begin(ic)) , eq)
486538e28dSMarshall Clow && !std::equal(BI(std::begin(ib)), BI(std::end(ib)), BI(std::begin(ic)), BI(std::end(ic)), eq)
496538e28dSMarshall Clow ;
506538e28dSMarshall Clow }
516538e28dSMarshall Clow #endif
526538e28dSMarshall Clow
536538e28dSMarshall Clow
545a83710eSEric Fiselier int comparison_count = 0;
555a83710eSEric Fiselier template <typename T>
counting_equals(const T & a,const T & b)565a83710eSEric Fiselier bool counting_equals ( const T &a, const T &b ) {
575a83710eSEric Fiselier ++comparison_count;
585a83710eSEric Fiselier return a == b;
595a83710eSEric Fiselier }
605a83710eSEric Fiselier
main(int,char **)612df59c50SJF Bastien int main(int, char**)
625a83710eSEric Fiselier {
635a83710eSEric Fiselier int ia[] = {0, 1, 2, 3, 4, 5};
645a83710eSEric Fiselier const unsigned s = sizeof(ia)/sizeof(ia[0]);
655a83710eSEric Fiselier int ib[s] = {0, 1, 2, 5, 4, 5};
66*773ae441SChristopher Di Bella assert(std::equal(cpp17_input_iterator<const int*>(ia),
67*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia+s),
68*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia),
695a83710eSEric Fiselier std::equal_to<int>()));
70e58baed3SEric Fiselier #if TEST_STD_VER >= 14
71*773ae441SChristopher Di Bella assert(std::equal(cpp17_input_iterator<const int*>(ia),
72*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia+s),
73*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia),
74*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia+s),
755a83710eSEric Fiselier std::equal_to<int>()));
765a83710eSEric Fiselier assert(std::equal(random_access_iterator<const int*>(ia),
775a83710eSEric Fiselier random_access_iterator<const int*>(ia+s),
785a83710eSEric Fiselier random_access_iterator<const int*>(ia),
795a83710eSEric Fiselier random_access_iterator<const int*>(ia+s),
805a83710eSEric Fiselier std::equal_to<int>()));
815a83710eSEric Fiselier
825a83710eSEric Fiselier comparison_count = 0;
83*773ae441SChristopher Di Bella assert(!std::equal(cpp17_input_iterator<const int*>(ia),
84*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia+s),
85*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia),
86*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia+s-1),
875a83710eSEric Fiselier counting_equals<int>));
885a83710eSEric Fiselier assert(comparison_count > 0);
895a83710eSEric Fiselier comparison_count = 0;
905a83710eSEric Fiselier assert(!std::equal(random_access_iterator<const int*>(ia),
915a83710eSEric Fiselier random_access_iterator<const int*>(ia+s),
925a83710eSEric Fiselier random_access_iterator<const int*>(ia),
935a83710eSEric Fiselier random_access_iterator<const int*>(ia+s-1),
945a83710eSEric Fiselier counting_equals<int>));
955a83710eSEric Fiselier assert(comparison_count == 0);
965a83710eSEric Fiselier #endif
97*773ae441SChristopher Di Bella assert(!std::equal(cpp17_input_iterator<const int*>(ia),
98*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia+s),
99*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ib),
1005a83710eSEric Fiselier std::equal_to<int>()));
101e58baed3SEric Fiselier #if TEST_STD_VER >= 14
102*773ae441SChristopher Di Bella assert(!std::equal(cpp17_input_iterator<const int*>(ia),
103*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ia+s),
104*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ib),
105*773ae441SChristopher Di Bella cpp17_input_iterator<const int*>(ib+s),
1065a83710eSEric Fiselier std::equal_to<int>()));
1075a83710eSEric Fiselier assert(!std::equal(random_access_iterator<const int*>(ia),
1085a83710eSEric Fiselier random_access_iterator<const int*>(ia+s),
1095a83710eSEric Fiselier random_access_iterator<const int*>(ib),
1105a83710eSEric Fiselier random_access_iterator<const int*>(ib+s),
1115a83710eSEric Fiselier std::equal_to<int>()));
1125a83710eSEric Fiselier #endif
1136538e28dSMarshall Clow
1146538e28dSMarshall Clow #if TEST_STD_VER > 17
1156538e28dSMarshall Clow static_assert(test_constexpr());
1166538e28dSMarshall Clow #endif
1172df59c50SJF Bastien
1182df59c50SJF Bastien return 0;
1195a83710eSEric Fiselier }
120