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