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