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<class ForwardIterator, class Size, class T, class BinaryPredicate>
13 //   ForwardIterator
14 //   search_n(ForwardIterator first, ForwardIterator last, Size count,
15 //            const T& value, BinaryPredicate pred);
16 
17 #include <algorithm>
18 #include <cassert>
19 
20 #include "test_iterators.h"
21 #include "user_defined_integral.hpp"
22 
23 struct count_equal
24 {
25     static unsigned count;
26     template <class T>
operator ()count_equal27     bool operator()(const T& x, const T& y)
28         {++count; return x == y;}
29 };
30 
31 unsigned count_equal::count = 0;
32 
33 
34 template <class Iter>
35 void
test()36 test()
37 {
38     int ia[] = {0, 1, 2, 3, 4, 5};
39     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
40     count_equal::count = 0;
41     assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 0, count_equal()) == Iter(ia));
42     assert(count_equal::count <= sa);
43     count_equal::count = 0;
44     assert(std::search_n(Iter(ia), Iter(ia+sa), 1, 0, count_equal()) == Iter(ia+0));
45     assert(count_equal::count <= sa);
46     count_equal::count = 0;
47     assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 0, count_equal()) == Iter(ia+sa));
48     assert(count_equal::count <= sa);
49     count_equal::count = 0;
50     assert(std::search_n(Iter(ia), Iter(ia+sa), sa, 0, count_equal()) == Iter(ia+sa));
51     assert(count_equal::count <= sa);
52     count_equal::count = 0;
53     assert(std::search_n(Iter(ia), Iter(ia+sa), 0, 3, 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, 3, count_equal()) == Iter(ia+3));
57     assert(count_equal::count <= sa);
58     count_equal::count = 0;
59     assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 3, 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, 3, 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, 5, 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, 5, count_equal()) == Iter(ia+5));
69     assert(count_equal::count <= sa);
70     count_equal::count = 0;
71     assert(std::search_n(Iter(ia), Iter(ia+sa), 2, 5, 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, 5, count_equal()) == Iter(ia+sa));
75     assert(count_equal::count <= sa);
76     count_equal::count = 0;
77 
78     int ib[] = {0, 0, 1, 1, 2, 2};
79     const unsigned sb = sizeof(ib)/sizeof(ib[0]);
80     assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 0, count_equal()) == Iter(ib));
81     assert(count_equal::count <= sb);
82     count_equal::count = 0;
83     assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 0, count_equal()) == Iter(ib+0));
84     assert(count_equal::count <= sb);
85     count_equal::count = 0;
86     assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 0, count_equal()) == Iter(ib+0));
87     assert(count_equal::count <= sb);
88     count_equal::count = 0;
89     assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 0, count_equal()) == Iter(ib+sb));
90     assert(count_equal::count <= sb);
91     count_equal::count = 0;
92     assert(std::search_n(Iter(ib), Iter(ib+sb), sb, 0, count_equal()) == Iter(ib+sb));
93     assert(count_equal::count <= sb);
94     count_equal::count = 0;
95     assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 1, count_equal()) == Iter(ib));
96     assert(count_equal::count <= sb);
97     count_equal::count = 0;
98     assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 1, count_equal()) == Iter(ib+2));
99     assert(count_equal::count <= sb);
100     count_equal::count = 0;
101     assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 1, count_equal()) == Iter(ib+2));
102     assert(count_equal::count <= sb);
103     count_equal::count = 0;
104     assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 1, 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), sb, 1, count_equal()) == Iter(ib+sb));
108     assert(count_equal::count <= sb);
109     count_equal::count = 0;
110     assert(std::search_n(Iter(ib), Iter(ib+sb), 0, 2, count_equal()) == Iter(ib));
111     assert(count_equal::count <= sb);
112     count_equal::count = 0;
113     assert(std::search_n(Iter(ib), Iter(ib+sb), 1, 2, count_equal()) == Iter(ib+4));
114     assert(count_equal::count <= sb);
115     count_equal::count = 0;
116     assert(std::search_n(Iter(ib), Iter(ib+sb), 2, 2, count_equal()) == Iter(ib+4));
117     assert(count_equal::count <= sb);
118     count_equal::count = 0;
119     assert(std::search_n(Iter(ib), Iter(ib+sb), 3, 2, 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), sb, 2, count_equal()) == Iter(ib+sb));
123     assert(count_equal::count <= sb);
124     count_equal::count = 0;
125 
126     int ic[] = {0, 0, 0};
127     const unsigned sc = sizeof(ic)/sizeof(ic[0]);
128     assert(std::search_n(Iter(ic), Iter(ic+sc), 0, 0, count_equal()) == Iter(ic));
129     assert(count_equal::count <= sc);
130     count_equal::count = 0;
131     assert(std::search_n(Iter(ic), Iter(ic+sc), 1, 0, count_equal()) == Iter(ic));
132     assert(count_equal::count <= sc);
133     count_equal::count = 0;
134     assert(std::search_n(Iter(ic), Iter(ic+sc), 2, 0, count_equal()) == Iter(ic));
135     assert(count_equal::count <= sc);
136     count_equal::count = 0;
137     assert(std::search_n(Iter(ic), Iter(ic+sc), 3, 0, count_equal()) == Iter(ic));
138     assert(count_equal::count <= sc);
139     count_equal::count = 0;
140     assert(std::search_n(Iter(ic), Iter(ic+sc), 4, 0, count_equal()) == Iter(ic+sc));
141     assert(count_equal::count <= sc);
142     count_equal::count = 0;
143 
144     // Check that we properly convert the size argument to an integral.
145     std::search_n(Iter(ic), Iter(ic+sc), UserDefinedIntegral<unsigned>(4), 0, count_equal());
146     count_equal::count = 0;
147 }
148 
main()149 int main()
150 {
151     test<forward_iterator<const int*> >();
152     test<bidirectional_iterator<const int*> >();
153     test<random_access_iterator<const int*> >();
154 }
155