[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / std / algorithms / alg.nonmodifying / alg.search / search_n_pred.pass.cpp
blob6a3ab75758dd7e9cc5b505bbf309946af38ff411
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<class ForwardIterator, class Size, class T, class BinaryPredicate>
12 // constexpr ForwardIterator // constexpr after C++17
13 // search_n(ForwardIterator first, ForwardIterator last, Size count,
14 // const T& value, BinaryPredicate pred);
16 #include <algorithm>
17 #include <cassert>
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 #include "user_defined_integral.h"
23 #if TEST_STD_VER > 17
24 TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
26 TEST_CONSTEXPR bool test_constexpr() {
27 int ia[] = {0, 0, 1, 1, 2, 2};
28 return (std::search_n(std::begin(ia), std::end(ia), 1, 0, eq) == ia)
29 && (std::search_n(std::begin(ia), std::end(ia), 2, 1, eq) == ia+2)
30 && (std::search_n(std::begin(ia), std::end(ia), 1, 3, eq) == std::end(ia))
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;
46 template <class Iter>
47 void
48 test()
50 int ia[] = {0, 1, 2, 3, 4, 5};
51 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
52 count_equal::count = 0;
53 assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 0, count_equal()) == Iter(ia));
54 assert(count_equal::count <= sa);
55 count_equal::count = 0;
56 assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 0, count_equal()) == Iter(ia+0));
57 assert(count_equal::count <= sa);
58 count_equal::count = 0;
59 assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 0, count_equal()) == Iter(ia+sa));
60 assert(count_equal::count <= sa);
61 count_equal::count = 0;
62 assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 0, count_equal()) == Iter(ia+sa));
63 assert(count_equal::count <= sa);
64 count_equal::count = 0;
65 assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 3, count_equal()) == Iter(ia));
66 assert(count_equal::count <= sa);
67 count_equal::count = 0;
68 assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 3, count_equal()) == Iter(ia+3));
69 assert(count_equal::count <= sa);
70 count_equal::count = 0;
71 assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 3, count_equal()) == Iter(ia+sa));
72 assert(count_equal::count <= sa);
73 count_equal::count = 0;
74 assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 3, count_equal()) == Iter(ia+sa));
75 assert(count_equal::count <= sa);
76 count_equal::count = 0;
77 assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 5, count_equal()) == Iter(ia));
78 assert(count_equal::count <= sa);
79 count_equal::count = 0;
80 assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 5, count_equal()) == Iter(ia+5));
81 assert(count_equal::count <= sa);
82 count_equal::count = 0;
83 assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 5, count_equal()) == Iter(ia+sa));
84 assert(count_equal::count <= sa);
85 count_equal::count = 0;
86 assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 5, count_equal()) == Iter(ia+sa));
87 assert(count_equal::count <= sa);
88 count_equal::count = 0;
90 int ib[] = {0, 0, 1, 1, 2, 2};
91 const unsigned sb = sizeof(ib)/sizeof(ib[0]);
92 assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 0, count_equal()) == Iter(ib));
93 assert(count_equal::count <= sb);
94 count_equal::count = 0;
95 assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 0, count_equal()) == Iter(ib+0));
96 assert(count_equal::count <= sb);
97 count_equal::count = 0;
98 assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 0, count_equal()) == Iter(ib+0));
99 assert(count_equal::count <= sb);
100 count_equal::count = 0;
101 assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 0, count_equal()) == Iter(ib+sb));
102 assert(count_equal::count <= sb);
103 count_equal::count = 0;
104 assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 0, count_equal()) == Iter(ib+sb));
105 assert(count_equal::count <= sb);
106 count_equal::count = 0;
107 assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 1, count_equal()) == Iter(ib));
108 assert(count_equal::count <= sb);
109 count_equal::count = 0;
110 assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 1, count_equal()) == Iter(ib+2));
111 assert(count_equal::count <= sb);
112 count_equal::count = 0;
113 assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 1, count_equal()) == Iter(ib+2));
114 assert(count_equal::count <= sb);
115 count_equal::count = 0;
116 assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 1, count_equal()) == Iter(ib+sb));
117 assert(count_equal::count <= sb);
118 count_equal::count = 0;
119 assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 1, count_equal()) == Iter(ib+sb));
120 assert(count_equal::count <= sb);
121 count_equal::count = 0;
122 assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 2, count_equal()) == Iter(ib));
123 assert(count_equal::count <= sb);
124 count_equal::count = 0;
125 assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 2, count_equal()) == Iter(ib+4));
126 assert(count_equal::count <= sb);
127 count_equal::count = 0;
128 assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 2, count_equal()) == Iter(ib+4));
129 assert(count_equal::count <= sb);
130 count_equal::count = 0;
131 assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 2, count_equal()) == Iter(ib+sb));
132 assert(count_equal::count <= sb);
133 count_equal::count = 0;
134 assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 2, count_equal()) == Iter(ib+sb));
135 assert(count_equal::count <= sb);
136 count_equal::count = 0;
138 int ic[] = {0, 0, 0};
139 const unsigned sc = sizeof(ic)/sizeof(ic[0]);
140 assert(std::search_n(Iter(ic), Iter(ic+sc), 0, 0, count_equal()) == Iter(ic));
141 assert(count_equal::count <= sc);
142 count_equal::count = 0;
143 assert(std::search_n(Iter(ic), Iter(ic+sc), 1, 0, count_equal()) == Iter(ic));
144 assert(count_equal::count <= sc);
145 count_equal::count = 0;
146 assert(std::search_n(Iter(ic), Iter(ic+sc), 2, 0, count_equal()) == Iter(ic));
147 assert(count_equal::count <= sc);
148 count_equal::count = 0;
149 assert(std::search_n(Iter(ic), Iter(ic+sc), 3, 0, count_equal()) == Iter(ic));
150 assert(count_equal::count <= sc);
151 count_equal::count = 0;
152 assert(std::search_n(Iter(ic), Iter(ic+sc), 4, 0, count_equal()) == Iter(ic+sc));
153 assert(count_equal::count <= sc);
154 count_equal::count = 0;
156 // Check that we properly convert the size argument to an integral.
157 TEST_IGNORE_NODISCARD std::search_n(Iter(ic), Iter(ic+sc), UserDefinedIntegral<unsigned>(4), 0, count_equal());
158 count_equal::count = 0;
161 int main(int, char**)
163 test<forward_iterator<const int*> >();
164 test<bidirectional_iterator<const int*> >();
165 test<random_access_iterator<const int*> >();
167 #if TEST_STD_VER > 17
168 static_assert(test_constexpr());
169 #endif
171 return 0;