Home | History | Annotate | Download | only in binary.search
      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 
     23 #include "test_iterators.h"
     24 
     25 template <class Iter, class T>
     26 void
     27 test(Iter first, Iter last, const T& value, bool x)
     28 {
     29     assert(std::binary_search(first, last, value, std::greater<int>()) == x);
     30 }
     31 
     32 template <class Iter>
     33 void
     34 test()
     35 {
     36     const unsigned N = 1000;
     37     const unsigned M = 10;
     38     std::vector<int> v(N);
     39     int x = 0;
     40     for (int 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(), std::greater<int>());
     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 
     53 int main()
     54 {
     55     int d[] = {6, 4, 2, 0};
     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*(e-d) + 8 <= 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