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<class ForwardIterator, class Size, class T> |
12 | // constexpr ForwardIterator // constexpr after C++17 |
13 | // search_n(ForwardIterator first, ForwardIterator last, Size count, |
14 | // const T& value); |
15 | |
16 | #include <algorithm> |
17 | #include <cassert> |
18 | |
19 | #include "test_macros.h" |
20 | #include "test_iterators.h" |
21 | #include "user_defined_integral.h" |
22 | |
23 | #if TEST_STD_VER > 17 |
24 | TEST_CONSTEXPR bool test_constexpr() { |
25 | int ia[] = {0, 0, 1, 1, 2, 2}; |
26 | return (std::search_n(std::begin(ia), std::end(ia), 1, 0) == ia) |
27 | && (std::search_n(std::begin(ia), std::end(ia), 2, 1) == ia+2) |
28 | && (std::search_n(std::begin(ia), std::end(ia), 1, 3) == std::end(ia)) |
29 | ; |
30 | } |
31 | #endif |
32 | |
33 | template <class Iter> |
34 | void |
35 | test() |
36 | { |
37 | int ia[] = {0, 1, 2, 3, 4, 5}; |
38 | const unsigned sa = sizeof(ia)/sizeof(ia[0]); |
39 | assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 0) == Iter(ia)); |
40 | assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 0) == Iter(ia+0)); |
41 | assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 0) == Iter(ia+sa)); |
42 | assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 0) == Iter(ia+sa)); |
43 | assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 3) == Iter(ia)); |
44 | assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 3) == Iter(ia+3)); |
45 | assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 3) == Iter(ia+sa)); |
46 | assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 3) == Iter(ia+sa)); |
47 | assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 5) == Iter(ia)); |
48 | assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 5) == Iter(ia+5)); |
49 | assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 5) == Iter(ia+sa)); |
50 | assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 5) == Iter(ia+sa)); |
51 | |
52 | int ib[] = {0, 0, 1, 1, 2, 2}; |
53 | const unsigned sb = sizeof(ib)/sizeof(ib[0]); |
54 | assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 0) == Iter(ib)); |
55 | assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 0) == Iter(ib+0)); |
56 | assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 0) == Iter(ib+0)); |
57 | assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 0) == Iter(ib+sb)); |
58 | assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 0) == Iter(ib+sb)); |
59 | assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 1) == Iter(ib)); |
60 | assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 1) == Iter(ib+2)); |
61 | assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 1) == Iter(ib+2)); |
62 | assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 1) == Iter(ib+sb)); |
63 | assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 1) == Iter(ib+sb)); |
64 | assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 2) == Iter(ib)); |
65 | assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 2) == Iter(ib+4)); |
66 | assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 2) == Iter(ib+4)); |
67 | assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 2) == Iter(ib+sb)); |
68 | assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 2) == Iter(ib+sb)); |
69 | |
70 | int ic[] = {0, 0, 0}; |
71 | const unsigned sc = sizeof(ic)/sizeof(ic[0]); |
72 | assert(std::search_n(Iter(ic), Iter(ic+sc), 0, 0) == Iter(ic)); |
73 | assert(std::search_n(Iter(ic), Iter(ic+sc), 1, 0) == Iter(ic)); |
74 | assert(std::search_n(Iter(ic), Iter(ic+sc), 2, 0) == Iter(ic)); |
75 | assert(std::search_n(Iter(ic), Iter(ic+sc), 3, 0) == Iter(ic)); |
76 | assert(std::search_n(Iter(ic), Iter(ic+sc), 4, 0) == Iter(ic+sc)); |
77 | |
78 | // Check that we properly convert the size argument to an integral. |
79 | (void)std::search_n(Iter(ic), Iter(ic+sc), UserDefinedIntegral<unsigned>(0), 0); |
80 | } |
81 | |
82 | int main(int, char**) |
83 | { |
84 | test<forward_iterator<const int*> >(); |
85 | test<bidirectional_iterator<const int*> >(); |
86 | test<random_access_iterator<const int*> >(); |
87 | |
88 | #if TEST_STD_VER > 17 |
89 | static_assert(test_constexpr()); |
90 | #endif |
91 | |
92 | return 0; |
93 | } |
94 | |