[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / std / algorithms / alg.nonmodifying / alg.search / search_pred.pass.cpp
blobf835d2f094581671ea4c1b748f6c7d35ab013032
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
11 // template<ForwardIterator Iter1, ForwardIterator Iter2>
12 // requires HasEqualTo<Iter1::value_type, Iter2::value_type>
13 // constexpr Iter1 // constexpr after C++17
14 // search(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
16 #include <algorithm>
17 #include <cassert>
19 #include "test_macros.h"
20 #include "test_iterators.h"
22 #if TEST_STD_VER > 17
23 TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
25 TEST_CONSTEXPR bool test_constexpr() {
26 int ia[] = {0, 1, 2, 3};
27 int ib[] = {0, 1, 5, 3};
28 int ic[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
29 return (std::search(std::begin(ic), std::end(ic), std::begin(ia), std::end(ia), eq) == ic+3)
30 && (std::search(std::begin(ic), std::end(ic), std::begin(ib), std::end(ib), eq) == std::end(ic))
33 #endif
35 struct count_equal
37 static unsigned count;
38 template <class T>
39 bool operator()(const T& x, const T& y)
40 {++count; return x == y;}
43 unsigned count_equal::count = 0;
45 template <class Iter1, class Iter2>
46 void
47 test()
49 int ia[] = {0, 1, 2, 3, 4, 5};
50 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
51 count_equal::count = 0;
52 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia), count_equal()) == Iter1(ia));
53 assert(count_equal::count <= 0);
54 count_equal::count = 0;
55 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+1), count_equal()) == Iter1(ia));
56 assert(count_equal::count <= sa);
57 count_equal::count = 0;
58 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+1), Iter2(ia+2), count_equal()) == Iter1(ia+1));
59 assert(count_equal::count <= sa);
60 count_equal::count = 0;
61 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+2), count_equal()) == Iter1(ia));
62 assert(count_equal::count <= 0);
63 count_equal::count = 0;
64 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), count_equal()) == Iter1(ia+2));
65 assert(count_equal::count <= sa);
66 count_equal::count = 0;
67 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), count_equal()) == Iter1(ia+2));
68 assert(count_equal::count <= sa);
69 count_equal::count = 0;
70 assert(std::search(Iter1(ia), Iter1(ia), Iter2(ia+2), Iter2(ia+3), count_equal()) == Iter1(ia));
71 assert(count_equal::count <= 0);
72 count_equal::count = 0;
73 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-1), Iter2(ia+sa), count_equal()) == Iter1(ia+sa-1));
74 assert(count_equal::count <= sa);
75 count_equal::count = 0;
76 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-3), Iter2(ia+sa), count_equal()) == Iter1(ia+sa-3));
77 assert(count_equal::count <= sa*3);
78 count_equal::count = 0;
79 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa), count_equal()) == Iter1(ia));
80 assert(count_equal::count <= sa*sa);
81 count_equal::count = 0;
82 assert(std::search(Iter1(ia), Iter1(ia+sa-1), Iter2(ia), Iter2(ia+sa), count_equal()) == Iter1(ia+sa-1));
83 assert(count_equal::count <= (sa-1)*sa);
84 count_equal::count = 0;
85 assert(std::search(Iter1(ia), Iter1(ia+1), Iter2(ia), Iter2(ia+sa), count_equal()) == Iter1(ia+1));
86 assert(count_equal::count <= sa);
87 count_equal::count = 0;
88 int ib[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
89 const unsigned sb = sizeof(ib)/sizeof(ib[0]);
90 int ic[] = {1};
91 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ic), Iter2(ic+1), count_equal()) == Iter1(ib+1));
92 assert(count_equal::count <= sb);
93 count_equal::count = 0;
94 int id[] = {1, 2};
95 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(id), Iter2(id+2), count_equal()) == Iter1(ib+1));
96 assert(count_equal::count <= sb*2);
97 count_equal::count = 0;
98 int ie[] = {1, 2, 3};
99 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ie), Iter2(ie+3), count_equal()) == Iter1(ib+4));
100 assert(count_equal::count <= sb*3);
101 count_equal::count = 0;
102 int ig[] = {1, 2, 3, 4};
103 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ig), Iter2(ig+4), count_equal()) == Iter1(ib+8));
104 assert(count_equal::count <= sb*4);
105 count_equal::count = 0;
106 int ih[] = {0, 1, 1, 1, 1, 2, 3, 0, 1, 2, 3, 4};
107 const unsigned sh = sizeof(ih)/sizeof(ih[0]);
108 int ii[] = {1, 1, 2};
109 assert(std::search(Iter1(ih), Iter1(ih+sh), Iter2(ii), Iter2(ii+3), count_equal()) == Iter1(ih+3));
110 assert(count_equal::count <= sh*3);
113 int main(int, char**)
115 test<forward_iterator<const int*>, forward_iterator<const int*> >();
116 test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
117 test<forward_iterator<const int*>, random_access_iterator<const int*> >();
118 test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
119 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
120 test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
121 test<random_access_iterator<const int*>, forward_iterator<const int*> >();
122 test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
123 test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
125 #if TEST_STD_VER > 17
126 static_assert(test_constexpr());
127 #endif
129 return 0;