upper_bound_comp.pass.cpp revision eb564e76cc3904d811c981a50ecce0659f444cc9
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <algorithm>
11
12// template<ForwardIterator Iter, class T, Predicate<auto, T, Iter::value_type> Compare>
13//   requires CopyConstructible<Compare>
14//   Iter
15//   upper_bound(Iter first, Iter last, const T& value, Compare comp);
16
17#include <algorithm>
18#include <functional>
19#include <vector>
20#include <cassert>
21
22#include "../../../iterators.h"
23
24template <class Iter, class T>
25void
26test(Iter first, Iter last, const T& value)
27{
28    Iter i = std::upper_bound(first, last, value, std::greater<int>());
29    for (Iter j = first; j != i; ++j)
30        assert(!std::greater<int>()(value, *j));
31    for (Iter j = i; j != last; ++j)
32        assert(std::greater<int>()(value, *j));
33}
34
35template <class Iter>
36void
37test()
38{
39    const unsigned N = 1000;
40    const unsigned M = 10;
41    std::vector<int> v(N);
42    int x = 0;
43    for (int i = 0; i < v.size(); ++i)
44    {
45        v[i] = x;
46        if (++x == M)
47            x = 0;
48    }
49    std::sort(v.begin(), v.end(), std::greater<int>());
50    for (x = 0; x <= M; ++x)
51        test(Iter(v.data()), Iter(v.data()+v.size()), x);
52}
53
54int main()
55{
56    int d[] = {3, 2, 1, 0};
57    for (int* e = d; e <= d+4; ++e)
58        for (int x = -1; x <= 4; ++x)
59            test(d, e, 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}
66