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<ForwardIterator Iter, class T>
13//   requires HasLess<T, Iter::value_type>
14//         && HasLess<Iter::value_type, T>
15//   bool
16//   binary_search(Iter first, Iter last, const T& value);
17
18#include <algorithm>
19#include <vector>
20#include <cassert>
21#include <cstddef>
22
23#include "test_iterators.h"
24
25template <class Iter, class T>
26void
27test(Iter first, Iter last, const T& value, bool x)
28{
29    assert(std::binary_search(first, last, value) == x);
30}
31
32template <class Iter>
33void
34test()
35{
36    const unsigned N = 1000;
37    const int M = 10;
38    std::vector<int> v(N);
39    int x = 0;
40    for (std::size_t i = 0; i < v.size(); ++i)
41    {
42        v[i] = x;
43        if (++x == M)
44            x = 0;
45    }
46    std::sort(v.begin(), v.end());
47    for (x = 0; x < M; ++x)
48        test(Iter(v.data()), Iter(v.data()+v.size()), x, true);
49    test(Iter(v.data()), Iter(v.data()+v.size()), -1, false);
50    test(Iter(v.data()), Iter(v.data()+v.size()), M, false);
51}
52
53int main()
54{
55    int d[] = {0, 2, 4, 6};
56    for (int* e = d; e <= d+4; ++e)
57        for (int x = -1; x <= 7; ++x)
58            test(d, e, x, (x % 2 == 0) && ((e-d)*2 > x));
59
60    test<forward_iterator<const int*> >();
61    test<bidirectional_iterator<const int*> >();
62    test<random_access_iterator<const int*> >();
63    test<const int*>();
64}
65